channel.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package web
  2. import (
  3. "context"
  4. tagmdl "go-common/app/interface/main/tag/model"
  5. "go-common/app/interface/main/web-goblin/model/web"
  6. "go-common/app/service/main/archive/api"
  7. "go-common/app/service/main/archive/model/archive"
  8. "go-common/library/log"
  9. "go-common/library/net/metadata"
  10. "go-common/library/sync/errgroup"
  11. )
  12. const (
  13. _chRqCnt = 40
  14. _chDisplayID = 1
  15. _chTypeArc = 3
  16. _chFrom = 1
  17. )
  18. var _emptyArcs = make([]*api.Arc, 0)
  19. // Channel .
  20. func (s *Service) Channel(c context.Context, id, mid int64, buvid string) (channel *web.Channel, err error) {
  21. var (
  22. aids []int64
  23. arcs map[int64]*api.Arc
  24. tagErr error
  25. )
  26. ip := metadata.String(c, metadata.RemoteIP)
  27. channel = new(web.Channel)
  28. if cards, ok := s.channelCards[id]; ok {
  29. for _, card := range cards {
  30. aids = append(aids, card.Value)
  31. }
  32. }
  33. group, errCtx := errgroup.WithContext(c)
  34. group.Go(func() error {
  35. arg := &tagmdl.ArgChannelResource{
  36. Tid: id,
  37. Mid: mid,
  38. RequestCNT: int32(_chRqCnt),
  39. DisplayID: _chDisplayID,
  40. Type: _chTypeArc,
  41. Buvid: buvid,
  42. From: _chFrom,
  43. RealIP: ip,
  44. }
  45. if channelResource, chErr := s.tag.ChannelResources(errCtx, arg); chErr != nil {
  46. log.Error("Channel s.tag.Resources error(%v)", chErr)
  47. } else if channelResource != nil {
  48. aids = append(aids, channelResource.Oids...)
  49. }
  50. return nil
  51. })
  52. group.Go(func() error {
  53. if channel.Tag, tagErr = s.tag.InfoByID(errCtx, &tagmdl.ArgID{ID: id, Mid: mid}); tagErr != nil {
  54. log.Error("Channel s.tag.InfoByID(%d, %d) error(%v)", id, mid, err)
  55. return tagErr
  56. }
  57. return nil
  58. })
  59. if err = group.Wait(); err != nil {
  60. return
  61. }
  62. if len(aids) == 0 {
  63. channel.Archives = _emptyArcs
  64. return
  65. }
  66. if arcs, err = s.arc.Archives3(c, &archive.ArgAids2{Aids: aids, RealIP: ip}); err != nil {
  67. log.Error("Channel s.arc.Archives3(%v) error(%v)", aids, err)
  68. err = nil
  69. channel.Archives = _emptyArcs
  70. return
  71. }
  72. for _, aid := range aids {
  73. if arc, ok := arcs[aid]; ok && arc.IsNormal() {
  74. channel.Archives = append(channel.Archives, arc)
  75. }
  76. }
  77. if len(channel.Archives) > _chRqCnt {
  78. channel.Archives = channel.Archives[:_chRqCnt]
  79. }
  80. return
  81. }