server.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package grpc
  2. import (
  3. "context"
  4. pb "go-common/app/service/main/account/api"
  5. "go-common/app/service/main/account/conf"
  6. "go-common/app/service/main/account/service"
  7. "go-common/library/net/rpc/warden"
  8. )
  9. // New warden rpc server
  10. func New(c *conf.Config, s *service.Service) (svr *warden.Server) {
  11. svr = warden.NewServer(c.WardenServer)
  12. pb.RegisterAccountServer(svr.Server(), &server{as: s})
  13. return svr
  14. }
  15. // Start create and start warden rpc server
  16. func Start(c *conf.Config, s *service.Service) (svr *warden.Server, err error) {
  17. svr = warden.NewServer(c.WardenServer)
  18. pb.RegisterAccountServer(svr.Server(), &server{as: s})
  19. if svr, err = svr.Start(); err != nil {
  20. return
  21. }
  22. return
  23. }
  24. type server struct {
  25. as *service.Service
  26. }
  27. var _ pb.AccountServer = &server{}
  28. func (s *server) Info3(ctx context.Context, req *pb.MidReq) (*pb.InfoReply, error) {
  29. info, err := s.as.Info(ctx, req.Mid)
  30. if err != nil {
  31. return nil, err
  32. }
  33. return &pb.InfoReply{Info: info}, nil
  34. }
  35. func (s *server) Infos3(ctx context.Context, req *pb.MidsReq) (*pb.InfosReply, error) {
  36. infos, err := s.as.Infos(ctx, req.Mids)
  37. if err != nil {
  38. return nil, err
  39. }
  40. return &pb.InfosReply{Infos: infos}, nil
  41. }
  42. func (s *server) InfosByName3(ctx context.Context, req *pb.NamesReq) (*pb.InfosReply, error) {
  43. infos, err := s.as.InfosByName(ctx, req.Names)
  44. if err != nil {
  45. return nil, err
  46. }
  47. return &pb.InfosReply{Infos: infos}, nil
  48. }
  49. func (s *server) Card3(ctx context.Context, req *pb.MidReq) (*pb.CardReply, error) {
  50. card, err := s.as.Card(ctx, req.Mid)
  51. if err != nil {
  52. return nil, err
  53. }
  54. return &pb.CardReply{Card: card}, nil
  55. }
  56. func (s *server) Cards3(ctx context.Context, req *pb.MidsReq) (*pb.CardsReply, error) {
  57. cards, err := s.as.Cards(ctx, req.Mids)
  58. if err != nil {
  59. return nil, err
  60. }
  61. return &pb.CardsReply{Cards: cards}, nil
  62. }
  63. func (s *server) Profile3(ctx context.Context, req *pb.MidReq) (*pb.ProfileReply, error) {
  64. profile, err := s.as.Profile(ctx, req.Mid)
  65. if err != nil {
  66. return nil, err
  67. }
  68. return &pb.ProfileReply{Profile: profile}, nil
  69. }
  70. func (s *server) ProfileWithStat3(ctx context.Context, req *pb.MidReq) (*pb.ProfileStatReply, error) {
  71. profileStat, err := s.as.ProfileWithStat(ctx, req.Mid)
  72. if err != nil {
  73. return nil, err
  74. }
  75. level := pb.LevelInfo{}
  76. level.DeepCopyFromLevelInfo(&profileStat.LevelExp)
  77. return &pb.ProfileStatReply{
  78. Profile: profileStat.Profile,
  79. LevelInfo: level,
  80. Coins: profileStat.Coins,
  81. Follower: profileStat.Follower,
  82. Following: profileStat.Following,
  83. }, nil
  84. }
  85. func (s *server) AddExp3(ctx context.Context, req *pb.ExpReq) (*pb.ExpReply, error) {
  86. return &pb.ExpReply{}, s.as.AddExp(ctx, req.Mid, req.Exp, req.Operater, req.Operate, req.Reason)
  87. }
  88. func (s *server) AddMoral3(ctx context.Context, req *pb.MoralReq) (*pb.MoralReply, error) {
  89. return &pb.MoralReply{}, s.as.AddMoral(ctx, req.Mid, req.Moral, req.Oper, req.Reason, req.Remark)
  90. }
  91. func (s *server) Relation3(ctx context.Context, req *pb.RelationReq) (*pb.RelationReply, error) {
  92. relation, err := s.as.Relation(ctx, req.Mid, req.Owner)
  93. if err != nil {
  94. return nil, err
  95. }
  96. return &pb.RelationReply{Following: relation.Following}, nil
  97. }
  98. func (s *server) Attentions3(ctx context.Context, req *pb.MidReq) (*pb.AttentionsReply, error) {
  99. attentions, err := s.as.Attentions(ctx, req.Mid)
  100. if err != nil {
  101. return nil, err
  102. }
  103. return &pb.AttentionsReply{Attentions: attentions}, nil
  104. }
  105. func (s *server) Blacks3(ctx context.Context, req *pb.MidReq) (*pb.BlacksReply, error) {
  106. blackList, err := s.as.Blacks(ctx, req.Mid)
  107. if err != nil {
  108. return nil, err
  109. }
  110. blackListBool := make(map[int64]bool, len(blackList))
  111. for k := range blackList {
  112. blackListBool[k] = true
  113. }
  114. return &pb.BlacksReply{BlackList: blackListBool}, nil
  115. }
  116. func (s *server) Relations3(ctx context.Context, req *pb.RelationsReq) (*pb.RelationsReply, error) {
  117. relations, err := s.as.Relations(ctx, req.Mid, req.Owners)
  118. if err != nil {
  119. return nil, err
  120. }
  121. newRelations := make(map[int64]*pb.RelationReply, len(relations))
  122. for k, v := range relations {
  123. newRelations[k] = &pb.RelationReply{Following: v.Following}
  124. }
  125. return &pb.RelationsReply{Relations: newRelations}, nil
  126. }
  127. func (s *server) RichRelations3(ctx context.Context, req *pb.RichRelationReq) (*pb.RichRelationsReply, error) {
  128. richRelations, err := s.as.RichRelations2(ctx, req.Owner, req.Mids)
  129. if err != nil {
  130. return nil, err
  131. }
  132. newRichRelations := make(map[int64]int32, len(richRelations))
  133. for k, v := range richRelations {
  134. newRichRelations[k] = int32(v)
  135. }
  136. return &pb.RichRelationsReply{RichRelations: newRichRelations}, nil
  137. }
  138. func (s *server) Vip3(ctx context.Context, req *pb.MidReq) (*pb.VipReply, error) {
  139. vip, err := s.as.Vip(ctx, req.Mid)
  140. if err != nil {
  141. return nil, err
  142. }
  143. reply := new(pb.VipReply)
  144. reply.DeepCopyFromVipInfo(vip)
  145. return reply, nil
  146. }
  147. func (s *server) Vips3(ctx context.Context, req *pb.MidsReq) (*pb.VipsReply, error) {
  148. vips, err := s.as.Vips(ctx, req.Mids)
  149. if err != nil {
  150. return nil, err
  151. }
  152. pvips := make(map[int64]*pb.VipReply, len(vips))
  153. for mid, vip := range vips {
  154. pvip := new(pb.VipReply)
  155. pvip.DeepCopyFromVipInfo(vip)
  156. pvips[mid] = pvip
  157. }
  158. return &pb.VipsReply{Vips: pvips}, nil
  159. }