复制项目

This commit is contained in:
kim.dev.6789
2026-01-14 22:16:44 +08:00
parent e2577b8cee
commit e50142a3b9
691 changed files with 97009 additions and 1 deletions

View File

@@ -0,0 +1,25 @@
package convert
func TokenMapDB2Pb(tokenMapDB map[string]int) map[string]int32 {
if tokenMapDB == nil {
return nil
}
tokenMapPB := make(map[string]int32, len(tokenMapDB))
for k, v := range tokenMapDB {
tokenMapPB[k] = int32(v)
}
return tokenMapPB
}
func TokenMapPb2DB(tokenMapPB map[string]int32) map[string]int {
if tokenMapPB == nil {
return nil
}
tokenMapDB := make(map[string]int, len(tokenMapPB))
for k, v := range tokenMapPB {
tokenMapDB[k] = int(v)
}
return tokenMapDB
}

View File

@@ -0,0 +1,56 @@
// Copyright © 2023 OpenIM. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package convert
import (
"context"
"git.imall.cloud/openim/open-im-server-deploy/pkg/common/storage/model"
"git.imall.cloud/openim/protocol/sdkws"
sdk "git.imall.cloud/openim/protocol/sdkws"
)
func BlackDB2Pb(ctx context.Context, blackDBs []*model.Black, f func(ctx context.Context, userIDs []string) (map[string]*sdkws.UserInfo, error)) (blackPbs []*sdk.BlackInfo, err error) {
if len(blackDBs) == 0 {
return nil, nil
}
var userIDs []string
for _, blackDB := range blackDBs {
userIDs = append(userIDs, blackDB.BlockUserID)
}
userInfos, err := f(ctx, userIDs)
if err != nil {
return nil, err
}
for _, blackDB := range blackDBs {
blackPb := &sdk.BlackInfo{
OwnerUserID: blackDB.OwnerUserID,
CreateTime: blackDB.CreateTime.Unix(),
AddSource: blackDB.AddSource,
Ex: blackDB.Ex,
OperatorUserID: blackDB.OperatorUserID,
BlackUserInfo: &sdkws.PublicUserInfo{
UserID: userInfos[blackDB.BlockUserID].UserID,
Nickname: userInfos[blackDB.BlockUserID].Nickname,
FaceURL: userInfos[blackDB.BlockUserID].FaceURL,
Ex: userInfos[blackDB.BlockUserID].Ex,
UserType: userInfos[blackDB.BlockUserID].UserType,
},
}
blackPbs = append(blackPbs, blackPb)
}
return blackPbs, nil
}

View File

@@ -0,0 +1,61 @@
// Copyright © 2023 OpenIM. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package convert
import (
"git.imall.cloud/openim/open-im-server-deploy/pkg/common/storage/model"
"git.imall.cloud/openim/protocol/conversation"
"github.com/openimsdk/tools/utils/datautil"
)
func ConversationDB2Pb(conversationDB *model.Conversation) *conversation.Conversation {
conversationPB := &conversation.Conversation{}
conversationPB.LatestMsgDestructTime = conversationDB.LatestMsgDestructTime.UnixMilli()
if err := datautil.CopyStructFields(conversationPB, conversationDB); err != nil {
return nil
}
return conversationPB
}
func ConversationsDB2Pb(conversationsDB []*model.Conversation) (conversationsPB []*conversation.Conversation) {
for _, conversationDB := range conversationsDB {
conversationPB := &conversation.Conversation{}
if err := datautil.CopyStructFields(conversationPB, conversationDB); err != nil {
continue
}
conversationPB.LatestMsgDestructTime = conversationDB.LatestMsgDestructTime.UnixMilli()
conversationsPB = append(conversationsPB, conversationPB)
}
return conversationsPB
}
func ConversationPb2DB(conversationPB *conversation.Conversation) *model.Conversation {
conversationDB := &model.Conversation{}
if err := datautil.CopyStructFields(conversationDB, conversationPB); err != nil {
return nil
}
return conversationDB
}
func ConversationsPb2DB(conversationsPB []*conversation.Conversation) (conversationsDB []*model.Conversation) {
for _, conversationPB := range conversationsPB {
conversationDB := &model.Conversation{}
if err := datautil.CopyStructFields(conversationDB, conversationPB); err != nil {
continue
}
conversationsDB = append(conversationsDB, conversationDB)
}
return conversationsDB
}

15
pkg/common/convert/doc.go Normal file
View File

@@ -0,0 +1,15 @@
// Copyright © 2024 OpenIM. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package convert // import "git.imall.cloud/openim/open-im-server-deploy/pkg/common/convert"

View File

@@ -0,0 +1,171 @@
// Copyright © 2023 OpenIM. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package convert
import (
"context"
"fmt"
"git.imall.cloud/openim/open-im-server-deploy/pkg/common/storage/model"
"git.imall.cloud/openim/open-im-server-deploy/pkg/notification/common_user"
"git.imall.cloud/openim/protocol/relation"
"git.imall.cloud/openim/protocol/sdkws"
"github.com/openimsdk/tools/utils/datautil"
"github.com/openimsdk/tools/utils/timeutil"
)
func FriendPb2DB(friend *sdkws.FriendInfo) *model.Friend {
dbFriend := &model.Friend{}
err := datautil.CopyStructFields(dbFriend, friend)
if err != nil {
return nil
}
dbFriend.FriendUserID = friend.FriendUser.UserID
dbFriend.CreateTime = timeutil.UnixSecondToTime(friend.CreateTime)
return dbFriend
}
func FriendDB2Pb(ctx context.Context, friendDB *model.Friend, getUsers func(ctx context.Context, userIDs []string) (map[string]*sdkws.UserInfo, error)) (*sdkws.FriendInfo, error) {
users, err := getUsers(ctx, []string{friendDB.FriendUserID})
if err != nil {
return nil, err
}
user, ok := users[friendDB.FriendUserID]
if !ok {
return nil, fmt.Errorf("user not found: %s", friendDB.FriendUserID)
}
return &sdkws.FriendInfo{
FriendUser: user,
CreateTime: friendDB.CreateTime.Unix(),
}, nil
}
func FriendsDB2Pb(ctx context.Context, friendsDB []*model.Friend, getUsers func(ctx context.Context, userIDs []string) (map[string]*sdkws.UserInfo, error)) (friendsPb []*sdkws.FriendInfo, err error) {
if len(friendsDB) == 0 {
return nil, nil
}
var userID []string
for _, friendDB := range friendsDB {
userID = append(userID, friendDB.FriendUserID)
}
users, err := getUsers(ctx, userID)
if err != nil {
return nil, err
}
for _, friend := range friendsDB {
friendPb := &sdkws.FriendInfo{FriendUser: &sdkws.UserInfo{}}
err := datautil.CopyStructFields(friendPb, friend)
if err != nil {
return nil, err
}
friendPb.FriendUser.UserID = users[friend.FriendUserID].UserID
friendPb.FriendUser.Nickname = users[friend.FriendUserID].Nickname
friendPb.FriendUser.FaceURL = users[friend.FriendUserID].FaceURL
friendPb.FriendUser.Ex = users[friend.FriendUserID].Ex
friendPb.CreateTime = friend.CreateTime.Unix()
friendPb.IsPinned = friend.IsPinned
friendsPb = append(friendsPb, friendPb)
}
return friendsPb, nil
}
func FriendOnlyDB2PbOnly(friendsDB []*model.Friend) []*relation.FriendInfoOnly {
return datautil.Slice(friendsDB, func(f *model.Friend) *relation.FriendInfoOnly {
return &relation.FriendInfoOnly{
OwnerUserID: f.OwnerUserID,
FriendUserID: f.FriendUserID,
Remark: f.Remark,
CreateTime: f.CreateTime.UnixMilli(),
AddSource: f.AddSource,
OperatorUserID: f.OperatorUserID,
Ex: f.Ex,
IsPinned: f.IsPinned,
}
})
}
func FriendRequestDB2Pb(ctx context.Context, friendRequests []*model.FriendRequest, getUsers func(ctx context.Context, userIDs []string) (map[string]common_user.CommonUser, error)) ([]*sdkws.FriendRequest, error) {
if len(friendRequests) == 0 {
return nil, nil
}
userIDMap := make(map[string]struct{})
for _, friendRequest := range friendRequests {
userIDMap[friendRequest.ToUserID] = struct{}{}
userIDMap[friendRequest.FromUserID] = struct{}{}
}
users, err := getUsers(ctx, datautil.Keys(userIDMap))
if err != nil {
return nil, err
}
res := make([]*sdkws.FriendRequest, 0, len(friendRequests))
for _, friendRequest := range friendRequests {
toUser := users[friendRequest.ToUserID]
fromUser := users[friendRequest.FromUserID]
res = append(res, &sdkws.FriendRequest{
FromUserID: friendRequest.FromUserID,
FromNickname: fromUser.GetNickname(),
FromFaceURL: fromUser.GetFaceURL(),
ToUserID: friendRequest.ToUserID,
ToNickname: toUser.GetNickname(),
ToFaceURL: toUser.GetFaceURL(),
HandleResult: friendRequest.HandleResult,
ReqMsg: friendRequest.ReqMsg,
CreateTime: friendRequest.CreateTime.UnixMilli(),
HandlerUserID: friendRequest.HandlerUserID,
HandleMsg: friendRequest.HandleMsg,
HandleTime: friendRequest.HandleTime.UnixMilli(),
Ex: friendRequest.Ex,
})
}
return res, nil
}
// FriendPb2DBMap converts a FriendInfo protobuf object to a map suitable for database operations.
// It only includes non-zero or non-empty fields in the map.
func FriendPb2DBMap(friend *sdkws.FriendInfo) map[string]any {
if friend == nil {
return nil
}
val := make(map[string]any)
// Assuming FriendInfo has similar fields to those in Friend.
// Add or remove fields based on your actual FriendInfo and Friend structures.
if friend.FriendUser != nil {
if friend.FriendUser.UserID != "" {
val["friend_user_id"] = friend.FriendUser.UserID
}
if friend.FriendUser.Nickname != "" {
val["nickname"] = friend.FriendUser.Nickname
}
if friend.FriendUser.FaceURL != "" {
val["face_url"] = friend.FriendUser.FaceURL
}
if friend.FriendUser.Ex != "" {
val["ex"] = friend.FriendUser.Ex
}
}
if friend.CreateTime != 0 {
val["create_time"] = friend.CreateTime // You might need to convert this to a proper time format.
}
// Include other fields from FriendInfo as needed, similar to the above pattern.
return val
}

174
pkg/common/convert/group.go Normal file
View File

@@ -0,0 +1,174 @@
// Copyright © 2023 OpenIM. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package convert
import (
"context"
"time"
"git.imall.cloud/openim/open-im-server-deploy/pkg/common/storage/model"
pbgroup "git.imall.cloud/openim/protocol/group"
sdkws "git.imall.cloud/openim/protocol/sdkws"
"github.com/openimsdk/tools/log"
)
func Db2PbGroupInfo(m *model.Group, ownerUserID string, memberCount uint32) *sdkws.GroupInfo {
return &sdkws.GroupInfo{
GroupID: m.GroupID,
GroupName: m.GroupName,
Notification: m.Notification,
Introduction: m.Introduction,
FaceURL: m.FaceURL,
OwnerUserID: ownerUserID,
CreateTime: m.CreateTime.UnixMilli(),
MemberCount: memberCount,
Ex: m.Ex,
Status: m.Status,
CreatorUserID: m.CreatorUserID,
GroupType: m.GroupType,
NeedVerification: m.NeedVerification,
LookMemberInfo: m.LookMemberInfo,
ApplyMemberFriend: m.ApplyMemberFriend,
NotificationUpdateTime: m.NotificationUpdateTime.UnixMilli(),
NotificationUserID: m.NotificationUserID,
}
}
func Pb2DbGroupRequest(req *pbgroup.GroupApplicationResponseReq, handleUserID string) *model.GroupRequest {
return &model.GroupRequest{
UserID: req.FromUserID,
GroupID: req.GroupID,
HandleResult: req.HandleResult,
HandledMsg: req.HandledMsg,
HandleUserID: handleUserID,
HandledTime: time.Now(),
}
}
func Db2PbCMSGroup(m *model.Group, ownerUserID string, ownerUserName string, memberCount uint32) *pbgroup.CMSGroup {
return &pbgroup.CMSGroup{
GroupInfo: Db2PbGroupInfo(m, ownerUserID, memberCount),
GroupOwnerUserID: ownerUserID,
GroupOwnerUserName: ownerUserName,
}
}
// Db2PbGroupMember 将数据库群成员模型转换为 protobuf 群成员信息
// 返回的 GroupMemberFullInfo 包含以下禁言相关字段:
// - MuteEndTime: 禁言结束时间(毫秒时间戳)
// 判断是否被禁言MuteEndTime >= 当前时间戳(毫秒)表示正在被禁言
// 判断剩余禁言时间max(0, MuteEndTime - 当前时间戳) / 1000 得到剩余秒数
func Db2PbGroupMember(m *model.GroupMember) *sdkws.GroupMemberFullInfo {
muteEndTime := int64(0)
if !m.MuteEndTime.IsZero() && m.MuteEndTime.After(time.Unix(0, 0)) {
muteEndTime = m.MuteEndTime.UnixMilli()
// 记录从数据库读取的禁言时间,用于排查自动禁言问题
now := time.Now()
if muteEndTime >= now.UnixMilli() {
// 只有在用户被禁言时才记录日志,避免日志过多
log.ZInfo(context.Background(), "Db2PbGroupMember: found muted user in database",
"groupID", m.GroupID,
"userID", m.UserID,
"muteEndTimeFromDB", m.MuteEndTime.Format(time.RFC3339),
"muteEndTimeTimestamp", muteEndTime,
"now", now.Format(time.RFC3339),
"mutedDurationSeconds", (muteEndTime-now.UnixMilli())/1000,
"isZero", m.MuteEndTime.IsZero(),
"afterUnixZero", m.MuteEndTime.After(time.Unix(0, 0)))
}
}
return &sdkws.GroupMemberFullInfo{
GroupID: m.GroupID,
UserID: m.UserID,
RoleLevel: m.RoleLevel,
JoinTime: m.JoinTime.UnixMilli(),
Nickname: m.Nickname,
FaceURL: m.FaceURL,
// AppMangerLevel: m.AppMangerLevel,
JoinSource: m.JoinSource,
OperatorUserID: m.OperatorUserID,
Ex: m.Ex,
MuteEndTime: muteEndTime,
InviterUserID: m.InviterUserID,
}
}
func Db2PbGroupRequest(m *model.GroupRequest, user *sdkws.UserInfo, group *sdkws.GroupInfo) *sdkws.GroupRequest {
var pu *sdkws.PublicUserInfo
if user != nil {
pu = &sdkws.PublicUserInfo{
UserID: user.UserID,
Nickname: user.Nickname,
FaceURL: user.FaceURL,
Ex: user.Ex,
UserType: user.UserType,
}
}
return &sdkws.GroupRequest{
UserInfo: pu,
GroupInfo: group,
HandleResult: m.HandleResult,
ReqMsg: m.ReqMsg,
HandleMsg: m.HandledMsg,
ReqTime: m.ReqTime.UnixMilli(),
HandleUserID: m.HandleUserID,
HandleTime: m.HandledTime.UnixMilli(),
Ex: m.Ex,
JoinSource: m.JoinSource,
InviterUserID: m.InviterUserID,
}
}
func Db2PbGroupAbstractInfo(
groupID string,
groupMemberNumber uint32,
groupMemberListHash uint64,
) *pbgroup.GroupAbstractInfo {
return &pbgroup.GroupAbstractInfo{
GroupID: groupID,
GroupMemberNumber: groupMemberNumber,
GroupMemberListHash: groupMemberListHash,
}
}
func Pb2DBGroupInfo(m *sdkws.GroupInfo) *model.Group {
return &model.Group{
GroupID: m.GroupID,
GroupName: m.GroupName,
Notification: m.Notification,
Introduction: m.Introduction,
FaceURL: m.FaceURL,
CreateTime: time.Now(),
Ex: m.Ex,
Status: m.Status,
CreatorUserID: m.CreatorUserID,
GroupType: m.GroupType,
NeedVerification: m.NeedVerification,
LookMemberInfo: m.LookMemberInfo,
ApplyMemberFriend: m.ApplyMemberFriend,
NotificationUpdateTime: time.UnixMilli(m.NotificationUpdateTime),
NotificationUserID: m.NotificationUserID,
}
}
// func Pb2DbGroupMember(m *sdkws.UserInfo) *relation.GroupMember {
// return &relation.GroupMember{
// UserID: m.UserID,
// Nickname: m.Nickname,
// FaceURL: m.FaceURL,
// Ex: m.Ex,
// }
//}

98
pkg/common/convert/msg.go Normal file
View File

@@ -0,0 +1,98 @@
// Copyright © 2023 OpenIM. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package convert
import (
"git.imall.cloud/openim/open-im-server-deploy/pkg/common/storage/model"
"git.imall.cloud/openim/protocol/constant"
"git.imall.cloud/openim/protocol/sdkws"
)
func MsgPb2DB(msg *sdkws.MsgData) *model.MsgDataModel {
if msg == nil {
return nil
}
var msgDataModel model.MsgDataModel
msgDataModel.SendID = msg.SendID
msgDataModel.RecvID = msg.RecvID
msgDataModel.GroupID = msg.GroupID
msgDataModel.ClientMsgID = msg.ClientMsgID
msgDataModel.ServerMsgID = msg.ServerMsgID
msgDataModel.SenderPlatformID = msg.SenderPlatformID
msgDataModel.SenderNickname = msg.SenderNickname
msgDataModel.SenderFaceURL = msg.SenderFaceURL
msgDataModel.SessionType = msg.SessionType
msgDataModel.MsgFrom = msg.MsgFrom
msgDataModel.ContentType = msg.ContentType
msgDataModel.Content = string(msg.Content)
msgDataModel.Seq = msg.Seq
msgDataModel.SendTime = msg.SendTime
msgDataModel.CreateTime = msg.CreateTime
msgDataModel.Status = msg.Status
msgDataModel.Options = msg.Options
if msg.OfflinePushInfo != nil {
msgDataModel.OfflinePush = &model.OfflinePushModel{
Title: msg.OfflinePushInfo.Title,
Desc: msg.OfflinePushInfo.Desc,
Ex: msg.OfflinePushInfo.Ex,
IOSPushSound: msg.OfflinePushInfo.IOSPushSound,
IOSBadgeCount: msg.OfflinePushInfo.IOSBadgeCount,
}
}
msgDataModel.AtUserIDList = msg.AtUserIDList
msgDataModel.AttachedInfo = msg.AttachedInfo
msgDataModel.Ex = msg.Ex
return &msgDataModel
}
func MsgDB2Pb(msgModel *model.MsgDataModel) *sdkws.MsgData {
if msgModel == nil {
return nil
}
var msg sdkws.MsgData
msg.SendID = msgModel.SendID
msg.RecvID = msgModel.RecvID
msg.GroupID = msgModel.GroupID
msg.ClientMsgID = msgModel.ClientMsgID
msg.ServerMsgID = msgModel.ServerMsgID
msg.SenderPlatformID = msgModel.SenderPlatformID
msg.SenderNickname = msgModel.SenderNickname
msg.SenderFaceURL = msgModel.SenderFaceURL
msg.SessionType = msgModel.SessionType
msg.MsgFrom = msgModel.MsgFrom
msg.ContentType = msgModel.ContentType
msg.Content = []byte(msgModel.Content)
msg.Seq = msgModel.Seq
msg.SendTime = msgModel.SendTime
msg.CreateTime = msgModel.CreateTime
msg.Status = msgModel.Status
if msgModel.SessionType == constant.SingleChatType {
msg.IsRead = msgModel.IsRead
}
msg.Options = msgModel.Options
if msgModel.OfflinePush != nil {
msg.OfflinePushInfo = &sdkws.OfflinePushInfo{
Title: msgModel.OfflinePush.Title,
Desc: msgModel.OfflinePush.Desc,
Ex: msgModel.OfflinePush.Ex,
IOSPushSound: msgModel.OfflinePush.IOSPushSound,
IOSBadgeCount: msgModel.OfflinePush.IOSBadgeCount,
}
}
msg.AtUserIDList = msgModel.AtUserIDList
msg.AttachedInfo = msgModel.AttachedInfo
msg.Ex = msgModel.Ex
return &msg
}

109
pkg/common/convert/user.go Normal file
View File

@@ -0,0 +1,109 @@
// Copyright © 2023 OpenIM. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package convert
import (
"time"
relationtb "git.imall.cloud/openim/open-im-server-deploy/pkg/common/storage/model"
"github.com/openimsdk/tools/utils/datautil"
"git.imall.cloud/openim/protocol/sdkws"
)
func UserDB2Pb(user *relationtb.User) *sdkws.UserInfo {
return &sdkws.UserInfo{
UserID: user.UserID,
Nickname: user.Nickname,
FaceURL: user.FaceURL,
Ex: user.Ex,
CreateTime: user.CreateTime.UnixMilli(),
AppMangerLevel: user.AppMangerLevel,
GlobalRecvMsgOpt: user.GlobalRecvMsgOpt,
UserType: user.UserType,
UserFlag: user.UserFlag,
}
}
func UsersDB2Pb(users []*relationtb.User) []*sdkws.UserInfo {
return datautil.Slice(users, UserDB2Pb)
}
func UserPb2DB(user *sdkws.UserInfo) *relationtb.User {
return &relationtb.User{
UserID: user.UserID,
Nickname: user.Nickname,
FaceURL: user.FaceURL,
Ex: user.Ex,
CreateTime: time.UnixMilli(user.CreateTime),
AppMangerLevel: user.AppMangerLevel,
GlobalRecvMsgOpt: user.GlobalRecvMsgOpt,
UserType: user.UserType,
UserFlag: user.UserFlag,
}
}
func UserPb2DBMap(user *sdkws.UserInfo) map[string]any {
if user == nil {
return nil
}
val := make(map[string]any)
fields := map[string]any{
"nickname": user.Nickname,
"face_url": user.FaceURL,
"ex": user.Ex,
"app_manager_level": user.AppMangerLevel,
"global_recv_msg_opt": user.GlobalRecvMsgOpt,
"user_type": user.UserType,
"user_flag": user.UserFlag,
}
for key, value := range fields {
if v, ok := value.(string); ok && v != "" {
val[key] = v
} else if v, ok := value.(int32); ok {
// 对于 int32 类型,包括 0 值也要更新
val[key] = v
}
}
return val
}
func UserPb2DBMapEx(user *sdkws.UserInfoWithEx) map[string]any {
if user == nil {
return nil
}
val := make(map[string]any)
// Map fields from UserInfoWithEx to val
if user.Nickname != nil {
val["nickname"] = user.Nickname.Value
}
if user.FaceURL != nil {
val["face_url"] = user.FaceURL.Value
}
if user.Ex != nil {
val["ex"] = user.Ex.Value
}
if user.GlobalRecvMsgOpt != nil {
val["global_recv_msg_opt"] = user.GlobalRecvMsgOpt.Value
}
if user.UserType != nil {
val["user_type"] = user.UserType.Value
}
if user.UserFlag != nil {
val["user_flag"] = user.UserFlag.Value
}
return val
}

View File

@@ -0,0 +1,87 @@
// Copyright © 2023 OpenIM. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package convert
import (
"reflect"
"testing"
relationtb "git.imall.cloud/openim/open-im-server-deploy/pkg/common/storage/model"
"git.imall.cloud/openim/protocol/sdkws"
)
func TestUsersDB2Pb(t *testing.T) {
type args struct {
users []*relationtb.User
}
tests := []struct {
name string
args args
wantResult []*sdkws.UserInfo
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotResult := UsersDB2Pb(tt.args.users); !reflect.DeepEqual(gotResult, tt.wantResult) {
t.Errorf("UsersDB2Pb() = %v, want %v", gotResult, tt.wantResult)
}
})
}
}
func TestUserPb2DB(t *testing.T) {
type args struct {
user *sdkws.UserInfo
}
tests := []struct {
name string
args args
want *relationtb.User
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := UserPb2DB(tt.args.user); !reflect.DeepEqual(got, tt.want) {
t.Errorf("UserPb2DB() = %v, want %v", got, tt.want)
}
})
}
}
func TestUserPb2DBMap(t *testing.T) {
user := &sdkws.UserInfo{
Nickname: "TestUser",
FaceURL: "http://openim.io/logo.jpg",
Ex: "Extra Data",
AppMangerLevel: 1,
GlobalRecvMsgOpt: 2,
}
expected := map[string]any{
"nickname": "TestUser",
"face_url": "http://openim.io/logo.jpg",
"ex": "Extra Data",
"app_manager_level": int32(1),
"global_recv_msg_opt": int32(2),
}
result := UserPb2DBMap(user)
if !reflect.DeepEqual(result, expected) {
t.Errorf("UserPb2DBMap returned unexpected map. Got %v, want %v", result, expected)
}
}