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.
Open-IM-Server/internal/rpc/msg/friend_notification.go

174 lines
7.7 KiB

3 years ago
package msg
import (
"Open_IM/internal/common/check"
3 years ago
"Open_IM/pkg/common/config"
3 years ago
"Open_IM/pkg/common/constant"
"Open_IM/pkg/common/log"
"Open_IM/pkg/common/tracelog"
3 years ago
pbFriend "Open_IM/pkg/proto/friend"
open_im_sdk "Open_IM/pkg/proto/sdk_ws"
"Open_IM/pkg/utils"
"context"
3 years ago
"github.com/golang/protobuf/jsonpb"
3 years ago
"github.com/golang/protobuf/proto"
3 years ago
)
3 years ago
func getFromToUserNickname(fromUserID, toUserID string) (string, string, error) {
users, err := check.GetUsersInfo(context.Background(), fromUserID, toUserID)
3 years ago
if err != nil {
return "", "", nil
3 years ago
}
if users[0].UserID == fromUserID {
return users[0].Nickname, users[1].Nickname, nil
3 years ago
}
return users[1].Nickname, users[0].Nickname, nil
3 years ago
}
func friendNotification(operationID, fromUserID, toUserID string, contentType int32, m proto.Message) {
log.Info(operationID, utils.GetSelfFuncName(), "args: ", contentType)
3 years ago
var err error
var tips open_im_sdk.TipsComm
tips.Detail, err = proto.Marshal(m)
3 years ago
if err != nil {
log.Error(operationID, "Marshal failed ", err.Error(), m.String())
3 years ago
return
}
3 years ago
3 years ago
marshaler := jsonpb.Marshaler{
OrigName: true,
EnumsAsInts: false,
EmitDefaults: false,
}
tips.JsonDetail, _ = marshaler.MarshalToString(m)
fromUserNickname, toUserNickname, err := getFromToUserNickname(fromUserID, toUserID)
3 years ago
if err != nil {
log.Error(operationID, "getFromToUserNickname failed ", err.Error(), fromUserID, toUserID)
3 years ago
return
}
cn := config.Config.Notification
switch contentType {
3 years ago
case constant.FriendApplicationNotification:
3 years ago
tips.DefaultTips = fromUserNickname + cn.FriendApplication.DefaultTips.Tips
3 years ago
case constant.FriendApplicationApprovedNotification:
tips.DefaultTips = fromUserNickname + cn.FriendApplicationApproved.DefaultTips.Tips
case constant.FriendApplicationRejectedNotification:
tips.DefaultTips = fromUserNickname + cn.FriendApplicationRejected.DefaultTips.Tips
case constant.FriendAddedNotification:
tips.DefaultTips = cn.FriendAdded.DefaultTips.Tips
case constant.FriendDeletedNotification:
tips.DefaultTips = cn.FriendDeleted.DefaultTips.Tips + toUserNickname
3 years ago
case constant.FriendRemarkSetNotification:
3 years ago
tips.DefaultTips = fromUserNickname + cn.FriendRemarkSet.DefaultTips.Tips
3 years ago
case constant.BlackAddedNotification:
3 years ago
tips.DefaultTips = cn.BlackAdded.DefaultTips.Tips
3 years ago
case constant.BlackDeletedNotification:
tips.DefaultTips = cn.BlackDeleted.DefaultTips.Tips + toUserNickname
3 years ago
case constant.UserInfoUpdatedNotification:
tips.DefaultTips = cn.UserInfoUpdated.DefaultTips.Tips
case constant.FriendInfoUpdatedNotification:
tips.DefaultTips = cn.FriendInfoUpdated.DefaultTips.Tips + toUserNickname
3 years ago
default:
log.Error(operationID, "contentType failed ", contentType)
3 years ago
return
}
3 years ago
var n NotificationMsg
n.SendID = fromUserID
n.RecvID = toUserID
3 years ago
n.ContentType = contentType
3 years ago
n.SessionType = constant.SingleChatType
n.MsgFrom = constant.SysMsgType
n.OperationID = operationID
3 years ago
n.Content, err = proto.Marshal(&tips)
if err != nil {
log.Error(operationID, "Marshal failed ", err.Error(), tips.String())
3 years ago
return
}
3 years ago
Notification(&n)
}
3 years ago
func FriendApplicationAddNotification(ctx context.Context, req *pbFriend.AddFriendReq) {
3 years ago
FriendApplicationTips := open_im_sdk.FriendApplicationTips{FromToUserID: &open_im_sdk.FromToUserID{}}
FriendApplicationTips.FromToUserID.FromUserID = req.FromUserID
FriendApplicationTips.FromToUserID.ToUserID = req.ToUserID
friendNotification(tracelog.GetOperationID(ctx), req.FromUserID, req.ToUserID, constant.FriendApplicationNotification, &FriendApplicationTips)
3 years ago
}
func FriendApplicationAgreedNotification(ctx context.Context, req *pbFriend.RespondFriendApplyReq) {
3 years ago
FriendApplicationApprovedTips := open_im_sdk.FriendApplicationApprovedTips{FromToUserID: &open_im_sdk.FromToUserID{}}
FriendApplicationApprovedTips.FromToUserID.FromUserID = req.FromUserID
FriendApplicationApprovedTips.FromToUserID.ToUserID = req.ToUserID
3 years ago
FriendApplicationApprovedTips.HandleMsg = req.HandleMsg
friendNotification(tracelog.GetOperationID(ctx), req.ToUserID, req.FromUserID, constant.FriendApplicationApprovedNotification, &FriendApplicationApprovedTips)
3 years ago
}
3 years ago
func FriendApplicationRefusedNotification(ctx context.Context, req *pbFriend.RespondFriendApplyReq) {
3 years ago
FriendApplicationApprovedTips := open_im_sdk.FriendApplicationApprovedTips{FromToUserID: &open_im_sdk.FromToUserID{}}
FriendApplicationApprovedTips.FromToUserID.FromUserID = req.FromUserID
FriendApplicationApprovedTips.FromToUserID.ToUserID = req.ToUserID
3 years ago
FriendApplicationApprovedTips.HandleMsg = req.HandleMsg
friendNotification(tracelog.GetOperationID(ctx), req.ToUserID, req.FromUserID, constant.FriendApplicationRejectedNotification, &FriendApplicationApprovedTips)
3 years ago
}
func FriendAddedNotification(ctx context.Context, operationID, opUserID, fromUserID, toUserID string) {
3 years ago
friendAddedTips := open_im_sdk.FriendAddedTips{Friend: &open_im_sdk.FriendInfo{}, OpUser: &open_im_sdk.PublicUserInfo{}}
user, err := check.GetUsersInfo(context.Background(), opUserID)
3 years ago
if err != nil {
3 years ago
return
3 years ago
}
friendAddedTips.OpUser.UserID = user[0].UserID
friendAddedTips.OpUser.Ex = user[0].Ex
friendAddedTips.OpUser.Nickname = user[0].Nickname
friendAddedTips.OpUser.FaceURL = user[0].FaceURL
friend, err := check.GetFriendsInfo(ctx, fromUserID, toUserID)
3 years ago
if err != nil {
3 years ago
return
3 years ago
}
friendAddedTips.Friend = friend
friendNotification(operationID, fromUserID, toUserID, constant.FriendAddedNotification, &friendAddedTips)
3 years ago
}
func FriendDeletedNotification(ctx context.Context, req *pbFriend.DeleteFriendReq) {
3 years ago
friendDeletedTips := open_im_sdk.FriendDeletedTips{FromToUserID: &open_im_sdk.FromToUserID{}}
friendDeletedTips.FromToUserID.FromUserID = req.OwnerUserID
friendDeletedTips.FromToUserID.ToUserID = req.FriendUserID
friendNotification(tracelog.GetOperationID(ctx), req.OwnerUserID, req.FriendUserID, constant.FriendDeletedNotification, &friendDeletedTips)
3 years ago
}
func FriendRemarkSetNotification(ctx context.Context, fromUserID, toUserID string) {
3 years ago
friendInfoChangedTips := open_im_sdk.FriendInfoChangedTips{FromToUserID: &open_im_sdk.FromToUserID{}}
3 years ago
friendInfoChangedTips.FromToUserID.FromUserID = fromUserID
friendInfoChangedTips.FromToUserID.ToUserID = toUserID
friendNotification(tracelog.GetOperationID(ctx), fromUserID, toUserID, constant.FriendRemarkSetNotification, &friendInfoChangedTips)
3 years ago
}
func BlackAddedNotification(ctx context.Context, req *pbFriend.AddBlackReq) {
3 years ago
blackAddedTips := open_im_sdk.BlackAddedTips{FromToUserID: &open_im_sdk.FromToUserID{}}
blackAddedTips.FromToUserID.FromUserID = req.OwnerUserID
blackAddedTips.FromToUserID.ToUserID = req.BlackUserID
friendNotification(tracelog.GetOperationID(ctx), req.OwnerUserID, req.BlackUserID, constant.BlackAddedNotification, &blackAddedTips)
3 years ago
}
func BlackDeletedNotification(ctx context.Context, req *pbFriend.RemoveBlackReq) {
3 years ago
blackDeletedTips := open_im_sdk.BlackDeletedTips{FromToUserID: &open_im_sdk.FromToUserID{}}
blackDeletedTips.FromToUserID.FromUserID = req.OwnerUserID
blackDeletedTips.FromToUserID.ToUserID = req.BlackUserID
friendNotification(tracelog.GetOperationID(ctx), req.OwnerUserID, req.BlackUserID, constant.BlackDeletedNotification, &blackDeletedTips)
3 years ago
}
// send to myself
func UserInfoUpdatedNotification(ctx context.Context, opUserID string, changedUserID string) {
selfInfoUpdatedTips := open_im_sdk.UserInfoUpdatedTips{UserID: changedUserID}
friendNotification(tracelog.GetOperationID(ctx), opUserID, changedUserID, constant.UserInfoUpdatedNotification, &selfInfoUpdatedTips)
3 years ago
}
func FriendInfoUpdatedNotification(ctx context.Context, changedUserID string, needNotifiedUserID string, opUserID string) {
selfInfoUpdatedTips := open_im_sdk.UserInfoUpdatedTips{UserID: changedUserID}
friendNotification(tracelog.GetOperationID(ctx), opUserID, needNotifiedUserID, constant.FriendInfoUpdatedNotification, &selfInfoUpdatedTips)
}