|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- package friend
-
- import (
- "context"
- svc "egg_im/internal/business/comm/service"
- "egg_im/internal/business/comm/utils"
- "egg_im/internal/logic/domain/message/repo"
- "egg_im/internal/logic/proxy"
- "egg_im/pkg/db"
- "egg_im/pkg/gerrors"
- "egg_im/pkg/pb"
- "egg_im/pkg/rpc"
- "google.golang.org/protobuf/proto"
- "time"
- )
-
- type friendService struct{}
-
- var FriendService = new(friendService)
-
- // List 获取好友列表
- func (s *friendService) List(ctx context.Context, userId int64) ([]*pb.Friend, error) {
- friends, err := FriendRepo.List(userId, FriendStatusAgree)
- if err != nil {
- return nil, err
- }
-
- userIds := make(map[int64]int32, len(friends))
- for i := range friends {
- userIds[friends[i].FriendId] = 0
- }
- resp, err := rpc.GetBusinessIntClient().GetUsers(ctx, &pb.GetUsersReq{UserIds: userIds})
- if err != nil {
- return nil, err
- }
-
- var infos = make([]*pb.Friend, len(friends))
- for i := range friends {
- friend := pb.Friend{
- UserId: friends[i].FriendId,
- Remarks: friends[i].Remarks,
- Extra: friends[i].Extra,
- }
-
- user, ok := resp.Users[friends[i].FriendId]
- if ok {
- friend.Nickname = user.Nickname
- friend.Sex = user.Sex
- friend.AvatarUrl = user.AvatarUrl
- friend.UserExtra = user.Extra
- }
- infos[i] = &friend
- }
-
- return infos, nil
- }
-
- // DeleteFriend 删除好友
- func (*friendService) DeleteFriend(ctx context.Context, userId, friendId int64) error {
- err := FriendRepo.Delete(userId, friendId)
- if err != nil {
- return err
- }
- return nil
- }
-
- // AddFriend 添加好友
- func (*friendService) AddFriend(ctx context.Context, userId, friendId int64, remarks, description string) error {
- if userId == friendId {
- return gerrors.ErrNotAllowAddSelf
- }
- isAutoAddFriends := svc.SysCfgGet("is_auto_add_friends")
-
- friend, err := FriendRepo.Get(userId, friendId)
- if err != nil {
- return err
- }
- if friend != nil {
- if friend.Status == FriendStatusApply {
- if isAutoAddFriends != "1" {
- return nil
- }
- //TODO::防止第一次加好友失败
- db.DB.Where("user_id = ? and friend_id = ?", userId, friendId).Delete(&friend)
- }
- if friend.Status == FriendStatusAgree {
- return gerrors.ErrAlreadyIsFriend
- }
- }
-
- now := time.Now()
- err = FriendRepo.Save(&Friend{
- UserId: userId,
- FriendId: friendId,
- Remarks: remarks,
- Status: FriendStatusApply,
- CreateTime: now,
- UpdateTime: now,
- })
- if err != nil {
- return err
- }
-
- resp, err := rpc.GetBusinessIntClient().GetUser(ctx, &pb.GetUserReq{UserId: userId})
- if err != nil {
- return err
- }
-
- err = proxy.MessageProxy.PushToUser(ctx, friendId, pb.PushCode_PC_ADD_FRIEND, &pb.AddFriendPush{
- FriendId: userId,
- Nickname: resp.User.Nickname,
- AvatarUrl: resp.User.AvatarUrl,
- Description: description,
- }, true)
- if err != nil {
- return err
- }
-
- //TODO::判断是否需要`自动添加好友`
- //if isAutoAddFriends == "1" && resp.User.IsAutoAddedFriends == 1 {
- if isAutoAddFriends == "1" {
- respFriend, err := rpc.GetBusinessIntClient().GetUser(ctx, &pb.GetUserReq{UserId: friendId})
- if err != nil {
- return err
- }
- utils.FilePutContents("addFriend", utils.SerializeStr(map[string]interface{}{
- "is_auto_add_friends": isAutoAddFriends,
- "user": resp.User,
- }))
-
- if description == "社交人脉圈添加好友" {
- return FriendService.AgreeAddFriend(ctx, friendId, userId, description)
- } else if description == "客服自动添加" {
- return FriendService.AgreeAddFriend(ctx, friendId, userId, "客服")
- } else {
- if respFriend.User.IsAutoAddedFriends == 1 {
- return FriendService.AgreeAddFriend(ctx, friendId, userId, "系统自动添加")
- }
- }
- }
- return nil
- }
-
- // AgreeAddFriend 同意添加好友
- func (*friendService) AgreeAddFriend(ctx context.Context, userId, friendId int64, remarks string) error {
- friend, err := FriendRepo.Get(friendId, userId)
- if err != nil {
- return err
- }
- if friend == nil {
- utils.FilePutContents("AgreeAddFriend", utils.SerializeStr(map[string]interface{}{
- "userId": userId,
- "friendId": friendId,
- }))
- return gerrors.ErrBadRequest
- }
- if friend.Status == FriendStatusAgree {
- return nil
- }
- friend.Status = FriendStatusAgree
- err = FriendRepo.Save(friend)
- if err != nil {
- return err
- }
-
- newFriend, err := FriendRepo.Get(userId, friendId)
- if newFriend != nil && newFriend.Status != FriendStatusAgree {
- newFriend.Status = FriendStatusAgree
- err = FriendRepo.Save(newFriend)
- } else {
- now := time.Now()
- err = FriendRepo.Save(&Friend{
- UserId: userId,
- FriendId: friendId,
- Remarks: remarks,
- Status: FriendStatusAgree,
- CreateTime: now,
- UpdateTime: now,
- })
- }
-
- if err != nil {
- return err
- }
-
- resp, err := rpc.GetBusinessIntClient().GetUser(ctx, &pb.GetUserReq{UserId: userId})
- if err != nil {
- return err
- }
-
- err = proxy.MessageProxy.PushToUser(ctx, friendId, pb.PushCode_PC_AGREE_ADD_FRIEND, &pb.AgreeAddFriendPush{
- FriendId: userId,
- Nickname: resp.User.Nickname,
- AvatarUrl: resp.User.AvatarUrl,
- }, true)
-
- utils.FilePutContents("push_AgreeAddFriend", utils.SerializeStr(map[string]interface{}{
- "friend_id": userId,
- "friend_nick_name": resp.User.Nickname,
- "friend_avatar_url": resp.User.AvatarUrl,
- "err": err,
- }))
- if err != nil {
- return err
- }
- return nil
- }
-
- // SendToFriend 消息发送至好友
- func (*friendService) SendToFriend(ctx context.Context, sender *pb.Sender, req *pb.SendMessageReq) (int64, error) {
- //TODO::判断是否为好友
- friend, err := FriendRepo.Get(sender.SenderId, req.ReceiverId)
- if err != nil {
- return 0, err
- }
- if friend == nil || friend.Status != FriendStatusAgree {
- return 0, gerrors.ErrNotIsFriend
- }
-
- //utils.FilePutContents("sendToFriend", utils.SerializeStr(map[string]interface{}{
- // "send": sender,
- // "req": req,
- //}))
- // 发给发送者
- seq, err := proxy.MessageProxy.SendToUser(ctx, sender, sender.SenderId, req)
- if err != nil {
- return 0, err
- }
-
- // 发给接收者
- _, err = proxy.MessageProxy.SendToUser(ctx, sender, req.ReceiverId, req)
- if err != nil {
- return 0, err
- }
-
- return seq, nil
- }
-
- // RecallMessageSendToFriend 撤回消息发送至好友
- func (*friendService) RecallMessageSendToFriend(ctx context.Context, sender *pb.Sender, req *pb.RecallMessageReq) (int64, error) {
- //TODO::判断是否为好友
- friend, err := FriendRepo.Get(sender.SenderId, req.ReceiverId)
- if err != nil {
- return 0, err
- }
- if friend == nil || friend.Status != FriendStatusAgree {
- return 0, gerrors.ErrNotIsFriend
- }
- utils.FilePutContents("RecallMessageSendToFriend", utils.SerializeStr(map[string]interface{}{
- "send": sender,
- "req": req,
- }))
-
- //查询到对应seq的消息
- msg := &pb.RECALL{}
- err = proto.Unmarshal(req.MessageContent, msg)
- if err != nil {
- return 0, err
- }
- message, err := repo.MessageRepo.GetMessage(sender.SenderId, msg.RecallSeq)
- if err != nil {
- return 0, err
- }
-
- // 发给发送者
- seq, err := proxy.MessageProxy.RecallMessageSendToUser(ctx, sender, sender.SenderId, req, message.SendTime)
- if err != nil {
- return 0, err
- }
-
- // 发给接收者
- _, err = proxy.MessageProxy.RecallMessageSendToUser(ctx, sender, req.ReceiverId, req, message.SendTime)
- if err != nil {
- return 0, err
- }
-
- return seq, nil
- }
-
- // RedPackageMessageSendToFriend 红包发送至好友
- func (*friendService) RedPackageMessageSendToFriend(ctx context.Context, sender *pb.Sender, req *pb.SendRedPacketReq) (int64, error) {
- //TODO::判断是否为好友
- friend, err := FriendRepo.Get(sender.SenderId, req.ReceiverId)
- if err != nil {
- return 0, err
- }
- if friend == nil || friend.Status != FriendStatusAgree {
- return 0, gerrors.ErrNotIsFriend
- }
-
- utils.FilePutContents("RedPackageMessageSendToFriend", utils.SerializeStr(map[string]interface{}{
- "send": sender,
- "req": req,
- }))
- // 发给发送者
- seq, err := proxy.MessageProxy.SendRedPackageToUser(ctx, sender, sender.SenderId, req)
- if err != nil {
- return 0, err
- }
-
- // 发给接收者
- _, err = proxy.MessageProxy.SendRedPackageToUser(ctx, sender, req.ReceiverId, req)
- if err != nil {
- return 0, err
- }
-
- return seq, nil
- }
|