test-errcode
withchao 2 years ago
commit d4b125d010

@ -5,6 +5,7 @@ import (
"Open_IM/pkg/common/db/table/relation"
discoveryRegistry "Open_IM/pkg/discoveryregistry"
sdk "Open_IM/pkg/proto/sdkws"
utils2 "Open_IM/pkg/utils"
"context"
utils "github.com/OpenIMSDK/open_utils"
"time"
@ -38,13 +39,18 @@ func (*PBFriend) PB2DB(friends []*sdk.FriendInfo) (DBFriends []*relation.FriendM
return
}
func (*DBFriend) DB2PB(ctx context.Context, zk discoveryRegistry.SvcDiscoveryRegistry, friends []*relation.FriendModel) (PBFriends []*sdk.FriendInfo, err error) {
for _, v := range friends {
u, err := NewDBFriend(v, zk).Convert(ctx)
func (db *DBFriend) DB2PB(ctx context.Context, friends []*relation.FriendModel) (PBFriends []*sdk.FriendInfo, err error) {
userIDs := utils2.Slice(friends, func(e *relation.FriendModel) string { return e.FriendUserID })
users, err := db.userCheck.GetUsersInfoMap(ctx, userIDs, true)
if err != nil {
return nil, err
}
PBFriends = append(PBFriends, u)
for _, v := range friends {
pbFriend := &sdk.FriendInfo{FriendUser: &sdk.UserInfo{}}
utils.CopyStructFields(pbFriend, users[v.OwnerUserID])
utils.CopyStructFields(pbFriend.FriendUser, users[v.FriendUserID])
pbFriend.CreateTime = v.CreateTime.Unix()
pbFriend.FriendUser.CreateTime = v.CreateTime.Unix()
}
return
}
@ -99,13 +105,26 @@ func (*PBFriendRequest) PB2DB(friendRequests []*sdk.FriendRequest) (DBFriendRequ
return
}
func (*DBFriendRequest) DB2PB(ctx context.Context, zk discoveryRegistry.SvcDiscoveryRegistry, friendRequests []*relation.FriendRequestModel) (PBFriendRequests []*sdk.FriendRequest, err error) {
for _, v := range friendRequests {
u, err := NewDBFriendRequest(v, zk).Convert(ctx)
func (db *DBFriendRequest) DB2PB(ctx context.Context, friendRequests []*relation.FriendRequestModel) (PBFriendRequests []*sdk.FriendRequest, err error) {
userIDs := make([]string, 0)
if len(friendRequests) > 0 {
userIDs = append(userIDs, friendRequests[0].FromUserID)
}
users, err := db.userCheck.GetUsersInfoMap(ctx, userIDs, true)
if err != nil {
return nil, err
}
PBFriendRequests = append(PBFriendRequests, u)
for _, v := range friendRequests {
pbFriendRequest := &sdk.FriendRequest{}
pbFriendRequest.FromNickname = users[v.FromUserID].Nickname
pbFriendRequest.FromFaceURL = users[v.FromUserID].FaceURL
pbFriendRequest.FromGender = users[v.FromUserID].Gender
pbFriendRequest.ToNickname = users[v.ToUserID].Nickname
pbFriendRequest.ToFaceURL = users[v.ToUserID].FaceURL
pbFriendRequest.ToGender = users[v.ToUserID].Gender
pbFriendRequest.CreateTime = db.CreateTime.Unix()
pbFriendRequest.HandleTime = db.HandleTime.Unix()
PBFriendRequests = append(PBFriendRequests, pbFriendRequest)
}
return
}
@ -155,13 +174,25 @@ func (*PBBlack) PB2DB(blacks []*sdk.BlackInfo) (DBBlacks []*relation.BlackModel,
return
}
func (*DBBlack) DB2PB(ctx context.Context, zk discoveryRegistry.SvcDiscoveryRegistry, blacks []*relation.BlackModel) (PBBlacks []*sdk.BlackInfo, err error) {
func (db *DBBlack) DB2PB(ctx context.Context, blacks []*relation.BlackModel) (PBBlacks []*sdk.BlackInfo, err error) {
userIDs := make([]string, 0)
for _, v := range blacks {
u, err := NewDBBlack(v, zk).Convert(ctx)
userIDs = append(userIDs, v.BlockUserID)
}
if len(blacks) > 0 {
userIDs = append(userIDs, blacks[0].OwnerUserID)
}
users, err := db.userCheck.GetUsersInfoMap(ctx, userIDs, true)
if err != nil {
return nil, err
}
PBBlacks = append(PBBlacks, u)
for _, v := range blacks {
pbBlack := &sdk.BlackInfo{}
utils.CopyStructFields(pbBlack, users[v.OwnerUserID])
utils.CopyStructFields(pbBlack.BlackUserInfo, users[v.BlockUserID])
PBBlacks = append(PBBlacks, pbBlack)
}
return
}
@ -181,7 +212,7 @@ func NewPBBlack(blackInfo *sdk.BlackInfo) *PBBlack {
func (pb *PBBlack) Convert() (*relation.BlackModel, error) {
dbBlack := &relation.BlackModel{}
dbBlack.BlockUserID = pb.BlackUserInfo.UserID
dbBlack.CreateTime = utils.UnixSecondToTime(int64(pb.CreateTime))
dbBlack.CreateTime = utils.UnixSecondToTime(pb.CreateTime)
return dbBlack, nil
}
func (db *DBBlack) Convert(ctx context.Context) (*sdk.BlackInfo, error) {
@ -213,16 +244,16 @@ func (*PBGroup) PB2DB(groups []*sdk.GroupInfo) (DBGroups []*relation.GroupModel,
return
}
func (db *DBGroup) DB2PB(ctx context.Context, zk discoveryRegistry.SvcDiscoveryRegistry, groups []*relation.GroupModel) (PBGroups []*sdk.GroupInfo, err error) {
for _, v := range groups {
u, err := NewDBGroup(v, zk).Convert(ctx)
if err != nil {
return nil, err
}
PBGroups = append(PBGroups, u)
}
return
}
//func (db *DBGroup) DB2PB(ctx context.Context, zk discoveryRegistry.SvcDiscoveryRegistry, groups []*relation.GroupModel) (PBGroups []*sdk.GroupInfo, err error) {
// for _, v := range groups {
// u, err := NewDBGroup(v, zk).Convert(ctx)
// if err != nil {
// return nil, err
// }
// PBGroups = append(PBGroups, u)
// }
// return
//}
func NewDBGroup(groupModel *relation.GroupModel, zk discoveryRegistry.SvcDiscoveryRegistry) *DBGroup {
return &DBGroup{GroupModel: groupModel, groupCheck: check.NewGroupChecker(zk)}
@ -279,16 +310,16 @@ func (*PBGroupMember) PB2DB(groupMembers []*sdk.GroupMemberFullInfo) (DBGroupMem
return
}
func (*DBGroupMember) DB2PB(ctx context.Context, groupMembers []*relation.GroupMemberModel) (PBGroupMembers []*sdk.GroupMemberFullInfo, err error) {
for _, v := range groupMembers {
u, err := NewDBGroupMember(v).Convert(ctx)
if err != nil {
return nil, err
}
PBGroupMembers = append(PBGroupMembers, u)
}
return
}
//func (*DBGroupMember) DB2PB(ctx context.Context, groupMembers []*relation.GroupMemberModel) (PBGroupMembers []*sdk.GroupMemberFullInfo, err error) {
// for _, v := range groupMembers {
// u, err := NewDBGroupMember(v).Convert(ctx)
// if err != nil {
// return nil, err
// }
// PBGroupMembers = append(PBGroupMembers, u)
// }
// return
//}
func NewDBGroupMember(groupMember *relation.GroupMemberModel) *DBGroupMember {
return &DBGroupMember{GroupMemberModel: groupMember}
@ -345,16 +376,16 @@ func (*PBGroupRequest) PB2DB(groupRequests []*sdk.GroupRequest) (DBGroupRequests
return
}
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
}
//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
//}
func NewDBGroupRequest(groupRequest *relation.GroupRequestModel) *DBGroupRequest {
return &DBGroupRequest{GroupRequestModel: groupRequest}

@ -18,7 +18,7 @@ func (s *friendServer) GetPaginationBlacks(ctx context.Context, req *pbFriend.Ge
if err != nil {
return nil, err
}
resp.Blacks, err = (*convert.DBBlack)(nil).DB2PB(blacks)
resp.Blacks, err = (*convert.NewDBBlack(nil, s.RegisterCenter)).DB2PB(ctx, blacks)
if err != nil {
return nil, err
}

@ -217,7 +217,7 @@ func (s *friendServer) GetDesignatedFriends(ctx context.Context, req *pbFriend.G
if err != nil {
return nil, err
}
resp.FriendsInfo, err = (*convert.DBFriend)(nil).DB2PB(ctx, s.RegisterCenter, friends)
resp.FriendsInfo, err = (*convert.NewDBFriend(nil, s.RegisterCenter)).DB2PB(ctx, friends)
if err != nil {
return nil, err
}
@ -235,7 +235,7 @@ func (s *friendServer) GetPaginationFriendsApplyTo(ctx context.Context, req *pbF
if err != nil {
return nil, err
}
resp.FriendRequests, err = (*convert.DBFriendRequest)(nil).DB2PB(ctx, s.RegisterCenter, friendRequests)
resp.FriendRequests, err = (*convert.NewDBFriendRequest(nil, s.RegisterCenter)).DB2PB(ctx, friendRequests)
if err != nil {
return nil, err
}
@ -253,7 +253,7 @@ func (s *friendServer) GetPaginationFriendsApplyFrom(ctx context.Context, req *p
if err != nil {
return nil, err
}
resp.FriendRequests, err = (*convert.DBFriendRequest)(nil).DB2PB(ctx, s.RegisterCenter, friendRequests)
resp.FriendRequests, err = (*convert.NewDBFriendRequest(nil, s.RegisterCenter)).DB2PB(ctx, friendRequests)
if err != nil {
return nil, err
}
@ -281,7 +281,7 @@ func (s *friendServer) GetPaginationFriends(ctx context.Context, req *pbFriend.G
if err != nil {
return nil, err
}
if resp.FriendsInfo, err = (*convert.DBFriend)(nil).DB2PB(ctx, s.RegisterCenter, friends); err != nil {
if resp.FriendsInfo, err = (*convert.NewDBFriend(nil, s.RegisterCenter)).DB2PB(ctx, friends); err != nil {
return nil, err
}
return resp, nil

Loading…
Cancel
Save