2023-07-17 17:44:08 +08:00
|
|
|
package action
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-08-22 17:07:45 +08:00
|
|
|
"github.com/segmentfault/pacman/log"
|
2023-07-18 18:11:18 +08:00
|
|
|
"time"
|
2023-07-17 17:44:08 +08:00
|
|
|
|
|
|
|
"github.com/answerdev/answer/internal/entity"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ValidationStrategy
|
|
|
|
// true pass
|
|
|
|
// false need captcha
|
|
|
|
func (cs *CaptchaService) ValidationStrategy(ctx context.Context, unit, actionType string) bool {
|
|
|
|
info, err := cs.captchaRepo.GetActionType(ctx, unit, actionType)
|
|
|
|
if err != nil {
|
2023-08-22 17:07:45 +08:00
|
|
|
log.Error(err)
|
|
|
|
return false
|
|
|
|
}
|
2023-08-23 17:43:43 +08:00
|
|
|
// If no operation previously, it is considered to be the first operation
|
2023-08-22 17:07:45 +08:00
|
|
|
if info == nil {
|
2023-08-23 17:43:43 +08:00
|
|
|
return true
|
2023-07-17 17:44:08 +08:00
|
|
|
}
|
|
|
|
switch actionType {
|
|
|
|
case entity.CaptchaActionEmail:
|
2023-07-18 18:11:18 +08:00
|
|
|
return cs.CaptchaActionEmail(ctx, unit, info)
|
2023-07-17 17:44:08 +08:00
|
|
|
case entity.CaptchaActionPassword:
|
2023-07-18 18:11:18 +08:00
|
|
|
return cs.CaptchaActionPassword(ctx, unit, info)
|
2023-07-17 17:44:08 +08:00
|
|
|
case entity.CaptchaActionEditUserinfo:
|
2023-07-18 18:11:18 +08:00
|
|
|
return cs.CaptchaActionEditUserinfo(ctx, unit, info)
|
2023-07-17 17:44:08 +08:00
|
|
|
case entity.CaptchaActionQuestion:
|
2023-07-18 18:11:18 +08:00
|
|
|
return cs.CaptchaActionQuestion(ctx, unit, info)
|
2023-07-17 17:44:08 +08:00
|
|
|
case entity.CaptchaActionAnswer:
|
2023-07-18 18:11:18 +08:00
|
|
|
return cs.CaptchaActionAnswer(ctx, unit, info)
|
2023-07-17 17:44:08 +08:00
|
|
|
case entity.CaptchaActionComment:
|
2023-07-18 18:11:18 +08:00
|
|
|
return cs.CaptchaActionComment(ctx, unit, info)
|
2023-07-17 17:44:08 +08:00
|
|
|
case entity.CaptchaActionEdit:
|
2023-07-18 18:11:18 +08:00
|
|
|
return cs.CaptchaActionEdit(ctx, unit, info)
|
2023-07-17 17:44:08 +08:00
|
|
|
case entity.CaptchaActionInvitationAnswer:
|
2023-07-18 18:11:18 +08:00
|
|
|
return cs.CaptchaActionInvitationAnswer(ctx, unit, info)
|
2023-07-17 17:44:08 +08:00
|
|
|
case entity.CaptchaActionSearch:
|
2023-07-18 18:11:18 +08:00
|
|
|
return cs.CaptchaActionSearch(ctx, unit, info)
|
2023-07-17 17:44:08 +08:00
|
|
|
case entity.CaptchaActionReport:
|
2023-07-18 18:11:18 +08:00
|
|
|
return cs.CaptchaActionReport(ctx, unit, info)
|
2023-07-17 17:44:08 +08:00
|
|
|
case entity.CaptchaActionDelete:
|
2023-07-18 18:11:18 +08:00
|
|
|
return cs.CaptchaActionDelete(ctx, unit, info)
|
2023-07-17 17:44:08 +08:00
|
|
|
case entity.CaptchaActionVote:
|
2023-07-18 18:11:18 +08:00
|
|
|
return cs.CaptchaActionVote(ctx, unit, info)
|
2023-07-17 17:44:08 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
//actionType not found
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-07-18 18:11:18 +08:00
|
|
|
func (cs *CaptchaService) CaptchaActionEmail(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
|
2023-07-17 17:44:08 +08:00
|
|
|
// You need a verification code every time
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-07-18 18:11:18 +08:00
|
|
|
func (cs *CaptchaService) CaptchaActionPassword(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
|
|
|
|
setNum := 3
|
|
|
|
setTime := int64(60 * 30) //seconds
|
|
|
|
now := time.Now().Unix()
|
2023-08-02 16:21:02 +08:00
|
|
|
if now-actioninfo.LastTime <= setTime && actioninfo.Num >= setNum {
|
2023-07-18 18:11:18 +08:00
|
|
|
return false
|
|
|
|
}
|
2023-08-02 16:21:02 +08:00
|
|
|
if now-actioninfo.LastTime != 0 && now-actioninfo.LastTime > setTime {
|
2023-07-18 18:11:18 +08:00
|
|
|
cs.captchaRepo.SetActionType(ctx, unit, entity.CaptchaActionPassword, "", 0)
|
|
|
|
}
|
|
|
|
return true
|
2023-07-17 17:44:08 +08:00
|
|
|
}
|
|
|
|
|
2023-07-18 18:11:18 +08:00
|
|
|
func (cs *CaptchaService) CaptchaActionEditUserinfo(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
|
|
|
|
setNum := 3
|
|
|
|
setTime := int64(60 * 30) //seconds
|
|
|
|
now := time.Now().Unix()
|
2023-08-02 16:21:02 +08:00
|
|
|
if now-actioninfo.LastTime <= setTime && actioninfo.Num >= setNum {
|
2023-07-18 18:11:18 +08:00
|
|
|
return false
|
|
|
|
}
|
2023-08-02 16:21:02 +08:00
|
|
|
if now-actioninfo.LastTime != 0 && now-actioninfo.LastTime > setTime {
|
2023-07-18 18:11:18 +08:00
|
|
|
cs.captchaRepo.SetActionType(ctx, unit, entity.CaptchaActionEditUserinfo, "", 0)
|
|
|
|
}
|
|
|
|
return true
|
2023-07-17 17:44:08 +08:00
|
|
|
}
|
|
|
|
|
2023-07-18 18:11:18 +08:00
|
|
|
func (cs *CaptchaService) CaptchaActionQuestion(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
|
2023-08-01 15:01:28 +08:00
|
|
|
setNum := 10
|
2023-07-18 18:11:18 +08:00
|
|
|
setTime := int64(5) //seconds
|
|
|
|
now := time.Now().Unix()
|
|
|
|
if now-actioninfo.LastTime <= setTime || actioninfo.Num >= setNum {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2023-07-17 17:44:08 +08:00
|
|
|
}
|
|
|
|
|
2023-07-18 18:11:18 +08:00
|
|
|
func (cs *CaptchaService) CaptchaActionAnswer(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
|
|
|
|
setNum := 10
|
|
|
|
setTime := int64(5) //seconds
|
|
|
|
now := time.Now().Unix()
|
|
|
|
if now-actioninfo.LastTime <= setTime || actioninfo.Num >= setNum {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2023-07-17 17:44:08 +08:00
|
|
|
}
|
|
|
|
|
2023-07-18 18:11:18 +08:00
|
|
|
func (cs *CaptchaService) CaptchaActionComment(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
|
|
|
|
setNum := 30
|
|
|
|
setTime := int64(1) //seconds
|
|
|
|
now := time.Now().Unix()
|
|
|
|
if now-actioninfo.LastTime <= setTime || actioninfo.Num >= setNum {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2023-07-17 17:44:08 +08:00
|
|
|
}
|
|
|
|
|
2023-07-18 18:11:18 +08:00
|
|
|
func (cs *CaptchaService) CaptchaActionEdit(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
|
|
|
|
setNum := 10
|
|
|
|
if actioninfo.Num >= setNum {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2023-07-17 17:44:08 +08:00
|
|
|
}
|
|
|
|
|
2023-07-18 18:11:18 +08:00
|
|
|
func (cs *CaptchaService) CaptchaActionInvitationAnswer(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
|
|
|
|
setNum := 30
|
|
|
|
if actioninfo.Num >= setNum {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2023-07-17 17:44:08 +08:00
|
|
|
}
|
|
|
|
|
2023-07-18 18:11:18 +08:00
|
|
|
func (cs *CaptchaService) CaptchaActionSearch(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
|
|
|
|
now := time.Now().Unix()
|
|
|
|
setNum := 20
|
|
|
|
setTime := int64(60) //seconds
|
|
|
|
if now-int64(actioninfo.LastTime) <= setTime && actioninfo.Num >= setNum {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if now-actioninfo.LastTime > setTime {
|
|
|
|
cs.captchaRepo.SetActionType(ctx, unit, entity.CaptchaActionSearch, "", 0)
|
|
|
|
}
|
|
|
|
return true
|
2023-07-17 17:44:08 +08:00
|
|
|
}
|
|
|
|
|
2023-07-18 18:11:18 +08:00
|
|
|
func (cs *CaptchaService) CaptchaActionReport(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
|
|
|
|
setNum := 30
|
|
|
|
setTime := int64(1) //seconds
|
|
|
|
now := time.Now().Unix()
|
|
|
|
if now-actioninfo.LastTime <= setTime || actioninfo.Num >= setNum {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2023-07-17 17:44:08 +08:00
|
|
|
}
|
|
|
|
|
2023-07-18 18:11:18 +08:00
|
|
|
func (cs *CaptchaService) CaptchaActionDelete(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
|
|
|
|
setNum := 5
|
|
|
|
setTime := int64(5) //seconds
|
|
|
|
now := time.Now().Unix()
|
|
|
|
if now-actioninfo.LastTime <= setTime || actioninfo.Num >= setNum {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2023-07-17 17:44:08 +08:00
|
|
|
}
|
|
|
|
|
2023-07-18 18:11:18 +08:00
|
|
|
func (cs *CaptchaService) CaptchaActionVote(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
|
|
|
|
setNum := 40
|
|
|
|
if actioninfo.Num >= setNum {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2023-07-17 17:44:08 +08:00
|
|
|
}
|