@@ -9,13 +9,14 @@ package logic
99import (
1010 "errors"
1111 "fmt"
12- "github.com/studygolang/studygolang/model"
13- "github.com/studygolang/studygolang/util"
1412 "os"
1513 "regexp"
1614 "strconv"
1715 "time"
1816
17+ "github.com/studygolang/studygolang/model"
18+ "github.com/studygolang/studygolang/util"
19+
1920 "github.com/gorilla/schema"
2021 "github.com/polaris1119/goutils"
2122 "github.com/polaris1119/logger"
@@ -241,6 +242,32 @@ func NeedCaptcha(user *model.Me) bool {
241242 return false
242243}
243244
245+ // SpamRecord 控制半夜 Spam
246+ // 避免误判,只针对最近 3 天内注册的用户
247+ func SpamRecord (ctx context.Context , user * model.Me , maxNum int ) {
248+ if time .Now ().Add (- 3 * 24 * time .Hour ).After (user .CreatedAt ) {
249+ return
250+ }
251+
252+ redis := nosql .NewRedisFromPool ()
253+ defer redis .Close ()
254+
255+ key := getSpamMidNightNumKey (user .Uid )
256+ publishTimes := goutils .MustInt (redis .GET (key ))
257+ if publishTimes >= maxNum - 1 {
258+ DefaultUser .UpdateUserStatus (ctx , user .Uid , model .UserStatusOutage )
259+
260+ // 将用户 IP 加入黑名单
261+ DefaultRisk .AddBlackIPByUID (user .Uid )
262+
263+ DefaultUser .DeleteUserContent (ctx , user .Uid )
264+
265+ logger .Infoln ("uid=" , user .Uid , "spam, so delete TA's content" )
266+ } else {
267+ redis .SET (key , publishTimes + 1 , 86400 )
268+ }
269+ }
270+
244271// incrPublishTimes 增加用户发布次数
245272func incrPublishTimes (uid int ) {
246273 redis := nosql .NewRedisFromPool ()
@@ -268,6 +295,10 @@ func getLastPublishTimeKey(uid int) string {
268295 return "last:publish:time:user:" + strconv .Itoa (uid )
269296}
270297
298+ func getSpamMidNightNumKey (uid int ) string {
299+ return "spam:mid:night:num:user:" + strconv .Itoa (uid )
300+ }
301+
271302func website () string {
272303 host := "http://"
273304 if WebsiteSetting .OnlyHttps {
0 commit comments