server.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package gorpc
  2. import (
  3. "go-common/app/service/main/push/conf"
  4. "go-common/app/service/main/push/model"
  5. "go-common/app/service/main/push/service"
  6. "go-common/library/net/rpc"
  7. "go-common/library/net/rpc/context"
  8. )
  9. // RPC rpc.
  10. type RPC struct {
  11. s *service.Service
  12. }
  13. // New .
  14. func New(c *conf.Config, s *service.Service) (svc *rpc.Server) {
  15. r := &RPC{s: s}
  16. svc = rpc.NewServer(c.RPCServer)
  17. if err := svc.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. // AddReport adds report by mid.
  31. func (r *RPC) AddReport(c context.Context, arg *model.ArgReport, res *struct{}) (err error) {
  32. report := &model.Report{
  33. APPID: arg.APPID,
  34. PlatformID: arg.PlatformID,
  35. Mid: arg.Mid,
  36. Buvid: arg.Buvid,
  37. DeviceToken: arg.DeviceToken,
  38. Build: arg.Build,
  39. TimeZone: arg.TimeZone,
  40. NotifySwitch: arg.NotifySwitch,
  41. DeviceBrand: arg.DeviceBrand,
  42. DeviceModel: arg.DeviceModel,
  43. OSVersion: arg.OSVersion,
  44. Extra: arg.Extra,
  45. }
  46. err = r.s.AddReport(c, report)
  47. return
  48. }
  49. // DelInvalidReports deletes invalid reports.
  50. func (r *RPC) DelInvalidReports(c context.Context, arg *model.ArgDelInvalidReport, res *struct{}) (err error) {
  51. err = r.s.DelInvalidReports(c, arg.Type)
  52. return
  53. }
  54. // DelReport deletes report.
  55. func (r *RPC) DelReport(c context.Context, arg *model.ArgReport, res *struct{}) (err error) {
  56. err = r.s.DelReport(c, arg.APPID, arg.Mid, arg.DeviceToken)
  57. return
  58. }
  59. // AddCallback adds callback data.
  60. func (r *RPC) AddCallback(c context.Context, arg *model.ArgCallback, res *struct{}) (err error) {
  61. cb := &model.Callback{
  62. Task: arg.Task,
  63. APP: arg.APP,
  64. Platform: arg.Platform,
  65. Mid: arg.Mid,
  66. Pid: arg.Pid,
  67. Token: arg.Token,
  68. Buvid: arg.Buvid,
  69. Click: arg.Click,
  70. Extra: arg.Extra,
  71. }
  72. err = r.s.AddCallback(c, cb)
  73. return
  74. }
  75. // AddReportCache adds report cache.
  76. func (r *RPC) AddReportCache(c context.Context, arg *model.ArgReport, res *struct{}) (err error) {
  77. report := &model.Report{
  78. ID: arg.ID,
  79. APPID: arg.APPID,
  80. PlatformID: arg.PlatformID,
  81. Mid: arg.Mid,
  82. Buvid: arg.Buvid,
  83. DeviceToken: arg.DeviceToken,
  84. Build: arg.Build,
  85. TimeZone: arg.TimeZone,
  86. NotifySwitch: arg.NotifySwitch,
  87. DeviceBrand: arg.DeviceBrand,
  88. DeviceModel: arg.DeviceModel,
  89. OSVersion: arg.OSVersion,
  90. Extra: arg.Extra,
  91. }
  92. err = r.s.AddReportCache(c, report)
  93. return
  94. }
  95. // AddUserReportCache adds user report cache.
  96. func (r *RPC) AddUserReportCache(c context.Context, arg *model.ArgUserReports, res *struct{}) (err error) {
  97. err = r.s.AddUserReportCache(c, arg.Mid, arg.Reports)
  98. return
  99. }
  100. // Setting gets user push switch setting.
  101. func (r *RPC) Setting(c context.Context, arg *model.ArgMid, res *map[int]int) (err error) {
  102. *res, err = r.s.Setting(c, arg.Mid)
  103. return
  104. }
  105. // SetSetting sets user push switch setting.
  106. func (r *RPC) SetSetting(c context.Context, arg *model.ArgSetting, res *struct{}) (err error) {
  107. err = r.s.SetSetting(c, arg.Mid, arg.Type, arg.Value)
  108. return
  109. }
  110. // AddMidProgress add mid count number to task progress field
  111. func (r *RPC) AddMidProgress(c context.Context, arg *model.ArgMidProgress, res *struct{}) (err error) {
  112. err = r.s.AddMidProgress(c, arg.Task, arg.MidTotal, arg.MidValid)
  113. return
  114. }
  115. // AddTokenCache add token cache
  116. func (r *RPC) AddTokenCache(ctx context.Context, arg *model.ArgReport, res *struct{}) (err error) {
  117. report := &model.Report{
  118. APPID: arg.APPID,
  119. PlatformID: arg.PlatformID,
  120. Mid: arg.Mid,
  121. Buvid: arg.Buvid,
  122. DeviceToken: arg.DeviceToken,
  123. Build: arg.Build,
  124. TimeZone: arg.TimeZone,
  125. NotifySwitch: arg.NotifySwitch,
  126. DeviceBrand: arg.DeviceBrand,
  127. DeviceModel: arg.DeviceModel,
  128. OSVersion: arg.OSVersion,
  129. Extra: arg.Extra,
  130. }
  131. err = r.s.AddTokenCache(ctx, report)
  132. return
  133. }
  134. // AddTokensCache add token cache
  135. func (r *RPC) AddTokensCache(ctx context.Context, arg *model.ArgReports, res *struct{}) (err error) {
  136. rs := make(map[string]*model.Report, len(arg.Reports))
  137. for _, v := range arg.Reports {
  138. rs[v.DeviceToken] = v
  139. }
  140. err = r.s.AddTokensCache(ctx, rs)
  141. return
  142. }