like.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/interface/main/web/model"
  5. arcmdl "go-common/app/service/main/archive/api"
  6. "go-common/app/service/main/archive/model/archive"
  7. coinmdl "go-common/app/service/main/coin/api"
  8. favmdl "go-common/app/service/main/favorite/model"
  9. thumbup "go-common/app/service/main/thumbup/model"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. "go-common/library/net/metadata"
  13. "go-common/library/sync/errgroup"
  14. )
  15. const (
  16. _businessLike = "archive"
  17. )
  18. // Like like archive.
  19. func (s *Service) Like(c context.Context, aid, mid int64, like int8) (upperID int64, err error) {
  20. var (
  21. arcReply *arcmdl.ArcReply
  22. ip = metadata.String(c, metadata.RemoteIP)
  23. )
  24. if arcReply, err = s.arcClient.Arc(c, &arcmdl.ArcRequest{Aid: aid}); err != nil {
  25. log.Error("s.arcClient.Arc(%d) error(%v)", aid, err)
  26. return
  27. }
  28. arc := arcReply.Arc
  29. if !arc.IsNormal() {
  30. err = ecode.ArchiveNotExist
  31. return
  32. }
  33. upperID = arc.Author.Mid
  34. err = s.thumbup.Like(c, &thumbup.ArgLike{Mid: mid, UpMid: upperID, Business: _businessLike, MessageID: aid, Type: like, RealIP: ip})
  35. return
  36. }
  37. // LikeTriple like & coin & fav
  38. func (s *Service) LikeTriple(c context.Context, aid, mid int64) (res *model.TripleRes, err error) {
  39. var (
  40. arcReply *arcmdl.ArcReply
  41. ip = metadata.String(c, metadata.RemoteIP)
  42. )
  43. res = new(model.TripleRes)
  44. maxCoin := int64(1)
  45. multiply := int64(1)
  46. if arcReply, err = s.arcClient.Arc(c, &arcmdl.ArcRequest{Aid: aid}); err != nil {
  47. log.Error("s.arcClient.Arc(%d) error(%v)", aid, err)
  48. return
  49. }
  50. a := arcReply.Arc
  51. if !a.IsNormal() {
  52. err = ecode.ArchiveNotExist
  53. return
  54. }
  55. if a.Copyright == int32(archive.CopyrightOriginal) {
  56. maxCoin = 2
  57. multiply = 2
  58. }
  59. res.UpID = a.Author.Mid
  60. eg := errgroup.Group{}
  61. eg.Go(func() (err error) {
  62. if multiply == 2 {
  63. if userCoins, e := s.coinClient.UserCoins(c, &coinmdl.UserCoinsReq{Mid: mid}); e != nil {
  64. log.Error("s.coinClient.UserCoins error(%v)", e)
  65. } else if userCoins != nil {
  66. if userCoins.Count < 1 {
  67. return
  68. }
  69. if userCoins.Count < 2 {
  70. multiply = 1
  71. }
  72. }
  73. }
  74. cArg := &coinmdl.AddCoinReq{
  75. IP: ip,
  76. Mid: mid,
  77. Upmid: a.Author.Mid,
  78. MaxCoin: maxCoin,
  79. Aid: aid,
  80. Business: model.CoinArcBusiness,
  81. Number: multiply,
  82. Typeid: a.TypeID,
  83. PubTime: int64(a.PubDate),
  84. }
  85. if _, err = s.coinClient.AddCoin(c, cArg); err != nil {
  86. log.Error("s.coinClient.AddCoin error(%v)", err)
  87. err = nil
  88. if arcUserCoins, e := s.coinClient.ItemUserCoins(c, &coinmdl.ItemUserCoinsReq{Mid: mid, Aid: aid, Business: model.CoinArcBusiness}); e != nil {
  89. log.Error("s.coinClient.ItemUserCoins error(%v)", e)
  90. } else {
  91. if arcUserCoins != nil && arcUserCoins.Number > 0 {
  92. res.Coin = true
  93. }
  94. }
  95. } else {
  96. res.Multiply = multiply
  97. res.Anticheat = true
  98. res.Coin = true
  99. }
  100. return
  101. })
  102. eg.Go(func() (err error) {
  103. var isFav bool
  104. if isFav, err = s.fav.IsFav(context.Background(), &favmdl.ArgIsFav{Type: favmdl.TypeVideo, Mid: mid, Oid: aid, RealIP: ip}); err != nil {
  105. log.Error("s.fav.IsFav error(%v)", err)
  106. err = nil
  107. } else if isFav {
  108. res.Fav = true
  109. return
  110. }
  111. fArg := &favmdl.ArgAdd{Type: favmdl.TypeVideo, Mid: mid, Oid: aid, Fid: 0, RealIP: ip}
  112. if err = s.fav.Add(c, fArg); err != nil {
  113. if ecode.FavVideoExist.Equal(err) {
  114. res.Fav = true
  115. return
  116. }
  117. log.Error("s.fav.Add error(%v)", err)
  118. err = nil
  119. } else {
  120. res.Fav = true
  121. res.Anticheat = true
  122. }
  123. return
  124. })
  125. eg.Go(func() (err error) {
  126. if err = s.thumbup.Like(c, &thumbup.ArgLike{Mid: mid, UpMid: res.UpID, Business: _businessLike, MessageID: aid, Type: thumbup.TypeLike, RealIP: ip}); err != nil {
  127. if ecode.ThumbupDupLikeErr.Equal(err) {
  128. res.Like = true
  129. return
  130. }
  131. log.Error("s.thumbup.Like error(%v)", err)
  132. err = nil
  133. } else {
  134. res.Like = true
  135. res.Anticheat = true
  136. }
  137. return
  138. })
  139. eg.Wait()
  140. return
  141. }
  142. // HasLike get if has like.
  143. func (s *Service) HasLike(c context.Context, aid, mid int64) (like int8, err error) {
  144. var (
  145. data map[int64]int8
  146. ip = metadata.String(c, metadata.RemoteIP)
  147. )
  148. if data, err = s.thumbup.HasLike(c, &thumbup.ArgHasLike{Business: _businessLike, MessageIDs: []int64{aid}, Mid: mid, RealIP: ip}); err != nil {
  149. log.Error("s.thumbup.HasLike aid(%d) mid(%d) error(%v)", aid, mid, err)
  150. return
  151. }
  152. if v, ok := data[aid]; ok {
  153. like = v
  154. }
  155. return
  156. }