spam.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. accmdl "go-common/app/service/main/account/api"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. )
  9. type spamMessage struct {
  10. Mid int64 `json:"mid"`
  11. IsUp bool `json:"is_up"`
  12. Tp int8 `json:"tp"`
  13. }
  14. func (s *Service) getLevel(c context.Context, mid int64) (cur int, err error) {
  15. arg := &accmdl.MidReq{Mid: mid}
  16. res, err := s.accSrv.Card3(c, arg)
  17. if err != nil {
  18. return
  19. }
  20. cur = int(res.Card.Level)
  21. return
  22. }
  23. func (s *Service) addRecReply(c context.Context, msg *consumerMsg) {
  24. var (
  25. d spamMessage
  26. exp int
  27. code = ecode.OK
  28. )
  29. if err := json.Unmarshal([]byte(msg.Data), &d); err != nil {
  30. log.Error("json.Unmarshal() error(%v)", err)
  31. return
  32. }
  33. count, err := s.spam.IncrReply(c, d.Mid, d.IsUp)
  34. if err != nil {
  35. log.Error("spam.IncrReply(%d) error(%v)", d.Mid, err)
  36. return
  37. }
  38. if d.IsUp && count >= 20 {
  39. exp = 5 * 60 // 5min
  40. code = ecode.ReplyDeniedAsCaptcha
  41. } else if count >= 5 {
  42. exp = 5 * 60 // 5min
  43. code = ecode.ReplyDeniedAsCaptcha
  44. }
  45. if code == ecode.OK {
  46. return
  47. }
  48. if err = s.spam.SetReplyRecSpam(c, d.Mid, code.Code(), exp); err == nil {
  49. log.Info("spam.SetReplyRecSpam(%d, %d, %d)", d.Mid, code, exp)
  50. } else {
  51. log.Error("spam.SetReplyRecSpam(%d, %d, %d), err (%v)", d.Mid, code, exp, err)
  52. }
  53. }
  54. func (s *Service) addDailyReply(c context.Context, msg *consumerMsg) {
  55. var d spamMessage
  56. if err := json.Unmarshal([]byte(msg.Data), &d); err != nil {
  57. log.Error("json.Unmarshal() error(%v)", err)
  58. return
  59. }
  60. ttl, err := s.spam.TTLDailyReply(c, d.Mid)
  61. if err != nil {
  62. log.Error("spam.TTLDailyReply(%d) error(%v)", d.Mid, err)
  63. return
  64. }
  65. count, err := s.spam.IncrDailyReply(c, d.Mid)
  66. if err != nil {
  67. log.Error("spam.IncrDailyReply(%d) error(%v)", d.Mid, err)
  68. return
  69. }
  70. if ttl == -2 || ttl == -1 {
  71. ttl = 24 * 60 * 60 // one day
  72. if err = s.spam.ExpireDailyReply(c, d.Mid, ttl); err != nil {
  73. log.Error("spam.ExpireDailyReply(%d) error(%v)", d.Mid, err)
  74. }
  75. }
  76. var code ecode.Codes
  77. // 23 BBQ 22 火鸟
  78. if d.Tp == 23 || d.Tp == 22 {
  79. if count <= 1000 {
  80. return
  81. }
  82. code = ecode.ReplyDeniedAsCD
  83. } else {
  84. lv, err := s.getLevel(c, d.Mid)
  85. if err != nil {
  86. log.Error("s.getLevel(%d) error(%v)", d.Mid, err)
  87. return
  88. }
  89. switch {
  90. case lv <= 1 && count < 25:
  91. return
  92. case lv == 2 && count < 250:
  93. return
  94. case lv == 3 && count < 300:
  95. return
  96. case lv == 4 && count < 400:
  97. return
  98. case lv >= 5 && count < 800:
  99. return
  100. }
  101. code = ecode.ReplyDeniedAsCaptcha
  102. if count >= 1000 {
  103. code = ecode.ReplyDeniedAsCD
  104. }
  105. }
  106. if err = s.spam.SetReplyDailySpam(c, d.Mid, code.Code(), ttl); err == nil {
  107. log.Info("spam.SetReplyDailySpam(%d, %d, %d)", d.Mid, code, ttl)
  108. } else {
  109. log.Error("spam.SetReplyDailySpam(%d, %d, %d) error(%v)", d.Mid, code, ttl, err)
  110. }
  111. }
  112. func (s *Service) recAct(c context.Context, cmsg *consumerMsg) {
  113. const _exp = 60
  114. var d spamMessage
  115. if err := json.Unmarshal([]byte(cmsg.Data), &d); err != nil {
  116. log.Error("json.Unmarshal() error(%v)", err)
  117. return
  118. }
  119. count, err := s.spam.IncrAct(c, d.Mid)
  120. if err != nil {
  121. log.Error("spam.IncUserRecAct(%d) error(%v)", d.Mid, err)
  122. return
  123. }
  124. if count < 15 {
  125. return
  126. }
  127. if err = s.spam.SetActionRecSpam(c, d.Mid, ecode.ReplyForbidAction.Code(), _exp); err == nil {
  128. log.Info("spam.SetActRecSpam(%d, %d, %d)", d.Mid, ecode.ReplyForbidAction, _exp)
  129. } else {
  130. log.Error("spam.SetActRecSpam(%d, %d, %d) error(%v)", d.Mid, ecode.ReplyForbidAction, _exp, err)
  131. }
  132. }