Skip to content

Commit

Permalink
harassment: add blockwords
Browse files Browse the repository at this point in the history
  • Loading branch information
wuhan005 committed Aug 17, 2024
1 parent 71fde1d commit c5bc204
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 7 deletions.
17 changes: 13 additions & 4 deletions internal/db/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type UsersStore interface {
GetByEmail(ctx context.Context, email string) (*User, error)
GetByDomain(ctx context.Context, domain string) (*User, error)
Update(ctx context.Context, id uint, opts UpdateUserOptions) error
UpdateHarassmentSetting(ctx context.Context, id uint, typ HarassmentSettingType) error
UpdateHarassmentSetting(ctx context.Context, id uint, options HarassmentSettingOptions) error
Authenticate(ctx context.Context, email, password string) (*User, error)
ChangePassword(ctx context.Context, id uint, oldPassword, newPassword string) error
UpdatePassword(ctx context.Context, id uint, newPassword string) error
Expand All @@ -50,6 +50,7 @@ type User struct {
Intro string `json:"intro"`
Notify NotifyType `json:"notify"`
HarassmentSetting HarassmentSettingType `json:"harassment_setting"`
BlockWords string `json:"-"`
}

type NotifyType string
Expand Down Expand Up @@ -170,15 +171,23 @@ func (db *users) Update(ctx context.Context, id uint, opts UpdateUserOptions) er
return nil
}

func (db *users) UpdateHarassmentSetting(ctx context.Context, id uint, typ HarassmentSettingType) error {
type HarassmentSettingOptions struct {
Type HarassmentSettingType
BlockWords string
}

func (db *users) UpdateHarassmentSetting(ctx context.Context, id uint, options HarassmentSettingOptions) error {
typ := options.Type

switch typ {
case HarassmentSettingNone, HarassmentSettingTypeRegisterOnly:
default:
return errors.Errorf("unexpected harassment setting type: %q", typ)
}

if err := db.WithContext(ctx).Where("id = ?", id).Updates(&User{
HarassmentSetting: typ,
if err := db.WithContext(ctx).Model(&User{}).Where("id = ?", id).Updates(map[string]interface{}{
"HarassmentSetting": typ,
"BlockWords": options.BlockWords,
}).Error; err != nil {
return errors.Wrap(err, "update user")
}
Expand Down
1 change: 1 addition & 0 deletions internal/form/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ type UpdateProfile struct {

type UpdateHarassment struct {
RegisterOnly string `label:"仅允许注册用户"`
BlockWords string `label:"屏蔽词"`
}
12 changes: 12 additions & 0 deletions route/question/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@ func New(ctx context.Context, f form.NewQuestion, pageUser *db.User, recaptcha r
content := f.Content
isPrivate := f.IsPrivate != ""

// 🚨 User's block words check.
if len(pageUser.BlockWords) > 0 {
blockWords := strings.Split(pageUser.BlockWords, ",")
for _, word := range blockWords {
if strings.Contains(content, word) {
ctx.SetError(errors.New(fmt.Sprintf("提问内容中包含了提问箱主人设置的屏蔽词,发送失败")), f)

Check failure on line 166 in route/question/page.go

View workflow job for this annotation

GitHub Actions / Lint

S1039: unnecessary use of fmt.Sprintf (gosimple)
ctx.Success("question/list")
return
}
}
}

// 🚨 Content security check.
censorResponse, err := censor.Text(ctx.Request().Context(), content)
if err != nil {
Expand Down
42 changes: 39 additions & 3 deletions route/user/harassment.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
package user

import (
"fmt"
"strings"

"github.com/sirupsen/logrus"

"github.com/NekoWheel/NekoBox/internal/context"
Expand All @@ -13,12 +16,45 @@ import (
)

func UpdateHarassment(ctx context.Context, f form.UpdateHarassment) {
harassmentSetting := db.HarassmentSettingNone
harassmentSettingType := db.HarassmentSettingNone
if f.RegisterOnly != "" {
harassmentSetting = db.HarassmentSettingTypeRegisterOnly
harassmentSettingType = db.HarassmentSettingTypeRegisterOnly
}

blockWords := f.BlockWords
blockWords = strings.ReplaceAll(blockWords, ",", ",")
blockWords = strings.TrimSpace(blockWords)

words := make([]string, 0)
wordSet := make(map[string]struct{})
for _, word := range strings.Split(blockWords, ",") {
word := strings.TrimSpace(word)
if word == "" {
continue
}
if _, ok := wordSet[word]; ok {
continue
}
wordSet[word] = struct{}{}

if len(word) > 10 {
ctx.SetErrorFlash(fmt.Sprintf("屏蔽词长度不能超过 10 个字符:%s", word))
ctx.Redirect("/user/profile")
return
}
words = append(words, word)
}

if len(words) > 10 {
ctx.SetErrorFlash("屏蔽词不能超过 10 个")
ctx.Redirect("/user/profile")
return
}

if err := db.Users.UpdateHarassmentSetting(ctx.Request().Context(), ctx.User.ID, harassmentSetting); err != nil {
if err := db.Users.UpdateHarassmentSetting(ctx.Request().Context(), ctx.User.ID, db.HarassmentSettingOptions{
Type: harassmentSettingType,
BlockWords: strings.Join(words, ","),
}); err != nil {
logrus.WithContext(ctx.Request().Context()).WithError(err).Error("Failed to update harassment setting")
ctx.SetInternalErrorFlash()
ctx.Redirect("/user/profile")
Expand Down
4 changes: 4 additions & 0 deletions templates/user/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
<span class="uk-text-small"> 仅允许注册用户向我提问</span>
</label>
</div>
<div class="uk-margin">
<label class="uk-form-label">屏蔽词设置(使用半角逗号 <code>,</code> 分隔,最多支持 10 个屏蔽词,每个屏蔽词最大长度为 10)</label>
<input name="block_words" class="uk-input" type="text" value="{{.LoggedUser.BlockWords}}">
</div>
<div class="uk-margin">
<button type="submit" class="uk-button uk-button-primary">更新防骚扰设置</button>
</div>
Expand Down

0 comments on commit c5bc204

Please sign in to comment.