Skip to content

Commit

Permalink
Comparison should ignore accents (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanaelhoun authored May 27, 2020
1 parent 55f49a5 commit 26f4491
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
go.uber.org/zap v1.8.0 // indirect
golang.org/x/net v0.0.0-20180706051357-32a936f46389 // indirect
golang.org/x/sync v0.0.0-20190423024810-112230192c58 // indirect
golang.org/x/text v0.3.0
google.golang.org/genproto v0.0.0-20180627194029-ff3583edef7d // indirect
google.golang.org/grpc v1.13.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0-20170531160350-a96e63847dc3 // indirect
Expand Down
19 changes: 17 additions & 2 deletions server/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ package main
import (
"net/http"
"strings"
"unicode"

"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"

"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/plugin"
Expand All @@ -23,7 +28,7 @@ func main() {
func (p *Plugin) OnActivate() error {
p.badWords = make(map[string]bool, len(badWords))
for _, word := range badWords {
p.badWords[strings.ToLower(word)] = true
p.badWords[strings.ToLower(removeAccents(word))] = true
}

return nil
Expand All @@ -37,7 +42,7 @@ func (p *Plugin) ServeHTTP(c *plugin.Context, w http.ResponseWriter, r *http.Req
}

func (p *Plugin) WordIsBad(word string) bool {
_, ok := p.badWords[strings.ToLower(word)]
_, ok := p.badWords[strings.ToLower(removeAccents(word))]
return ok
}

Expand All @@ -64,3 +69,13 @@ func (p *Plugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*mode
func (p *Plugin) MessageWillBeUpdated(c *plugin.Context, newPost *model.Post, _ *model.Post) (*model.Post, string) {
return p.FilterPost(newPost)
}

func removeAccents(s string) string {
t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
output, _, e := transform.String(t, s)
if e != nil {
return s
}

return output
}

0 comments on commit 26f4491

Please sign in to comment.