46 lines
1.4 KiB
Go
46 lines
1.4 KiB
Go
// unsubscribe.go Day 42:退订标识拦截——营销内容末尾须含「拒收请回复R」类标识(可配)。
|
|
|
|
package interceptor
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
errs "go-sms/internal/pkg/errors"
|
|
)
|
|
|
|
// DefaultUnsubKeywords 默认退订标识关键词。
|
|
var DefaultUnsubKeywords = []string{"拒收请回复R", "拒收请回复r", "回T退订", "回N退订"}
|
|
|
|
// Unsubscribe 退订标识拦截器。
|
|
type Unsubscribe struct {
|
|
Required bool // 是否强制(营销类);false=放行
|
|
Keywords []string // 标识关键词(默认 DefaultUnsubKeywords)
|
|
}
|
|
|
|
// NewUnsubscribe 构造(keywords 空用默认)。
|
|
func NewUnsubscribe(required bool, keywords []string) *Unsubscribe {
|
|
if len(keywords) == 0 {
|
|
keywords = DefaultUnsubKeywords
|
|
}
|
|
return &Unsubscribe{Required: required, Keywords: keywords}
|
|
}
|
|
|
|
// Name 拦截器名。
|
|
func (u *Unsubscribe) Name() string { return "unsubscribe" }
|
|
|
|
// Check Required 时内容(去尾空白)须以任一关键词结尾。
|
|
func (u *Unsubscribe) Check(_ context.Context, r *Req) *Block {
|
|
if u == nil || !u.Required {
|
|
return nil
|
|
}
|
|
tail := strings.TrimRight(r.Content, " \t\r\n")
|
|
for _, k := range u.Keywords {
|
|
if strings.HasSuffix(tail, k) {
|
|
return nil
|
|
}
|
|
}
|
|
e := errs.ContentNonCompliant.WithMsg("内容末尾缺少退订标识(如「拒收请回复R」)")
|
|
return &Block{Interceptor: u.Name(), Code: e.Code, Msg: e.Msg, HTTP: e}
|
|
}
|