service.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package service
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/main/space/conf"
  6. "go-common/app/interface/main/space/dao"
  7. tagrpc "go-common/app/interface/main/tag/rpc/client"
  8. artrpc "go-common/app/interface/openplatform/article/rpc/client"
  9. accclient "go-common/app/service/main/account/api"
  10. accwar "go-common/app/service/main/account/api"
  11. accmdl "go-common/app/service/main/account/model"
  12. arcclient "go-common/app/service/main/archive/api"
  13. assrpc "go-common/app/service/main/assist/rpc/client"
  14. coinclient "go-common/app/service/main/coin/api"
  15. favrpc "go-common/app/service/main/favorite/api/gorpc"
  16. fltrpc "go-common/app/service/main/filter/rpc/client"
  17. member "go-common/app/service/main/member/api/gorpc"
  18. "go-common/app/service/main/relation/rpc/client"
  19. thumbup "go-common/app/service/main/thumbup/rpc/client"
  20. upclient "go-common/app/service/main/up/api/v1"
  21. "go-common/library/ecode"
  22. "go-common/library/log"
  23. "go-common/library/sync/pipeline/fanout"
  24. )
  25. // Service service struct.
  26. type Service struct {
  27. c *conf.Config
  28. dao *dao.Dao
  29. // rpc
  30. art *artrpc.Service
  31. ass *assrpc.Service
  32. tag *tagrpc.Service
  33. filter *fltrpc.Service
  34. fav *favrpc.Service
  35. thumbup *thumbup.Service
  36. relation *relation.Service
  37. member *member.Service
  38. // grpc
  39. accClient accwar.AccountClient
  40. arcClient arcclient.ArchiveClient
  41. coinClient coinclient.CoinClient
  42. upClient upclient.UpClient
  43. // cache proc
  44. cache *fanout.Fanout
  45. // noNoticeMids
  46. noNoticeMids map[int64]struct{}
  47. BlacklistValue map[int64]struct{}
  48. }
  49. // New new service.
  50. func New(c *conf.Config) *Service {
  51. s := &Service{
  52. c: c,
  53. dao: dao.New(c),
  54. art: artrpc.New(c.ArticleRPC),
  55. ass: assrpc.New(c.AssistRPC),
  56. tag: tagrpc.New2(c.TagRPC),
  57. fav: favrpc.New2(c.FavoriteRPC),
  58. filter: fltrpc.New(c.FilterRPC),
  59. thumbup: thumbup.New(c.ThumbupRPC),
  60. relation: relation.New(c.RelationRPC),
  61. member: member.New(c.MemberRPC),
  62. cache: fanout.New("cache"),
  63. }
  64. var err error
  65. if s.accClient, err = accclient.NewClient(c.AccClient); err != nil {
  66. panic(err)
  67. }
  68. if s.arcClient, err = arcclient.NewClient(c.ArcClient); err != nil {
  69. panic(err)
  70. }
  71. if s.coinClient, err = coinclient.NewClient(c.CoinClient); err != nil {
  72. panic(err)
  73. }
  74. if s.upClient, err = upclient.NewClient(c.UpClient); err != nil {
  75. panic(err)
  76. }
  77. s.initMids()
  78. go s.loadBlacklist()
  79. return s
  80. }
  81. // Ping ping service
  82. func (s *Service) Ping(c context.Context) (err error) {
  83. if err = s.dao.Ping(c); err != nil {
  84. log.Error("s.dao.Ping error(%v)", err)
  85. }
  86. return
  87. }
  88. func (s *Service) initMids() {
  89. tmp := make(map[int64]struct{}, len(s.c.Rule.NoNoticeMids))
  90. for _, id := range s.c.Rule.NoNoticeMids {
  91. tmp[id] = struct{}{}
  92. }
  93. s.noNoticeMids = tmp
  94. }
  95. func (s *Service) realName(c context.Context, mid int64) (profile *accmdl.Profile, err error) {
  96. var reply *accwar.ProfileReply
  97. if reply, err = s.accClient.Profile3(c, &accwar.MidReq{Mid: mid}); err != nil || reply == nil {
  98. log.Error("s.accClient.Profile3(%d) error(%v)", mid, err)
  99. return
  100. }
  101. profile = reply.Profile
  102. if !s.c.Rule.RealNameOn {
  103. return
  104. }
  105. if profile.Identification == 0 && profile.TelStatus == 0 {
  106. err = ecode.UserCheckNoPhone
  107. return
  108. }
  109. if profile.Identification == 0 && profile.TelStatus == 2 {
  110. err = ecode.UserCheckInvalidPhone
  111. return
  112. }
  113. return
  114. }
  115. func (s *Service) privacyCheck(c context.Context, vmid int64, field string) (err error) {
  116. privacy := s.privacy(c, vmid)
  117. if value, ok := privacy[field]; !ok || value != _defaultPrivacy {
  118. err = ecode.SpaceNoPrivacy
  119. return
  120. }
  121. return
  122. }
  123. // loadBlacklist load spack blacklist
  124. func (s *Service) loadBlacklist() {
  125. for {
  126. time.Sleep(time.Duration(conf.Conf.Rule.BlackFre))
  127. s.Blacklist(context.Background())
  128. }
  129. }