rpc.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. package server
  2. import (
  3. "go-common/app/interface/openplatform/article/model"
  4. "go-common/app/interface/openplatform/article/service"
  5. "go-common/library/ecode"
  6. "go-common/library/net/rpc"
  7. "go-common/library/net/rpc/context"
  8. )
  9. // RPC .
  10. type RPC struct {
  11. s *service.Service
  12. }
  13. // New creates rpc server.
  14. func New(s *service.Service) (svr *rpc.Server) {
  15. r := &RPC{s: s}
  16. svr = rpc.NewServer(nil)
  17. if err := svr.Register(r); err != nil {
  18. panic(err)
  19. }
  20. return
  21. }
  22. // Ping checks connection success.
  23. func (r *RPC) Ping(c context.Context, arg *struct{}, res *struct{}) (err error) {
  24. return
  25. }
  26. // Auth check connection success.
  27. func (r *RPC) Auth(c context.Context, arg *rpc.Auth, res *struct{}) (err error) {
  28. return
  29. }
  30. // AddArticle adds article when article passed. purge cache.
  31. func (r *RPC) AddArticle(c context.Context, arg *model.ArgArticle, res *int64) (err error) {
  32. var (
  33. aid int64
  34. a = model.TransformArticle(arg)
  35. )
  36. if aid, err = r.s.AddArticle(c, a, arg.ActivityID, arg.ListID, arg.RealIP); err != nil {
  37. return
  38. }
  39. *res = aid
  40. return
  41. }
  42. // UpdateArticle updates article when article passed. purge cache.
  43. func (r *RPC) UpdateArticle(c context.Context, arg *model.ArgArticle, res *struct{}) (err error) {
  44. var a = model.TransformArticle(arg)
  45. if err = r.s.UpdateArticle(c, a, arg.ActivityID, arg.ListID, arg.RealIP); err != nil {
  46. return
  47. }
  48. return
  49. }
  50. // CreationArticle gets article info.
  51. func (r *RPC) CreationArticle(c context.Context, arg *model.ArgAidMid, res *model.Article) (err error) {
  52. var rr *model.Article
  53. if rr, err = r.s.CreationArticle(c, arg.Aid, arg.Mid); err == nil {
  54. *res = *rr
  55. }
  56. return
  57. }
  58. // DelArticle drops article when article not passed. purge cache.
  59. func (r *RPC) DelArticle(c context.Context, arg *model.ArgAidMid, res *struct{}) error {
  60. return r.s.DelArticle(c, arg.Aid, arg.Mid)
  61. }
  62. // AddArticleCache adds article cache.
  63. func (r *RPC) AddArticleCache(c context.Context, arg *model.ArgAid, res *struct{}) (err error) {
  64. err = r.s.AddArticleCache(c, arg.Aid)
  65. return
  66. }
  67. // UpdateArticleCache adds article cache.
  68. func (r *RPC) UpdateArticleCache(c context.Context, arg *model.ArgAidCid, res *struct{}) (err error) {
  69. err = r.s.UpdateArticleCache(c, arg.Aid, arg.Cid)
  70. return
  71. }
  72. // DelArticleCache adds article cache.
  73. func (r *RPC) DelArticleCache(c context.Context, arg *model.ArgAidMid, res *struct{}) (err error) {
  74. err = r.s.DelArticleCache(c, arg.Mid, arg.Aid)
  75. return
  76. }
  77. // CreationWithdrawArticle author withdraw model.
  78. func (r *RPC) CreationWithdrawArticle(c context.Context, arg *model.ArgAidMid, res *struct{}) (err error) {
  79. err = r.s.CreationWithdrawArticle(c, arg.Mid, arg.Aid)
  80. return
  81. }
  82. // Categories list article categories
  83. func (r *RPC) Categories(c context.Context, arg *model.ArgIP, res *model.Categories) (err error) {
  84. *res, err = r.s.ListCategories(c, arg.RealIP)
  85. return
  86. }
  87. // CategoriesMap list article categories map
  88. func (r *RPC) CategoriesMap(c context.Context, arg *model.ArgIP, res *map[int64]*model.Category) (err error) {
  89. *res, err = r.s.ListCategoriesMap(c, arg.RealIP)
  90. return
  91. }
  92. // CreationUpperArticles gets upper's article list for creation center.
  93. func (r *RPC) CreationUpperArticles(c context.Context, arg *model.ArgCreationArts, res *model.CreationArts) (err error) {
  94. var rr *model.CreationArts
  95. if rr, err = r.s.CreationUpperArticlesMeta(c, arg.Mid, arg.Group, arg.Category, arg.Sort, arg.Pn, arg.Ps, arg.RealIP); err == nil {
  96. *res = *rr
  97. }
  98. return
  99. }
  100. // SetStat set all stat cache(redis)
  101. func (r *RPC) SetStat(c context.Context, arg *model.ArgStats, res *struct{}) (err error) {
  102. err = r.s.SetStat(c, arg.Aid, arg.Stats)
  103. return
  104. }
  105. // UpsArtMetas list passed article meta of ups
  106. func (r *RPC) UpsArtMetas(c context.Context, arg *model.ArgUpsArts, res *map[int64][]*model.Meta) (err error) {
  107. if arg.Pn < 1 {
  108. arg.Pn = 1
  109. }
  110. var (
  111. start = (arg.Pn - 1) * arg.Ps
  112. end = start + arg.Ps - 1
  113. )
  114. *res, err = r.s.UpsArticleMetas(c, arg.Mids, start, end)
  115. return
  116. }
  117. // UpArtMetas list up article metas
  118. func (r *RPC) UpArtMetas(c context.Context, arg *model.ArgUpArts, res *model.UpArtMetas) (err error) {
  119. if arg.Pn < 1 {
  120. arg.Pn = 1
  121. }
  122. if arg.Ps <= 0 {
  123. err = ecode.RequestErr
  124. return
  125. }
  126. var rr *model.UpArtMetas
  127. if rr, err = r.s.UpArticleMetas(c, arg.Mid, arg.Pn, arg.Ps, arg.Sort); err == nil {
  128. *res = *rr
  129. }
  130. return
  131. }
  132. // ArticleMetas list article metas
  133. func (r *RPC) ArticleMetas(c context.Context, arg *model.ArgAids, res *map[int64]*model.Meta) (err error) {
  134. *res, err = r.s.ArticleMetas(c, arg.Aids)
  135. return
  136. }
  137. // UpdateRecommends refresh recommend data.
  138. func (r *RPC) UpdateRecommends(c context.Context, arg *model.ArgIP, res *struct{}) (err error) {
  139. err = r.s.UpdateRecommends(c)
  140. return
  141. }
  142. // Recommends list recommend articles
  143. func (r *RPC) Recommends(c context.Context, arg *model.ArgRecommends, res *[]*model.Meta) (err error) {
  144. rs, err := r.s.Recommends(c, arg.Cid, arg.Pn, arg.Ps, arg.Aids, arg.Sort)
  145. if err != nil {
  146. return
  147. }
  148. for _, r := range rs {
  149. *res = append(*res, &r.Meta)
  150. }
  151. return
  152. }
  153. // AddArtDraft adds or updates draft.
  154. func (r *RPC) AddArtDraft(c context.Context, arg *model.ArgArticle, res *int64) (err error) {
  155. d := model.TransformDraft(arg)
  156. *res, err = r.s.AddArtDraft(c, d)
  157. return
  158. }
  159. // DelArtDraft .
  160. func (r *RPC) DelArtDraft(c context.Context, arg *model.ArgAidMid, res *struct{}) (err error) {
  161. err = r.s.DelArtDraft(c, arg.Aid, arg.Mid)
  162. return
  163. }
  164. // ArtDraft get article draft
  165. func (r *RPC) ArtDraft(c context.Context, arg *model.ArgAidMid, res *model.Draft) (err error) {
  166. var v *model.Draft
  167. if v, err = r.s.ArtDraft(c, arg.Aid, arg.Mid); err == nil && v != nil {
  168. *res = *v
  169. }
  170. return
  171. }
  172. // UpperDrafts get article drafts by mid
  173. func (r *RPC) UpperDrafts(c context.Context, arg *model.ArgUpDraft, res *model.Drafts) (err error) {
  174. var v *model.Drafts
  175. if v, err = r.s.UpperDrafts(c, arg.Mid, arg.Pn, arg.Ps); err == nil && v != nil {
  176. *res = *v
  177. }
  178. return
  179. }
  180. // ArticleRemainCount returns the number that user could be use to posting new articles.
  181. func (r *RPC) ArticleRemainCount(c context.Context, arg *model.ArgMid, res *int) (err error) {
  182. *res, err = r.s.ArticleRemainCount(c, arg.Mid)
  183. return
  184. }
  185. // DelRecommendArtCache del recommend article cache
  186. func (r *RPC) DelRecommendArtCache(c context.Context, arg *model.ArgAidCid, res *struct{}) (err error) {
  187. err = r.s.DelRecommendArtCache(c, arg.Aid, arg.Cid)
  188. return
  189. }
  190. // Favorites list user's favorite articles
  191. func (r *RPC) Favorites(c context.Context, arg *model.ArgFav, res *[]*model.Favorite) (err error) {
  192. *res, _, err = r.s.ValidFavs(c, arg.Mid, 0, arg.Pn, arg.Ps, arg.RealIP)
  193. return
  194. }
  195. // UpdateAuthorCache update author cache
  196. func (r *RPC) UpdateAuthorCache(c context.Context, arg *model.ArgAuthor, res *struct{}) (err error) {
  197. err = r.s.UpdateAuthorCache(c, arg.Mid)
  198. return
  199. }
  200. // UpdateSortCache update sort cache
  201. func (r *RPC) UpdateSortCache(c context.Context, arg *model.ArgSort, res *struct{}) (err error) {
  202. err = r.s.UpdateSortCache(c, arg.Aid, arg.Changed, arg.RealIP)
  203. return
  204. }
  205. // IsAuthor checks that whether user has permission to write model.
  206. func (r *RPC) IsAuthor(c context.Context, arg *model.ArgMid, res *bool) (err error) {
  207. *res, _, err = r.s.IsAuthor(c, arg.Mid)
  208. return
  209. }
  210. // NewArticleCount get new article count since given pubtime
  211. func (r *RPC) NewArticleCount(c context.Context, arg *model.ArgNewArt, res *int64) (err error) {
  212. *res, err = r.s.NewArticleCount(c, arg.PubTime)
  213. return
  214. }
  215. // HadLikesByMid check user if has liked articles
  216. func (r *RPC) HadLikesByMid(c context.Context, arg *model.ArgMidAids, res *map[int64]int8) (err error) {
  217. *res, err = r.s.HadLikesByMid(c, arg.Mid, arg.Aids)
  218. return
  219. }
  220. // UpMoreArts get upper more arts
  221. func (r *RPC) UpMoreArts(c context.Context, arg *model.ArgAid, res *[]*model.Meta) (err error) {
  222. *res, err = r.s.MoreArts(c, arg.Aid)
  223. return
  224. }
  225. // CreationUpStat creation up stat
  226. func (r *RPC) CreationUpStat(c context.Context, arg *model.ArgMid, res *model.UpStat) (err error) {
  227. *res, err = r.s.UpStat(c, arg.Mid)
  228. return
  229. }
  230. // CreationUpThirtyDayStat creation up thirty day stat
  231. func (r *RPC) CreationUpThirtyDayStat(c context.Context, arg *model.ArgMid, res *[]*model.ThirtyDayArticle) (err error) {
  232. *res, err = r.s.UpThirtyDayStat(c, arg.Mid)
  233. return
  234. }
  235. // UpLists get upper article lists
  236. func (r *RPC) UpLists(c context.Context, arg *model.ArgMid, res *model.UpLists) (err error) {
  237. *res, err = r.s.UpLists(c, arg.Mid, model.ListSortPtime)
  238. return
  239. }
  240. // RebuildAllListReadCount rebuild all list read count
  241. func (r *RPC) RebuildAllListReadCount(c context.Context, arg *struct{}, res *struct{}) (err error) {
  242. r.s.RebuildAllListReadCount(c)
  243. return
  244. }
  245. // UpdateHotspots update hotspots
  246. func (r *RPC) UpdateHotspots(c context.Context, arg *model.ArgForce, res *struct{}) (err error) {
  247. err = r.s.UpdateHotspots(arg.Force)
  248. return
  249. }