You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

304 regels
8.2 KiB

  1. package api
  2. import (
  3. "context"
  4. "egg-im/internal/business/app"
  5. comm "egg-im/internal/business/comm"
  6. "egg-im/internal/business/comm/utils"
  7. "egg-im/internal/business/domain/user/repo"
  8. friend2 "egg-im/internal/logic/domain/friend"
  9. "egg-im/internal/logic/domain/group/model"
  10. repo2 "egg-im/internal/logic/domain/group/repo"
  11. "egg-im/internal/logic/domain/message/service"
  12. "egg-im/pkg/grpclib"
  13. "egg-im/pkg/pb"
  14. "encoding/json"
  15. "errors"
  16. "strconv"
  17. "time"
  18. )
  19. type BusinessExtServer struct{}
  20. func (s *BusinessExtServer) ViewGroupNotice(ctx context.Context, req *pb.ViewGroupNoticeReq) (*pb.ViewGroupNoticeResp, error) {
  21. userId, _, err := grpclib.GetCtxData(ctx)
  22. if err != nil {
  23. return nil, err
  24. }
  25. groupNotice, err := repo2.GroupNoticeDao.Get(req.GroupId)
  26. if err != nil {
  27. return nil, err
  28. }
  29. if groupNotice == nil {
  30. return &pb.ViewGroupNoticeResp{
  31. GroupId: 0,
  32. UserId: 0,
  33. Content: "",
  34. LikeNums: 0,
  35. ReadNums: 0,
  36. PublishType: 0,
  37. PublishTime: "",
  38. IsLike: false,
  39. }, nil
  40. }
  41. groupNoticeWithLikeRecords, err := repo2.GroupNoticeWithLikeRecordsDao.Get(userId, req.GroupId)
  42. if err != nil {
  43. return nil, err
  44. }
  45. return &pb.ViewGroupNoticeResp{
  46. GroupId: groupNotice.GroupId,
  47. UserId: groupNotice.UserId,
  48. Content: groupNotice.Content,
  49. LikeNums: int64(groupNotice.LikeNums),
  50. ReadNums: int64(groupNotice.ReadNums),
  51. PublishType: pb.GroupNoticePublishType(groupNotice.PublishType),
  52. PublishTime: groupNotice.CreateTime.Format("2006-01-02 15:04:05"),
  53. IsLike: func() bool {
  54. if groupNoticeWithLikeRecords == nil {
  55. return false
  56. }
  57. return true
  58. }(),
  59. }, nil
  60. }
  61. func (s *BusinessExtServer) LikeGroupNotice(ctx context.Context, req *pb.LikeGroupNoticeReq) (*pb.Empty, error) {
  62. userId, _, err := grpclib.GetCtxData(ctx)
  63. if err != nil {
  64. return nil, err
  65. }
  66. groupNoticeWithLikeRecords, err := repo2.GroupNoticeWithLikeRecordsDao.Get(userId, req.GroupId)
  67. if err != nil {
  68. return nil, err
  69. }
  70. if groupNoticeWithLikeRecords != nil {
  71. return nil, errors.New("请勿重复点赞!")
  72. }
  73. groupNotice, err := repo2.GroupNoticeDao.Get(req.GroupId)
  74. if err != nil {
  75. return nil, err
  76. }
  77. if groupNotice == nil {
  78. return nil, errors.New("群公告记录不存在!")
  79. }
  80. err = repo2.GroupNoticeWithLikeRecordsDao.Save(&model.GroupNoticeWithLikeRecords{
  81. GroupId: req.GroupId,
  82. UserId: userId,
  83. })
  84. if err != nil {
  85. return nil, err
  86. }
  87. groupNotice.LikeNums++
  88. return new(pb.Empty), repo2.GroupNoticeDao.Save(groupNotice)
  89. }
  90. func (s *BusinessExtServer) CancelLikeGroupNotice(ctx context.Context, req *pb.CancelLikeGroupNoticeReq) (*pb.Empty, error) {
  91. userId, _, err := grpclib.GetCtxData(ctx)
  92. if err != nil {
  93. return nil, err
  94. }
  95. groupNoticeWithLikeRecords, err := repo2.GroupNoticeWithLikeRecordsDao.Get(userId, req.GroupId)
  96. if err != nil {
  97. return nil, err
  98. }
  99. if groupNoticeWithLikeRecords == nil {
  100. return nil, errors.New("不存在的点赞记录!")
  101. }
  102. groupNotice, err := repo2.GroupNoticeDao.Get(req.GroupId)
  103. if err != nil {
  104. return nil, err
  105. }
  106. if groupNotice == nil {
  107. return nil, errors.New("群公告记录不存在!")
  108. }
  109. err = repo2.GroupNoticeWithLikeRecordsDao.Delete(userId, req.GroupId)
  110. if err != nil {
  111. return nil, err
  112. }
  113. err = repo2.GroupNoticeWithLikeRecordsDao.Save(&model.GroupNoticeWithLikeRecords{
  114. GroupId: req.GroupId,
  115. UserId: userId,
  116. })
  117. if err != nil {
  118. return nil, err
  119. }
  120. groupNotice.LikeNums--
  121. return new(pb.Empty), repo2.GroupNoticeDao.Save(groupNotice)
  122. }
  123. func (s *BusinessExtServer) PublishGroupNotice(ctx context.Context, req *pb.PublishGroupNoticeReq) (*pb.Empty, error) {
  124. now := time.Now()
  125. userId, _, err := grpclib.GetCtxData(ctx)
  126. if err != nil {
  127. return nil, err
  128. }
  129. //1、判断是否可以进行发布群公告
  130. groupUser, err := repo2.GroupUserRepo.Get(req.GroupId, userId)
  131. if groupUser.MemberType == int(pb.MemberType_GMT_MEMBER) {
  132. return nil, errors.New("普通成员不能发布群公告")
  133. }
  134. groupComplain := model.GroupNotice{
  135. GroupId: req.GroupId,
  136. UserId: userId,
  137. PublishType: int32(req.PublishType),
  138. Content: req.Content,
  139. LikeNums: 0,
  140. ReadNums: 0,
  141. CreateTime: now,
  142. UpdateTime: now,
  143. }
  144. return new(pb.Empty), repo2.GroupNoticeDao.Save(&groupComplain)
  145. }
  146. func (s *BusinessExtServer) ComplainGroup(ctx context.Context, req *pb.ComplainGroupReq) (*pb.Empty, error) {
  147. now := time.Now()
  148. userId, _, err := grpclib.GetCtxData(ctx)
  149. if err != nil {
  150. return nil, err
  151. }
  152. imageList, _ := json.Marshal(req.ImageList)
  153. groupComplain := model.GroupComplain{
  154. GroupId: req.GroupId,
  155. UserId: userId,
  156. ComplainType: int(req.ComplainType),
  157. Text: req.Text,
  158. ImageList: string(imageList),
  159. Status: 0,
  160. CreateTime: now,
  161. UpdateTime: now,
  162. }
  163. return new(pb.Empty), repo2.GroupComplainDao.Save(&groupComplain)
  164. }
  165. func (s *BusinessExtServer) IsFriends(ctx context.Context, req *pb.IsFriendsReq) (*pb.IsFriendsResp, error) {
  166. user, err := repo.UserRepo.GetByPhoneNumber(req.UserPhone)
  167. if err != nil {
  168. return nil, err
  169. }
  170. if user == nil {
  171. return nil, errors.New("未查询到自身用户信息")
  172. }
  173. userFriend, err := repo.UserRepo.GetByPhoneNumber(req.FriendPhone)
  174. if err != nil {
  175. return nil, err
  176. }
  177. if userFriend == nil {
  178. return nil, errors.New("未查询到好友用户信息")
  179. }
  180. friend, err := friend2.FriendRepo.Get(user.Id, userFriend.Id)
  181. if err != nil {
  182. return nil, err
  183. }
  184. var isFriend = int64(1)
  185. if friend == nil || friend.Status == friend2.FriendStatusApply {
  186. isFriend = 2
  187. }
  188. return &pb.IsFriendsResp{
  189. User: &pb.User{
  190. UserId: userFriend.Id,
  191. Nickname: userFriend.Nickname,
  192. Sex: userFriend.Sex,
  193. AvatarUrl: userFriend.AvatarUrl,
  194. IsAutoAddedFriends: int64(userFriend.IsAutoAddedFriends),
  195. },
  196. IsFriend: isFriend,
  197. }, err
  198. }
  199. func (s *BusinessExtServer) EmoticonList(ctx context.Context, empty *pb.Empty) (*pb.EmoticonListResp, error) {
  200. list, err := service.EmoticonList()
  201. if err != nil {
  202. return nil, err
  203. }
  204. return &pb.EmoticonListResp{Emoticons: list}, err
  205. }
  206. func (s *BusinessExtServer) SignIn(ctx context.Context, req *pb.SignInReq) (*pb.SignInResp, error) {
  207. utils.FilePutContents("sign_in", utils.SerializeStr(map[string]interface{}{
  208. "args": req,
  209. }))
  210. isNew, userId, token, err := app.AuthApp.SignIn(ctx, req.PhoneNumber, req.Code, req.DeviceId, req.PushAlia, req.Nickname, req.AvatarUrl)
  211. if err != nil {
  212. return nil, err
  213. }
  214. return &pb.SignInResp{
  215. IsNew: isNew,
  216. UserId: userId,
  217. Token: token,
  218. }, nil
  219. }
  220. func (s *BusinessExtServer) GetUser(ctx context.Context, req *pb.GetUserReq) (*pb.GetUserResp, error) {
  221. var user *pb.User
  222. if req.Phone != "" {
  223. useInfo, err := repo.UserRepo.GetByPhoneNumber(req.Phone)
  224. user = &pb.User{
  225. UserId: useInfo.Id,
  226. Nickname: useInfo.Nickname,
  227. Sex: useInfo.Sex,
  228. AvatarUrl: useInfo.AvatarUrl,
  229. IsAutoAddedFriends: int64(useInfo.IsAutoAddedFriends),
  230. PhoneNumber: useInfo.PhoneNumber,
  231. }
  232. if err != nil {
  233. return nil, err
  234. }
  235. } else {
  236. userId, _, err := grpclib.GetCtxData(ctx)
  237. if err != nil {
  238. return nil, err
  239. }
  240. if req.UserId != 0 {
  241. userId = req.UserId
  242. }
  243. user, err = app.UserApp.Get(ctx, userId)
  244. }
  245. return &pb.GetUserResp{User: user}, nil
  246. }
  247. func (s *BusinessExtServer) UpdateUser(ctx context.Context, req *pb.UpdateUserReq) (*pb.Empty, error) {
  248. userId, _, err := grpclib.GetCtxData(ctx)
  249. if err != nil {
  250. return nil, err
  251. }
  252. return new(pb.Empty), app.UserApp.Update(ctx, userId, req)
  253. }
  254. func (s *BusinessExtServer) SearchUser(ctx context.Context, req *pb.SearchUserReq) (*pb.SearchUserResp, error) {
  255. users, err := app.UserApp.Search(ctx, req.Key)
  256. return &pb.SearchUserResp{Users: users}, err
  257. }
  258. func (s *BusinessExtServer) CloudUploadFile(ctx context.Context, req *pb.CloudUploadFileReq) (*pb.CloudUploadFileResp, error) {
  259. userId, _, err := grpclib.GetCtxData(ctx)
  260. if err != nil {
  261. return nil, err
  262. }
  263. upload, err := comm.CloudUpload.FileReqUpload(ctx, strconv.FormatInt(userId, 10), req.Dir, req.FileName, utils.StrToInt64(req.FileSize))
  264. data := utils.SerializeStr(upload)
  265. var result map[string]string
  266. utils.Unserialize([]byte(data), &result)
  267. return &pb.CloudUploadFileResp{
  268. Method: result["method"],
  269. Host: result["host"],
  270. Key: result["key"],
  271. Token: result["token"],
  272. }, err
  273. }