Files
open-im-server/internal/rpc/friend/add_friend.go
T

134 lines
5.6 KiB
Go
Raw Normal View History

2021-05-26 19:37:45 +08:00
package friend
import (
2021-10-11 21:51:37 +08:00
"Open_IM/internal/push/content_struct"
"Open_IM/internal/push/logic"
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant"
"Open_IM/pkg/common/db/mysql_model/im_mysql_model"
"Open_IM/pkg/common/log"
2021-11-25 14:12:52 +08:00
"Open_IM/pkg/common/token_verify"
2021-10-11 22:00:38 +08:00
pbChat "Open_IM/pkg/proto/chat"
pbFriend "Open_IM/pkg/proto/friend"
"Open_IM/pkg/utils"
2021-05-26 19:37:45 +08:00
"context"
)
func (s *friendServer) AddFriend(ctx context.Context, req *pbFriend.AddFriendReq) (*pbFriend.CommonResp, error) {
log.Info(req.Token, req.OperationID, "rpc add friend is server,userid=%s", req.Uid)
//Parse token, to find current user information
2021-11-25 14:12:52 +08:00
claims, err := token_verify.ParseToken(req.Token)
2021-05-26 19:37:45 +08:00
if err != nil {
log.Error(req.Token, req.OperationID, "err=%s,parse token failed", err.Error())
2021-11-25 14:12:52 +08:00
return &pbFriend.CommonResp{ErrorCode: constant.ErrParseToken.ErrCode, ErrorMsg: constant.ErrParseToken.ErrMsg}, nil
2021-05-26 19:37:45 +08:00
}
2021-06-28 15:35:41 +08:00
//Cannot add non-existent users
if _, err = im_mysql_model.FindUserByUID(req.Uid); err != nil {
log.Error(req.Token, req.OperationID, "this user not exists,cant not add friend")
2021-11-25 14:12:52 +08:00
return &pbFriend.CommonResp{ErrorCode: constant.ErrAddFriend.ErrCode, ErrorMsg: constant.ErrSearchUserInfo.ErrMsg}, nil
2021-06-28 15:35:41 +08:00
}
2021-05-27 19:17:45 +08:00
//Establish a latest relationship in the friend request table
2021-06-28 15:35:41 +08:00
err = im_mysql_model.ReplaceIntoFriendReq(claims.UID, req.Uid, constant.ApplicationFriendFlag, req.ReqMessage)
2021-05-26 19:37:45 +08:00
if err != nil {
2021-06-28 15:35:41 +08:00
log.Error(req.Token, req.OperationID, "err=%s,create friend request record failed", err.Error())
2021-11-25 14:12:52 +08:00
return &pbFriend.CommonResp{ErrorCode: constant.ErrAddFriend.ErrCode, ErrorMsg: constant.ErrAddFriend.ErrMsg}, nil
2021-05-26 19:37:45 +08:00
}
log.Info(req.Token, req.OperationID, "rpc add friend is success return,uid=%s", req.Uid)
//Push message when add friend successfully
senderInfo, errSend := im_mysql_model.FindUserByUID(claims.UID)
receiverInfo, errReceive := im_mysql_model.FindUserByUID(req.Uid)
if errSend == nil && errReceive == nil {
logic.SendMsgByWS(&pbChat.WSToMsgSvrChatMsg{
SendID: senderInfo.UID,
RecvID: receiverInfo.UID,
2021-05-27 11:40:39 +08:00
Content: content_struct.NewContentStructString(0, "", senderInfo.Name+" asked to add you as a friend"),
2021-05-26 19:37:45 +08:00
SendTime: utils.GetCurrentTimestampBySecond(),
MsgFrom: constant.SysMsgType,
ContentType: constant.AddFriendTip,
SessionType: constant.SingleChatType,
OperationID: req.OperationID,
})
}
return &pbFriend.CommonResp{}, nil
}
2021-09-22 20:10:38 +08:00
2021-09-26 14:26:45 +08:00
func (s *friendServer) ImportFriend(ctx context.Context, req *pbFriend.ImportFriendReq) (*pbFriend.ImportFriendResp, error) {
log.Info(req.Token, req.OperationID, "ImportFriend come here,args=%s", req.String())
var resp pbFriend.ImportFriendResp
var c pbFriend.CommonResp
2021-09-22 20:10:38 +08:00
//Parse token, to find current user information
2021-11-25 14:12:52 +08:00
claims, err := token_verify.ParseToken(req.Token)
2021-09-22 20:10:38 +08:00
if err != nil {
2021-11-15 10:22:20 +08:00
log.NewError(req.OperationID, "parse token failed", err.Error())
2021-11-25 14:12:52 +08:00
c.ErrorCode = constant.ErrAddFriend.ErrCode
c.ErrorMsg = constant.ErrParseToken.ErrMsg
return &pbFriend.ImportFriendResp{CommonResp: &c, FailedUidList: req.UidList}, nil
2021-09-22 20:10:38 +08:00
}
2021-09-26 14:26:45 +08:00
if !utils.IsContain(claims.UID, config.Config.Manager.AppManagerUid) {
2021-11-15 10:18:59 +08:00
log.NewError(req.OperationID, "not manager uid", claims.UID)
2021-11-25 14:12:52 +08:00
c.ErrorCode = constant.ErrAddFriend.ErrCode
c.ErrorMsg = "not authorized"
return &pbFriend.ImportFriendResp{CommonResp: &c, FailedUidList: req.UidList}, nil
2021-09-22 20:10:38 +08:00
}
if _, err = im_mysql_model.FindUserByUID(req.OwnerUid); err != nil {
2021-11-15 10:22:20 +08:00
log.NewError(req.OperationID, "this user not exists,cant not add friend", req.OwnerUid)
2021-11-25 14:12:52 +08:00
c.ErrorCode = constant.ErrAddFriend.ErrCode
c.ErrorMsg = "this user not exists,cant not add friend"
return &pbFriend.ImportFriendResp{CommonResp: &c, FailedUidList: req.UidList}, nil
2021-09-22 20:10:38 +08:00
}
2021-09-26 14:26:45 +08:00
for _, v := range req.UidList {
if _, fErr := im_mysql_model.FindUserByUID(v); fErr != nil {
c.ErrorMsg = "some uid establish failed"
c.ErrorCode = 408
2021-09-26 14:26:45 +08:00
resp.FailedUidList = append(resp.FailedUidList, v)
} else {
if _, err = im_mysql_model.FindFriendRelationshipFromFriend(req.OwnerUid, v); err != nil {
//Establish two single friendship
err1 := im_mysql_model.InsertToFriend(req.OwnerUid, v, 1)
if err1 != nil {
resp.FailedUidList = append(resp.FailedUidList, v)
log.NewError(req.OperationID, "err1,create friendship failed", req.OwnerUid, v, err1.Error())
2021-09-26 14:26:45 +08:00
}
err2 := im_mysql_model.InsertToFriend(v, req.OwnerUid, 1)
if err2 != nil {
log.NewError(req.OperationID, "err2,create friendship failed", v, req.OwnerUid, err2.Error())
2021-09-26 14:26:45 +08:00
}
if err1 == nil && err2 == nil {
var name, faceUrl string
n := content_struct.NotificationContent{IsDisplay: 1, DefaultTips: constant.FriendAcceptTip}
2021-09-26 14:26:45 +08:00
r, err := im_mysql_model.FindUserByUID(v)
if err != nil {
log.NewError(req.OperationID, "get info failed", err.Error(), v)
2021-09-26 14:26:45 +08:00
}
if r != nil {
name, faceUrl = r.Name, r.Icon
}
2021-09-22 20:10:38 +08:00
2021-09-26 14:26:45 +08:00
logic.SendMsgByWS(&pbChat.WSToMsgSvrChatMsg{
SendID: v,
RecvID: req.OwnerUid,
SenderFaceURL: faceUrl,
SenderNickName: name,
Content: n.ContentToString(),
SendTime: utils.GetCurrentTimestampByNano(),
MsgFrom: constant.UserMsgType, //Notification message identification
ContentType: constant.AcceptFriendApplicationTip, //Add friend flag
SessionType: constant.SingleChatType,
OperationID: req.OperationID,
})
2021-10-08 17:50:09 +08:00
} else {
2021-11-15 11:46:02 +08:00
c.ErrorMsg = "some uid establish failed"
c.ErrorCode = 408
2021-10-08 17:50:09 +08:00
resp.FailedUidList = append(resp.FailedUidList, v)
2021-09-26 14:26:45 +08:00
}
}
}
2021-09-22 20:10:38 +08:00
}
2021-11-15 11:44:04 +08:00
resp.CommonResp = &c
2021-11-15 11:31:38 +08:00
log.NewDebug(req.OperationID, "rpc come end", resp.CommonResp.ErrorCode, resp.CommonResp.ErrorMsg, resp.FailedUidList)
2021-09-26 14:26:45 +08:00
return &resp, nil
2021-09-22 20:10:38 +08:00
}