account.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package account
  2. import (
  3. "context"
  4. "go-common/library/log"
  5. "go-common/library/net/metadata"
  6. "go-common/library/sync/errgroup"
  7. "time"
  8. accmdl "go-common/app/interface/main/creative/model/account"
  9. account "go-common/app/service/main/account/model"
  10. "go-common/library/ecode"
  11. xtime "go-common/library/time"
  12. "sync"
  13. )
  14. const creatorMID = 37090048
  15. // MyInfo get use level.
  16. func (s *Service) MyInfo(c context.Context, mid int64, ip string, now time.Time) (m *accmdl.MyInfo, err error) {
  17. var (
  18. idCkCode = 0
  19. res *account.Profile
  20. )
  21. // init myinfo
  22. m = &accmdl.MyInfo{
  23. Mid: mid,
  24. Level: 1,
  25. Activated: true,
  26. Deftime: xtime.Time(now.Add(time.Duration(14400) * time.Second).Unix()),
  27. DeftimeEnd: xtime.Time(now.Add(time.Duration(15*24) * time.Hour).Unix()),
  28. DeftimeMsg: ecode.String(ecode.VideoupDelayTimeErr.Error()).Message(),
  29. IdentifyInfo: &accmdl.IdentifyInfo{
  30. Code: idCkCode,
  31. Msg: accmdl.IdentifyEnum[idCkCode],
  32. },
  33. UploadSize: map[string]bool{
  34. "4-8": false,
  35. "8-16": false,
  36. },
  37. DmSubtitle: true,
  38. }
  39. if res, err = s.acc.Profile(c, mid, ip); err != nil {
  40. log.Error("s.acc.Profile (%d) error(%v)", mid, err)
  41. return
  42. }
  43. if res == nil {
  44. err = ecode.NothingFound
  45. return
  46. }
  47. if res.Silence == 1 {
  48. m.Banned = true
  49. }
  50. if res.EmailStatus == 1 || res.TelStatus == 1 {
  51. m.Activated = true
  52. }
  53. m.Name = res.Name
  54. m.Face = res.Face
  55. m.Level = int(res.Level)
  56. if _, ok := s.exemptIDCheckUps[mid]; ok {
  57. log.Info("s.exemptIDCheckUps (%d) info(%v)", mid, s.exemptIDCheckUps)
  58. idCkCode = 0
  59. } else {
  60. idCkCode, _ = s.acc.IdentifyInfo(c, mid, 0, ip)
  61. }
  62. m.IdentifyInfo.Code = idCkCode
  63. m.IdentifyInfo.Msg = accmdl.IdentifyEnum[idCkCode] // NOTE: exist check
  64. m = s.ignoreLevelUpAndActivated(m, mid, s.exemptZeroLevelAndAnswerUps)
  65. if _, ok := s.uploadTopSizeUps[mid]; ok {
  66. m.UploadSize["4-8"] = true
  67. m.UploadSize["8-16"] = true
  68. }
  69. return
  70. }
  71. // type为12的白名单人员,免账号激活,免账号升级到1级
  72. func (s *Service) ignoreLevelUpAndActivated(m *accmdl.MyInfo, mid int64, exemptZeroLevelAndAnswerUps map[int64]int64) *accmdl.MyInfo {
  73. if len(exemptZeroLevelAndAnswerUps) > 0 {
  74. _, ok := exemptZeroLevelAndAnswerUps[mid]
  75. if ok && (m.Level < 1 || !m.Activated) {
  76. m.Activated = true
  77. m.Level = 1
  78. }
  79. }
  80. return m
  81. }
  82. // UpInfo get video article pic blink up infos.
  83. func (s *Service) UpInfo(c context.Context, mid int64, ip string) (v *accmdl.UpInfo, err error) {
  84. cache := true
  85. if v, err = s.acc.UpInfoCache(c, mid); err != nil {
  86. s.pCacheMiss.Incr("upinfo_cache")
  87. err = nil
  88. cache = false
  89. } else if v != nil {
  90. s.pCacheHit.Incr("upinfo_cache")
  91. return
  92. }
  93. s.pCacheMiss.Incr("upinfo_cache")
  94. var (
  95. g = &errgroup.Group{}
  96. ctx = context.TODO()
  97. )
  98. v = &accmdl.UpInfo{
  99. Archive: accmdl.NotUp,
  100. Article: accmdl.NotUp,
  101. Pic: accmdl.NotUp,
  102. Blink: accmdl.NotUp,
  103. }
  104. g.Go(func() error {
  105. blikCnt, _ := s.acc.Blink(ctx, mid, ip)
  106. if blikCnt > 0 {
  107. v.Blink = accmdl.IsUp
  108. }
  109. return nil
  110. })
  111. g.Go(func() error {
  112. picCnt, _ := s.acc.Pic(c, mid, ip)
  113. if picCnt > 0 {
  114. v.Pic = accmdl.IsUp
  115. }
  116. return nil
  117. })
  118. g.Go(func() error {
  119. arcCnt, _ := s.archive.UpCount(c, mid)
  120. if arcCnt > 0 {
  121. v.Archive = accmdl.IsUp
  122. }
  123. return nil
  124. })
  125. g.Go(func() error {
  126. isAuthor, _ := s.article.IsAuthor(c, mid, ip)
  127. if isAuthor {
  128. v.Article = accmdl.IsUp
  129. }
  130. return nil
  131. })
  132. g.Wait()
  133. if cache {
  134. s.addCache(func() {
  135. s.acc.AddUpInfoCache(context.Background(), mid, v)
  136. })
  137. }
  138. return
  139. }
  140. // RecFollows 推荐关注
  141. func (s *Service) RecFollows(c context.Context, mid int64) (fs []*accmdl.Friend, err error) {
  142. var (
  143. fsMids = []int64{creatorMID}
  144. shouldFollowMids = []int64{}
  145. infos = make(map[int64]*account.Info)
  146. )
  147. ip := metadata.String(c, metadata.RemoteIP)
  148. if shouldFollowMids, err = s.acc.ShouldFollow(c, mid, fsMids, ip); err != nil {
  149. log.Error("s.acc.ShouldFollow mid(%d)|ip(%s)|error(%v)", mid, ip, err)
  150. return
  151. }
  152. if len(shouldFollowMids) > 0 {
  153. if infos, err = s.acc.Infos(c, shouldFollowMids, ip); err != nil {
  154. log.Error("s.acc.Infos mid(%d)|ip(%s)|shouldFollowMids(%+v)|error(%v)", mid, ip, shouldFollowMids, err)
  155. return
  156. }
  157. for mid, info := range infos {
  158. f := &accmdl.Friend{
  159. Mid: mid,
  160. Sign: info.Sign,
  161. Face: info.Face,
  162. Name: info.Name,
  163. }
  164. if mid == creatorMID {
  165. f.Comment = "关注创作中心,获取更多创作情报"
  166. f.ShouldFollow = 0
  167. }
  168. fs = append(fs, f)
  169. }
  170. }
  171. return
  172. }
  173. // Infos 获取多个UP主的信息
  174. func (s *Service) Infos(c context.Context, mids []int64, ip string) (res map[int64]*account.Info, err error) {
  175. return s.acc.Infos(c, mids, ip)
  176. }
  177. // Relations 批量获取mid与其它用户的关系
  178. func (s *Service) Relations(c context.Context, mid int64, fids []int64, ip string) (res map[int64]int, err error) {
  179. return s.acc.Relations(c, mid, fids, ip)
  180. }
  181. // FRelations 获取用户与mid的关系(Relations的反向)
  182. func (s *Service) FRelations(c context.Context, mid int64, fids []int64, ip string) (res map[int64]int, err error) {
  183. var (
  184. g errgroup.Group
  185. sm sync.RWMutex
  186. )
  187. res = make(map[int64]int)
  188. for _, v := range fids {
  189. g.Go(func() error {
  190. var r map[int64]int
  191. if r, err = s.acc.Relations(c, v, []int64{mid}, ip); err != nil {
  192. return err
  193. }
  194. sm.Lock()
  195. res[v] = r[mid]
  196. sm.Unlock()
  197. return nil
  198. })
  199. }
  200. if err = g.Wait(); err != nil {
  201. log.Error("s.FRelations(%d,%v) error(%v)", mid, fids, err)
  202. }
  203. return
  204. }
  205. // Cards 批量获取用户的Card
  206. func (s *Service) Cards(c context.Context, mids []int64, ip string) (cards map[int64]*account.Card, err error) {
  207. return s.acc.Cards(c, mids, ip)
  208. }