diff --git a/internal/rpc/conversation/conversaion.go b/internal/rpc/conversation/conversaion.go index 79db9faa1..0b3bf222a 100644 --- a/internal/rpc/conversation/conversaion.go +++ b/internal/rpc/conversation/conversaion.go @@ -4,6 +4,7 @@ import ( "context" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/convert" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/controller" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/relation" @@ -13,7 +14,7 @@ import ( "github.com/OpenIMSDK/Open-IM-Server/pkg/errs" pbConversation "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/conversation" "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" - notification "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient/notification2" + "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient/notification" "github.com/OpenIMSDK/Open-IM-Server/pkg/utils" "google.golang.org/grpc" ) @@ -52,9 +53,7 @@ func (c *conversationServer) GetConversation(ctx context.Context, req *pbConvers return nil, err } if len(conversations) > 0 { - if err := utils.CopyStructFields(resp.Conversation, &conversations[0]); err != nil { - return nil, err - } + resp.Conversation = convert.ConversationDB2Pb(conversations[0]) return resp, nil } return nil, errs.ErrRecordNotFound.Wrap("conversation not found") @@ -66,9 +65,7 @@ func (c *conversationServer) GetAllConversations(ctx context.Context, req *pbCon if err != nil { return nil, err } - if err := utils.CopyStructFields(&resp.Conversations, conversations); err != nil { - return nil, err - } + resp.Conversations = convert.ConversationsDB2Pb(conversations) return resp, nil } @@ -78,24 +75,18 @@ func (c *conversationServer) GetConversations(ctx context.Context, req *pbConver return nil, err } resp := &pbConversation.GetConversationsResp{Conversations: []*pbConversation.Conversation{}} - if err := utils.CopyStructFields(&resp.Conversations, conversations); err != nil { - return nil, err - } + resp.Conversations = convert.ConversationsDB2Pb(conversations) return resp, nil } func (c *conversationServer) BatchSetConversations(ctx context.Context, req *pbConversation.BatchSetConversationsReq) (*pbConversation.BatchSetConversationsResp, error) { - var conversations []*tableRelation.ConversationModel - if err := utils.CopyStructFields(&conversations, req.Conversations); err != nil { - return nil, err - } + conversations := convert.ConversationsPb2DB(req.Conversations) err := c.ConversationDatabase.SetUserConversations(ctx, req.OwnerUserID, conversations) if err != nil { return nil, err } c.conversationNotificationSender.ConversationChangeNotification(ctx, req.OwnerUserID) - resp := &pbConversation.BatchSetConversationsResp{} - return resp, nil + return &pbConversation.BatchSetConversationsResp{}, nil } func (c *conversationServer) SetConversation(ctx context.Context, req *pbConversation.SetConversationReq) (*pbConversation.SetConversationResp, error) { @@ -116,6 +107,7 @@ func (c *conversationServer) SetRecvMsgOpt(ctx context.Context, req *pbConversat if err := c.ConversationDatabase.SetUsersConversationFiledTx(ctx, []string{req.OwnerUserID}, &tableRelation.ConversationModel{OwnerUserID: req.OwnerUserID, ConversationID: req.ConversationID, RecvMsgOpt: req.RecvMsgOpt}, map[string]interface{}{"recv_msg_opt": req.RecvMsgOpt}); err != nil { return nil, err } + c.conversationNotificationSender.ConversationChangeNotification(ctx, req.OwnerUserID) return &pbConversation.SetRecvMsgOptResp{}, nil } @@ -187,7 +179,5 @@ func (c *conversationServer) GetRecvMsgNotNotifyUserIDs(ctx context.Context, req if err != nil { return nil, err } - resp := &pbConversation.GetRecvMsgNotNotifyUserIDsResp{} - resp.UserIDs = userIDs - return resp, nil + return &pbConversation.GetRecvMsgNotNotifyUserIDsResp{UserIDs: userIDs}, nil } diff --git a/internal/rpc/friend/black.go b/internal/rpc/friend/black.go index 73933cbe6..692e83439 100644 --- a/internal/rpc/friend/black.go +++ b/internal/rpc/friend/black.go @@ -4,11 +4,11 @@ import ( "context" "time" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/convert" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/tokenverify" pbFriend "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/friend" - "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient/convert" ) func (s *friendServer) GetPaginationBlacks(ctx context.Context, req *pbFriend.GetPaginationBlacksReq) (resp *pbFriend.GetPaginationBlacksResp, err error) { diff --git a/internal/rpc/friend/friend.go b/internal/rpc/friend/friend.go index cedc7fb45..8b7031cf2 100644 --- a/internal/rpc/friend/friend.go +++ b/internal/rpc/friend/friend.go @@ -3,6 +3,7 @@ package friend import ( "context" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/convert" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/log" "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" @@ -16,8 +17,7 @@ import ( registry "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" "github.com/OpenIMSDK/Open-IM-Server/pkg/errs" pbfriend "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/friend" - "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient/convert" - notification "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient/notification2" + "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient/notification" "github.com/OpenIMSDK/Open-IM-Server/pkg/utils" "google.golang.org/grpc" ) diff --git a/internal/rpc/group/group.go b/internal/rpc/group/group.go index e13a6afda..5027aa7a1 100644 --- a/internal/rpc/group/group.go +++ b/internal/rpc/group/group.go @@ -3,13 +3,14 @@ package group import ( "context" "fmt" - "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient/notification2" "math/big" "math/rand" "strconv" "strings" "time" + "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient/notification" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/mw/specialerror" "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" @@ -52,7 +53,7 @@ func Start(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) e pbGroup.RegisterGroupServer(server, &groupServer{ GroupDatabase: database, User: user, - Notification: notification2.NewGroupNotificationSender(database, client, func(ctx context.Context, userIDs []string) ([]rpcclient.CommonUser, error) { + Notification: notification.NewGroupNotificationSender(database, client, func(ctx context.Context, userIDs []string) ([]rpcclient.CommonUser, error) { users, err := user.GetUsersInfo(ctx, userIDs) if err != nil { return nil, err @@ -68,7 +69,7 @@ type groupServer struct { GroupDatabase controller.GroupDatabase User *rpcclient.UserClient //Notification *notification.Check - Notification *notification2.GroupNotificationSender + Notification *notification.GroupNotificationSender conversationRpcClient *rpcclient.ConversationClient } diff --git a/internal/rpc/user/user.go b/internal/rpc/user/user.go index 5731ca073..7f88ce635 100644 --- a/internal/rpc/user/user.go +++ b/internal/rpc/user/user.go @@ -6,6 +6,7 @@ import ( "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/convert" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/controller" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/relation" @@ -18,8 +19,8 @@ import ( "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws" pbuser "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/user" "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" - "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient/convert" - notification "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient/notification2" + "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient/notification" + "github.com/OpenIMSDK/Open-IM-Server/pkg/utils" "google.golang.org/grpc" ) diff --git a/pkg/rpcclient/convert/black.go b/pkg/common/convert/black.go similarity index 100% rename from pkg/rpcclient/convert/black.go rename to pkg/common/convert/black.go diff --git a/pkg/common/convert/conversation.go b/pkg/common/convert/conversation.go new file mode 100644 index 000000000..66d1d771c --- /dev/null +++ b/pkg/common/convert/conversation.go @@ -0,0 +1,45 @@ +package convert + +import ( + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation" + "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/conversation" + "github.com/OpenIMSDK/Open-IM-Server/pkg/utils" +) + +func ConversationDB2Pb(conversationDB *relation.ConversationModel) *conversation.Conversation { + conversationPB := &conversation.Conversation{} + if err := utils.CopyStructFields(conversationPB, conversationDB); err != nil { + return nil + } + return conversationPB +} + +func ConversationsDB2Pb(conversationsDB []*relation.ConversationModel) (conversationsPB []*conversation.Conversation) { + for _, conversationDB := range conversationsDB { + conversationPB := &conversation.Conversation{} + if err := utils.CopyStructFields(conversationPB, conversationDB); err != nil { + continue + } + conversationsPB = append(conversationsPB, conversationPB) + } + return conversationsPB +} + +func ConversationPb2DB(conversationPB *conversation.Conversation) *relation.ConversationModel { + conversationDB := &relation.ConversationModel{} + if err := utils.CopyStructFields(conversationDB, conversationPB); err != nil { + return nil + } + return conversationDB +} + +func ConversationsPb2DB(conversationsPB []*conversation.Conversation) (conversationsDB []*relation.ConversationModel) { + for _, conversationPB := range conversationsPB { + conversationDB := &relation.ConversationModel{} + if err := utils.CopyStructFields(conversationDB, conversationPB); err != nil { + continue + } + conversationsDB = append(conversationsDB, conversationDB) + } + return conversationsDB +} diff --git a/pkg/rpcclient/convert/friend.go b/pkg/common/convert/friend.go similarity index 98% rename from pkg/rpcclient/convert/friend.go rename to pkg/common/convert/friend.go index f18acd416..02c5a05c4 100644 --- a/pkg/rpcclient/convert/friend.go +++ b/pkg/common/convert/friend.go @@ -5,7 +5,7 @@ import ( "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation" "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws" - utils "github.com/OpenIMSDK/open_utils" + "github.com/OpenIMSDK/Open-IM-Server/pkg/utils" ) func FriendPb2DB(friend *sdkws.FriendInfo) *relation.FriendModel { diff --git a/pkg/rpcclient/convert/group.go b/pkg/common/convert/group.go similarity index 100% rename from pkg/rpcclient/convert/group.go rename to pkg/common/convert/group.go diff --git a/pkg/rpcclient/convert/user.go b/pkg/common/convert/user.go similarity index 100% rename from pkg/rpcclient/convert/user.go rename to pkg/common/convert/user.go diff --git a/pkg/rpcclient/check/group.go b/pkg/rpcclient/check/group.go deleted file mode 100644 index 994b5349e..000000000 --- a/pkg/rpcclient/check/group.go +++ /dev/null @@ -1,132 +0,0 @@ -package check - -import ( - "context" - "strings" - - "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" - "github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant" - discoveryRegistry "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" - "github.com/OpenIMSDK/Open-IM-Server/pkg/errs" - "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/group" - sdkws "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws" - "github.com/OpenIMSDK/Open-IM-Server/pkg/utils" - "google.golang.org/grpc" -) - -type GroupChecker struct { - zk discoveryRegistry.SvcDiscoveryRegistry -} - -func NewGroupChecker(zk discoveryRegistry.SvcDiscoveryRegistry) *GroupChecker { - return &GroupChecker{ - zk: zk, - } -} - -func (g *GroupChecker) getConn() (*grpc.ClientConn, error) { - return g.zk.GetConn(config.Config.RpcRegisterName.OpenImGroupName) -} - -func (g *GroupChecker) GetGroupInfos(ctx context.Context, groupIDs []string, complete bool) ([]*sdkws.GroupInfo, error) { - cc, err := g.getConn() - if err != nil { - return nil, err - } - resp, err := group.NewGroupClient(cc).GetGroupsInfo(ctx, &group.GetGroupsInfoReq{ - GroupIDs: groupIDs, - }) - if err != nil { - return nil, err - } - if complete { - if ids := utils.Single(groupIDs, utils.Slice(resp.GroupInfos, func(e *sdkws.GroupInfo) string { - return e.GroupID - })); len(ids) > 0 { - return nil, errs.ErrGroupIDNotFound.Wrap(strings.Join(ids, ",")) - } - } - return resp.GroupInfos, nil -} - -func (g *GroupChecker) GetGroupInfo(ctx context.Context, groupID string) (*sdkws.GroupInfo, error) { - groups, err := g.GetGroupInfos(ctx, []string{groupID}, true) - if err != nil { - return nil, err - } - return groups[0], nil -} - -func (g *GroupChecker) GetGroupInfoMap(ctx context.Context, groupIDs []string, complete bool) (map[string]*sdkws.GroupInfo, error) { - groups, err := g.GetGroupInfos(ctx, groupIDs, complete) - if err != nil { - return nil, err - } - return utils.SliceToMap(groups, func(e *sdkws.GroupInfo) string { - return e.GroupID - }), nil -} - -func (g *GroupChecker) GetGroupMemberInfos(ctx context.Context, groupID string, userIDs []string, complete bool) ([]*sdkws.GroupMemberFullInfo, error) { - cc, err := g.getConn() - if err != nil { - return nil, err - } - resp, err := group.NewGroupClient(cc).GetGroupMembersInfo(ctx, &group.GetGroupMembersInfoReq{ - GroupID: groupID, - UserIDs: userIDs, - }) - if err != nil { - return nil, err - } - if complete { - if ids := utils.Single(userIDs, utils.Slice(resp.Members, func(e *sdkws.GroupMemberFullInfo) string { - return e.UserID - })); len(ids) > 0 { - return nil, errs.ErrNotInGroupYet.Wrap(strings.Join(ids, ",")) - } - } - return resp.Members, nil -} - -func (g *GroupChecker) GetGroupMemberInfo(ctx context.Context, groupID string, userID string) (*sdkws.GroupMemberFullInfo, error) { - members, err := g.GetGroupMemberInfos(ctx, groupID, []string{userID}, true) - if err != nil { - return nil, err - } - return members[0], nil -} - -func (g *GroupChecker) GetGroupMemberInfoMap(ctx context.Context, groupID string, userIDs []string, complete bool) (map[string]*sdkws.GroupMemberFullInfo, error) { - members, err := g.GetGroupMemberInfos(ctx, groupID, userIDs, true) - if err != nil { - return nil, err - } - return utils.SliceToMap(members, func(e *sdkws.GroupMemberFullInfo) string { - return e.UserID - }), nil -} - -func (g *GroupChecker) GetOwnerAndAdminInfos(ctx context.Context, groupID string) ([]*sdkws.GroupMemberFullInfo, error) { - cc, err := g.getConn() - if err != nil { - return nil, err - } - resp, err := group.NewGroupClient(cc).GetGroupMemberRoleLevel(ctx, &group.GetGroupMemberRoleLevelReq{ - GroupID: groupID, - RoleLevels: []int32{constant.GroupOwner, constant.GroupAdmin}, - }) - return resp.Members, err -} - -func (g *GroupChecker) GetOwnerInfo(ctx context.Context, groupID string) (*sdkws.GroupMemberFullInfo, error) { - cc, err := g.getConn() - if err != nil { - return nil, err - } - resp, err := group.NewGroupClient(cc).GetGroupMemberRoleLevel(ctx, &group.GetGroupMemberRoleLevelReq{ - GroupID: groupID, - RoleLevels: []int32{constant.GroupOwner}, - }) - return resp.Members[0], err -} diff --git a/pkg/rpcclient/notification2/conevrsation.go b/pkg/rpcclient/notification/conevrsation.go similarity index 98% rename from pkg/rpcclient/notification2/conevrsation.go rename to pkg/rpcclient/notification/conevrsation.go index 0bd646e91..646fedd0d 100644 --- a/pkg/rpcclient/notification2/conevrsation.go +++ b/pkg/rpcclient/notification/conevrsation.go @@ -1,4 +1,4 @@ -package notification2 +package notification import ( "context" diff --git a/pkg/rpcclient/notification2/extend_msg.go b/pkg/rpcclient/notification/extend_msg.go similarity index 99% rename from pkg/rpcclient/notification2/extend_msg.go rename to pkg/rpcclient/notification/extend_msg.go index be3865b57..2f28f9dd3 100644 --- a/pkg/rpcclient/notification2/extend_msg.go +++ b/pkg/rpcclient/notification/extend_msg.go @@ -1,4 +1,4 @@ -package notification2 +package notification import ( "context" diff --git a/pkg/rpcclient/notification2/friend.go b/pkg/rpcclient/notification/friend.go similarity index 98% rename from pkg/rpcclient/notification2/friend.go rename to pkg/rpcclient/notification/friend.go index e3ccd30be..fe2c15edf 100644 --- a/pkg/rpcclient/notification2/friend.go +++ b/pkg/rpcclient/notification/friend.go @@ -1,17 +1,18 @@ -package notification2 +package notification import ( "context" "encoding/json" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/convert" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/controller" relationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation" "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" pbFriend "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/friend" "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws" "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" - "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient/convert" + "github.com/golang/protobuf/proto" ) diff --git a/pkg/rpcclient/notification2/group.go b/pkg/rpcclient/notification/group.go similarity index 99% rename from pkg/rpcclient/notification2/group.go rename to pkg/rpcclient/notification/group.go index b81230e83..44f44ee71 100644 --- a/pkg/rpcclient/notification2/group.go +++ b/pkg/rpcclient/notification/group.go @@ -1,9 +1,11 @@ -package notification2 +package notification import ( "context" "encoding/json" "fmt" + "time" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/controller" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation" @@ -17,7 +19,6 @@ import ( "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" "github.com/OpenIMSDK/Open-IM-Server/pkg/utils" "github.com/golang/protobuf/proto" - "time" ) func NewGroupNotificationSender(db controller.GroupDatabase, sdr discoveryregistry.SvcDiscoveryRegistry, fn func(ctx context.Context, userIDs []string) ([]rpcclient.CommonUser, error)) *GroupNotificationSender { diff --git a/pkg/utils/options.go b/pkg/utils/options.go new file mode 100644 index 000000000..d4b585bf7 --- /dev/null +++ b/pkg/utils/options.go @@ -0,0 +1 @@ +package utils