This is an automated email from the ASF dual-hosted git repository. kumfo pushed a commit to branch feat/reviewer-basic in repository https://gitbox.apache.org/repos/asf/incubator-answer-plugins.git
commit 5ba06d0ace93c3e49388515f23fe7a390f7e43a0 Author: kumfo <[email protected]> AuthorDate: Wed Mar 27 17:48:19 2024 +0800 feat(review): Improve basic review plugin --- reviewer-basic/basic.go | 71 ++++++++++++++++++++++++++++++++++++-- reviewer-basic/i18n/en_US.yaml | 13 ++++++- reviewer-basic/i18n/translation.go | 17 +++++---- reviewer-basic/i18n/zh_CN.yaml | 13 ++++++- 4 files changed, 103 insertions(+), 11 deletions(-) diff --git a/reviewer-basic/basic.go b/reviewer-basic/basic.go index d396c61..befb792 100644 --- a/reviewer-basic/basic.go +++ b/reviewer-basic/basic.go @@ -21,6 +21,9 @@ package basic import ( "encoding/json" + "fmt" + "github.com/segmentfault/pacman/log" + "strings" "github.com/apache/incubator-answer-plugins/reviewer-basic/i18n" "github.com/apache/incubator-answer/plugin" @@ -32,7 +35,9 @@ type Reviewer struct { } type ReviewerConfig struct { - PostNeedReview bool `json:"review_post"` + PostNeedReview bool `json:"review_post"` + PostReviewKeywords string `json:"review_post_keywords"` + PostDisallowedKeywords string `json:"disallowed_keywords"` } func init() { @@ -53,18 +58,55 @@ func (r *Reviewer) Info() plugin.Info { } func (r *Reviewer) Review(content *plugin.ReviewContent) (result *plugin.ReviewResult) { - result = &plugin.ReviewResult{Approved: true} + result = &plugin.ReviewResult{Approved: true, ReviewStatus: plugin.ReviewStatusApproved} // If the post does not need review, return directly if !r.Config.PostNeedReview { return result } // If the author is admin, no need to review - if content.Author.Role > 1 { + /*if content.Author.Role > 1 { return result } if content.Author.ApprovedQuestionAmount+content.Author.ApprovedAnswerAmount > 1 { return result + }*/ + + keywords := strings.Split(r.Config.PostReviewKeywords, "\n") + disallowedKeywords := strings.Split(r.Config.PostDisallowedKeywords, "\n") + log.Error(keywords) + log.Error(disallowedKeywords) + // Check if the post contains the keywords that need review + for _, keyword := range keywords { + keyword = strings.ToLower(keyword) + if strings.Contains(strings.ToLower(content.Title), keyword) || + strings.Contains(strings.ToLower(content.Content), keyword) || + strings.Contains(content.IP, keyword) || + strings.Contains(content.UserAgent, keyword) || + r.checkTags(content.Tags, keyword) { + return &plugin.ReviewResult{ + Approved: false, + ReviewStatus: plugin.ReviewStatusNeedReview, + Reason: fmt.Sprintf(plugin.TranslateWithData(myI18n.Language(content.Language), i18n.CommentMatchWordReview, nil), keyword), + } + } + } + + // If the post contains the disallowed keywords + for _, disallowedKeyword := range disallowedKeywords { + disallowedKeyword = strings.ToLower(disallowedKeyword) + if strings.Contains(strings.ToLower(content.Title), disallowedKeyword) || + strings.Contains(strings.ToLower(content.Content), disallowedKeyword) || + strings.Contains(content.IP, disallowedKeyword) || + strings.Contains(content.UserAgent, disallowedKeyword) || + r.checkTags(content.Tags, disallowedKeyword) { + return &plugin.ReviewResult{ + Approved: false, + ReviewStatus: plugin.ReviewStatusDeleteDirectly, + Reason: fmt.Sprintf(plugin.TranslateWithData(myI18n.Language(content.Language), i18n.CommentMatchWordReview, nil), disallowedKeyword), + } + } } + return &plugin.ReviewResult{ Approved: false, Reason: plugin.TranslateWithData(myI18n.Language(content.Language), i18n.CommentNeedReview, nil), @@ -83,6 +125,20 @@ func (r *Reviewer) ConfigFields() []plugin.ConfigField { }, Value: r.Config.PostNeedReview, }, + { + Name: "review_post_keywords", + Type: plugin.ConfigTypeTextarea, + Title: plugin.MakeTranslator(i18n.ConfigReviewPostKeywordsTitle), + Description: plugin.MakeTranslator(i18n.ConfigReviewPostKeywordsDescription), + Value: r.Config.PostReviewKeywords, + }, + { + Name: "disallowed_keywords", + Type: plugin.ConfigTypeTextarea, + Title: plugin.MakeTranslator(i18n.ConfigDisallowedKeywordsTitle), + Description: plugin.MakeTranslator(i18n.ConfigDisallowedKeywordsDescription), + Value: r.Config.PostDisallowedKeywords, + }, } } @@ -92,3 +148,12 @@ func (r *Reviewer) ConfigReceiver(config []byte) error { r.Config = c return nil } + +func (r *Reviewer) checkTags(tags []string, keyword string) bool { + for _, tag := range tags { + if strings.Contains(strings.ToLower(tag), keyword) { + return true + } + } + return false +} diff --git a/reviewer-basic/i18n/en_US.yaml b/reviewer-basic/i18n/en_US.yaml index 5a16c7b..a7c717b 100644 --- a/reviewer-basic/i18n/en_US.yaml +++ b/reviewer-basic/i18n/en_US.yaml @@ -31,5 +31,16 @@ plugin: other: Review post when author do not have any other approved post description: other: Questions or answers are not visible until they are approved. + review_post_keywords: + title: + other: Review post keywords + description: + other: When a post contains any of these words in its content, IP address, or browser’s user agent string, it will be held in the review queue. One word or IP address per line. It will match inside words, so “alpha” will match “Alphabet”. + disallowed_post_keywords: + title: + other: Disallowed post keywords + description: + other: When a post contains any of these words in its content, IP address, or browser’s user agent string, it will be deleted. One word or IP address per line. It will match inside words, so “alpha” will match “Alphabet”. comment: - need_review: Needs approval. \ No newline at end of file + need_review: Needs approval. + match_word_review: Needs approval, match words "%s". \ No newline at end of file diff --git a/reviewer-basic/i18n/translation.go b/reviewer-basic/i18n/translation.go index e7f10f6..b3d2623 100644 --- a/reviewer-basic/i18n/translation.go +++ b/reviewer-basic/i18n/translation.go @@ -20,10 +20,15 @@ package i18n const ( - InfoName = "plugin.basic_reviewer.backend.info.name" - InfoDescription = "plugin.basic_reviewer.backend.info.description" - ConfigReviewPostLabel = "plugin.basic_reviewer.backend.config.review_post.label" - ConfigReviewPostTitle = "plugin.basic_reviewer.backend.config.review_post.title" - ConfigReviewPostDescription = "plugin.basic_reviewer.backend.config.review_post.description" - CommentNeedReview = "plugin.basic_reviewer.backend.comment.need_review" + InfoName = "plugin.basic_reviewer.backend.info.name" + InfoDescription = "plugin.basic_reviewer.backend.info.description" + ConfigReviewPostLabel = "plugin.basic_reviewer.backend.config.review_post.label" + ConfigReviewPostTitle = "plugin.basic_reviewer.backend.config.review_post.title" + ConfigReviewPostDescription = "plugin.basic_reviewer.backend.config.review_post.description" + ConfigReviewPostKeywordsTitle = "plugin.basic_reviewer.backend.config.review_post_keywords.title" + ConfigReviewPostKeywordsDescription = "plugin.basic_reviewer.backend.config.review_post_keywords.description" + ConfigDisallowedKeywordsTitle = "plugin.basic_reviewer.backend.config.disallowed_post_keywords.title" + ConfigDisallowedKeywordsDescription = "plugin.basic_reviewer.backend.config.disallowed_post_keywords.description" + CommentNeedReview = "plugin.basic_reviewer.backend.comment.need_review" + CommentMatchWordReview = "plugin.basic_reviewer.backend.comment.match_word_review" ) diff --git a/reviewer-basic/i18n/zh_CN.yaml b/reviewer-basic/i18n/zh_CN.yaml index 62ca37f..306e954 100644 --- a/reviewer-basic/i18n/zh_CN.yaml +++ b/reviewer-basic/i18n/zh_CN.yaml @@ -31,5 +31,16 @@ plugin: other: 当该作者没有任何已批准的问答内容时,他发布的问答内容需要审核 description: other: 问题或回答在被批准之前是不可见的。 + review_post_keywords: + title: + other: 审核帖子关键词 + description: + other: 当提交的内容、IP地址或浏览器标识符中包含这些单词中的任何一个时,它将进入审核队列中。每行一个关键字或IP地址。它将匹配内部单词,例如“alpha”将匹配“Alphabet”。 + disallowed_post_keywords: + title: + other: 禁止发布的关键词 + description: + other: 当提交的内容、IP地址或浏览器标识符中包含这些单词中的任何一个时,它将被删除。每行一个字或IP地址。它将匹配内部单词,例如“alpha”将匹配“Alphabet”。 comment: - need_review: 需要审核。 \ No newline at end of file + need_review: 需要审核。 + match_word_review: 需要审核, 匹配关键字 "%s". \ No newline at end of file
