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/pkg/common/utils/utils.go

161 lines
4.7 KiB

3 years ago
package utils
import (
imdb "Open_IM/pkg/common/db/mysql_model/im_mysql_model"
"Open_IM/pkg/common/token_verify"
open_im_sdk "Open_IM/pkg/proto/sdk_ws"
"Open_IM/pkg/utils"
"math/rand"
"strconv"
"time"
)
func OperationIDGenerator() string {
return strconv.FormatInt(time.Now().UnixNano()+int64(rand.Uint32()), 10)
}
func FriendOpenIMCopyDB(dst *imdb.Friend, src *open_im_sdk.FriendInfo) {
3 years ago
utils.CopyStructFields(dst, src)
dst.FriendUserID = src.FriendUser.UserID
3 years ago
dst.CreateTime = utils.UnixSecondToTime(int64(src.CreateTime))
3 years ago
}
func FriendDBCopyOpenIM(dst *open_im_sdk.FriendInfo, src *imdb.Friend) error {
3 years ago
utils.CopyStructFields(dst, src)
3 years ago
user, err := imdb.GetUserByUserID(src.FriendUserID)
if err != nil {
return utils.Wrap(err, "")
3 years ago
}
3 years ago
utils.CopyStructFields(dst.FriendUser, user)
3 years ago
dst.CreateTime = uint32(src.CreateTime.Unix())
if dst.FriendUser == nil {
dst.FriendUser = &open_im_sdk.UserInfo{}
}
3 years ago
dst.FriendUser.CreateTime = uint32(user.CreateTime.Unix())
3 years ago
return nil
3 years ago
}
func FriendRequestOpenIMCopyDB(dst *imdb.FriendRequest, src *open_im_sdk.FriendRequest) {
3 years ago
utils.CopyStructFields(dst, src)
3 years ago
dst.CreateTime = utils.UnixSecondToTime(int64(src.CreateTime))
dst.HandleTime = utils.UnixSecondToTime(int64(src.HandleTime))
3 years ago
}
func FriendRequestDBCopyOpenIM(dst *open_im_sdk.FriendRequest, src *imdb.FriendRequest) error {
3 years ago
utils.CopyStructFields(dst, src)
3 years ago
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
3 years ago
dst.CreateTime = uint32(src.CreateTime.Unix())
dst.HandleTime = uint32(src.HandleTime.Unix())
3 years ago
return nil
}
func BlackOpenIMCopyDB(dst *imdb.Black, src *open_im_sdk.BlackInfo) {
3 years ago
utils.CopyStructFields(dst, src)
dst.BlockUserID = src.BlackUserInfo.UserID
dst.CreateTime = utils.UnixSecondToTime(int64(src.CreateTime))
}
func BlackDBCopyOpenIM(dst *open_im_sdk.BlackInfo, src *imdb.Black) error {
3 years ago
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
3 years ago
}
func GroupOpenIMCopyDB(dst *imdb.Group, src *open_im_sdk.GroupInfo) {
3 years ago
utils.CopyStructFields(dst, src)
}
func GroupDBCopyOpenIM(dst *open_im_sdk.GroupInfo, src *imdb.Group) error {
3 years ago
utils.CopyStructFields(dst, src)
3 years ago
user, err := imdb.GetGroupOwnerInfoByGroupID(src.GroupID)
if err != nil {
return utils.Wrap(err, "")
}
dst.OwnerUserID = user.UserID
2 years ago
memberCount, err := imdb.GetGroupMemberNumByGroupID(src.GroupID)
dst.MemberCount = uint32(memberCount)
3 years ago
if err != nil {
return utils.Wrap(err, "")
3 years ago
}
3 years ago
dst.CreateTime = uint32(src.CreateTime.Unix())
dst.NotificationUpdateTime = uint32(src.NotificationUpdateTime.Unix())
if src.NotificationUpdateTime.Unix() < 0 {
dst.NotificationUpdateTime = 0
}
3 years ago
return nil
3 years ago
}
func GroupMemberOpenIMCopyDB(dst *imdb.GroupMember, src *open_im_sdk.GroupMemberFullInfo) {
3 years ago
utils.CopyStructFields(dst, src)
}
func GroupMemberDBCopyOpenIM(dst *open_im_sdk.GroupMemberFullInfo, src *imdb.GroupMember) error {
3 years ago
utils.CopyStructFields(dst, src)
3 years ago
if token_verify.IsManagerUserID(src.UserID) {
3 years ago
u, err := imdb.GetUserByUserID(src.UserID)
if err != nil {
return utils.Wrap(err, "")
3 years ago
}
3 years ago
utils.CopyStructFields(dst, u)
3 years ago
dst.AppMangerLevel = 1
}
3 years ago
dst.JoinTime = int32(src.JoinTime.Unix())
if src.JoinTime.Unix() < 0 {
3 years ago
dst.JoinTime = 0
return nil
}
3 years ago
dst.MuteEndTime = uint32(src.MuteEndTime.Unix())
3 years ago
if dst.MuteEndTime < uint32(time.Now().Unix()) {
dst.MuteEndTime = 0
}
3 years ago
return nil
3 years ago
}
func GroupRequestOpenIMCopyDB(dst *imdb.GroupRequest, src *open_im_sdk.GroupRequest) {
3 years ago
utils.CopyStructFields(dst, src)
}
func GroupRequestDBCopyOpenIM(dst *open_im_sdk.GroupRequest, src *imdb.GroupRequest) {
3 years ago
utils.CopyStructFields(dst, src)
3 years ago
dst.ReqTime = uint32(src.ReqTime.Unix())
dst.HandleTime = uint32(src.HandledTime.Unix())
3 years ago
}
func UserOpenIMCopyDB(dst *imdb.User, src *open_im_sdk.UserInfo) {
3 years ago
utils.CopyStructFields(dst, src)
dst.Birth, _ = utils.TimeStringToTime(src.BirthStr)
3 years ago
dst.CreateTime = utils.UnixSecondToTime(int64(src.CreateTime))
3 years ago
}
func UserDBCopyOpenIM(dst *open_im_sdk.UserInfo, src *imdb.User) {
3 years ago
utils.CopyStructFields(dst, src)
3 years ago
dst.CreateTime = uint32(src.CreateTime.Unix())
//dst.Birth = uint32(src.Birth.Unix())
dst.BirthStr = utils.TimeToString(src.Birth)
3 years ago
}
func UserDBCopyOpenIMPublicUser(dst *open_im_sdk.PublicUserInfo, src *imdb.User) {
3 years ago
utils.CopyStructFields(dst, src)
}