like.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/interface/openplatform/article/dao"
  5. artmdl "go-common/app/interface/openplatform/article/model"
  6. thumbupmdl "go-common/app/service/main/thumbup/model"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. )
  10. func (s *Service) isLike(c context.Context, mid, aid int64) (res int8, err error) {
  11. r, err := s.HadLikesByMid(c, mid, []int64{aid})
  12. if err != nil {
  13. return
  14. }
  15. res = r[aid]
  16. return
  17. }
  18. // HadLikesByMid .
  19. func (s *Service) HadLikesByMid(c context.Context, mid int64, aids []int64) (res map[int64]int8, err error) {
  20. if mid == 0 || len(aids) == 0 {
  21. return
  22. }
  23. arg := &thumbupmdl.ArgHasLike{Business: "article", MessageIDs: aids, Mid: mid}
  24. res, err = s.thumbupRPC.HasLike(c, arg)
  25. return
  26. }
  27. // Like like article
  28. func (s *Service) Like(c context.Context, mid, aid int64, likeType int) (err error) {
  29. var art *artmdl.Meta
  30. if (likeType < 0) || (likeType > 4) {
  31. err = ecode.RequestErr
  32. return
  33. }
  34. if art, err = s.ArticleMeta(c, aid); err != nil || art == nil {
  35. err = ecode.NothingFound
  36. return
  37. }
  38. arg := &thumbupmdl.ArgLike{
  39. Mid: mid,
  40. UpMid: art.Author.Mid,
  41. Business: "article",
  42. MessageID: aid,
  43. Type: int8(likeType),
  44. }
  45. if err = s.thumbupRPC.Like(c, arg); err != nil {
  46. dao.PromError("like:thumbup-service")
  47. log.Error("s.thumbupRPC.Like(%+v) err: %+v", arg, err)
  48. }
  49. return
  50. }
  51. // RecommendsWithLike recommends with like state
  52. func (s *Service) RecommendsWithLike(c context.Context, cid int64, pn, ps int, lastAids []int64, sort int, mid int64) (res []*artmdl.RecommendArtWithLike, err error) {
  53. var recs []*artmdl.RecommendArt
  54. if recs, err = s.Recommends(c, cid, pn, ps, lastAids, sort); err != nil {
  55. return
  56. }
  57. var aids []int64
  58. for _, rec := range recs {
  59. aids = append(aids, rec.ID)
  60. }
  61. states, _ := s.HadLikesByMid(c, mid, aids)
  62. for _, rec := range recs {
  63. r := &artmdl.RecommendArtWithLike{RecommendArt: *rec}
  64. if states != nil {
  65. r.LikeState = int(states[rec.ID])
  66. }
  67. res = append(res, r)
  68. }
  69. return
  70. }