history.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package v1
  2. import (
  3. "context"
  4. "github.com/pkg/errors"
  5. historypb "go-common/app/interface/live/web-ucenter/api/http/v1"
  6. "go-common/app/interface/live/web-ucenter/conf"
  7. "go-common/app/interface/live/web-ucenter/dao"
  8. historydao "go-common/app/interface/live/web-ucenter/dao/history"
  9. "go-common/app/service/live/room/api/liverpc/v2"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. "go-common/library/net/metadata"
  13. )
  14. // Service struct
  15. type Service struct {
  16. c *conf.Config
  17. dao *historydao.Dao
  18. }
  19. // New init
  20. func New(c *conf.Config) (s *Service) {
  21. s = &Service{
  22. c: c,
  23. dao: historydao.New(c),
  24. }
  25. return s
  26. }
  27. // GetHistoryByUid 获取直播历史记录
  28. func (s *Service) GetHistoryByUid(ctx context.Context, req *historypb.GetHistoryReq) (resp *historypb.GetHistoryResp, err error) {
  29. uid, ok := metadata.Value(ctx, metadata.Mid).(int64)
  30. if !ok {
  31. err = errors.Wrap(err, "未取到uid")
  32. return
  33. }
  34. mainHistoryInfo, err := s.dao.GetMainHistory(ctx, int32(uid))
  35. if err != nil {
  36. err = errors.Wrap(err, "Call GetMainHistory err")
  37. return
  38. }
  39. if mainHistoryInfo == nil {
  40. return
  41. }
  42. RoomIds := make([]int64, 0)
  43. for _, v := range mainHistoryInfo {
  44. RoomIds = append(RoomIds, v.RoomId)
  45. }
  46. reply, err := dao.RoomAPI.V2Room.GetByIds(ctx, &v2.RoomGetByIdsReq{Ids: RoomIds})
  47. if err != nil {
  48. err = errors.Wrap(err, "Call GetByIds err")
  49. return
  50. }
  51. if reply.GetCode() != 0 {
  52. err = ecode.Int(int(reply.GetCode()))
  53. return
  54. }
  55. roomInfos := reply.Data
  56. resp = &historypb.GetHistoryResp{}
  57. for _, RoomId := range RoomIds {
  58. list := &historypb.GetHistoryResp_List{}
  59. room, ok := roomInfos[RoomId]
  60. if !ok {
  61. log.Warn("[GetHistoryByUid] req(%v), uid(%d), failed to get room(%d) info from (%v)", req, uid, RoomId, roomInfos)
  62. continue
  63. }
  64. list.Roomid = RoomId
  65. list.Uid = int32(room.Uid)
  66. list.Uname = room.Uname
  67. list.Title = room.Title
  68. list.Face = room.Face
  69. list.LiveStatus = int32(room.LiveStatus)
  70. list.FansNum = int32(room.Attentions)
  71. list.AreaV2Id = int32(room.AreaV2Id)
  72. list.AreaV2Name = room.AreaV2Name
  73. list.LiveStatus = int32(room.LiveStatus)
  74. list.UserCover = room.UserCover
  75. list.AreaV2ParentId = int32(room.AreaV2ParentId)
  76. list.AreaV2ParentName = room.AreaV2ParentName
  77. list.Tags = room.Tags
  78. resp.List = append(resp.List, list)
  79. }
  80. resp.Title = "哔哩哔哩直播 - 观看历史"
  81. resp.Count = int32(len(roomInfos))
  82. return
  83. }
  84. // DelHistory 删除直播历史记录
  85. func (s *Service) DelHistory(ctx context.Context, req *historypb.DelHistoryReq) (resp *historypb.DelHistoryResp, err error) {
  86. uid, ok := metadata.Value(ctx, metadata.Mid).(int64)
  87. if !ok {
  88. err = errors.Wrap(err, "未取到uid")
  89. return
  90. }
  91. reply, err := s.dao.DelHistory(ctx, uid)
  92. resp = &historypb.DelHistoryResp{}
  93. if err != nil || reply != 0 {
  94. err = ecode.Int(int(reply))
  95. return
  96. }
  97. return
  98. }