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

156 lines
7.0 KiB

3 years ago
package msg
import (
3 years ago
"Open_IM/pkg/common/config"
3 years ago
"Open_IM/pkg/common/constant"
imdb "Open_IM/pkg/common/db/mysql_model/im_mysql_model"
"Open_IM/pkg/common/log"
3 years ago
utils2 "Open_IM/pkg/common/utils"
3 years ago
pbFriend "Open_IM/pkg/proto/friend"
open_im_sdk "Open_IM/pkg/proto/sdk_ws"
"Open_IM/pkg/utils"
3 years ago
"github.com/golang/protobuf/proto"
3 years ago
)
3 years ago
func getFromToUserNickname(fromUserID, toUserID string) (string, string, error) {
from, err := imdb.GetUserByUserID(fromUserID)
if err != nil {
return "", "", utils.Wrap(err, "")
3 years ago
}
3 years ago
to, err := imdb.GetUserByUserID(toUserID)
if err != nil {
return "", "", utils.Wrap(err, "")
3 years ago
}
3 years ago
return from.Nickname, to.Nickname, nil
3 years ago
}
3 years ago
func friendNotification(commID *pbFriend.CommID, contentType int32, m proto.Message) {
log.Info(commID.OperationID, utils.GetSelfFuncName(), "args: ", commID, contentType)
var err error
var tips open_im_sdk.TipsComm
tips.Detail, err = proto.Marshal(m)
3 years ago
if err != nil {
3 years ago
log.Error(commID.OperationID, "Marshal failed ", err.Error(), m.String())
3 years ago
return
}
3 years ago
fromUserNickname, toUserNickname, err := getFromToUserNickname(commID.FromUserID, commID.ToUserID)
3 years ago
if err != nil {
3 years ago
log.Error(commID.OperationID, "getFromToUserNickname failed ", err.Error(), commID.FromUserID, commID.ToUserID)
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:
tips.DefaultTips = cn.BlackAdded.DefaultTips.Tips + toUserNickname
case constant.BlackDeletedNotification:
tips.DefaultTips = cn.BlackDeleted.DefaultTips.Tips + toUserNickname
default:
log.Error(commID.OperationID, "contentType failed ", contentType)
3 years ago
return
}
3 years ago
var n NotificationMsg
3 years ago
n.SendID = commID.FromUserID
n.RecvID = commID.ToUserID
n.ContentType = contentType
3 years ago
n.SessionType = constant.SingleChatType
n.MsgFrom = constant.SysMsgType
3 years ago
n.OperationID = commID.OperationID
3 years ago
n.Content, err = proto.Marshal(&tips)
if err != nil {
3 years ago
log.Error(commID.OperationID, "Marshal failed ", err.Error(), tips.String())
3 years ago
return
}
3 years ago
Notification(&n)
}
3 years ago
3 years ago
func FriendApplicationNotification(req *pbFriend.AddFriendReq) {
3 years ago
FriendApplicationTips := open_im_sdk.FriendApplicationTips{FromToUserID: &open_im_sdk.FromToUserID{}}
3 years ago
FriendApplicationTips.FromToUserID.FromUserID = req.CommID.FromUserID
FriendApplicationTips.FromToUserID.ToUserID = req.CommID.ToUserID
friendNotification(req.CommID, constant.FriendApplicationNotification, &FriendApplicationTips)
3 years ago
}
3 years ago
func FriendApplicationApprovedNotification(req *pbFriend.AddFriendResponseReq) {
FriendApplicationApprovedTips := open_im_sdk.FriendApplicationApprovedTips{FromToUserID: &open_im_sdk.FromToUserID{}}
FriendApplicationApprovedTips.FromToUserID.FromUserID = req.CommID.FromUserID
FriendApplicationApprovedTips.FromToUserID.ToUserID = req.CommID.ToUserID
FriendApplicationApprovedTips.HandleMsg = req.HandleMsg
friendNotification(req.CommID, constant.FriendApplicationApprovedNotification, &FriendApplicationApprovedTips)
3 years ago
}
3 years ago
3 years ago
func FriendApplicationRejectedNotification(req *pbFriend.AddFriendResponseReq) {
FriendApplicationApprovedTips := open_im_sdk.FriendApplicationApprovedTips{FromToUserID: &open_im_sdk.FromToUserID{}}
FriendApplicationApprovedTips.FromToUserID.FromUserID = req.CommID.FromUserID
FriendApplicationApprovedTips.FromToUserID.ToUserID = req.CommID.ToUserID
FriendApplicationApprovedTips.HandleMsg = req.HandleMsg
friendNotification(req.CommID, constant.FriendApplicationRejectedNotification, &FriendApplicationApprovedTips)
}
3 years ago
func FriendAddedNotification(operationID, opUserID, fromUserID, toUserID string) {
3 years ago
return
3 years ago
friendAddedTips := open_im_sdk.FriendAddedTips{Friend: &open_im_sdk.FriendInfo{}, OpUser: &open_im_sdk.PublicUserInfo{}}
3 years ago
user, err := imdb.GetUserByUserID(opUserID)
if err != nil {
3 years ago
log.NewError(operationID, "GetUserByUserID failed ", err.Error(), opUserID)
return
3 years ago
}
3 years ago
utils2.UserDBCopyOpenIMPublicUser(friendAddedTips.OpUser, user)
3 years ago
friend, err := imdb.GetFriendRelationshipFromFriend(fromUserID, toUserID)
if err != nil {
3 years ago
log.NewError(operationID, "GetFriendRelationshipFromFriend failed ", err.Error(), fromUserID, toUserID)
return
3 years ago
}
3 years ago
utils2.FriendDBCopyOpenIM(friendAddedTips.Friend, friend)
3 years ago
commID := pbFriend.CommID{FromUserID: fromUserID, ToUserID: toUserID, OpUserID: opUserID, OperationID: operationID}
friendNotification(&commID, constant.FriendAddedNotification, &friendAddedTips)
3 years ago
}
func FriendDeletedNotification(req *pbFriend.DeleteFriendReq) {
3 years ago
friendDeletedTips := open_im_sdk.FriendDeletedTips{FromToUserID: &open_im_sdk.FromToUserID{}}
3 years ago
friendDeletedTips.FromToUserID.FromUserID = req.CommID.FromUserID
friendDeletedTips.FromToUserID.ToUserID = req.CommID.ToUserID
3 years ago
friendNotification(req.CommID, constant.FriendDeletedNotification, &friendDeletedTips)
3 years ago
}
3 years ago
func FriendRemarkSetNotification(operationID, opUserID, 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
3 years ago
commID := pbFriend.CommID{FromUserID: fromUserID, ToUserID: toUserID, OpUserID: opUserID, OperationID: operationID}
3 years ago
friendNotification(&commID, constant.FriendRemarkSetNotification, &friendInfoChangedTips)
3 years ago
}
func BlackAddedNotification(req *pbFriend.AddBlacklistReq) {
3 years ago
blackAddedTips := open_im_sdk.BlackAddedTips{FromToUserID: &open_im_sdk.FromToUserID{}}
3 years ago
blackAddedTips.FromToUserID.FromUserID = req.CommID.FromUserID
blackAddedTips.FromToUserID.ToUserID = req.CommID.ToUserID
3 years ago
friendNotification(req.CommID, constant.BlackAddedNotification, &blackAddedTips)
3 years ago
}
func BlackDeletedNotification(req *pbFriend.RemoveBlacklistReq) {
3 years ago
blackDeletedTips := open_im_sdk.BlackDeletedTips{FromToUserID: &open_im_sdk.FromToUserID{}}
3 years ago
blackDeletedTips.FromToUserID.FromUserID = req.CommID.FromUserID
blackDeletedTips.FromToUserID.ToUserID = req.CommID.ToUserID
3 years ago
friendNotification(req.CommID, constant.BlackDeletedNotification, &blackDeletedTips)
3 years ago
}
3 years ago
func UserInfoUpdatedNotification(operationID, userID string, needNotifiedUserID string) {
3 years ago
selfInfoUpdatedTips := open_im_sdk.UserInfoUpdatedTips{UserID: userID}
3 years ago
commID := pbFriend.CommID{FromUserID: userID, ToUserID: userID, OpUserID: needNotifiedUserID, OperationID: operationID}
friendNotification(&commID, constant.UserInfoUpdatedNotification, &selfInfoUpdatedTips)
3 years ago
}