Refactor code

pull/141/head
wenxu12345 3 years ago
parent a2440a2e80
commit 7e13cc84c8

@ -8,7 +8,6 @@ import (
pbFriend "Open_IM/pkg/proto/friend"
open_im_sdk "Open_IM/pkg/proto/sdk_ws"
"Open_IM/pkg/utils"
"encoding/json"
"github.com/golang/protobuf/proto"
)
@ -42,16 +41,21 @@ func getFromToUserNickname(fromUserID, toUserID string) (string, string, error)
}
func FriendApplicationAddedNotification(req *pbFriend.AddFriendReq) {
var FriendApplicationAddedTips open_im_sdk.FriendApplicationAddedTips
FriendApplicationAddedTips.FromToUserID.FromUserID = req.CommID.FromUserID
FriendApplicationAddedTips.FromToUserID.ToUserID = req.CommID.ToUserID
log.Info(req.CommID.OperationID, utils.GetSelfFuncName(), "args: ", req.String())
var friendApplicationAddedTips open_im_sdk.FriendApplicationAddedTips
friendApplicationAddedTips.FromToUserID.FromUserID = req.CommID.FromUserID
friendApplicationAddedTips.FromToUserID.ToUserID = req.CommID.ToUserID
fromUserNickname, toUserNickname, err := getFromToUserNickname(req.CommID.FromUserID, req.CommID.ToUserID)
if err != nil {
log.Error(req.CommID.OperationID, "getFromToUserNickname failed ", err.Error(), req.CommID.FromUserID, req.CommID.ToUserID)
return
}
var tips open_im_sdk.TipsComm
tips.Detail, _ = json.Marshal(FriendApplicationAddedTips)
tips.Detail, err = proto.Marshal(&friendApplicationAddedTips)
if err != nil {
log.Error(req.CommID.OperationID, "Marshal failed ", err.Error(), friendApplicationAddedTips.String())
return
}
tips.DefaultTips = fromUserNickname + " FriendApplicationAddedNotification " + toUserNickname
var n NotificationMsg
@ -61,14 +65,18 @@ func FriendApplicationAddedNotification(req *pbFriend.AddFriendReq) {
n.SessionType = constant.SingleChatType
n.MsgFrom = constant.SysMsgType
n.OperationID = req.CommID.OperationID
n.Content, _ = json.Marshal(tips)
n.Content, err = proto.Marshal(&tips)
if err != nil {
log.Error(req.CommID.OperationID, "Marshal failed ", err.Error(), tips.String())
return
}
Notification(&n)
}
func FriendApplicationProcessedNotification(req *pbFriend.AddFriendResponseReq) {
var FriendApplicationProcessedTips open_im_sdk.FriendApplicationProcessedTips
FriendApplicationProcessedTips.FromToUserID.FromUserID = req.CommID.FromUserID
FriendApplicationProcessedTips.FromToUserID.ToUserID = req.CommID.ToUserID
var friendApplicationProcessedTips open_im_sdk.FriendApplicationProcessedTips
friendApplicationProcessedTips.FromToUserID.FromUserID = req.CommID.FromUserID
friendApplicationProcessedTips.FromToUserID.ToUserID = req.CommID.ToUserID
fromUserNickname, toUserNickname, err := getFromToUserNickname(req.CommID.FromUserID, req.CommID.ToUserID)
if err != nil {
log.Error(req.CommID.OperationID, "getFromToUserNickname failed ", err.Error(), req.CommID.FromUserID, req.CommID.ToUserID)
@ -76,9 +84,9 @@ func FriendApplicationProcessedNotification(req *pbFriend.AddFriendResponseReq)
}
var tips open_im_sdk.TipsComm
tips.Detail, err = proto.Marshal(&FriendApplicationProcessedTips)
tips.Detail, err = proto.Marshal(&friendApplicationProcessedTips)
if err != nil {
log.Error(req.CommID.OperationID, "Marshal failed ", err.Error(), FriendApplicationProcessedTips)
log.Error(req.CommID.OperationID, "Marshal failed ", err.Error(), friendApplicationProcessedTips.String())
return
}
tips.DefaultTips = fromUserNickname + " FriendApplicationProcessedNotification " + toUserNickname
@ -90,39 +98,39 @@ func FriendApplicationProcessedNotification(req *pbFriend.AddFriendResponseReq)
n.SessionType = constant.SingleChatType
n.MsgFrom = constant.SysMsgType
n.OperationID = req.CommID.OperationID
n.Content, _ = json.Marshal(tips)
n.Content, err = proto.Marshal(&tips)
if err != nil {
log.Error(req.CommID.OperationID, "Marshal failed ", err.Error(), tips)
log.Error(req.CommID.OperationID, "Marshal failed ", err.Error(), tips.String())
return
}
Notification(&n)
}
func FriendAddedNotification(operationID, opUserID, fromUserID, toUserID string) {
var FriendAddedTips open_im_sdk.FriendAddedTips
var friendAddedTips open_im_sdk.FriendAddedTips
user, err := imdb.GetUserByUserID(opUserID)
if err != nil {
log.NewError(operationID, "GetUserByUserID failed ", err.Error(), opUserID)
return
}
utils.CopyStructFields(FriendAddedTips.OpUser, user)
utils2.UserDBCopyOpenIMPublicUser(friendAddedTips.OpUser, user)
friend, err := imdb.GetFriendRelationshipFromFriend(fromUserID, toUserID)
if err != nil {
log.NewError(operationID, "GetFriendRelationshipFromFriend failed ", err.Error(), fromUserID, toUserID)
return
}
utils2.FriendDBCopyOpenIM(FriendAddedTips.Friend, friend)
utils2.FriendDBCopyOpenIM(friendAddedTips.Friend, friend)
fromUserNickname, toUserNickname, err := getFromToUserNickname(fromUserID, toUserID)
if err != nil {
log.Error(operationID, "getFromToUserNickname failed ", err.Error(), fromUserID, toUserID)
return
}
var tips open_im_sdk.TipsComm
tips.Detail, _ = json.Marshal(FriendAddedTips)
tips.Detail, err = proto.Marshal(&friendAddedTips)
if err != nil {
log.Error(operationID, "Marshal failed ", err.Error(), friendAddedTips)
return
}
tips.DefaultTips = fromUserNickname + " FriendAddedNotification " + toUserNickname
var n NotificationMsg
n.SendID = fromUserID
@ -131,7 +139,11 @@ func FriendAddedNotification(operationID, opUserID, fromUserID, toUserID string)
n.SessionType = constant.SingleChatType
n.MsgFrom = constant.SysMsgType
n.OperationID = operationID
n.Content, _ = json.Marshal(tips)
n.Content, _ = proto.Marshal(&tips)
if err != nil {
log.Error(operationID, "Marshal failed ", err.Error(), tips.String())
return
}
Notification(&n)
}
@ -139,17 +151,20 @@ func FriendAddedNotification(operationID, opUserID, fromUserID, toUserID string)
// FriendInfo Friend = 1;
//}
func FriendDeletedNotification(req *pbFriend.DeleteFriendReq) {
var FriendDeletedTips open_im_sdk.FriendDeletedTips
FriendDeletedTips.FromToUserID.FromUserID = req.CommID.FromUserID
FriendDeletedTips.FromToUserID.ToUserID = req.CommID.ToUserID
var friendDeletedTips open_im_sdk.FriendDeletedTips
friendDeletedTips.FromToUserID.FromUserID = req.CommID.FromUserID
friendDeletedTips.FromToUserID.ToUserID = req.CommID.ToUserID
fromUserNickname, toUserNickname, err := getFromToUserNickname(req.CommID.FromUserID, req.CommID.ToUserID)
if err != nil {
log.Error(req.CommID.OperationID, "getFromToUserNickname failed ", req.CommID.FromUserID, req.CommID.ToUserID)
return
}
var tips open_im_sdk.TipsComm
tips.Detail, _ = json.Marshal(FriendDeletedTips)
tips.Detail, err = proto.Marshal(&friendDeletedTips)
if err != nil {
log.Error(req.CommID.OperationID, "Marshal failed ", err.Error(), friendDeletedTips.String())
return
}
tips.DefaultTips = fromUserNickname + " FriendDeletedNotification " + toUserNickname
var n NotificationMsg
n.SendID = req.CommID.FromUserID
@ -158,7 +173,11 @@ func FriendDeletedNotification(req *pbFriend.DeleteFriendReq) {
n.SessionType = constant.SingleChatType
n.MsgFrom = constant.SysMsgType
n.OperationID = req.CommID.OperationID
n.Content, _ = json.Marshal(tips)
n.Content, err = proto.Marshal(&tips)
if err != nil {
log.Error(req.CommID.OperationID, "Marshal failed ", err.Error(), tips.String())
return
}
Notification(&n)
}
@ -168,17 +187,20 @@ func FriendDeletedNotification(req *pbFriend.DeleteFriendReq) {
// uint64 OperationTime = 3;
//}
func FriendInfoChangedNotification(operationID, opUserID, fromUserID, toUserID string) {
var FriendInfoChangedTips open_im_sdk.FriendInfoChangedTips
FriendInfoChangedTips.FromToUserID.FromUserID = fromUserID
FriendInfoChangedTips.FromToUserID.ToUserID = toUserID
var friendInfoChangedTips open_im_sdk.FriendInfoChangedTips
friendInfoChangedTips.FromToUserID.FromUserID = fromUserID
friendInfoChangedTips.FromToUserID.ToUserID = toUserID
fromUserNickname, toUserNickname, err := getFromToUserNickname(fromUserID, toUserID)
if err != nil {
log.Error(operationID, "getFromToUserNickname failed ", fromUserID, toUserID)
return
}
var tips open_im_sdk.TipsComm
tips.Detail, _ = json.Marshal(FriendInfoChangedTips)
tips.Detail, err = proto.Marshal(&friendInfoChangedTips)
if err != nil {
log.Error(operationID, "Marshal failed ", err.Error(), friendInfoChangedTips.String())
return
}
tips.DefaultTips = fromUserNickname + " FriendDeletedNotification " + toUserNickname
var n NotificationMsg
n.SendID = fromUserID
@ -188,22 +210,29 @@ func FriendInfoChangedNotification(operationID, opUserID, fromUserID, toUserID s
n.MsgFrom = constant.SysMsgType
n.OperationID = operationID
n.Content, _ = json.Marshal(tips)
n.Content, err = proto.Marshal(&tips)
if err != nil {
log.Error(operationID, "Marshal failed ", err.Error(), tips.String())
return
}
Notification(&n)
}
func BlackAddedNotification(req *pbFriend.AddBlacklistReq) {
var BlackAddedTips open_im_sdk.BlackAddedTips
BlackAddedTips.FromToUserID.FromUserID = req.CommID.FromUserID
BlackAddedTips.FromToUserID.ToUserID = req.CommID.ToUserID
var blackAddedTips open_im_sdk.BlackAddedTips
blackAddedTips.FromToUserID.FromUserID = req.CommID.FromUserID
blackAddedTips.FromToUserID.ToUserID = req.CommID.ToUserID
fromUserNickname, toUserNickname, err := getFromToUserNickname(req.CommID.FromUserID, req.CommID.ToUserID)
if err != nil {
log.Error(req.CommID.OperationID, "getFromToUserNickname failed ", req.CommID.FromUserID, req.CommID.ToUserID)
return
}
var tips open_im_sdk.TipsComm
tips.Detail, _ = json.Marshal(BlackAddedTips)
tips.Detail, err = proto.Marshal(&blackAddedTips)
if err != nil {
log.Error(req.CommID.OperationID, "Marshal failed ", err.Error(), blackAddedTips.String())
return
}
tips.DefaultTips = fromUserNickname + " BlackAddedNotification " + toUserNickname
var n NotificationMsg
n.SendID = req.CommID.FromUserID
@ -212,7 +241,11 @@ func BlackAddedNotification(req *pbFriend.AddBlacklistReq) {
n.SessionType = constant.SingleChatType
n.MsgFrom = constant.SysMsgType
n.OperationID = req.CommID.OperationID
n.Content, _ = json.Marshal(tips)
n.Content, err = proto.Marshal(&tips)
if err != nil {
log.Error(req.CommID.OperationID, "Marshal failed ", err.Error(), tips.String())
return
}
Notification(&n)
}
@ -220,16 +253,20 @@ func BlackAddedNotification(req *pbFriend.AddBlacklistReq) {
// BlackInfo Black = 1;
//}
func BlackDeletedNotification(req *pbFriend.RemoveBlacklistReq) {
var BlackDeletedTips open_im_sdk.BlackDeletedTips
BlackDeletedTips.FromToUserID.FromUserID = req.CommID.FromUserID
BlackDeletedTips.FromToUserID.ToUserID = req.CommID.ToUserID
var blackDeletedTips open_im_sdk.BlackDeletedTips
blackDeletedTips.FromToUserID.FromUserID = req.CommID.FromUserID
blackDeletedTips.FromToUserID.ToUserID = req.CommID.ToUserID
fromUserNickname, toUserNickname, err := getFromToUserNickname(req.CommID.FromUserID, req.CommID.ToUserID)
if err != nil {
log.Error(req.CommID.OperationID, "getFromToUserNickname failed ", err.Error(), req.CommID.FromUserID, req.CommID.ToUserID)
return
}
var tips open_im_sdk.TipsComm
tips.Detail, _ = json.Marshal(BlackDeletedTips)
tips.Detail, err = proto.Marshal(&blackDeletedTips)
if err != nil {
log.Error(req.CommID.OperationID, "Marshal failed ", err.Error(), blackDeletedTips.String())
return
}
tips.DefaultTips = fromUserNickname + " BlackDeletedNotification " + toUserNickname
var n NotificationMsg
n.SendID = req.CommID.FromUserID
@ -238,7 +275,11 @@ func BlackDeletedNotification(req *pbFriend.RemoveBlacklistReq) {
n.SessionType = constant.SingleChatType
n.MsgFrom = constant.SysMsgType
n.OperationID = req.CommID.OperationID
n.Content, _ = json.Marshal(tips)
n.Content, err = proto.Marshal(&tips)
if err != nil {
log.Error(req.CommID.OperationID, "Marshal failed ", err.Error(), tips.String())
return
}
Notification(&n)
}
@ -248,15 +289,19 @@ func BlackDeletedNotification(req *pbFriend.RemoveBlacklistReq) {
// uint64 OperationTime = 3;
//}
func SelfInfoUpdatedNotification(operationID, userID string) {
var SelfInfoUpdatedTips open_im_sdk.SelfInfoUpdatedTips
SelfInfoUpdatedTips.UserID = userID
var selfInfoUpdatedTips open_im_sdk.SelfInfoUpdatedTips
selfInfoUpdatedTips.UserID = userID
u, err := imdb.GetUserByUserID(userID)
if err != nil {
log.NewError(operationID, "FindUserByUID failed ", err.Error(), userID)
return
}
var tips open_im_sdk.TipsComm
tips.Detail, _ = json.Marshal(SelfInfoUpdatedTips)
tips.Detail, err = proto.Marshal(&selfInfoUpdatedTips)
if err != nil {
log.Error(operationID, "Marshal failed ", err.Error(), selfInfoUpdatedTips.String())
return
}
var n NotificationMsg
n.SendID = userID
n.RecvID = userID
@ -266,6 +311,10 @@ func SelfInfoUpdatedNotification(operationID, userID string) {
n.OperationID = operationID
tips.DefaultTips = u.Nickname + " SelfInfoUpdatedNotification "
n.Content, _ = json.Marshal(tips)
n.Content, err = proto.Marshal(&tips)
if err != nil {
log.Error(operationID, "Marshal failed ", err.Error(), tips.String())
return
}
Notification(&n)
}

@ -34,7 +34,7 @@ type Friend struct {
//int64 HandleTime = 8;
//string Ex = 9;
//}
//open_im_sdk.FriendRequest == imdb.FriendRequest
//open_im_sdk.FriendRequest(nickname, farce url ...) != imdb.FriendRequest
type FriendRequest struct {
FromUserID string `gorm:"column:from_user_id;primary_key;size:64"`
ToUserID string `gorm:"column:to_user_id;primary_key;size:64"`

@ -3,6 +3,7 @@ package utils
import (
db "Open_IM/pkg/common/db"
imdb "Open_IM/pkg/common/db/mysql_model/im_mysql_model"
"Open_IM/pkg/common/log"
"Open_IM/pkg/common/token_verify"
open_im_sdk "Open_IM/pkg/proto/sdk_ws"
"Open_IM/pkg/utils"
@ -18,27 +19,64 @@ func OperationIDGenerator() string {
func FriendOpenIMCopyDB(dst *db.Friend, src *open_im_sdk.FriendInfo) {
utils.CopyStructFields(dst, src)
dst.FriendUserID = src.FriendUser.UserID
dst.CreateTime = utils.UnixSecondToTime(int64(src.CreateTime))
}
func FriendDBCopyOpenIM(dst *open_im_sdk.FriendInfo, src *db.Friend) {
func FriendDBCopyOpenIM(dst *open_im_sdk.FriendInfo, src *db.Friend) error {
utils.CopyStructFields(dst, src)
user, _ := imdb.GetUserByUserID(src.FriendUserID)
if user != nil {
utils.CopyStructFields(dst.FriendUser, user)
user, err := imdb.GetUserByUserID(src.FriendUserID)
if err != nil {
return utils.Wrap(err, "")
}
utils.CopyStructFields(dst.FriendUser, user)
dst.CreateTime = uint32(src.CreateTime.Unix())
dst.FriendUser.CreateTime = uint32(user.CreateTime.Unix())
return nil
}
//
func FriendRequestOpenIMCopyDB(dst *db.FriendRequest, src *open_im_sdk.FriendRequest) {
utils.CopyStructFields(dst, src)
dst.CreateTime = utils.UnixSecondToTime(int64(src.CreateTime))
dst.HandleTime = utils.UnixSecondToTime(int64(src.HandleTime))
}
func FriendRequestDBCopyOpenIM(dst *open_im_sdk.FriendRequest, src *db.FriendRequest) {
func FriendRequestDBCopyOpenIM(dst *open_im_sdk.FriendRequest, src *db.FriendRequest) error {
utils.CopyStructFields(dst, src)
user, err := imdb.GetUserByUserID(src.FromUserID)
if err != nil {
return utils.Wrap(err, "")
}
dst.FromNickname = user.Nickname
dst.FromFaceURL = user.FaceURL
dst.FromGender = user.Gender
user, err = imdb.GetUserByUserID(src.ToUserID)
if err != nil {
return utils.Wrap(err, "")
}
dst.ToNickname = user.Nickname
dst.ToFaceURL = user.FaceURL
dst.ToGender = user.Gender
dst.CreateTime = uint32(src.CreateTime.Unix())
dst.HandleTime = uint32(src.HandleTime.Unix())
return nil
}
func BlackOpenIMCopyDB(dst *db.Black, src *open_im_sdk.BlackInfo) {
utils.CopyStructFields(dst, src)
dst.BlockUserID = src.BlackUserInfo.UserID
dst.CreateTime = utils.UnixSecondToTime(int64(src.CreateTime))
}
func BlackDBCopyOpenIM(dst *open_im_sdk.BlackInfo, src *db.Black) error {
utils.CopyStructFields(dst, src)
dst.CreateTime = uint32(src.CreateTime.Unix())
user, err := imdb.GetUserByUserID(src.BlockUserID)
if err != nil {
return utils.Wrap(err, "")
}
utils.CopyStructFields(dst.BlackUserInfo, user)
return nil
}
func GroupOpenIMCopyDB(dst *db.Group, src *open_im_sdk.GroupInfo) {
@ -93,28 +131,17 @@ func GroupRequestDBCopyOpenIM(dst *open_im_sdk.GroupRequest, src *db.GroupReques
func UserOpenIMCopyDB(dst *db.User, src *open_im_sdk.UserInfo) {
utils.CopyStructFields(dst, src)
dst.Birth = utils.UnixSecondToTime(int64(src.Birth))
dst.CreateTime = utils.UnixSecondToTime(int64(src.CreateTime))
}
func UserDBCopyOpenIM(dst *open_im_sdk.UserInfo, src *db.User) {
utils.CopyStructFields(dst, src)
dst.CreateTime = uint32(src.CreateTime.Unix())
dst.Birth = uint32(src.Birth.Unix())
}
func BlackOpenIMCopyDB(dst *db.Black, src *open_im_sdk.BlackInfo) {
utils.CopyStructFields(dst, src)
dst.BlockUserID = src.BlackUserInfo.UserID
}
func BlackDBCopyOpenIM(dst *open_im_sdk.BlackInfo, src *db.Black) {
utils.CopyStructFields(dst, src)
dst.CreateTime = uint32(src.CreateTime.Unix())
user, _ := imdb.GetUserByUserID(src.BlockUserID)
if user != nil {
utils.CopyStructFields(dst.BlackUserInfo, user)
}
}
func UserDBCopyOpenIMPublic(dst *open_im_sdk.PublicUserInfo, src *db.User) {
func UserDBCopyOpenIMPublicUser(dst *open_im_sdk.PublicUserInfo, src *db.User) {
utils.CopyStructFields(dst, src)
}

@ -5,6 +5,7 @@ import (
"github.com/pkg/errors"
"runtime"
"strconv"
"strings"
)
// copy a by b b->a
@ -24,3 +25,15 @@ func printCallerNameAndLine() string {
pc, _, line, _ := runtime.Caller(2)
return runtime.FuncForPC(pc).Name() + "()@" + strconv.Itoa(line) + ": "
}
func GetSelfFuncName() string {
pc, _, _, _ := runtime.Caller(1)
return cleanUpFuncName(runtime.FuncForPC(pc).Name())
}
func cleanUpFuncName(funcName string) string {
end := strings.LastIndex(funcName, ".")
if end == -1 {
return ""
}
return funcName[end+1:]
}

Loading…
Cancel
Save