message.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. artmdl "go-common/app/interface/openplatform/article/model"
  6. )
  7. var (
  8. _likeMessage = int64(1)
  9. )
  10. // SendMessage send message to uppper
  11. func (s *Service) SendMessage(c context.Context, aid int64, stat *artmdl.Stats) (err error) {
  12. var (
  13. title, msg string
  14. meta *artmdl.Meta
  15. max int64
  16. )
  17. if exist, _ := s.dao.ExpireMaxLikeCache(c, aid); exist {
  18. max, _ = s.dao.MaxLikeCache(c, aid)
  19. }
  20. if (stat.Like <= max) || (!shouldNofify(stat.Like)) {
  21. return
  22. }
  23. if meta, err = s.ArticleMeta(c, aid); (err != nil) || (meta == nil) {
  24. return
  25. }
  26. mid := meta.Author.Mid
  27. if len(s.c.Article.MessageMids) > 0 {
  28. var exist bool
  29. for _, m := range s.c.Article.MessageMids {
  30. if m == mid {
  31. exist = true
  32. break
  33. }
  34. }
  35. if !exist {
  36. return
  37. }
  38. }
  39. title = fmt.Sprintf("有%v人点赞了你的专栏文章", stat.Like)
  40. msg = fmt.Sprintf("有%v个小伙伴点赞你投稿的专栏文章“#{%s}{\"http://www.bilibili.com/read/cv%d\"}”~快去看看吧!#{点击前往}{\"http://www.bilibili.com/read/cv%d\"}", stat.Like, meta.Title, aid, aid)
  41. err = s.dao.SendMessage(c, _likeMessage, mid, aid, title, msg)
  42. cache.Save(func() {
  43. s.dao.SetMaxLikeCache(context.TODO(), aid, stat.Like)
  44. })
  45. return
  46. }
  47. func shouldNofify(n int64) (res bool) {
  48. switch {
  49. case n <= 0:
  50. res = false
  51. case n <= 10:
  52. res = true
  53. case n <= 100:
  54. res = (n%10 == 0)
  55. case n <= 1000:
  56. res = (n%100 == 0)
  57. default:
  58. res = (n%10000 == 0)
  59. }
  60. return
  61. }