article.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. artmdl "go-common/app/interface/openplatform/article/model"
  6. "go-common/app/job/openplatform/article/dao"
  7. "go-common/app/job/openplatform/article/model"
  8. "go-common/library/conf/env"
  9. "go-common/library/log"
  10. )
  11. func (s *Service) upArticles(c context.Context, action string, newMsg []byte, oldMsg []byte) {
  12. log.Info("s.upArticles action(%s) old(%s) new(%s)", action, string(oldMsg), string(newMsg))
  13. if action != model.ActUpdate && action != model.ActInsert && action != model.ActDelete {
  14. return
  15. }
  16. var (
  17. err error
  18. aid int64
  19. newArticle = &model.Article{}
  20. oldArticle = &model.Article{}
  21. )
  22. if err = json.Unmarshal(newMsg, newArticle); err != nil {
  23. log.Error("json.Unmarshal(%s) error(%+v)", newMsg, err)
  24. dao.PromError("article:解析过审文章databus新内容")
  25. return
  26. }
  27. if action == model.ActUpdate {
  28. if err = json.Unmarshal(oldMsg, oldArticle); err != nil {
  29. log.Error("json.Unmarshal(%s) error(%+v)", oldMsg, err)
  30. dao.PromError("article:解析过审文章databus旧内容")
  31. return
  32. }
  33. }
  34. aid = newArticle.ID
  35. mid := newArticle.Mid
  36. show := true
  37. var comment string
  38. if artmdl.NoDistributeAttr(newArticle.Attributes) {
  39. show = false
  40. comment = "禁止分发"
  41. }
  42. switch action {
  43. case model.ActInsert:
  44. s.openReply(c, aid, mid)
  45. s.addArtCache(c, aid)
  46. s.updateSearchArt(c, newArticle)
  47. s.flowSync(c, aid, mid)
  48. s.addURLNode(c, aid)
  49. if comment == "" {
  50. comment = "文章过审"
  51. }
  52. case model.ActUpdate:
  53. s.updateArtCache(c, aid, oldArticle.CategoryID)
  54. s.addURLNode(c, aid)
  55. if !artmdl.NoDistributeAttr(oldArticle.Attributes) && artmdl.NoDistributeAttr(newArticle.Attributes) {
  56. s.delSearchArt(c, aid)
  57. } else {
  58. s.updateSearchArt(c, newArticle)
  59. }
  60. if comment == "" {
  61. comment = "文章修改"
  62. }
  63. case model.ActDelete:
  64. s.closeReply(c, aid, mid)
  65. s.deleteArtCache(c, aid, mid)
  66. s.deleteArtRecommendCache(c, aid, newArticle.CategoryID)
  67. s.delSearchArt(c, aid)
  68. // s.delMediaScore(c, aid, newArticle.MediaID, mid)
  69. show = false
  70. comment = "文章不可见"
  71. }
  72. if e := s.dao.PubDynamic(c, mid, aid, show, comment, int64(newArticle.PublishTime), newArticle.DynamicIntro); e != nil {
  73. s.dao.PushDynamicCache(c, &model.DynamicCacheRetry{
  74. Aid: aid,
  75. Mid: mid,
  76. Show: show,
  77. Comment: comment,
  78. Ts: int64(newArticle.PublishTime),
  79. DynamicIntro: newArticle.DynamicIntro,
  80. })
  81. }
  82. if env.DeployEnv != env.DeployEnvProd {
  83. return
  84. }
  85. s.gameSync(c, aid, mid, action)
  86. urls := model.ReadURLs(aid)
  87. for _, u := range urls {
  88. if err = s.dao.PurgeCDN(c, u); err == nil {
  89. log.Info("s.dao.PurgeCDN(%s) success.", u)
  90. dao.PromInfo("article:刷新CDN")
  91. }
  92. }
  93. }
  94. func (s *Service) gameSync(c context.Context, aid, mid int64, action string) {
  95. authors, err := s.dao.GameList(c)
  96. if err != nil {
  97. dao.PromError("service:获得游戏数据")
  98. log.Error("s.gameSync(aid: %v, mid: %v) init err: %+v", aid, mid, err)
  99. return
  100. }
  101. var exist bool
  102. for _, author := range authors {
  103. if author == mid {
  104. exist = true
  105. break
  106. }
  107. }
  108. if !exist {
  109. return
  110. }
  111. if err = s.dao.GameSync(c, action, aid); err != nil {
  112. log.Error("s.gameSync(%d, %d, %s) error(%+v)", aid, mid, action, err)
  113. dao.PromError("service:同步游戏数据")
  114. s.dao.PushGameCache(c, &model.GameCacheRetry{
  115. Action: action,
  116. Aid: aid,
  117. })
  118. return
  119. }
  120. log.Info("s.gameSync(%d, %d, %s) success", aid, mid, action)
  121. }
  122. func (s *Service) flowSync(c context.Context, aid, mid int64) {
  123. if err := s.dao.FlowSync(c, mid, aid); err != nil {
  124. s.dao.PushFlowCache(c, &model.FlowCacheRetry{
  125. Aid: aid,
  126. Mid: mid,
  127. })
  128. return
  129. }
  130. log.Info("s.flowSync(aid: %d, mid: %d) success", aid, mid)
  131. }
  132. func (s *Service) openReply(c context.Context, aid, mid int64) (err error) {
  133. if err = s.dao.OpenReply(c, aid, mid); err == nil {
  134. log.Info("OpenReply(%d,%d) success.", aid, mid)
  135. dao.PromInfo("article:打开评论区")
  136. }
  137. return
  138. }
  139. func (s *Service) closeReply(c context.Context, aid, mid int64) (err error) {
  140. if err = s.dao.CloseReply(c, aid, mid); err == nil {
  141. log.Info("CloseReply(%d,%d) success.", aid, mid)
  142. dao.PromInfo("article:关闭评论区")
  143. }
  144. return
  145. }
  146. func (s *Service) addArtCache(c context.Context, aid int64) (err error) {
  147. arg := &artmdl.ArgAid{Aid: aid}
  148. if err = s.articleRPC.AddArticleCache(c, arg); err != nil {
  149. log.Error("s.articleRPC.AddArticleCache(%d) error(%+v)", aid, err)
  150. dao.PromError("article:新增文章缓存")
  151. s.dao.PushArtCache(c, &dao.CacheRetry{
  152. Action: dao.RetryAddArtCache,
  153. Aid: aid,
  154. })
  155. } else {
  156. log.Info("s.articleRPC.AddArticleCache(%d) success", aid)
  157. dao.PromInfo("article:新增文章缓存")
  158. }
  159. return
  160. }
  161. func (s *Service) updateArtCache(c context.Context, aid, cid int64) (err error) {
  162. arg := &artmdl.ArgAidCid{Aid: aid, Cid: cid}
  163. if err = s.articleRPC.UpdateArticleCache(c, arg); err != nil {
  164. log.Error("s.articleRPC.UpdateArticleCache(%d,%d) error(%+v)", aid, cid, err)
  165. dao.PromError("article:更新文章缓存")
  166. s.dao.PushArtCache(c, &dao.CacheRetry{
  167. Action: dao.RetryUpdateArtCache,
  168. Aid: aid,
  169. Cid: cid,
  170. })
  171. } else {
  172. log.Info("s.articleRPC.UpdateArticleCache(%d,%d) success", aid, cid)
  173. dao.PromInfo("article:更新文章缓存")
  174. }
  175. return
  176. }
  177. func (s *Service) deleteArtCache(c context.Context, aid, mid int64) (err error) {
  178. arg := &artmdl.ArgAidMid{Aid: aid, Mid: mid}
  179. if err = s.articleRPC.DelArticleCache(c, arg); err != nil {
  180. log.Error("s.articleRPC.DelArticleCache(%d,%d) error(%+v)", aid, mid, err)
  181. dao.PromError("article:删除文章缓存")
  182. s.dao.PushArtCache(c, &dao.CacheRetry{
  183. Action: dao.RetryDeleteArtCache,
  184. Aid: aid,
  185. Mid: mid,
  186. })
  187. } else {
  188. log.Info("s.articleRPC.DelArticleCache(%d,%d) success", aid, mid)
  189. dao.PromInfo("article:删除文章缓存")
  190. }
  191. return
  192. }
  193. func (s *Service) deleteArtRecommendCache(c context.Context, aid, cid int64) (err error) {
  194. arg := &artmdl.ArgAidCid{Aid: aid, Cid: cid}
  195. if err = s.articleRPC.DelRecommendArtCache(c, arg); err != nil {
  196. log.Error("s.articleRPC.DelRecommendArtCache(%d,%d) error(%+v)", aid, cid, err)
  197. dao.PromError("article:删除文章推荐缓存")
  198. s.dao.PushArtCache(c, &dao.CacheRetry{
  199. Action: dao.RetryDeleteArtRecCache,
  200. Aid: aid,
  201. Cid: cid,
  202. })
  203. } else {
  204. log.Info("s.articleRPC.DelRecommendArtCache(%d,%d) success", aid, cid)
  205. dao.PromInfo("article:删除文章推荐缓存")
  206. }
  207. return
  208. }
  209. func (s *Service) delMediaScore(c context.Context, aid, mediaID, mid int64) (err error) {
  210. err = s.dao.DelScore(c, aid, mediaID, mid)
  211. return
  212. }