dm.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "time"
  7. "go-common/app/interface/main/dm/model"
  8. dm2Mdl "go-common/app/interface/main/dm2/model"
  9. "go-common/app/interface/main/dm2/model/oplog"
  10. account "go-common/app/service/main/account/api"
  11. "go-common/library/ecode"
  12. "go-common/library/log"
  13. )
  14. const (
  15. _recallLt = 5 //普通用户每天可以撤回几个
  16. _recallTO = 120 //多久以前的弹幕可以撤回
  17. _recallCap = 20000 // 字幕君
  18. _recallOK = "撤回成功,你还有%s次撤回机会" // 弹幕撤回成功提示
  19. )
  20. // Recall 撤回弹幕
  21. func (s *Service) Recall(c context.Context, mid, cid int64, id int64) (msg string, err error) {
  22. var (
  23. card *account.CardReply
  24. cnt int
  25. ts string
  26. isSuper bool
  27. )
  28. if card, err = s.accountSvc.Card3(c, &account.MidReq{Mid: mid}); err != nil {
  29. log.Error("s.actSvc.Card3(%d) error(%v)", mid, err)
  30. return
  31. }
  32. ts = "无限"
  33. isSuper = card.GetCard().GetRank() >= _recallCap
  34. if !isSuper {
  35. if cnt, err = s.dao.RecallCnt(c, mid); err != nil {
  36. log.Error("s.dao.RecallCnt(%d) error(%v)", mid, err)
  37. return
  38. }
  39. if cnt >= _recallLt {
  40. err = ecode.DMRecallLimit
  41. return
  42. }
  43. ts = strconv.Itoa(_recallLt - cnt - 1)
  44. }
  45. dm, err := s.dao.Index(c, model.SubTypeVideo, cid, id)
  46. if err != nil {
  47. return
  48. }
  49. if dm == nil || !dm.NeedDisplay() || dm.Mid != mid {
  50. err = ecode.DMRecallDeleted
  51. return
  52. }
  53. if (time.Now().Unix()-int64(dm.Ctime)) > _recallTO && !(isSuper && (dm.Pool == 1 || dm.Pool == 2)) {
  54. err = ecode.DMRecallTimeout
  55. return
  56. }
  57. if err = s.EditDMState(c, 1, cid, mid, dm2Mdl.StateUserDelete, oplog.SourcePlayer, oplog.OperatorMember, id); err != nil {
  58. log.Error("s.EditDMStat(%d,%d) error(%v)", cid, id, err)
  59. err = ecode.DMRecallError
  60. return
  61. }
  62. if err = s.dao.UptRecallCnt(c, mid); err != nil {
  63. log.Error("s.dao.Item(%d,%d) error(%v)", cid, mid, err)
  64. err = nil
  65. }
  66. msg = fmt.Sprintf(_recallOK, ts)
  67. return
  68. }
  69. // EditDMState edit dm state used rpc method in dm2.
  70. func (s *Service) EditDMState(c context.Context, tp int32, oid, mid int64, state int32, source oplog.Source, operatorType oplog.OperatorType, dmids ...int64) (err error) {
  71. arg := &dm2Mdl.ArgEditDMState{
  72. Type: tp,
  73. Oid: oid,
  74. Mid: mid,
  75. State: state,
  76. Dmids: dmids,
  77. Source: source,
  78. OperatorType: operatorType,
  79. }
  80. if err = s.dmRPC.EditDMState(c, arg); err != nil {
  81. log.Error("dmRPC.EditDMState(%v) error(%v)", arg, err)
  82. }
  83. return
  84. }
  85. // MidHash 弹幕发送者mid hash.
  86. func (s *Service) MidHash(c context.Context, mid int64) (hash string, err error) {
  87. hash = model.Hash(mid, 0)
  88. return
  89. }
  90. // TransferJob set task to db
  91. func (s *Service) TransferJob(c context.Context, mid, fromCid, toCid int64, offset float64) (err error) {
  92. job, err := s.dao.CheckTransferJob(c, fromCid, toCid)
  93. if err != nil {
  94. log.Error("dao.CheckTransferJob(from:%d,to:%d) err(%v)", fromCid, toCid, err)
  95. return
  96. }
  97. if job != nil && job.FromCID == fromCid && job.ToCID == toCid && job.State != model.TransferJobStatFailed {
  98. err = ecode.DMTransferRepet
  99. return
  100. }
  101. _, err = s.dao.AddTransferJob(c, fromCid, toCid, mid, offset, model.TransferJobStatInit)
  102. if err != nil {
  103. log.Error("dao.AddTransferJob(from:%d,to:%d) err(%v)", fromCid, toCid, err)
  104. }
  105. return
  106. }
  107. // TransferList service
  108. func (s *Service) TransferList(c context.Context, cid int64) (hiss []*model.TransferHistory, err error) {
  109. hiss, err = s.dao.TransferList(c, cid)
  110. if err != nil || len(hiss) == 0 {
  111. return
  112. }
  113. for _, his := range hiss {
  114. cidInfo, err := s.dao.CidInfo(c, his.CID)
  115. if err != nil {
  116. log.Error("dao.CidInfo(%d) err(%v)", cid, err)
  117. continue
  118. }
  119. his.Title = cidInfo.Title
  120. his.PartID = int32(cidInfo.Index)
  121. }
  122. return
  123. }
  124. // TransferRetry change transferjob state
  125. func (s *Service) TransferRetry(c context.Context, id, mid int64) (err error) {
  126. job, err := s.dao.CheckTransferID(c, id)
  127. if err != nil {
  128. log.Error("dao.CheckTransferID(%d) err(%v)", id, err)
  129. return
  130. }
  131. if job.State != model.TransferJobStatFailed || job.MID != mid {
  132. err = ecode.RequestErr
  133. return
  134. }
  135. _, err = s.dao.SetTransferState(c, id, model.TransferJobStatInit)
  136. if err != nil {
  137. log.Error("dao.TransferList(%d %d %d) err(%v)", id, err)
  138. }
  139. return
  140. }
  141. // CheckExist check exit of up id.
  142. func (s *Service) CheckExist(c context.Context, mid, cid int64) (err error) {
  143. sub, err := s.subject(c, 1, cid)
  144. if err != nil {
  145. if ecode.Cause(err) == ecode.NothingFound {
  146. err = ecode.DMTransferNotFound
  147. }
  148. return
  149. }
  150. if sub.Mid == 0 {
  151. err = ecode.DMTransferNotFound
  152. return
  153. }
  154. if sub.Mid != mid {
  155. err = ecode.DMTransferNotBelong
  156. return
  157. }
  158. return
  159. }
  160. // dms get dm list by dmid from database
  161. func (s *Service) dms(c context.Context, tp int32, oid int64, ids []int64) (dms []*model.DM, err error) {
  162. var (
  163. idxMap = make(map[int64]*model.DM)
  164. contentSpe = make(map[int64]*model.ContentSpecial)
  165. special []int64
  166. contents []*model.Content
  167. )
  168. if idxMap, special, err = s.dao.IndexsByID(c, tp, oid, ids); err != nil || len(idxMap) == 0 {
  169. return
  170. }
  171. if contents, err = s.dao.Contents(c, oid, ids); err != nil {
  172. return
  173. }
  174. if len(special) > 0 {
  175. if contentSpe, err = s.dao.ContentsSpecial(c, special); err != nil {
  176. return
  177. }
  178. }
  179. for _, content := range contents {
  180. if dm, ok := idxMap[content.ID]; ok {
  181. dm.Content = content
  182. if dm.Pool == model.PoolSpecial {
  183. if _, ok = contentSpe[dm.ID]; ok {
  184. dm.ContentSpe = contentSpe[dm.ID]
  185. }
  186. }
  187. dms = append(dms, dm)
  188. }
  189. }
  190. return
  191. }