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

447 lines
11 KiB

package convert
import (
2 years ago
"Open_IM/pkg/common/db/table/relation"
2 years ago
sdk "Open_IM/pkg/proto/sdkws"
utils "github.com/OpenIMSDK/open_utils"
"time"
)
func getUsersInfo(userIDs []string) ([]*sdk.UserInfo, error) {
return nil, nil
}
func getGroupOwnerInfo(groupID string) (*sdk.GroupMemberFullInfo, error) {
return nil, nil
}
func getNumberOfGroupMember(groupID string) (int32, error) {
return 0, nil
}
type DBFriend struct {
2 years ago
*relation.FriendModel
}
2 years ago
func NewDBFriend(friend *relation.FriendModel) *DBFriend {
return &DBFriend{FriendModel: friend}
}
type PBFriend struct {
*sdk.FriendInfo
}
func NewPBFriend(friendInfo *sdk.FriendInfo) *PBFriend {
return &PBFriend{FriendInfo: friendInfo}
}
2 years ago
func (*PBFriend) PB2DB(friends []*sdk.FriendInfo) (DBFriends []*relation.FriendModel, err error) {
for _, v := range friends {
u, err := NewPBFriend(v).Convert()
if err != nil {
return nil, err
}
DBFriends = append(DBFriends, u)
}
return
}
2 years ago
func (*DBFriend) DB2PB(friends []*relation.FriendModel) (PBFriends []*sdk.FriendInfo, err error) {
for _, v := range friends {
u, err := NewDBFriend(v).Convert()
if err != nil {
return nil, err
}
PBFriends = append(PBFriends, u)
}
return
}
func (db *DBFriend) Convert() (*sdk.FriendInfo, error) {
pbFriend := &sdk.FriendInfo{FriendUser: &sdk.UserInfo{}}
utils.CopyStructFields(pbFriend, db)
user, err := getUsersInfo([]string{db.FriendUserID})
if err != nil {
return nil, err
}
utils.CopyStructFields(pbFriend.FriendUser, user[0])
pbFriend.CreateTime = db.CreateTime.Unix()
pbFriend.FriendUser.CreateTime = db.CreateTime.Unix()
return pbFriend, nil
}
2 years ago
func (pb *PBFriend) Convert() (*relation.FriendModel, error) {
dbFriend := &relation.FriendModel{}
utils.CopyStructFields(dbFriend, pb)
dbFriend.FriendUserID = pb.FriendUser.UserID
dbFriend.CreateTime = utils.UnixSecondToTime(pb.CreateTime)
return dbFriend, nil
}
type DBFriendRequest struct {
2 years ago
*relation.FriendRequestModel
}
2 years ago
func NewDBFriendRequest(friendRequest *relation.FriendRequestModel) *DBFriendRequest {
return &DBFriendRequest{FriendRequestModel: friendRequest}
}
type PBFriendRequest struct {
*sdk.FriendRequest
}
func NewPBFriendRequest(friendRequest *sdk.FriendRequest) *PBFriendRequest {
return &PBFriendRequest{FriendRequest: friendRequest}
}
2 years ago
func (*PBFriendRequest) PB2DB(friendRequests []*sdk.FriendRequest) (DBFriendRequests []*relation.FriendRequestModel, err error) {
for _, v := range friendRequests {
u, err := NewPBFriendRequest(v).Convert()
if err != nil {
return nil, err
}
DBFriendRequests = append(DBFriendRequests, u)
}
return
}
2 years ago
func (*DBFriendRequest) DB2PB(friendRequests []*relation.FriendRequestModel) (PBFriendRequests []*sdk.FriendRequest, err error) {
for _, v := range friendRequests {
u, err := NewDBFriendRequest(v).Convert()
if err != nil {
return nil, err
}
PBFriendRequests = append(PBFriendRequests, u)
}
return
}
2 years ago
func (pb *PBFriendRequest) Convert() (*relation.FriendRequestModel, error) {
dbFriendRequest := &relation.FriendRequestModel{}
utils.CopyStructFields(dbFriendRequest, pb)
dbFriendRequest.CreateTime = utils.UnixSecondToTime(int64(pb.CreateTime))
dbFriendRequest.HandleTime = utils.UnixSecondToTime(int64(pb.HandleTime))
return dbFriendRequest, nil
}
func (db *DBFriendRequest) Convert() (*sdk.FriendRequest, error) {
pbFriendRequest := &sdk.FriendRequest{}
utils.CopyStructFields(pbFriendRequest, db)
user, err := getUsersInfo([]string{db.FromUserID})
if err != nil {
return nil, err
}
pbFriendRequest.FromNickname = user[0].Nickname
pbFriendRequest.FromFaceURL = user[0].FaceURL
pbFriendRequest.FromGender = user[0].Gender
user, err = getUsersInfo([]string{db.ToUserID})
if err != nil {
return nil, err
}
pbFriendRequest.ToNickname = user[0].Nickname
pbFriendRequest.ToFaceURL = user[0].FaceURL
pbFriendRequest.ToGender = user[0].Gender
pbFriendRequest.CreateTime = db.CreateTime.Unix()
pbFriendRequest.HandleTime = db.HandleTime.Unix()
return pbFriendRequest, nil
}
type DBBlack struct {
2 years ago
*relation.BlackModel
}
2 years ago
func (*PBBlack) PB2DB(blacks []*sdk.BlackInfo) (DBBlacks []*relation.BlackModel, err error) {
for _, v := range blacks {
u, err := NewPBBlack(v).Convert()
if err != nil {
return nil, err
}
DBBlacks = append(DBBlacks, u)
}
return
}
2 years ago
func (*DBBlack) DB2PB(blacks []*relation.BlackModel) (PBBlacks []*sdk.BlackInfo, err error) {
for _, v := range blacks {
u, err := NewDBBlack(v).Convert()
if err != nil {
return nil, err
}
PBBlacks = append(PBBlacks, u)
}
return
}
2 years ago
func NewDBBlack(black *relation.BlackModel) *DBBlack {
return &DBBlack{BlackModel: black}
}
type PBBlack struct {
*sdk.BlackInfo
}
func NewPBBlack(blackInfo *sdk.BlackInfo) *PBBlack {
return &PBBlack{BlackInfo: blackInfo}
}
2 years ago
func (pb *PBBlack) Convert() (*relation.BlackModel, error) {
dbBlack := &relation.BlackModel{}
dbBlack.BlockUserID = pb.BlackUserInfo.UserID
dbBlack.CreateTime = utils.UnixSecondToTime(int64(pb.CreateTime))
return dbBlack, nil
}
func (db *DBBlack) Convert() (*sdk.BlackInfo, error) {
pbBlack := &sdk.BlackInfo{}
utils.CopyStructFields(pbBlack, db)
pbBlack.CreateTime = db.CreateTime.Unix()
user, err := getUsersInfo([]string{db.BlockUserID})
if err != nil {
return nil, err
}
utils.CopyStructFields(pbBlack.BlackUserInfo, user)
return pbBlack, nil
}
type DBGroup struct {
2 years ago
*relation.GroupModel
}
2 years ago
func (*PBGroup) PB2DB(groups []*sdk.GroupInfo) (DBGroups []*relation.GroupModel, err error) {
for _, v := range groups {
u, err := NewPBGroup(v).Convert()
if err != nil {
return nil, err
}
DBGroups = append(DBGroups, u)
}
return
}
2 years ago
func (*DBGroup) DB2PB(groups []*relation.GroupModel) (PBGroups []*sdk.GroupInfo, err error) {
for _, v := range groups {
u, err := NewDBGroup(v).Convert()
if err != nil {
return nil, err
}
PBGroups = append(PBGroups, u)
}
return
}
2 years ago
func NewDBGroup(group *relation.GroupModel) *DBGroup {
return &DBGroup{GroupModel: group}
}
type PBGroup struct {
*sdk.GroupInfo
}
func NewPBGroup(groupInfo *sdk.GroupInfo) *PBGroup {
return &PBGroup{GroupInfo: groupInfo}
}
func (pb *PBGroup) Convert() (*relation.GroupModel, error) {
2 years ago
dst := &relation.GroupModel{}
err := utils.CopyStructFields(dst, pb)
return dst, err
}
func (db *DBGroup) Convert() (*sdk.GroupInfo, error) {
dst := &sdk.GroupInfo{}
utils.CopyStructFields(dst, db)
user, err := getGroupOwnerInfo(db.GroupID)
if err != nil {
return nil, err
}
dst.OwnerUserID = user.UserID
memberCount, err := getNumberOfGroupMember(db.GroupID)
if err != nil {
return nil, err
}
dst.MemberCount = uint32(memberCount)
dst.CreateTime = db.CreateTime.Unix()
dst.NotificationUpdateTime = db.NotificationUpdateTime.Unix()
if db.NotificationUpdateTime.Unix() < 0 {
dst.NotificationUpdateTime = 0
}
return dst, nil
}
type DBGroupMember struct {
2 years ago
*relation.GroupMemberModel
}
2 years ago
func (*PBGroupMember) PB2DB(groupMembers []*sdk.GroupMemberFullInfo) (DBGroupMembers []*relation.GroupMemberModel, err error) {
for _, v := range groupMembers {
u, err := NewPBGroupMember(v).Convert()
if err != nil {
return nil, err
}
DBGroupMembers = append(DBGroupMembers, u)
}
return
}
2 years ago
func (*DBGroupMember) DB2PB(groupMembers []*relation.GroupMemberModel) (PBGroupMembers []*sdk.GroupMemberFullInfo, err error) {
for _, v := range groupMembers {
u, err := NewDBGroupMember(v).Convert()
if err != nil {
return nil, err
}
PBGroupMembers = append(PBGroupMembers, u)
}
return
}
2 years ago
func NewDBGroupMember(groupMember *relation.GroupMemberModel) *DBGroupMember {
return &DBGroupMember{GroupMemberModel: groupMember}
}
type PBGroupMember struct {
*sdk.GroupMemberFullInfo
}
func NewPBGroupMember(groupMemberFullInfo *sdk.GroupMemberFullInfo) *PBGroupMember {
return &PBGroupMember{GroupMemberFullInfo: groupMemberFullInfo}
}
2 years ago
func (pb *PBGroupMember) Convert() (*relation.GroupMemberModel, error) {
dst := &relation.GroupMemberModel{}
utils.CopyStructFields(dst, pb)
dst.JoinTime = utils.UnixSecondToTime(int64(pb.JoinTime))
dst.MuteEndTime = utils.UnixSecondToTime(int64(pb.MuteEndTime))
return dst, nil
}
func (db *DBGroupMember) Convert() (*sdk.GroupMemberFullInfo, error) {
dst := &sdk.GroupMemberFullInfo{}
utils.CopyStructFields(dst, db)
user, err := getUsersInfo([]string{db.UserID})
if err != nil {
return nil, err
}
dst.AppMangerLevel = user[0].AppMangerLevel
dst.JoinTime = db.JoinTime.Unix()
if db.JoinTime.Unix() < 0 {
dst.JoinTime = 0
}
dst.MuteEndTime = db.MuteEndTime.Unix()
if dst.MuteEndTime < time.Now().Unix() {
dst.MuteEndTime = 0
}
return dst, nil
}
type DBGroupRequest struct {
2 years ago
*relation.GroupRequestModel
}
2 years ago
func (*PBGroupRequest) PB2DB(groupRequests []*sdk.GroupRequest) (DBGroupRequests []*relation.GroupRequestModel, err error) {
for _, v := range groupRequests {
u, err := NewPBGroupRequest(v).Convert()
if err != nil {
return nil, err
}
DBGroupRequests = append(DBGroupRequests, u)
}
return
}
2 years ago
func (*DBGroupRequest) DB2PB(groupRequests []*relation.GroupRequestModel) (PBGroupRequests []*sdk.GroupRequest, err error) {
for _, v := range groupRequests {
u, err := NewDBGroupRequest(v).Convert()
if err != nil {
return nil, err
}
PBGroupRequests = append(PBGroupRequests, u)
}
return
}
2 years ago
func NewDBGroupRequest(groupRequest *relation.GroupRequestModel) *DBGroupRequest {
return &DBGroupRequest{GroupRequestModel: groupRequest}
}
type PBGroupRequest struct {
*sdk.GroupRequest
}
func NewPBGroupRequest(groupRequest *sdk.GroupRequest) *PBGroupRequest {
return &PBGroupRequest{GroupRequest: groupRequest}
}
2 years ago
func (pb *PBGroupRequest) Convert() (*relation.GroupRequestModel, error) {
dst := &relation.GroupRequestModel{}
utils.CopyStructFields(dst, pb)
dst.ReqTime = utils.UnixSecondToTime(int64(pb.ReqTime))
dst.HandledTime = utils.UnixSecondToTime(int64(pb.HandleTime))
return dst, nil
}
func (db *DBGroupRequest) Convert() (*sdk.GroupRequest, error) {
dst := &sdk.GroupRequest{}
utils.CopyStructFields(dst, db)
dst.ReqTime = db.ReqTime.Unix()
dst.HandleTime = db.HandledTime.Unix()
return dst, nil
}
type DBUser struct {
2 years ago
*relation.UserModel
}
2 years ago
func NewDBUser(user *relation.UserModel) *DBUser {
return &DBUser{UserModel: user}
}
type PBUser struct {
*sdk.UserInfo
}
func NewPBUser(userInfo *sdk.UserInfo) *PBUser {
return &PBUser{UserInfo: userInfo}
}
2 years ago
func (*PBUser) PB2DB(users []*sdk.UserInfo) (DBUsers []*relation.UserModel, err error) {
for _, v := range users {
u, err := NewPBUser(v).Convert()
if err != nil {
return nil, err
}
DBUsers = append(DBUsers, u)
}
return
}
2 years ago
func (*DBUser) DB2PB(users []*relation.UserModel) (PBUsers []*sdk.UserInfo, err error) {
for _, v := range users {
u, err := NewDBUser(v).Convert()
if err != nil {
return nil, err
}
PBUsers = append(PBUsers, u)
}
return
}
2 years ago
func (pb *PBUser) Convert() (*relation.UserModel, error) {
dst := &relation.UserModel{}
utils.CopyStructFields(dst, pb)
dst.Birth = utils.UnixSecondToTime(pb.Birthday)
dst.CreateTime = utils.UnixSecondToTime(pb.CreateTime)
return dst, nil
}
func (db *DBUser) Convert() (*sdk.UserInfo, error) {
dst := &sdk.UserInfo{}
utils.CopyStructFields(dst, db)
dst.CreateTime = db.CreateTime.Unix()
dst.Birthday = db.Birth.Unix()
return dst, nil
}
func (db *DBUser) ConvertPublic() (*sdk.PublicUserInfo, error) {
dst := &sdk.PublicUserInfo{}
utils.CopyStructFields(dst, db)
return dst, nil
}