server.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package grpc
  2. import (
  3. "context"
  4. pb "go-common/app/service/main/push/api/grpc/v1"
  5. "go-common/app/service/main/push/model"
  6. "go-common/app/service/main/push/service"
  7. "go-common/library/net/rpc/warden"
  8. )
  9. // New warden rpc server
  10. func New(c *warden.ServerConfig, svc *service.Service) *warden.Server {
  11. svr := warden.NewServer(c)
  12. pb.RegisterPushServer(svr.Server(), &server{svc: svc})
  13. ws, err := svr.Start()
  14. if err != nil {
  15. panic(err)
  16. }
  17. return ws
  18. }
  19. type server struct {
  20. svc *service.Service
  21. }
  22. // AddReport 上报
  23. func (s *server) AddReport(ctx context.Context, req *pb.AddReportRequest) (reply *pb.AddReportReply, err error) {
  24. reply = new(pb.AddReportReply)
  25. if req.Report == nil {
  26. return
  27. }
  28. r := req.Report
  29. err = s.svc.AddReport(ctx, &model.Report{
  30. APPID: int64(r.APPID),
  31. PlatformID: int(r.PlatformID),
  32. Mid: r.Mid,
  33. Buvid: r.Buvid,
  34. DeviceToken: r.DeviceToken,
  35. Build: int(r.Build),
  36. TimeZone: int(r.TimeZone),
  37. NotifySwitch: int(r.NotifySwitch),
  38. DeviceBrand: r.DeviceBrand,
  39. DeviceModel: r.DeviceModel,
  40. OSVersion: r.OSVersion,
  41. Extra: r.Extra,
  42. })
  43. return
  44. }
  45. // DelReport 删除上报
  46. func (s *server) DelReport(ctx context.Context, req *pb.DelReportRequest) (reply *pb.DelReportReply, err error) {
  47. reply = new(pb.DelReportReply)
  48. err = s.svc.DelReport(ctx, int64(req.APPID), req.Mid, req.DeviceToken)
  49. return
  50. }
  51. // DelInvalidReports 删除无效token
  52. func (s *server) DelInvalidReports(ctx context.Context, req *pb.DelInvalidReportsRequest) (reply *pb.DelInvalidReportsReply, err error) {
  53. reply = new(pb.DelInvalidReportsReply)
  54. err = s.svc.DelInvalidReports(ctx, int(req.Type))
  55. return
  56. }
  57. // AddReportCache 上报缓存
  58. func (s *server) AddReportCache(ctx context.Context, req *pb.AddReportCacheRequest) (reply *pb.AddReportCacheReply, err error) {
  59. reply = new(pb.AddReportCacheReply)
  60. if req.Report == nil {
  61. return
  62. }
  63. r := req.Report
  64. err = s.svc.AddReportCache(ctx, &model.Report{
  65. APPID: int64(r.APPID),
  66. PlatformID: int(r.PlatformID),
  67. Mid: r.Mid,
  68. Buvid: r.Buvid,
  69. DeviceToken: r.DeviceToken,
  70. Build: int(r.Build),
  71. TimeZone: int(r.TimeZone),
  72. NotifySwitch: int(r.NotifySwitch),
  73. DeviceBrand: r.DeviceBrand,
  74. DeviceModel: r.DeviceModel,
  75. OSVersion: r.OSVersion,
  76. Extra: r.Extra,
  77. })
  78. return
  79. }
  80. // AddUserReportCache 用户的上报缓存
  81. func (s *server) AddUserReportCache(ctx context.Context, req *pb.AddUserReportCacheRequest) (reply *pb.AddUserReportCacheReply, err error) {
  82. reply = new(pb.AddUserReportCacheReply)
  83. var reports []*model.Report
  84. for _, r := range req.Reports {
  85. reports = append(reports, &model.Report{
  86. APPID: int64(r.APPID),
  87. PlatformID: int(r.PlatformID),
  88. Mid: r.Mid,
  89. Buvid: r.Buvid,
  90. DeviceToken: r.DeviceToken,
  91. Build: int(r.Build),
  92. TimeZone: int(r.TimeZone),
  93. NotifySwitch: int(r.NotifySwitch),
  94. DeviceBrand: r.DeviceBrand,
  95. DeviceModel: r.DeviceModel,
  96. OSVersion: r.OSVersion,
  97. Extra: r.Extra,
  98. })
  99. }
  100. err = s.svc.AddUserReportCache(ctx, req.Mid, reports)
  101. return
  102. }
  103. // AddTokenCache token缓存
  104. func (s *server) AddTokenCache(ctx context.Context, req *pb.AddTokenCacheRequest) (reply *pb.AddTokenCacheReply, err error) {
  105. reply = new(pb.AddTokenCacheReply)
  106. if req.Report == nil {
  107. return
  108. }
  109. r := req.Report
  110. err = s.svc.AddTokenCache(ctx, &model.Report{
  111. APPID: int64(r.APPID),
  112. PlatformID: int(r.PlatformID),
  113. Mid: r.Mid,
  114. Buvid: r.Buvid,
  115. DeviceToken: r.DeviceToken,
  116. Build: int(r.Build),
  117. TimeZone: int(r.TimeZone),
  118. NotifySwitch: int(r.NotifySwitch),
  119. DeviceBrand: r.DeviceBrand,
  120. DeviceModel: r.DeviceModel,
  121. OSVersion: r.OSVersion,
  122. Extra: r.Extra,
  123. })
  124. return
  125. }
  126. // AddTokensCache 批量token缓存
  127. func (s *server) AddTokensCache(ctx context.Context, req *pb.AddTokensCacheRequest) (reply *pb.AddTokensCacheReply, err error) {
  128. reply = new(pb.AddTokensCacheReply)
  129. if len(req.Reports) == 0 {
  130. return
  131. }
  132. reports := make(map[string]*model.Report, len(req.Reports))
  133. for _, v := range req.Reports {
  134. reports[v.DeviceToken] = &model.Report{
  135. APPID: int64(v.APPID),
  136. PlatformID: int(v.PlatformID),
  137. Mid: v.Mid,
  138. Buvid: v.Buvid,
  139. DeviceToken: v.DeviceToken,
  140. Build: int(v.Build),
  141. TimeZone: int(v.TimeZone),
  142. NotifySwitch: int(v.NotifySwitch),
  143. DeviceBrand: v.DeviceBrand,
  144. DeviceModel: v.DeviceModel,
  145. OSVersion: v.OSVersion,
  146. Extra: v.Extra,
  147. }
  148. }
  149. err = s.svc.AddTokensCache(ctx, reports)
  150. return
  151. }
  152. // AddCallback 回执
  153. func (s *server) AddCallback(ctx context.Context, req *pb.AddCallbackRequest) (reply *pb.AddCallbackReply, err error) {
  154. reply = new(pb.AddCallbackReply)
  155. cb := &model.Callback{
  156. Task: req.Task,
  157. APP: req.APP,
  158. Platform: int(req.Platform),
  159. Mid: int64(req.Mid),
  160. Pid: int(req.Pid),
  161. Token: req.Token,
  162. Buvid: req.Buvid,
  163. Click: uint8(req.Click),
  164. }
  165. if req.Extra != nil {
  166. cb.Extra = &model.CallbackExtra{
  167. Status: int(req.Extra.Status),
  168. Channel: int(req.Extra.Channel),
  169. }
  170. }
  171. err = s.svc.AddCallback(ctx, cb)
  172. return
  173. }
  174. // AddMidProgress 记录任务中mid数
  175. func (s *server) AddMidProgress(ctx context.Context, req *pb.AddMidProgressRequest) (reply *pb.AddMidProgressReply, err error) {
  176. reply = new(pb.AddMidProgressReply)
  177. err = s.svc.AddMidProgress(ctx, req.Task, req.MidTotal, req.MidValid)
  178. return
  179. }
  180. // Setting 获取用户业务开关配置
  181. func (s *server) Setting(ctx context.Context, req *pb.SettingRequest) (reply *pb.SettingReply, err error) {
  182. reply = new(pb.SettingReply)
  183. res, err := s.svc.Setting(ctx, req.Mid)
  184. if err != nil || res == nil {
  185. return
  186. }
  187. reply.Settings = make(map[int32]int32, len(res))
  188. for k, v := range res {
  189. reply.Settings[int32(k)] = int32(v)
  190. }
  191. return
  192. }
  193. // SetSetting 设置用户业务开关
  194. func (s *server) SetSetting(ctx context.Context, req *pb.SetSettingRequest) (reply *pb.SetSettingReply, err error) {
  195. reply = new(pb.SetSettingReply)
  196. err = s.svc.SetSetting(ctx, req.Mid, int(req.Type), int(req.Value))
  197. return
  198. }