server.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // Package server generate by warden_gen
  2. package server
  3. import (
  4. "context"
  5. "go-common/library/ecode"
  6. pb "go-common/app/service/main/favorite/api"
  7. "go-common/app/service/main/favorite/model"
  8. service "go-common/app/service/main/favorite/service"
  9. "go-common/library/net/rpc/warden"
  10. empty "github.com/golang/protobuf/ptypes/empty"
  11. )
  12. // New Favorite warden rpc server
  13. func New(c *warden.ServerConfig, svr *service.Service) *warden.Server {
  14. ws := warden.NewServer(c)
  15. pb.RegisterFavoriteServer(ws.Server(), &server{svr})
  16. return ws
  17. }
  18. type server struct {
  19. svr *service.Service
  20. }
  21. var _ pb.FavoriteServer = &server{}
  22. func (s *server) SortFavs(ctx context.Context, req *pb.SortFavsReq) (*empty.Empty, error) {
  23. var sorts []model.SortFav
  24. for _, data := range req.Sorts {
  25. sorts = append(sorts, model.SortFav{
  26. Pre: data.Pre,
  27. Insert: data.Insert,
  28. })
  29. }
  30. if len(sorts) == 0 {
  31. return nil, ecode.RequestErr
  32. }
  33. s.svr.SortFavs(ctx, int8(req.Typ), req.Fid, req.Mid, sorts)
  34. return &empty.Empty{}, nil
  35. }
  36. func (s *server) AdminUpdateFolder(ctx context.Context, req *pb.AdminUpdateFolderReq) (*empty.Empty, error) {
  37. attr := int32(req.Attr)
  38. state := int8(req.State)
  39. err := s.svr.UpdateFolder(ctx, int8(req.Typ), req.Fid, req.Mid, req.Name, req.Description, req.Cover, 0, &attr, &state)
  40. if err != nil {
  41. return nil, err
  42. }
  43. return &empty.Empty{}, nil
  44. }
  45. func (s *server) UpFolderName(ctx context.Context, req *pb.UpFolderNameReq) (*empty.Empty, error) {
  46. err := s.svr.UpFolderName(ctx, int8(req.Typ), req.Mid, req.Fid, req.Name, req.Cookie, req.AccessKey)
  47. if err != nil {
  48. return nil, err
  49. }
  50. return &empty.Empty{}, nil
  51. }
  52. func (s *server) FavDelFolders(ctx context.Context, req *pb.FavDelFoldersReq) (*empty.Empty, error) {
  53. for _, fid := range req.Fids {
  54. err := s.svr.DelFav(ctx, int8(req.Typ), req.Mid, fid, req.Oid, int8(req.Otype))
  55. if err != nil {
  56. return nil, err
  57. }
  58. }
  59. return &empty.Empty{}, nil
  60. }
  61. func (s *server) FavAddFolders(ctx context.Context, req *pb.FavAddFoldersReq) (*empty.Empty, error) {
  62. for _, fid := range req.Fids {
  63. err := s.svr.AddFav(ctx, int8(req.Typ), req.Mid, fid, req.Oid, int8(req.Otype))
  64. if err != nil {
  65. return nil, err
  66. }
  67. }
  68. return &empty.Empty{}, nil
  69. }
  70. func (s *server) UpFolderAttr(ctx context.Context, req *pb.UpFolderAttrReq) (*empty.Empty, error) {
  71. err := s.svr.UpFolderAttr(ctx, int8(req.Typ), req.Mid, req.Fid, req.Public)
  72. if err != nil {
  73. return nil, err
  74. }
  75. return &empty.Empty{}, nil
  76. }
  77. func (s *server) CleanState(ctx context.Context, req *pb.CleanStateReq) (*pb.CleanStateReply, error) {
  78. state, err := s.svr.CleanState(ctx, int8(req.Typ), req.Mid, req.Fid)
  79. if err != nil {
  80. return nil, err
  81. }
  82. return &pb.CleanStateReply{CleanState: int32(state)}, nil
  83. }
  84. func (s *server) CleanInvalidFavs(ctx context.Context, req *pb.CleanInvalidFavsReq) (*empty.Empty, error) {
  85. err := s.svr.CleanInvalidArcs(ctx, int8(req.Typ), req.Mid, req.Fid)
  86. if err != nil {
  87. return nil, err
  88. }
  89. return &empty.Empty{}, nil
  90. }
  91. func (s *server) CopyFavs(ctx context.Context, req *pb.CopyFavsReq) (*empty.Empty, error) {
  92. err := s.svr.CopyFavs(ctx, int8(req.Typ), req.OldMid, req.Mid, req.OldFid, req.NewFid, req.Oids)
  93. if err != nil {
  94. return nil, err
  95. }
  96. return &empty.Empty{}, nil
  97. }
  98. func (s *server) MoveFavs(ctx context.Context, req *pb.MoveFavsReq) (*empty.Empty, error) {
  99. err := s.svr.MoveFavs(ctx, int8(req.Typ), req.Mid, req.OldFid, req.NewFid, req.Oids)
  100. if err != nil {
  101. return nil, err
  102. }
  103. return &empty.Empty{}, nil
  104. }
  105. func (s *server) SetFolderSort(ctx context.Context, req *pb.SetFolderSortReq) (*empty.Empty, error) {
  106. err := s.svr.SetFolderSort(ctx, int8(req.Typ), req.Mid, req.Fids)
  107. if err != nil {
  108. return nil, err
  109. }
  110. return &empty.Empty{}, nil
  111. }
  112. func (s *server) FavedUsers(ctx context.Context, req *pb.FavedUsersReq) (*pb.FavedUsersReply, error) {
  113. list, err := s.svr.UserList(ctx, int8(req.Type), req.Oid, int(req.Pn), int(req.Ps))
  114. if err != nil {
  115. return nil, err
  116. }
  117. reply := &pb.FavedUsersReply{
  118. Page: &pb.ModelPage{
  119. Num: int32(list.Page.Num),
  120. Size_: int32(list.Page.Size),
  121. Count: int32(list.Page.Total),
  122. },
  123. }
  124. for _, data := range list.List {
  125. reply.User = append(reply.User, &pb.User{
  126. Id: data.ID,
  127. Oid: data.Oid,
  128. Mid: data.Mid,
  129. Typ: int32(data.Type),
  130. State: int32(data.State),
  131. Ctime: int64(data.CTime),
  132. Mtime: int64(data.MTime),
  133. })
  134. }
  135. return reply, nil
  136. }
  137. func (s *server) CntUserFolders(ctx context.Context, req *pb.CntUserFoldersReq) (*pb.CntUserFoldersReply, error) {
  138. count, err := s.svr.CntUserFolders(ctx, int8(req.Typ), req.Mid, req.Vmid)
  139. if err != nil {
  140. return nil, err
  141. }
  142. return &pb.CntUserFoldersReply{Count: int32(count)}, nil
  143. }
  144. func (s *server) InDefault(ctx context.Context, req *pb.InDefaultFolderReq) (*pb.InDefaultFolderReply, error) {
  145. isIn, err := s.svr.InDefaultFolder(ctx, int8(req.Typ), req.Mid, req.Oid)
  146. if err != nil {
  147. return nil, err
  148. }
  149. return &pb.InDefaultFolderReply{IsIn: isIn}, nil
  150. }
  151. func (s *server) MultiDel(ctx context.Context, req *pb.MultiDelReq) (*empty.Empty, error) {
  152. err := s.svr.MultiDelFavs(ctx, int8(req.Typ), req.Mid, req.Fid, req.Oids)
  153. if err != nil {
  154. return nil, err
  155. }
  156. return &empty.Empty{}, nil
  157. }
  158. func (s *server) MultiAdd(ctx context.Context, req *pb.MultiAddReq) (*empty.Empty, error) {
  159. err := s.svr.MultiAddFavs(ctx, int8(req.Typ), req.Mid, req.Fid, req.Oids)
  160. if err != nil {
  161. return nil, err
  162. }
  163. return &empty.Empty{}, nil
  164. }
  165. func (s *server) DelFolder(ctx context.Context, req *pb.DelFolderReq) (*empty.Empty, error) {
  166. err := s.svr.DelFolder(ctx, int8(req.Typ), req.Mid, req.Fid)
  167. if err != nil {
  168. return nil, err
  169. }
  170. return &empty.Empty{}, nil
  171. }
  172. func (s *server) UpdateFolder(ctx context.Context, req *pb.UpdateFolderReq) (*empty.Empty, error) {
  173. err := s.svr.UpdateFolder(ctx, int8(req.Typ), req.Fid, req.Mid, req.Name, req.Description, req.Cover, req.Public, nil, nil)
  174. if err != nil {
  175. return nil, err
  176. }
  177. return &empty.Empty{}, nil
  178. }
  179. func (s *server) AddFolder(ctx context.Context, req *pb.AddFolderReq) (*pb.AddFolderReply, error) {
  180. fid, err := s.svr.AddFolder(ctx, int8(req.Typ), req.Mid, req.Name, req.Description, req.Cover, req.Public, req.Cookie, req.AccessKey)
  181. if err != nil {
  182. return nil, err
  183. }
  184. return &pb.AddFolderReply{Fid: fid}, nil
  185. }
  186. // IsFavoredByFid return folders by mid.
  187. func (s *server) IsFavoredByFid(ctx context.Context, req *pb.IsFavoredByFidReq) (*pb.IsFavoredReply, error) {
  188. ok, err := s.svr.IsFavedByFid(ctx, int8(req.Type), req.Mid, req.Oid, req.Fid)
  189. return &pb.IsFavoredReply{Faved: ok}, err
  190. }
  191. // UserFolders return folders by mid.
  192. func (s *server) UserFolders(ctx context.Context, req *pb.UserFoldersReq) (*pb.UserFoldersReply, error) {
  193. if req.Otype <= 0 {
  194. req.Otype = req.Typ
  195. }
  196. uf, err := s.svr.UserFolders(ctx, int8(req.Typ), req.Mid, req.Vmid, req.Oid, int8(req.Otype))
  197. return &pb.UserFoldersReply{Res: uf}, err
  198. }
  199. // UserFolder return one folder by mid.
  200. func (s *server) UserFolder(ctx context.Context, req *pb.UserFolderReq) (*pb.UserFolderReply, error) {
  201. f, err := s.svr.UserFolder(ctx, int8(req.Typ), req.Mid, req.Vmid, req.Fid)
  202. return &pb.UserFolderReply{Res: f}, err
  203. }
  204. // Tlists return partitions .
  205. func (s *server) Tlists(ctx context.Context, req *pb.TlistsReq) (*pb.TlistsReply, error) {
  206. ts, err := s.svr.Tlists(ctx, int8(req.Tp), req.Mid, req.Uid, req.Fid)
  207. if err != nil {
  208. return nil, err
  209. }
  210. reply := &pb.TlistsReply{}
  211. for _, v := range ts {
  212. reply.Res = append(reply.Res, &pb.ModelPartition{
  213. Tid: int32(v.Tid),
  214. Name: v.Name,
  215. Count: int32(v.Count),
  216. })
  217. }
  218. return reply, err
  219. }
  220. // RecentFavs return favs by mid.
  221. func (s *server) RecentFavs(ctx context.Context, req *pb.RecentFavsReq) (*pb.RecentFavsReply, error) {
  222. ids, err := s.svr.RecentFavs(ctx, int8(req.Tp), req.Mid, int(req.Size_))
  223. return &pb.RecentFavsReply{Res: ids}, err
  224. }
  225. // RecentFavs return favs by mid.
  226. func (s *server) RecentResources(ctx context.Context, req *pb.RecentResourcesReq) (*pb.RecentResourcesReply, error) {
  227. recents, err := s.svr.RecentResources(ctx, int8(req.Tp), req.Mid, int(req.Size_))
  228. return &pb.RecentResourcesReply{Res: recents}, err
  229. }
  230. // Favorites return favorieds info by fid.
  231. func (s *server) Favorites(ctx context.Context, req *pb.FavoritesReq) (*pb.FavoritesReply, error) {
  232. f, err := s.svr.Favorites(ctx, int8(req.Tp), req.Mid, req.Uid, req.Fid, int(req.Tid), int(req.Tv), int(req.Pn), int(req.Ps), req.Keyword, req.Order)
  233. if err != nil {
  234. return nil, err
  235. }
  236. reply := &pb.FavoritesReply{Res: &pb.ModelFavorites{Page: &pb.ModelPage{
  237. Num: int32(f.Page.Num),
  238. Size_: int32(f.Page.Size),
  239. Count: int32(f.Page.Count),
  240. }}}
  241. for _, data := range f.List {
  242. reply.Res.List = append(reply.Res.List, &pb.ModelFavorite{
  243. Id: data.ID,
  244. Oid: data.Oid,
  245. Mid: data.Mid,
  246. Fid: data.Fid,
  247. Type: int32(data.Type),
  248. State: int32(data.State),
  249. Ctime: int64(data.CTime),
  250. Mtime: int64(data.MTime),
  251. })
  252. }
  253. return reply, err
  254. }
  255. // AddFav add a favorite into folder.
  256. func (s *server) AddFav(ctx context.Context, req *pb.AddFavReq) (*pb.AddFavReply, error) {
  257. err := s.svr.AddFav(ctx, int8(req.Tp), req.Mid, req.Fid, req.Oid, int8(req.Otype))
  258. return &pb.AddFavReply{}, err
  259. }
  260. // DelFav delete a favorite.
  261. func (s *server) DelFav(ctx context.Context, req *pb.DelFavReq) (*pb.DelFavReply, error) {
  262. err := s.svr.DelFav(ctx, int8(req.Tp), req.Mid, req.Fid, req.Oid, int8(req.Otype))
  263. return &pb.DelFavReply{}, err
  264. }
  265. // IsFavored check if oid faved by user
  266. func (s *server) IsFavored(ctx context.Context, req *pb.IsFavoredReq) (*pb.IsFavoredReply, error) {
  267. is, err := s.svr.IsFavored(ctx, int8(req.Typ), req.Mid, req.Oid)
  268. return &pb.IsFavoredReply{Faved: is}, err
  269. }
  270. // IsFavoreds check if oids faved by user
  271. func (s *server) IsFavoreds(ctx context.Context, req *pb.IsFavoredsReq) (*pb.IsFavoredsReply, error) {
  272. res, err := s.svr.IsFavoreds(ctx, int8(req.Typ), req.Mid, req.Oids)
  273. return &pb.IsFavoredsReply{Faveds: res}, err
  274. }
  275. // FavoritesAll return favorieds info by fid.
  276. func (s *server) FavoritesAll(ctx context.Context, req *pb.FavoritesReq) (*pb.FavoritesReply, error) {
  277. if req.Pn <= 0 {
  278. req.Pn = 1
  279. }
  280. if req.Ps <= 0 {
  281. req.Ps = 20
  282. }
  283. var f *model.Favorites
  284. var err error
  285. if int8(req.Tp) != model.TypeVideo {
  286. f, err = s.svr.Favorites(ctx, int8(req.Tp), req.Mid, req.Uid, req.Fid, int(req.Tid), int(req.Tv), int(req.Pn), int(req.Ps), req.Keyword, req.Order)
  287. } else {
  288. f, err = s.svr.FavoritesAll(ctx, int8(req.Tp), req.Mid, req.Uid, req.Fid, int(req.Tid), int(req.Tv), int(req.Pn), int(req.Ps), req.Keyword, req.Order)
  289. }
  290. if err != nil {
  291. return nil, err
  292. }
  293. reply := &pb.FavoritesReply{Res: &pb.ModelFavorites{Page: &pb.ModelPage{
  294. Num: int32(f.Page.Num),
  295. Size_: int32(f.Page.Size),
  296. Count: int32(f.Page.Count),
  297. }}}
  298. for _, data := range f.List {
  299. reply.Res.List = append(reply.Res.List, &pb.ModelFavorite{
  300. Id: data.ID,
  301. Oid: data.Oid,
  302. Mid: data.Mid,
  303. Fid: data.Fid,
  304. Type: int32(data.Type),
  305. State: int32(data.State),
  306. Ctime: int64(data.CTime),
  307. Mtime: int64(data.MTime),
  308. })
  309. }
  310. return reply, err
  311. }
  312. // Folders return folders by ids.
  313. func (s *server) Folders(ctx context.Context, req *pb.FoldersReq) (*pb.FoldersReply, error) {
  314. var afids []*model.ArgFVmid
  315. for _, id := range req.Ids {
  316. afids = append(afids, &model.ArgFVmid{
  317. Fid: id.Fid,
  318. Vmid: id.Mid % 100,
  319. })
  320. }
  321. res, err := s.svr.Folders(ctx, int8(req.Typ), req.Mid, afids)
  322. if err != nil {
  323. return nil, err
  324. }
  325. return &pb.FoldersReply{Res: res}, nil
  326. }