103 lines
2.9 KiB
Go
103 lines
2.9 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
|
|
modentity "spark-server/internal/module/moderation/entity"
|
|
"spark-server/internal/pkg/errs"
|
|
"spark-server/internal/pkg/sensitive"
|
|
)
|
|
|
|
type CreateWordInput struct {
|
|
WordRaw string `json:"word" validate:"required,max=64" label:"field.sensitive_word" errmsg:"required:validate.moderation.word_required"`
|
|
Category int8 `json:"category" validate:"required,min=1,max=9" label:"field.sensitive_category"`
|
|
Action int8 `json:"action" validate:"required,min=1,max=6" label:"field.sensitive_action"`
|
|
MuteSeconds int `json:"mute_seconds" validate:"min=0" label:"field.mute_seconds"`
|
|
Enabled *bool `json:"enabled"`
|
|
}
|
|
|
|
type PatchWordInput struct {
|
|
Category *int8 `json:"category" validate:"omitempty,min=1,max=9"`
|
|
Action *int8 `json:"action" validate:"omitempty,min=1,max=6"`
|
|
MuteSeconds *int `json:"mute_seconds" validate:"omitempty,min=0"`
|
|
Enabled *bool `json:"enabled"`
|
|
WordRaw *string `json:"word" validate:"omitempty,max=64"`
|
|
}
|
|
|
|
func (s *Service) ListWords(ctx context.Context, q string, category int, enabled *bool, page, size int) ([]modentity.Word, int64, error) {
|
|
if !s.Ready() {
|
|
return nil, 0, ErrNotReady
|
|
}
|
|
return s.repo.ListWords(ctx, q, category, enabled, page, size)
|
|
}
|
|
|
|
func (s *Service) CreateWord(ctx context.Context, in *CreateWordInput) (*modentity.Word, error) {
|
|
if !s.Ready() || in == nil {
|
|
return nil, ErrNotReady
|
|
}
|
|
cat := modentity.Category(in.Category)
|
|
act := modentity.Action(in.Action).Effective()
|
|
if !cat.Valid() || !act.Valid() {
|
|
return nil, errs.ErrInvalidArgument
|
|
}
|
|
norm := sensitive.FoldWord(in.WordRaw)
|
|
if norm == "" {
|
|
return nil, errs.ErrInvalidArgument
|
|
}
|
|
enabled := true
|
|
if in.Enabled != nil {
|
|
enabled = *in.Enabled
|
|
}
|
|
w := &modentity.Word{
|
|
WordRaw: in.WordRaw, Word: norm, Category: cat, Action: act,
|
|
MuteSeconds: in.MuteSeconds, Enabled: enabled,
|
|
}
|
|
if err := s.repo.CreateWord(ctx, w); err != nil {
|
|
return nil, err
|
|
}
|
|
return w, nil
|
|
}
|
|
|
|
func (s *Service) PatchWord(ctx context.Context, id int64, in *PatchWordInput) error {
|
|
if !s.Ready() || id <= 0 || in == nil {
|
|
return errs.ErrInvalidArgument
|
|
}
|
|
return s.repo.UpdateWord(ctx, id, func(w *modentity.Word) error {
|
|
if in.WordRaw != nil {
|
|
w.WordRaw = *in.WordRaw
|
|
w.Word = sensitive.FoldWord(w.WordRaw)
|
|
if w.Word == "" {
|
|
return errs.ErrInvalidArgument
|
|
}
|
|
}
|
|
if in.Category != nil {
|
|
c := modentity.Category(*in.Category)
|
|
if !c.Valid() {
|
|
return errs.ErrInvalidArgument
|
|
}
|
|
w.Category = c
|
|
}
|
|
if in.Action != nil {
|
|
a := modentity.Action(*in.Action).Effective()
|
|
if !a.Valid() {
|
|
return errs.ErrInvalidArgument
|
|
}
|
|
w.Action = a
|
|
}
|
|
if in.MuteSeconds != nil {
|
|
w.MuteSeconds = *in.MuteSeconds
|
|
}
|
|
if in.Enabled != nil {
|
|
w.Enabled = *in.Enabled
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func (s *Service) DeleteWord(ctx context.Context, id int64) error {
|
|
if !s.Ready() || id <= 0 {
|
|
return errs.ErrInvalidArgument
|
|
}
|
|
return s.repo.SoftDeleteWord(ctx, id)
|
|
}
|