view.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package service
  2. import (
  3. "context"
  4. "strconv"
  5. "go-common/app/interface/openplatform/article/dao"
  6. "go-common/app/interface/openplatform/article/model"
  7. account "go-common/app/service/main/account/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. "go-common/library/sync/errgroup"
  11. )
  12. // ViewInfo get view info
  13. func (s *Service) ViewInfo(c context.Context, mid, id int64, ip string, cheat *model.CheatInfo, plat int8, from string) (res *model.ViewInfo, err error) {
  14. var art *model.Meta
  15. group := &errgroup.Group{}
  16. if art, err = s.ArticleMeta(c, id); (err != nil) || (art == nil) || (!art.IsNormal()) {
  17. err = ecode.NothingFound
  18. return
  19. }
  20. res = &model.ViewInfo{
  21. Title: art.Title,
  22. BannerURL: art.BannerURL,
  23. Mid: art.Author.Mid,
  24. ImageURLs: art.ImageURLs,
  25. OriginImageURLs: art.OriginImageURLs,
  26. ShowLaterWatch: s.setting.ShowLaterWatch,
  27. ShowSmallWindow: s.setting.ShowSmallWindow,
  28. }
  29. res.Shareable = !art.AttrVal(model.AttrBitNoDistribute)
  30. if mid > 0 {
  31. res.IsAuthor, _, _ = s.IsAuthor(c, mid)
  32. group.Go(func() error {
  33. res.Like, _ = s.isLike(c, mid, id)
  34. return nil
  35. })
  36. group.Go(func() error {
  37. if art.Author != nil {
  38. res.Attention, _ = s.isAttention(c, mid, art.Author.Mid)
  39. }
  40. return nil
  41. })
  42. group.Go(func() error {
  43. res.Favorite, _ = s.IsFav(c, mid, id)
  44. return nil
  45. })
  46. group.Go(func() error {
  47. res.Coin, _ = s.Coin(c, mid, id, ip)
  48. return nil
  49. })
  50. }
  51. group.Go(func() error {
  52. if stat, e := s.stat(c, id); (e == nil) && (stat != nil) {
  53. res.Stats = *stat
  54. res.Stats.Dynamic, _ = s.dao.DynamicCount(c, id)
  55. }
  56. return nil
  57. })
  58. group.Go(func() error {
  59. var lid int64
  60. lists, _ := s.dao.ArtsList(c, []int64{id})
  61. if lists[id] != nil {
  62. res.InList = true
  63. lid = lists[id].ID
  64. }
  65. if mid > 0 {
  66. s.AddHistory(c, mid, id, lid, ip, plat, from)
  67. }
  68. return nil
  69. })
  70. group.Go(func() error {
  71. info, _ := s.authorDetail(c, art.Author.Mid)
  72. if info != nil {
  73. res.AuthorName = info.Name
  74. }
  75. return nil
  76. })
  77. cache.Save(func() {
  78. if mid == 0 || from == "articleSlide" {
  79. return
  80. }
  81. if info, _ := s.accountInfo(context.TODO(), mid); info != nil {
  82. cheat.Lv = strconv.FormatInt(int64(info.Level), 10)
  83. }
  84. s.dao.PubView(context.TODO(), mid, id, ip, cheat)
  85. })
  86. group.Wait()
  87. return
  88. }
  89. func (s *Service) isAttention(c context.Context, mid, up int64) (ok bool, err error) {
  90. arg := account.ArgRelation{Mid: mid, Owner: up}
  91. relation, err := s.accountRPC.Relation3(c, &arg)
  92. if err != nil {
  93. dao.PromError("view:获取关注列表")
  94. log.Error("s.accountRPC.Relation2(%+v) err: %+v", arg, err)
  95. return
  96. }
  97. ok = relation.Following
  98. return
  99. }
  100. func (s *Service) isAttentions(c context.Context, mid int64, ups []int64) (res map[int64]bool, err error) {
  101. arg := account.ArgRelations{Mid: mid, Owners: ups}
  102. relations, err := s.accountRPC.Relations3(c, &arg)
  103. if err != nil {
  104. dao.PromError("view:批量获取关注列表")
  105. log.Error("s.accountRPC.Relations3(%+v) err: %+v", arg, err)
  106. return
  107. }
  108. res = make(map[int64]bool)
  109. for id, r := range relations {
  110. res[id] = r.Following
  111. }
  112. return
  113. }
  114. func (s *Service) isBlacks(c context.Context, mid int64, ups []int64) (res map[int64]struct{}, err error) {
  115. arg := account.ArgMid{Mid: mid}
  116. res, err = s.accountRPC.Blacks3(c, &arg)
  117. if err != nil {
  118. dao.PromError("view:获取黑名单列表")
  119. log.Error("s.accountRPC.Blacks3(%+v) err: %+v", arg, err)
  120. return
  121. }
  122. return
  123. }
  124. func (s *Service) checkArticle(c context.Context, id int64) (err error) {
  125. var art *model.Meta
  126. if art, err = s.ArticleMeta(c, id); (err != nil) || (art == nil) {
  127. err = ecode.NothingFound
  128. return
  129. }
  130. return
  131. }