server.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. package grpc
  2. import (
  3. "context"
  4. pb "go-common/app/service/main/relation/api"
  5. "go-common/app/service/main/relation/conf"
  6. "go-common/app/service/main/relation/model"
  7. "go-common/app/service/main/relation/service"
  8. "go-common/library/net/rpc/warden"
  9. )
  10. // New warden rpc server
  11. func New(c *conf.Config, s *service.Service) *warden.Server {
  12. svr := warden.NewServer(c.WardenServer)
  13. pb.RegisterRelationServer(svr.Server(), &server{as: s})
  14. svr, err := svr.Start()
  15. if err != nil {
  16. panic(err)
  17. }
  18. return svr
  19. }
  20. type server struct {
  21. as *service.Service
  22. }
  23. var _ pb.RelationServer = &server{}
  24. func (s *server) Relation(ctx context.Context, req *pb.RelationReq) (*pb.FollowingReply, error) {
  25. following, err := s.as.Relation(ctx, req.Mid, req.Fid)
  26. if err != nil {
  27. return nil, err
  28. }
  29. followingReply := new(pb.FollowingReply)
  30. followingReply.DeepCopyFromFollowing(following)
  31. return followingReply, nil
  32. }
  33. func (s *server) Relations(ctx context.Context, req *pb.RelationsReq) (*pb.FollowingMapReply, error) {
  34. followsing, err := s.as.Relations(ctx, req.Mid, req.Fid)
  35. if err != nil {
  36. return nil, err
  37. }
  38. followingMap := map[int64]*pb.FollowingReply{}
  39. for key, value := range followsing {
  40. followingReply := new(pb.FollowingReply)
  41. followingReply.DeepCopyFromFollowing(value)
  42. followingMap[key] = followingReply
  43. }
  44. return &pb.FollowingMapReply{FollowingMap: followingMap}, nil
  45. }
  46. func (s *server) Stat(ctx context.Context, req *pb.MidReq) (*pb.StatReply, error) {
  47. stat, err := s.as.Stat(ctx, req.Mid)
  48. if err != nil {
  49. return nil, err
  50. }
  51. statReply := new(pb.StatReply)
  52. statReply.DeepCopyFromStat(stat)
  53. return statReply, nil
  54. }
  55. func (s *server) Stats(ctx context.Context, req *pb.MidsReq) (*pb.StatsReply, error) {
  56. stat, err := s.as.Stats(ctx, req.Mids)
  57. if err != nil {
  58. return nil, err
  59. }
  60. statMap := map[int64]*pb.StatReply{}
  61. for key, value := range stat {
  62. statReply := new(pb.StatReply)
  63. statReply.DeepCopyFromStat(value)
  64. statMap[key] = statReply
  65. }
  66. return &pb.StatsReply{StatReplyMap: statMap}, nil
  67. }
  68. func (s *server) Attentions(ctx context.Context, req *pb.MidReq) (*pb.FollowingsReply, error) {
  69. followings, err := s.as.Attentions(ctx, req.Mid)
  70. if err != nil {
  71. return nil, err
  72. }
  73. followingList := make([]*pb.FollowingReply, len(followings))
  74. for index, value := range followings {
  75. followingReply := new(pb.FollowingReply)
  76. followingReply.DeepCopyFromFollowing(value)
  77. followingList[index] = followingReply
  78. }
  79. return &pb.FollowingsReply{FollowingList: followingList}, nil
  80. }
  81. func (s *server) Followings(ctx context.Context, req *pb.MidReq) (*pb.FollowingsReply, error) {
  82. followings, err := s.as.Followings(ctx, req.Mid)
  83. if err != nil {
  84. return nil, err
  85. }
  86. followingList := make([]*pb.FollowingReply, len(followings))
  87. for index, value := range followings {
  88. followingReply := new(pb.FollowingReply)
  89. followingReply.DeepCopyFromFollowing(value)
  90. followingList[index] = followingReply
  91. }
  92. return &pb.FollowingsReply{FollowingList: followingList}, nil
  93. }
  94. func (s *server) AddFollowing(ctx context.Context, req *pb.FollowingReq) (*pb.EmptyReply, error) {
  95. if err := s.as.AddFollowing(ctx, req.Mid, req.Fid, req.Source, req.Infoc); err != nil {
  96. return nil, err
  97. }
  98. return &pb.EmptyReply{}, nil
  99. }
  100. func (s *server) DelFollowing(ctx context.Context, req *pb.FollowingReq) (*pb.EmptyReply, error) {
  101. if err := s.as.DelFollowing(ctx, req.Mid, req.Fid, req.Source, req.Infoc); err != nil {
  102. return nil, err
  103. }
  104. return &pb.EmptyReply{}, nil
  105. }
  106. func (s *server) Whispers(ctx context.Context, req *pb.MidReq) (*pb.FollowingsReply, error) {
  107. followings, err := s.as.Whispers(ctx, req.Mid)
  108. if err != nil {
  109. return nil, err
  110. }
  111. followingList := make([]*pb.FollowingReply, len(followings))
  112. for index, value := range followings {
  113. followingReply := new(pb.FollowingReply)
  114. followingReply.DeepCopyFromFollowing(value)
  115. followingList[index] = followingReply
  116. }
  117. return &pb.FollowingsReply{FollowingList: followingList}, nil
  118. }
  119. func (s *server) AddWhisper(ctx context.Context, req *pb.FollowingReq) (*pb.EmptyReply, error) {
  120. if err := s.as.AddWhisper(ctx, req.Mid, req.Fid, req.Source, req.Infoc); err != nil {
  121. return nil, err
  122. }
  123. return &pb.EmptyReply{}, nil
  124. }
  125. func (s *server) DelWhisper(ctx context.Context, req *pb.FollowingReq) (*pb.EmptyReply, error) {
  126. if err := s.as.DelWhisper(ctx, req.Mid, req.Fid, req.Source, req.Infoc); err != nil {
  127. return nil, err
  128. }
  129. return &pb.EmptyReply{}, nil
  130. }
  131. func (s *server) Blacks(ctx context.Context, req *pb.MidReq) (*pb.FollowingsReply, error) {
  132. followings, err := s.as.Blacks(ctx, req.Mid)
  133. if err != nil {
  134. return nil, err
  135. }
  136. followingList := make([]*pb.FollowingReply, len(followings))
  137. for index, value := range followings {
  138. followingReply := new(pb.FollowingReply)
  139. followingReply.DeepCopyFromFollowing(value)
  140. followingList[index] = followingReply
  141. }
  142. return &pb.FollowingsReply{FollowingList: followingList}, nil
  143. }
  144. func (s *server) AddBlack(ctx context.Context, req *pb.FollowingReq) (*pb.EmptyReply, error) {
  145. if err := s.as.AddBlack(ctx, req.Mid, req.Fid, req.Source, req.Infoc); err != nil {
  146. return nil, err
  147. }
  148. return &pb.EmptyReply{}, nil
  149. }
  150. func (s *server) DelBlack(ctx context.Context, req *pb.FollowingReq) (*pb.EmptyReply, error) {
  151. if err := s.as.DelBlack(ctx, req.Mid, req.Fid, req.Source, req.Infoc); err != nil {
  152. return nil, err
  153. }
  154. return &pb.EmptyReply{}, nil
  155. }
  156. func (s *server) Followers(ctx context.Context, req *pb.MidReq) (*pb.FollowingsReply, error) {
  157. followings, err := s.as.Followers(ctx, req.Mid)
  158. if err != nil {
  159. return nil, err
  160. }
  161. followingList := make([]*pb.FollowingReply, len(followings))
  162. for index, value := range followings {
  163. followingReply := new(pb.FollowingReply)
  164. followingReply.DeepCopyFromFollowing(value)
  165. followingList[index] = followingReply
  166. }
  167. return &pb.FollowingsReply{FollowingList: followingList}, nil
  168. }
  169. func (s *server) DelFollower(ctx context.Context, req *pb.FollowingReq) (*pb.EmptyReply, error) {
  170. if err := s.as.DelFollower(ctx, req.Mid, req.Fid, req.Source, req.Infoc); err != nil {
  171. return nil, err
  172. }
  173. return &pb.EmptyReply{}, nil
  174. }
  175. func (s *server) Tag(ctx context.Context, req *pb.TagIdReq) (*pb.TagReply, error) {
  176. mids, err := s.as.Tag(ctx, req.Mid, req.TagId, req.RealIp)
  177. if err != nil {
  178. return nil, err
  179. }
  180. return &pb.TagReply{Mids: mids}, nil
  181. }
  182. func (s *server) Tags(ctx context.Context, req *pb.MidReq) (*pb.TagsCountReply, error) {
  183. tagCount, err := s.as.Tags(ctx, req.Mid, req.RealIp)
  184. if err != nil {
  185. return nil, err
  186. }
  187. tagCountList := make([]*pb.TagCountReply, len(tagCount))
  188. for index, value := range tagCount {
  189. tagCountReply := new(pb.TagCountReply)
  190. tagCountReply.DeepCopyFromTagCount(value)
  191. tagCountList[index] = tagCountReply
  192. }
  193. return &pb.TagsCountReply{TagCountList: tagCountList}, nil
  194. }
  195. func (s *server) UserTag(ctx context.Context, req *pb.RelationReq) (*pb.UserTagReply, error) {
  196. res, err := s.as.UserTag(ctx, req.Mid, req.Fid, req.RealIp)
  197. if err != nil {
  198. return nil, err
  199. }
  200. return &pb.UserTagReply{Tags: res}, nil
  201. }
  202. func (s *server) CreateTag(ctx context.Context, req *pb.TagReq) (*pb.CreateTagReply, error) {
  203. res, err := s.as.CreateTag(ctx, req.Mid, req.Tag, req.RealIp)
  204. if err != nil {
  205. return nil, err
  206. }
  207. return &pb.CreateTagReply{TagId: res}, nil
  208. }
  209. func (s *server) UpdateTag(ctx context.Context, req *pb.TagUpdateReq) (*pb.EmptyReply, error) {
  210. if err := s.as.UpdateTag(ctx, req.Mid, req.TagId, req.New, req.RealIp); err != nil {
  211. return nil, err
  212. }
  213. return &pb.EmptyReply{}, nil
  214. }
  215. func (s *server) DelTag(ctx context.Context, req *pb.TagDelReq) (*pb.EmptyReply, error) {
  216. if err := s.as.DelTag(ctx, req.Mid, req.TagId, req.RealIp); err != nil {
  217. return nil, err
  218. }
  219. return &pb.EmptyReply{}, nil
  220. }
  221. func (s *server) TagsAddUsers(ctx context.Context, req *pb.TagsMoveUsersReq) (*pb.EmptyReply, error) {
  222. if err := s.as.TagsAddUsers(ctx, req.Mid, req.AfterTagIds, req.Fids, req.RealIp); err != nil {
  223. return nil, err
  224. }
  225. return &pb.EmptyReply{}, nil
  226. }
  227. func (s *server) TagsCopyUsers(ctx context.Context, req *pb.TagsMoveUsersReq) (*pb.EmptyReply, error) {
  228. if err := s.as.TagsMoveUsers(ctx, req.Mid, req.BeforeId, req.AfterTagIds, req.Fids, req.RealIp); err != nil {
  229. return nil, err
  230. }
  231. return &pb.EmptyReply{}, nil
  232. }
  233. func (s *server) TagsMoveUsers(ctx context.Context, req *pb.TagsMoveUsersReq) (*pb.EmptyReply, error) {
  234. if err := s.as.TagsMoveUsers(ctx, req.Mid, req.BeforeId, req.AfterTagIds, req.Fids, req.RealIp); err != nil {
  235. return nil, err
  236. }
  237. return &pb.EmptyReply{}, nil
  238. }
  239. func (s *server) Prompt(ctx context.Context, req *pb.PromptReq) (*pb.PromptReply, error) {
  240. argPrompt := &model.ArgPrompt{Mid: req.Mid, Fid: req.Fid, Btype: req.Btype}
  241. success, err := s.as.Prompt(ctx, argPrompt)
  242. if err != nil {
  243. return nil, err
  244. }
  245. return &pb.PromptReply{Success: success}, nil
  246. }
  247. func (s *server) ClosePrompt(ctx context.Context, req *pb.PromptReq) (*pb.EmptyReply, error) {
  248. argPrompt := &model.ArgPrompt{Mid: req.Mid, Fid: req.Fid, Btype: req.Btype}
  249. if err := s.as.ClosePrompt(ctx, argPrompt); err != nil {
  250. return nil, err
  251. }
  252. return &pb.EmptyReply{}, nil
  253. }
  254. func (s *server) AddSpecial(ctx context.Context, req *pb.FollowingReq) (*pb.EmptyReply, error) {
  255. if err := s.as.AddSpecial(ctx, req.Mid, req.Fid); err != nil {
  256. return nil, err
  257. }
  258. return &pb.EmptyReply{}, nil
  259. }
  260. func (s *server) DelSpecial(ctx context.Context, req *pb.FollowingReq) (*pb.EmptyReply, error) {
  261. if err := s.as.DelSpecial(ctx, req.Mid, req.Fid); err != nil {
  262. return nil, err
  263. }
  264. return &pb.EmptyReply{}, nil
  265. }
  266. func (s *server) Special(ctx context.Context, req *pb.MidReq) (*pb.SpecialReply, error) {
  267. mids, err := s.as.Special(ctx, req.Mid)
  268. if err != nil {
  269. return nil, err
  270. }
  271. return &pb.SpecialReply{Mids: mids}, nil
  272. }
  273. func (s *server) FollowersUnread(ctx context.Context, req *pb.MidReq) (*pb.FollowersUnreadReply, error) {
  274. hasUnread, err := s.as.Unread(ctx, req.Mid)
  275. if err != nil {
  276. return nil, err
  277. }
  278. return &pb.FollowersUnreadReply{HasUnread: hasUnread}, nil
  279. }
  280. func (s *server) FollowersUnreadCount(ctx context.Context, req *pb.MidReq) (*pb.FollowersUnreadCountReply, error) {
  281. count, err := s.as.UnreadCount(ctx, req.Mid)
  282. if err != nil {
  283. return nil, err
  284. }
  285. return &pb.FollowersUnreadCountReply{UnreadCount: count}, nil
  286. }
  287. func (s *server) AchieveGet(ctx context.Context, req *pb.AchieveGetReq) (*pb.AchieveGetReply, error) {
  288. argAchieveGet := &model.ArgAchieveGet{Mid: req.Mid, Award: req.Award}
  289. achieveGetReply, err := s.as.AchieveGet(ctx, argAchieveGet)
  290. if err != nil {
  291. return nil, err
  292. }
  293. return &pb.AchieveGetReply{AwardToken: achieveGetReply.AwardToken}, nil
  294. }
  295. func (s *server) Achieve(ctx context.Context, req *pb.AchieveReq) (*pb.AchieveReply, error) {
  296. argAchieve := &model.ArgAchieve{AwardToken: req.AwardToken}
  297. achieveReply, err := s.as.Achieve(ctx, argAchieve)
  298. if err != nil {
  299. return nil, err
  300. }
  301. return &pb.AchieveReply{Award: achieveReply.Award, Mid: achieveReply.Mid}, nil
  302. }
  303. func (s *server) ResetFollowersUnread(ctx context.Context, req *pb.MidReq) (*pb.EmptyReply, error) {
  304. if err := s.as.ResetUnread(ctx, req.Mid); err != nil {
  305. return nil, err
  306. }
  307. return &pb.EmptyReply{}, nil
  308. }
  309. func (s *server) ResetFollowersUnreadCount(ctx context.Context, req *pb.MidReq) (*pb.EmptyReply, error) {
  310. if err := s.as.ResetUnreadCount(ctx, req.Mid); err != nil {
  311. return nil, err
  312. }
  313. return &pb.EmptyReply{}, nil
  314. }
  315. func (s *server) DisableFollowerNotify(ctx context.Context, req *pb.MidReq) (*pb.EmptyReply, error) {
  316. if err := s.as.DisableFollowerNotify(ctx, &model.ArgMid{Mid: req.Mid}); err != nil {
  317. return nil, err
  318. }
  319. return &pb.EmptyReply{}, nil
  320. }
  321. func (s *server) EnableFollowerNotify(ctx context.Context, req *pb.MidReq) (*pb.EmptyReply, error) {
  322. if err := s.as.EnableFollowerNotify(ctx, &model.ArgMid{Mid: req.Mid}); err != nil {
  323. return nil, err
  324. }
  325. return &pb.EmptyReply{}, nil
  326. }
  327. func (s *server) FollowerNotifySetting(ctx context.Context, req *pb.MidReq) (*pb.FollowerNotifySettingReply, error) {
  328. followerNotifySetting, err := s.as.FollowerNotifySetting(ctx, &model.ArgMid{Mid: req.Mid})
  329. if err != nil {
  330. return nil, err
  331. }
  332. return &pb.FollowerNotifySettingReply{Mid: followerNotifySetting.Mid, Enabled: followerNotifySetting.Enabled}, nil
  333. }
  334. func (s *server) SameFollowings(ctx context.Context, req *pb.SameFollowingReq) (*pb.FollowingsReply, error) {
  335. followings, err := s.as.SameFollowings(ctx, &model.ArgSameFollowing{Mid1: req.Mid, Mid2: req.Mid2})
  336. if err != nil {
  337. return nil, err
  338. }
  339. followingList := make([]*pb.FollowingReply, len(followings))
  340. for index, value := range followings {
  341. followingReply := new(pb.FollowingReply)
  342. followingReply.DeepCopyFromFollowing(value)
  343. followingList[index] = followingReply
  344. }
  345. return &pb.FollowingsReply{FollowingList: followingList}, nil
  346. }