Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

Revert "Revert "コメントのNGワードのマッチをaho-corasickでやる"" #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions webapp/go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
)

require (
github.com/BobuSumisu/aho-corasick v1.0.3 // indirect
github.com/cockroachdb/errors v1.11.1 // indirect
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
github.com/cockroachdb/redact v1.1.5 // indirect
Expand Down
2 changes: 2 additions & 0 deletions webapp/go/go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/BobuSumisu/aho-corasick v1.0.3 h1:uuf+JHwU9CHP2Vx+wAy6jcksJThhJS9ehR8a+4nPE9g=
github.com/BobuSumisu/aho-corasick v1.0.3/go.mod h1:hm4jLcvZKI2vRF2WDU1N4p/jpWtpOzp3nLmi9AzX/XE=
github.com/cockroachdb/errors v1.11.1 h1:xSEW75zKaKCWzR3OfxXUxgrk/NtT4G1MiOv5lWZazG8=
github.com/cockroachdb/errors v1.11.1/go.mod h1:8MUxA3Gi6b25tYlFEBGLf+D8aISL+M4MIpiWMSNRfxw=
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b h1:r6VH0faHjZeQy818SGhaone5OnYfxFR/+AzdY3sf5aE=
Expand Down
23 changes: 7 additions & 16 deletions webapp/go/livecomment_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strconv"
"time"

ahocorasick "github.com/BobuSumisu/aho-corasick"
"github.com/jmoiron/sqlx"
"github.com/labstack/echo-contrib/session"
"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -202,23 +203,13 @@ func postLivecommentHandler(c echo.Context) error {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get NG words: "+err.Error())
}

var hitSpam int
trie := ahocorasick.NewTrieBuilder()
for _, ngword := range ngwords {
query := `
SELECT COUNT(*)
FROM
(SELECT ? AS text) AS texts
INNER JOIN
(SELECT CONCAT('%', ?, '%') AS pattern) AS patterns
ON texts.text LIKE patterns.pattern;
`
if err := tx.GetContext(ctx, &hitSpam, query, req.Comment, ngword.Word); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get hitspam: "+err.Error())
}
c.Logger().Infof("[hitSpam=%d] comment = %s", hitSpam, req.Comment)
if hitSpam >= 1 {
return echo.NewHTTPError(http.StatusBadRequest, "このコメントがスパム判定されました")
}
trie.AddString(ngword.Word)
}

if m := trie.Build().MatchString(req.Comment); len(m) > 0 {
return echo.NewHTTPError(http.StatusBadRequest, "このコメントがスパム判定されました")
}

now := time.Now().Unix()
Expand Down