notice.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "html/template"
  6. "strings"
  7. "go-common/app/interface/main/space/model"
  8. accmdl "go-common/app/service/main/account/model"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. )
  12. const _noticeTable = "member_up_notice"
  13. // Notice get notice.
  14. func (s *Service) Notice(c context.Context, mid int64) (res string, err error) {
  15. if _, ok := s.noNoticeMids[mid]; ok {
  16. return
  17. }
  18. var notice *model.Notice
  19. if notice, err = s.dao.Notice(c, mid); err != nil {
  20. return
  21. }
  22. if notice.IsForbid == _noticeForbid {
  23. notice.Notice = ""
  24. }
  25. res = template.HTMLEscapeString(notice.Notice)
  26. return
  27. }
  28. // SetNotice set notice.
  29. func (s *Service) SetNotice(c context.Context, mid int64, notice string) (err error) {
  30. var (
  31. info *accmdl.Profile
  32. preData *model.Notice
  33. )
  34. if info, err = s.realName(c, mid); err != nil {
  35. return
  36. }
  37. if info.Silence == _silenceForbid {
  38. err = ecode.UserDisabled
  39. return
  40. }
  41. if preData, err = s.dao.Notice(c, mid); err != nil {
  42. return
  43. }
  44. if notice == preData.Notice {
  45. err = ecode.NotModified
  46. return
  47. }
  48. if err = s.dao.SetNotice(c, mid, notice); err != nil {
  49. log.Error("s.dao.SetNotice(%d,%s) error(%v)", mid, notice, err)
  50. return
  51. }
  52. s.cache.Do(c, func(c context.Context) {
  53. s.dao.AddCacheNotice(c, mid, &model.Notice{Notice: notice})
  54. })
  55. return
  56. }
  57. // ClearCache del match and object cache
  58. func (s *Service) ClearCache(c context.Context, msg string) (err error) {
  59. var m struct {
  60. Table string `json:"table"`
  61. Old struct {
  62. Mid int64 `json:"mid"`
  63. Notice string `json:"notice"`
  64. IsForbid int `json:"is_forbid"`
  65. } `json:"old,omitempty"`
  66. New struct {
  67. Mid int64 `json:"mid"`
  68. Notice string `json:"notice"`
  69. IsForbid int `json:"is_forbid"`
  70. } `json:"new,omitempty"`
  71. }
  72. if err = json.Unmarshal([]byte(msg), &m); err != nil || m.Table == "" {
  73. log.Error("ClearCache json.Unmarshal msg(%s) error(%v)", msg, err)
  74. return
  75. }
  76. log.Info("ClearCache json.Unmarshal msg(%s)", msg)
  77. if strings.HasPrefix(m.Table, _noticeTable) && (m.Old.IsForbid != m.New.IsForbid || m.Old.Notice != m.New.Notice) {
  78. if err = s.dao.DelCacheNotice(c, m.New.Mid); err != nil {
  79. log.Error("s.dao.DelCacheNotice mid(%d) error(%v)", m.New.Mid, err)
  80. }
  81. }
  82. return
  83. }