diff --git a/internal/api/auth.go b/internal/api/auth.go index bd598d4bd..3fdbc10ac 100644 --- a/internal/api/auth.go +++ b/internal/api/auth.go @@ -1,43 +1,27 @@ package api import ( - "context" - "github.com/OpenIMSDK/Open-IM-Server/pkg/a2r" - "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/auth" + "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" "github.com/gin-gonic/gin" - "google.golang.org/grpc" ) -func NewAuth(discov discoveryregistry.SvcDiscoveryRegistry) *Auth { - conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImAuthName) - if err != nil { - panic(err) - } - client := auth.NewAuthClient(conn) - return &Auth{discov: discov, conn: conn, client: client} +type AuthApi rpcclient.Auth + +func NewAuthApi(discov discoveryregistry.SvcDiscoveryRegistry) AuthApi { + return AuthApi(*rpcclient.NewAuth(discov)) } -type Auth struct { - conn *grpc.ClientConn - client auth.AuthClient - discov discoveryregistry.SvcDiscoveryRegistry -} - -func (o *Auth) Client() auth.AuthClient { - return o.client -} - -func (o *Auth) UserToken(c *gin.Context) { +func (o *AuthApi) UserToken(c *gin.Context) { a2r.Call(auth.AuthClient.UserToken, o.Client, c) } -func (o *Auth) ParseToken(c *gin.Context) { +func (o *AuthApi) ParseToken(c *gin.Context) { a2r.Call(auth.AuthClient.ParseToken, o.Client, c) } -func (o *Auth) ForceLogout(c *gin.Context) { +func (o *AuthApi) ForceLogout(c *gin.Context) { a2r.Call(auth.AuthClient.ForceLogout, o.Client, c) } diff --git a/internal/api/conversation.go b/internal/api/conversation.go index 45dc20e0e..98e1e725b 100644 --- a/internal/api/conversation.go +++ b/internal/api/conversation.go @@ -1,65 +1,49 @@ package api import ( - "context" - "github.com/OpenIMSDK/Open-IM-Server/pkg/a2r" - "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/conversation" + "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" "github.com/gin-gonic/gin" - "google.golang.org/grpc" ) -func NewConversation(discov discoveryregistry.SvcDiscoveryRegistry) *Conversation { - conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImConversationName) - if err != nil { - panic(err) - } - client := conversation.NewConversationClient(conn) - return &Conversation{discov: discov, conn: conn, client: client} +type ConversationApi rpcclient.Conversation + +func NewConversationApi(discov discoveryregistry.SvcDiscoveryRegistry) ConversationApi { + return ConversationApi(*rpcclient.NewConversation(discov)) } -type Conversation struct { - client conversation.ConversationClient - conn *grpc.ClientConn - discov discoveryregistry.SvcDiscoveryRegistry -} - -func (o *Conversation) Client() conversation.ConversationClient { - return o.client -} - -func (o *Conversation) GetAllConversations(c *gin.Context) { +func (o *ConversationApi) GetAllConversations(c *gin.Context) { a2r.Call(conversation.ConversationClient.GetAllConversations, o.Client, c) } -func (o *Conversation) GetConversation(c *gin.Context) { +func (o *ConversationApi) GetConversation(c *gin.Context) { a2r.Call(conversation.ConversationClient.GetConversation, o.Client, c) } -func (o *Conversation) GetConversations(c *gin.Context) { +func (o *ConversationApi) GetConversations(c *gin.Context) { a2r.Call(conversation.ConversationClient.GetConversations, o.Client, c) } // deprecated -func (o *Conversation) SetConversation(c *gin.Context) { +func (o *ConversationApi) SetConversation(c *gin.Context) { a2r.Call(conversation.ConversationClient.SetConversation, o.Client, c) } // deprecated -func (o *Conversation) BatchSetConversations(c *gin.Context) { +func (o *ConversationApi) BatchSetConversations(c *gin.Context) { a2r.Call(conversation.ConversationClient.BatchSetConversations, o.Client, c) } -func (o *Conversation) SetRecvMsgOpt(c *gin.Context) { +func (o *ConversationApi) SetRecvMsgOpt(c *gin.Context) { a2r.Call(conversation.ConversationClient.SetRecvMsgOpt, o.Client, c) } -func (o *Conversation) ModifyConversationField(c *gin.Context) { +func (o *ConversationApi) ModifyConversationField(c *gin.Context) { a2r.Call(conversation.ConversationClient.ModifyConversationField, o.Client, c) } -func (o *Conversation) SetConversations(c *gin.Context) { +func (o *ConversationApi) SetConversations(c *gin.Context) { a2r.Call(conversation.ConversationClient.SetConversations, o.Client, c) } diff --git a/internal/api/friend.go b/internal/api/friend.go index afe7e60b7..8fe9033d5 100644 --- a/internal/api/friend.go +++ b/internal/api/friend.go @@ -1,80 +1,64 @@ package api import ( - "context" - "github.com/OpenIMSDK/Open-IM-Server/pkg/a2r" - "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/friend" - "google.golang.org/grpc" + "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" "github.com/gin-gonic/gin" ) -func NewFriend(discov discoveryregistry.SvcDiscoveryRegistry) *Friend { - conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImFriendName) - if err != nil { - panic(err) - } - client := friend.NewFriendClient(conn) - return &Friend{discov: discov, conn: conn, client: client} +type FriendApi rpcclient.Friend + +func NewFriendApi(discov discoveryregistry.SvcDiscoveryRegistry) FriendApi { + return FriendApi(*rpcclient.NewFriend(discov)) } -type Friend struct { - conn *grpc.ClientConn - client friend.FriendClient - discov discoveryregistry.SvcDiscoveryRegistry -} - -func (o *Friend) Client() friend.FriendClient { - return friend.NewFriendClient(o.conn) -} - -func (o *Friend) ApplyToAddFriend(c *gin.Context) { +func (o *FriendApi) ApplyToAddFriend(c *gin.Context) { a2r.Call(friend.FriendClient.ApplyToAddFriend, o.Client, c) } -func (o *Friend) RespondFriendApply(c *gin.Context) { +func (o *FriendApi) RespondFriendApply(c *gin.Context) { a2r.Call(friend.FriendClient.RespondFriendApply, o.Client, c) } -func (o *Friend) DeleteFriend(c *gin.Context) { +func (o *FriendApi) DeleteFriend(c *gin.Context) { a2r.Call(friend.FriendClient.DeleteFriend, o.Client, c) } -func (o *Friend) GetFriendApplyList(c *gin.Context) { +func (o *FriendApi) GetFriendApplyList(c *gin.Context) { a2r.Call(friend.FriendClient.GetPaginationFriendsApplyTo, o.Client, c) } -func (o *Friend) GetSelfApplyList(c *gin.Context) { +func (o *FriendApi) GetSelfApplyList(c *gin.Context) { a2r.Call(friend.FriendClient.GetPaginationFriendsApplyFrom, o.Client, c) } -func (o *Friend) GetFriendList(c *gin.Context) { +func (o *FriendApi) GetFriendList(c *gin.Context) { a2r.Call(friend.FriendClient.GetPaginationFriends, o.Client, c) } -func (o *Friend) SetFriendRemark(c *gin.Context) { +func (o *FriendApi) SetFriendRemark(c *gin.Context) { a2r.Call(friend.FriendClient.SetFriendRemark, o.Client, c) } -func (o *Friend) AddBlack(c *gin.Context) { +func (o *FriendApi) AddBlack(c *gin.Context) { a2r.Call(friend.FriendClient.AddBlack, o.Client, c) } -func (o *Friend) GetPaginationBlacks(c *gin.Context) { +func (o *FriendApi) GetPaginationBlacks(c *gin.Context) { a2r.Call(friend.FriendClient.GetPaginationBlacks, o.Client, c) } -func (o *Friend) RemoveBlack(c *gin.Context) { +func (o *FriendApi) RemoveBlack(c *gin.Context) { a2r.Call(friend.FriendClient.RemoveBlack, o.Client, c) } -func (o *Friend) ImportFriends(c *gin.Context) { +func (o *FriendApi) ImportFriends(c *gin.Context) { a2r.Call(friend.FriendClient.ImportFriends, o.Client, c) } -func (o *Friend) IsFriend(c *gin.Context) { +func (o *FriendApi) IsFriend(c *gin.Context) { a2r.Call(friend.FriendClient.IsFriend, o.Client, c) } diff --git a/internal/api/group.go b/internal/api/group.go index 72e1f1774..c165f8aab 100644 --- a/internal/api/group.go +++ b/internal/api/group.go @@ -1,117 +1,101 @@ package api import ( - "context" - "github.com/OpenIMSDK/Open-IM-Server/pkg/a2r" - "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/group" - "google.golang.org/grpc" + "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" "github.com/gin-gonic/gin" ) -func NewGroup(discov discoveryregistry.SvcDiscoveryRegistry) *Group { - conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImGroupName) - if err != nil { - panic(err) - } - client := group.NewGroupClient(conn) - return &Group{discov: discov, conn: conn, client: client} +type GroupApi rpcclient.Group + +func NewGroupApi(discov discoveryregistry.SvcDiscoveryRegistry) GroupApi { + return GroupApi(*rpcclient.NewGroup(discov)) } -type Group struct { - conn *grpc.ClientConn - client group.GroupClient - discov discoveryregistry.SvcDiscoveryRegistry -} - -func (o *Group) Client() group.GroupClient { - return o.client -} - -func (o *Group) CreateGroup(c *gin.Context) { +func (o *GroupApi) CreateGroup(c *gin.Context) { a2r.Call(group.GroupClient.CreateGroup, o.Client, c) } -func (o *Group) SetGroupInfo(c *gin.Context) { +func (o *GroupApi) SetGroupInfo(c *gin.Context) { a2r.Call(group.GroupClient.SetGroupInfo, o.Client, c) } -func (o *Group) JoinGroup(c *gin.Context) { +func (o *GroupApi) JoinGroup(c *gin.Context) { a2r.Call(group.GroupClient.JoinGroup, o.Client, c) } -func (o *Group) QuitGroup(c *gin.Context) { +func (o *GroupApi) QuitGroup(c *gin.Context) { a2r.Call(group.GroupClient.QuitGroup, o.Client, c) } -func (o *Group) ApplicationGroupResponse(c *gin.Context) { +func (o *GroupApi) ApplicationGroupResponse(c *gin.Context) { a2r.Call(group.GroupClient.GroupApplicationResponse, o.Client, c) } -func (o *Group) TransferGroupOwner(c *gin.Context) { +func (o *GroupApi) TransferGroupOwner(c *gin.Context) { a2r.Call(group.GroupClient.TransferGroupOwner, o.Client, c) } -func (o *Group) GetRecvGroupApplicationList(c *gin.Context) { +func (o *GroupApi) GetRecvGroupApplicationList(c *gin.Context) { a2r.Call(group.GroupClient.GetGroupApplicationList, o.Client, c) } -func (o *Group) GetUserReqGroupApplicationList(c *gin.Context) { +func (o *GroupApi) GetUserReqGroupApplicationList(c *gin.Context) { a2r.Call(group.GroupClient.GetUserReqApplicationList, o.Client, c) } -func (o *Group) GetGroupsInfo(c *gin.Context) { +func (o *GroupApi) GetGroupsInfo(c *gin.Context) { a2r.Call(group.GroupClient.GetGroupsInfo, o.Client, c) } -func (o *Group) KickGroupMember(c *gin.Context) { +func (o *GroupApi) KickGroupMember(c *gin.Context) { a2r.Call(group.GroupClient.KickGroupMember, o.Client, c) } -func (o *Group) GetGroupMembersInfo(c *gin.Context) { +func (o *GroupApi) GetGroupMembersInfo(c *gin.Context) { a2r.Call(group.GroupClient.GetGroupMembersInfo, o.Client, c) } -func (o *Group) GetGroupMemberList(c *gin.Context) { +func (o *GroupApi) GetGroupMemberList(c *gin.Context) { a2r.Call(group.GroupClient.GetGroupMemberList, o.Client, c) } -func (o *Group) InviteUserToGroup(c *gin.Context) { +func (o *GroupApi) InviteUserToGroup(c *gin.Context) { a2r.Call(group.GroupClient.InviteUserToGroup, o.Client, c) } -func (o *Group) GetJoinedGroupList(c *gin.Context) { +func (o *GroupApi) GetJoinedGroupList(c *gin.Context) { a2r.Call(group.GroupClient.GetJoinedGroupList, o.Client, c) } -func (o *Group) DismissGroup(c *gin.Context) { +func (o *GroupApi) DismissGroup(c *gin.Context) { a2r.Call(group.GroupClient.DismissGroup, o.Client, c) } -func (o *Group) MuteGroupMember(c *gin.Context) { +func (o *GroupApi) MuteGroupMember(c *gin.Context) { a2r.Call(group.GroupClient.MuteGroupMember, o.Client, c) } -func (o *Group) CancelMuteGroupMember(c *gin.Context) { +func (o *GroupApi) CancelMuteGroupMember(c *gin.Context) { a2r.Call(group.GroupClient.CancelMuteGroupMember, o.Client, c) } -func (o *Group) MuteGroup(c *gin.Context) { +func (o *GroupApi) MuteGroup(c *gin.Context) { a2r.Call(group.GroupClient.MuteGroup, o.Client, c) } -func (o *Group) CancelMuteGroup(c *gin.Context) { +func (o *GroupApi) CancelMuteGroup(c *gin.Context) { a2r.Call(group.GroupClient.CancelMuteGroup, o.Client, c) } -func (o *Group) SetGroupMemberInfo(c *gin.Context) { +func (o *GroupApi) SetGroupMemberInfo(c *gin.Context) { a2r.Call(group.GroupClient.SetGroupMemberInfo, o.Client, c) } -func (o *Group) GetGroupAbstractInfo(c *gin.Context) { +func (o *GroupApi) GetGroupAbstractInfo(c *gin.Context) { a2r.Call(group.GroupClient.GetGroupAbstractInfo, o.Client, c) } @@ -123,10 +107,10 @@ func (o *Group) GetGroupAbstractInfo(c *gin.Context) { // a2r.Call(group.GroupClient.GetGroupAllMember, g.userClient, c) //} -func (o *Group) GetJoinedSuperGroupList(c *gin.Context) { +func (o *GroupApi) GetJoinedSuperGroupList(c *gin.Context) { a2r.Call(group.GroupClient.GetJoinedSuperGroupList, o.Client, c) } -func (o *Group) GetSuperGroupsInfo(c *gin.Context) { +func (o *GroupApi) GetSuperGroupsInfo(c *gin.Context) { a2r.Call(group.GroupClient.GetSuperGroupsInfo, o.Client, c) } diff --git a/internal/api/msg.go b/internal/api/msg.go index 2da970d3d..ea3e63d7e 100644 --- a/internal/api/msg.go +++ b/internal/api/msg.go @@ -1,50 +1,40 @@ package api import ( - "context" - "github.com/OpenIMSDK/Open-IM-Server/pkg/a2r" "github.com/OpenIMSDK/Open-IM-Server/pkg/apiresp" "github.com/OpenIMSDK/Open-IM-Server/pkg/apistruct" - "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/log" "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" "github.com/OpenIMSDK/Open-IM-Server/pkg/errs" "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/msg" "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws" + "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" "github.com/OpenIMSDK/Open-IM-Server/pkg/utils" "github.com/gin-gonic/gin" "github.com/go-playground/validator/v10" "github.com/mitchellh/mapstructure" - "google.golang.org/grpc" "google.golang.org/protobuf/proto" ) -func NewMsg(discov discoveryregistry.SvcDiscoveryRegistry) *Message { - conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImMsgName) - if err != nil { - panic(err) - } - client := msg.NewMsgClient(conn) - return &Message{validate: validator.New(), discov: discov, conn: conn, client: client} -} - -type Message struct { - conn *grpc.ClientConn - client msg.MsgClient +type MessageApi struct { + rpcclient.Message validate *validator.Validate - discov discoveryregistry.SvcDiscoveryRegistry } -func (Message) SetOptions(options map[string]bool, value bool) { +func NewMessageApi(discov discoveryregistry.SvcDiscoveryRegistry) MessageApi { + return MessageApi{Message: *rpcclient.NewMessage(discov), validate: validator.New()} +} + +func (MessageApi) SetOptions(options map[string]bool, value bool) { utils.SetSwitchFromOptions(options, constant.IsHistory, value) utils.SetSwitchFromOptions(options, constant.IsPersistent, value) utils.SetSwitchFromOptions(options, constant.IsSenderSync, value) utils.SetSwitchFromOptions(options, constant.IsConversationUpdate, value) } -func (m Message) newUserSendMsgReq(c *gin.Context, params *apistruct.ManagementSendMsgReq) *msg.SendMsgReq { +func (m MessageApi) newUserSendMsgReq(c *gin.Context, params *apistruct.ManagementSendMsgReq) *msg.SendMsgReq { var newContent string var err error switch params.ContentType { @@ -109,75 +99,71 @@ func (m Message) newUserSendMsgReq(c *gin.Context, params *apistruct.ManagementS return &pbData } -func (m *Message) Client() msg.MsgClient { - return m.client -} - -func (m *Message) GetSeq(c *gin.Context) { +func (m *MessageApi) GetSeq(c *gin.Context) { a2r.Call(msg.MsgClient.GetMaxSeq, m.Client, c) } -func (m *Message) PullMsgBySeqs(c *gin.Context) { +func (m *MessageApi) PullMsgBySeqs(c *gin.Context) { a2r.Call(msg.MsgClient.PullMessageBySeqs, m.Client, c) } -func (m *Message) RevokeMsg(c *gin.Context) { +func (m *MessageApi) RevokeMsg(c *gin.Context) { a2r.Call(msg.MsgClient.RevokeMsg, m.Client, c) } -func (m *Message) MarkMsgsAsRead(c *gin.Context) { +func (m *MessageApi) MarkMsgsAsRead(c *gin.Context) { a2r.Call(msg.MsgClient.MarkMsgsAsRead, m.Client, c) } -func (m *Message) MarkConversationAsRead(c *gin.Context) { +func (m *MessageApi) MarkConversationAsRead(c *gin.Context) { a2r.Call(msg.MsgClient.MarkConversationAsRead, m.Client, c) } -func (m *Message) GetConversationsHasReadAndMaxSeq(c *gin.Context) { +func (m *MessageApi) GetConversationsHasReadAndMaxSeq(c *gin.Context) { a2r.Call(msg.MsgClient.GetConversationsHasReadAndMaxSeq, m.Client, c) } -func (m *Message) SetConversationHasReadSeq(c *gin.Context) { +func (m *MessageApi) SetConversationHasReadSeq(c *gin.Context) { a2r.Call(msg.MsgClient.SetConversationHasReadSeq, m.Client, c) } -func (m *Message) ClearConversationsMsg(c *gin.Context) { +func (m *MessageApi) ClearConversationsMsg(c *gin.Context) { a2r.Call(msg.MsgClient.ClearConversationsMsg, m.Client, c) } -func (m *Message) UserClearAllMsg(c *gin.Context) { +func (m *MessageApi) UserClearAllMsg(c *gin.Context) { a2r.Call(msg.MsgClient.UserClearAllMsg, m.Client, c) } -func (m *Message) DeleteMsgs(c *gin.Context) { +func (m *MessageApi) DeleteMsgs(c *gin.Context) { a2r.Call(msg.MsgClient.DeleteMsgs, m.Client, c) } -func (m *Message) DeleteMsgPhysicalBySeq(c *gin.Context) { +func (m *MessageApi) DeleteMsgPhysicalBySeq(c *gin.Context) { a2r.Call(msg.MsgClient.DeleteMsgPhysicalBySeq, m.Client, c) } -func (m *Message) DeleteMsgPhysical(c *gin.Context) { +func (m *MessageApi) DeleteMsgPhysical(c *gin.Context) { a2r.Call(msg.MsgClient.DeleteMsgPhysical, m.Client, c) } -func (m *Message) SetMessageReactionExtensions(c *gin.Context) { +func (m *MessageApi) SetMessageReactionExtensions(c *gin.Context) { a2r.Call(msg.MsgClient.SetMessageReactionExtensions, m.Client, c) } -func (m *Message) GetMessageListReactionExtensions(c *gin.Context) { +func (m *MessageApi) GetMessageListReactionExtensions(c *gin.Context) { a2r.Call(msg.MsgClient.GetMessagesReactionExtensions, m.Client, c) } -func (m *Message) AddMessageReactionExtensions(c *gin.Context) { +func (m *MessageApi) AddMessageReactionExtensions(c *gin.Context) { a2r.Call(msg.MsgClient.AddMessageReactionExtensions, m.Client, c) } -func (m *Message) DeleteMessageReactionExtensions(c *gin.Context) { +func (m *MessageApi) DeleteMessageReactionExtensions(c *gin.Context) { a2r.Call(msg.MsgClient.DeleteMessageReactionExtensions, m.Client, c) } -func (m *Message) SendMessage(c *gin.Context) { +func (m *MessageApi) SendMessage(c *gin.Context) { params := apistruct.ManagementSendMsgReq{} if err := c.BindJSON(¶ms); err != nil { apiresp.GinError(c, errs.ErrArgs.WithDetail(err.Error()).Wrap()) @@ -224,16 +210,15 @@ func (m *Message) SendMessage(c *gin.Context) { return } pbReq := m.newUserSendMsgReq(c, ¶ms) - client := msg.NewMsgClient(m.conn) var status int - respPb, err := client.SendMsg(c, pbReq) + respPb, err := m.Client.SendMsg(c, pbReq) if err != nil { status = constant.MsgSendFailed apiresp.GinError(c, err) return } status = constant.MsgSendSuccessed - _, err = client.SetSendMsgStatus(c, &msg.SetSendMsgStatusReq{ + _, err = m.Client.SetSendMsgStatus(c, &msg.SetSendMsgStatusReq{ Status: int32(status), }) if err != nil { @@ -242,14 +227,14 @@ func (m *Message) SendMessage(c *gin.Context) { apiresp.GinSuccess(c, respPb) } -func (m *Message) ManagementBatchSendMsg(c *gin.Context) { +func (m *MessageApi) ManagementBatchSendMsg(c *gin.Context) { a2r.Call(msg.MsgClient.SendMsg, m.Client, c) } -func (m *Message) CheckMsgIsSendSuccess(c *gin.Context) { +func (m *MessageApi) CheckMsgIsSendSuccess(c *gin.Context) { a2r.Call(msg.MsgClient.GetSendMsgStatus, m.Client, c) } -func (m *Message) GetUsersOnlineStatus(c *gin.Context) { +func (m *MessageApi) GetUsersOnlineStatus(c *gin.Context) { a2r.Call(msg.MsgClient.GetSendMsgStatus, m.Client, c) } diff --git a/internal/api/route.go b/internal/api/route.go index 2062ec3fe..2ba4e66fa 100644 --- a/internal/api/route.go +++ b/internal/api/route.go @@ -37,7 +37,7 @@ func NewGinRouter(discov discoveryregistry.SvcDiscoveryRegistry, rdb redis.Unive } userRouterGroup := r.Group("/user") { - u := NewUser(discov) + u := NewUserApi(discov) userRouterGroupChild := mw.NewRouterGroup(userRouterGroup, "") userRouterGroupChildToken := mw.NewRouterGroup(userRouterGroup, "", mw.WithGinParseToken(rdb)) userRouterGroupChild.POST("/user_register", u.UserRegister) @@ -52,7 +52,7 @@ func NewGinRouter(discov discoveryregistry.SvcDiscoveryRegistry, rdb redis.Unive //friend routing group friendRouterGroup := r.Group("/friend") { - f := NewFriend(discov) + f := NewFriendApi(discov) friendRouterGroup.Use(mw.GinParseToken(rdb)) friendRouterGroup.POST("/delete_friend", f.DeleteFriend) //1 friendRouterGroup.POST("/get_friend_apply_list", f.GetFriendApplyList) //1 @@ -67,7 +67,7 @@ func NewGinRouter(discov discoveryregistry.SvcDiscoveryRegistry, rdb redis.Unive friendRouterGroup.POST("/import_friend", f.ImportFriends) //1 friendRouterGroup.POST("/is_friend", f.IsFriend) //1 } - g := NewGroup(discov) + g := NewGroupApi(discov) groupRouterGroup := r.Group("/group") { @@ -105,8 +105,8 @@ func NewGinRouter(discov discoveryregistry.SvcDiscoveryRegistry, rdb redis.Unive ////certificate authRouterGroup := r.Group("/auth") { - a := NewAuth(discov) - u := NewUser(discov) + a := NewAuthApi(discov) + u := NewUserApi(discov) authRouterGroupChild := mw.NewRouterGroup(authRouterGroup, "") authRouterGroupChildToken := mw.NewRouterGroup(authRouterGroup, "", mw.WithGinParseToken(rdb)) authRouterGroupChild.POST("/user_register", u.UserRegister) //1 @@ -117,7 +117,7 @@ func NewGinRouter(discov discoveryregistry.SvcDiscoveryRegistry, rdb redis.Unive ////Third service thirdGroup := r.Group("/third") { - t := NewThird(discov) + t := NewThirdApi(discov) thirdGroup.Use(mw.GinParseToken(rdb)) thirdGroup.POST("/fcm_update_token", t.FcmUpdateToken) thirdGroup.POST("/set_app_badge", t.SetAppBadge) @@ -132,7 +132,7 @@ func NewGinRouter(discov discoveryregistry.SvcDiscoveryRegistry, rdb redis.Unive ////Message msgGroup := r.Group("/msg") { - m := NewMsg(discov) + m := NewMessageApi(discov) msgGroup.Use(mw.GinParseToken(rdb)) msgGroup.POST("/newest_seq", m.GetSeq) msgGroup.POST("/send_msg", m.SendMessage) @@ -160,7 +160,7 @@ func NewGinRouter(discov discoveryregistry.SvcDiscoveryRegistry, rdb redis.Unive ////Conversation conversationGroup := r.Group("/conversation") { - c := NewConversation(discov) + c := NewConversationApi(discov) conversationGroup.Use(mw.GinParseToken(rdb)) conversationGroup.POST("/get_all_conversations", c.GetAllConversations) conversationGroup.POST("/get_conversation", c.GetConversation) @@ -174,7 +174,7 @@ func NewGinRouter(discov discoveryregistry.SvcDiscoveryRegistry, rdb redis.Unive statisticsGroup := r.Group("/statistics") { - s := NewStatistics(discov) + s := NewStatisticsApi(discov) conversationGroup.Use(mw.GinParseToken(rdb)) statisticsGroup.POST("/user_register", s.UserRegister) } diff --git a/internal/api/statistics.go b/internal/api/statistics.go index ea9fded7f..9d2006212 100644 --- a/internal/api/statistics.go +++ b/internal/api/statistics.go @@ -1,35 +1,19 @@ package api import ( - "context" - "github.com/OpenIMSDK/Open-IM-Server/pkg/a2r" - "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/user" + "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" "github.com/gin-gonic/gin" - "google.golang.org/grpc" ) -func NewStatistics(discov discoveryregistry.SvcDiscoveryRegistry) *Statistics { - conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImUserName) - if err != nil { - panic(err) - } - client := user.NewUserClient(conn) - return &Statistics{discov: discov, client: client, conn: conn} +type StatisticsApi rpcclient.User + +func NewStatisticsApi(discov discoveryregistry.SvcDiscoveryRegistry) StatisticsApi { + return StatisticsApi(*rpcclient.NewUser(discov)) } -type Statistics struct { - conn *grpc.ClientConn - client user.UserClient - discov discoveryregistry.SvcDiscoveryRegistry -} - -func (s *Statistics) Client() user.UserClient { - return s.client -} - -func (s *Statistics) UserRegister(c *gin.Context) { +func (s *StatisticsApi) UserRegister(c *gin.Context) { a2r.Call(user.UserClient.UserRegisterCount, s.Client, c) } diff --git a/internal/api/third.go b/internal/api/third.go index e776f704b..b04589503 100644 --- a/internal/api/third.go +++ b/internal/api/third.go @@ -1,66 +1,51 @@ package api import ( - "context" "math/rand" "net/http" "strconv" "github.com/OpenIMSDK/Open-IM-Server/pkg/a2r" - "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/mcontext" "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" "github.com/OpenIMSDK/Open-IM-Server/pkg/errs" "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/third" + "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" "github.com/gin-gonic/gin" - "google.golang.org/grpc" ) -func NewThird(discov discoveryregistry.SvcDiscoveryRegistry) *Third { - conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImThirdName) - if err != nil { - panic(err) - } - client := third.NewThirdClient(conn) - return &Third{discov: discov, client: client, conn: conn} +type ThirdApi rpcclient.Third + +func NewThirdApi(discov discoveryregistry.SvcDiscoveryRegistry) ThirdApi { + return ThirdApi(*rpcclient.NewThird(discov)) } -type Third struct { - conn *grpc.ClientConn - client third.ThirdClient - discov discoveryregistry.SvcDiscoveryRegistry -} - -func (o *Third) Client() third.ThirdClient { - return o.client -} - -func (o *Third) ApplyPut(c *gin.Context) { +func (o *ThirdApi) ApplyPut(c *gin.Context) { a2r.Call(third.ThirdClient.ApplyPut, o.Client, c) } -func (o *Third) GetPut(c *gin.Context) { +func (o *ThirdApi) GetPut(c *gin.Context) { a2r.Call(third.ThirdClient.GetPut, o.Client, c) } -func (o *Third) ConfirmPut(c *gin.Context) { +func (o *ThirdApi) ConfirmPut(c *gin.Context) { a2r.Call(third.ThirdClient.ConfirmPut, o.Client, c) } -func (o *Third) GetHash(c *gin.Context) { +func (o *ThirdApi) GetHash(c *gin.Context) { a2r.Call(third.ThirdClient.GetHashInfo, o.Client, c) } -func (o *Third) FcmUpdateToken(c *gin.Context) { +func (o *ThirdApi) FcmUpdateToken(c *gin.Context) { a2r.Call(third.ThirdClient.FcmUpdateToken, o.Client, c) } -func (o *Third) SetAppBadge(c *gin.Context) { +func (o *ThirdApi) SetAppBadge(c *gin.Context) { a2r.Call(third.ThirdClient.SetAppBadge, o.Client, c) } -func (o *Third) GetURL(c *gin.Context) { +func (o *ThirdApi) GetURL(c *gin.Context) { if c.Request.Method == http.MethodPost { a2r.Call(third.ThirdClient.GetUrl, o.Client, c) return @@ -80,7 +65,7 @@ func (o *Third) GetURL(c *gin.Context) { } attachment, _ := strconv.ParseBool(c.Query("attachment")) c.Set(constant.OperationID, operationID) - resp, err := o.client.GetUrl(mcontext.SetOperationID(c, operationID), &third.GetUrlReq{Name: name, Expires: expires, Attachment: attachment}) + resp, err := o.Client.GetUrl(mcontext.SetOperationID(c, operationID), &third.GetUrlReq{Name: name, Expires: expires, Attachment: attachment}) if err != nil { if errs.ErrArgs.Is(err) { c.String(http.StatusBadRequest, err.Error()) diff --git a/internal/api/user.go b/internal/api/user.go index 8b387ff07..ba3b6f59f 100644 --- a/internal/api/user.go +++ b/internal/api/user.go @@ -1,68 +1,52 @@ package api import ( - "context" - "github.com/OpenIMSDK/Open-IM-Server/pkg/a2r" "github.com/OpenIMSDK/Open-IM-Server/pkg/apiresp" "github.com/OpenIMSDK/Open-IM-Server/pkg/apistruct" - "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/tokenverify" "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" "github.com/OpenIMSDK/Open-IM-Server/pkg/errs" "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/user" + "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" "github.com/gin-gonic/gin" - "google.golang.org/grpc" ) -func NewUser(discov discoveryregistry.SvcDiscoveryRegistry) *User { - conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImUserName) - if err != nil { - panic(err) - } - client := user.NewUserClient(conn) - return &User{discov: discov, client: client, conn: conn} +type UserApi rpcclient.User + +func NewUserApi(discov discoveryregistry.SvcDiscoveryRegistry) UserApi { + return UserApi(*rpcclient.NewUser(discov)) } -type User struct { - conn *grpc.ClientConn - client user.UserClient - discov discoveryregistry.SvcDiscoveryRegistry -} - -func (s *User) Client() user.UserClient { - return s.client -} - -func (u *User) UserRegister(c *gin.Context) { +func (u *UserApi) UserRegister(c *gin.Context) { a2r.Call(user.UserClient.UserRegister, u.Client, c) } -func (u *User) UpdateUserInfo(c *gin.Context) { +func (u *UserApi) UpdateUserInfo(c *gin.Context) { a2r.Call(user.UserClient.UpdateUserInfo, u.Client, c) } -func (u *User) SetGlobalRecvMessageOpt(c *gin.Context) { +func (u *UserApi) SetGlobalRecvMessageOpt(c *gin.Context) { a2r.Call(user.UserClient.SetGlobalRecvMessageOpt, u.Client, c) } -func (u *User) GetUsersPublicInfo(c *gin.Context) { +func (u *UserApi) GetUsersPublicInfo(c *gin.Context) { a2r.Call(user.UserClient.GetDesignateUsers, u.Client, c) } -func (u *User) GetAllUsersID(c *gin.Context) { +func (u *UserApi) GetAllUsersID(c *gin.Context) { a2r.Call(user.UserClient.GetDesignateUsers, u.Client, c) } -func (u *User) AccountCheck(c *gin.Context) { +func (u *UserApi) AccountCheck(c *gin.Context) { a2r.Call(user.UserClient.AccountCheck, u.Client, c) } -func (u *User) GetUsers(c *gin.Context) { +func (u *UserApi) GetUsers(c *gin.Context) { a2r.Call(user.UserClient.GetPaginationUsers, u.Client, c) } -func (u *User) GetUsersOnlineStatus(c *gin.Context) { +func (u *UserApi) GetUsersOnlineStatus(c *gin.Context) { params := apistruct.ManagementSendMsgReq{} if err := c.BindJSON(¶ms); err != nil { apiresp.GinError(c, errs.ErrArgs.WithDetail(err.Error()).Wrap()) diff --git a/internal/msggateway/message_handler.go b/internal/msggateway/message_handler.go index f049a1973..d931f7c3f 100644 --- a/internal/msggateway/message_handler.go +++ b/internal/msggateway/message_handler.go @@ -52,13 +52,14 @@ type MessageHandler interface { var _ MessageHandler = (*GrpcHandler)(nil) type GrpcHandler struct { - msgRpcClient *rpcclient.MsgClient + msgRpcClient *rpcclient.MessageRpcClient pushClient *rpcclient.PushClient validate *validator.Validate } func NewGrpcHandler(validate *validator.Validate, client discoveryregistry.SvcDiscoveryRegistry) *GrpcHandler { - return &GrpcHandler{msgRpcClient: rpcclient.NewMsgClient(client), + msgRpcClient := rpcclient.NewMessageRpcClient(client) + return &GrpcHandler{msgRpcClient: &msgRpcClient, pushClient: rpcclient.NewPushClient(client), validate: validate} } diff --git a/internal/msgtransfer/init.go b/internal/msgtransfer/init.go index debe529f3..40bbcfaa2 100644 --- a/internal/msgtransfer/init.go +++ b/internal/msgtransfer/init.go @@ -61,16 +61,16 @@ func StartTransfer(prometheusPort int) error { chatLogDatabase := controller.NewChatLogDatabase(relation.NewChatLogGorm(db)) extendMsgDatabase := controller.NewExtendMsgDatabase(extendMsgModel, extendMsgCache, tx.NewMongo(mongo.GetClient())) msgDatabase := controller.NewCommonMsgDatabase(msgDocModel, msgModel) - conversationRpcClient := rpcclient.NewConversationClient(client) - groupRpcClient := rpcclient.NewGroupClient(client) - msgTransfer := NewMsgTransfer(chatLogDatabase, extendMsgDatabase, msgDatabase, conversationRpcClient, groupRpcClient) + conversationRpcClient := rpcclient.NewConversationRpcClient(client) + groupRpcClient := rpcclient.NewGroupRpcClient(client) + msgTransfer := NewMsgTransfer(chatLogDatabase, extendMsgDatabase, msgDatabase, &conversationRpcClient, &groupRpcClient) msgTransfer.initPrometheus() return msgTransfer.Start(prometheusPort) } func NewMsgTransfer(chatLogDatabase controller.ChatLogDatabase, extendMsgDatabase controller.ExtendMsgDatabase, msgDatabase controller.CommonMsgDatabase, - conversationRpcClient *rpcclient.ConversationClient, groupRpcClient *rpcclient.GroupClient) *MsgTransfer { + conversationRpcClient *rpcclient.ConversationRpcClient, groupRpcClient *rpcclient.GroupRpcClient) *MsgTransfer { return &MsgTransfer{persistentCH: NewPersistentConsumerHandler(chatLogDatabase), historyCH: NewOnlineHistoryRedisConsumerHandler(msgDatabase, conversationRpcClient, groupRpcClient), historyMongoCH: NewOnlineHistoryMongoConsumerHandler(msgDatabase), modifyCH: NewModifyMsgConsumerHandler(extendMsgDatabase)} } diff --git a/internal/msgtransfer/online_history_msg_handler.go b/internal/msgtransfer/online_history_msg_handler.go index 468371512..94facfef3 100644 --- a/internal/msgtransfer/online_history_msg_handler.go +++ b/internal/msgtransfer/online_history_msg_handler.go @@ -59,11 +59,11 @@ type OnlineHistoryRedisConsumerHandler struct { singleMsgFailedCountMutex sync.Mutex msgDatabase controller.CommonMsgDatabase - conversationRpcClient *rpcclient.ConversationClient - groupRpcClient *rpcclient.GroupClient + conversationRpcClient *rpcclient.ConversationRpcClient + groupRpcClient *rpcclient.GroupRpcClient } -func NewOnlineHistoryRedisConsumerHandler(database controller.CommonMsgDatabase, conversationRpcClient *rpcclient.ConversationClient, groupRpcClient *rpcclient.GroupClient) *OnlineHistoryRedisConsumerHandler { +func NewOnlineHistoryRedisConsumerHandler(database controller.CommonMsgDatabase, conversationRpcClient *rpcclient.ConversationRpcClient, groupRpcClient *rpcclient.GroupRpcClient) *OnlineHistoryRedisConsumerHandler { var och OnlineHistoryRedisConsumerHandler och.msgDatabase = database och.msgDistributionCh = make(chan Cmd2Value) //no buffer channel diff --git a/internal/push/push_to_client.go b/internal/push/push_to_client.go index c1f6b2b55..3df748792 100644 --- a/internal/push/push_to_client.go +++ b/internal/push/push_to_client.go @@ -31,8 +31,8 @@ type Pusher struct { offlinePusher offlinepush.OfflinePusher groupLocalCache *localcache.GroupLocalCache conversationLocalCache *localcache.ConversationLocalCache - msgClient *rpcclient.MsgClient - conversationClient *rpcclient.ConversationClient + msgClient *rpcclient.MessageRpcClient + conversationClient *rpcclient.ConversationRpcClient successCount int } @@ -40,15 +40,17 @@ var errNoOfflinePusher = errors.New("no offlinePusher is configured") func NewPusher(client discoveryregistry.SvcDiscoveryRegistry, offlinePusher offlinepush.OfflinePusher, database controller.PushDatabase, groupLocalCache *localcache.GroupLocalCache, conversationLocalCache *localcache.ConversationLocalCache) *Pusher { - rpcclient.NewGroupClient(client) + + msgClient := rpcclient.NewMessageRpcClient(client) + conversationClient := rpcclient.NewConversationRpcClient(client) return &Pusher{ database: database, client: client, offlinePusher: offlinePusher, groupLocalCache: groupLocalCache, conversationLocalCache: conversationLocalCache, - msgClient: rpcclient.NewMsgClient(client), - conversationClient: rpcclient.NewConversationClient(client), + msgClient: &msgClient, + conversationClient: &conversationClient, } } diff --git a/internal/rpc/auth/auth.go b/internal/rpc/auth/auth.go index b2ff3fea2..c4e4877bf 100644 --- a/internal/rpc/auth/auth.go +++ b/internal/rpc/auth/auth.go @@ -20,7 +20,7 @@ import ( type authServer struct { authDatabase controller.AuthDatabase - userRpcClient *rpcclient.UserClient + userRpcClient *rpcclient.UserRpcClient RegisterCenter discoveryregistry.SvcDiscoveryRegistry } @@ -29,8 +29,9 @@ func Start(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) e if err != nil { return err } + userRpcClient := rpcclient.NewUserRpcClient(client) pbAuth.RegisterAuthServer(server, &authServer{ - userRpcClient: rpcclient.NewUserClient(client), + userRpcClient: &userRpcClient, RegisterCenter: client, authDatabase: controller.NewAuthDatabase(cache.NewMsgCacheModel(rdb), config.Config.TokenPolicy.AccessSecret, config.Config.TokenPolicy.AccessExpire), }) diff --git a/internal/rpc/conversation/conversaion.go b/internal/rpc/conversation/conversaion.go index 7d7b512a1..9af5baad8 100644 --- a/internal/rpc/conversation/conversaion.go +++ b/internal/rpc/conversation/conversaion.go @@ -21,10 +21,10 @@ import ( ) type conversationServer struct { - groupRpcClient *rpcclient.GroupClient + groupRpcClient *rpcclient.GroupRpcClient conversationDatabase controller.ConversationDatabase conversationNotificationSender *notification.ConversationNotificationSender - msgRpcClient *rpcclient.MsgClient + msgRpcClient *rpcclient.MessageRpcClient } func Start(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) error { @@ -40,10 +40,12 @@ func Start(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) e return err } conversationDB := relation.NewConversationGorm(db) + groupRpcClient := rpcclient.NewGroupRpcClient(client) + msgRpcClient := rpcclient.NewMessageRpcClient(client) pbConversation.RegisterConversationServer(server, &conversationServer{ conversationNotificationSender: notification.NewConversationNotificationSender(client), - groupRpcClient: rpcclient.NewGroupClient(client), - msgRpcClient: rpcclient.NewMsgClient(client), + groupRpcClient: &groupRpcClient, + msgRpcClient: &msgRpcClient, conversationDatabase: controller.NewConversationDatabase(conversationDB, cache.NewConversationRedis(rdb, cache.GetDefaultOpt(), conversationDB), tx.NewGorm(db)), }) return nil diff --git a/internal/rpc/friend/friend.go b/internal/rpc/friend/friend.go index 8fb5698f0..9c830b7fe 100644 --- a/internal/rpc/friend/friend.go +++ b/internal/rpc/friend/friend.go @@ -25,7 +25,7 @@ import ( type friendServer struct { friendDatabase controller.FriendDatabase blackDatabase controller.BlackDatabase - userRpcClient *rpcclient.UserClient + userRpcClient *rpcclient.UserRpcClient notificationSender *notification.FriendNotificationSender RegisterCenter registry.SvcDiscoveryRegistry } @@ -44,12 +44,12 @@ func Start(client registry.SvcDiscoveryRegistry, server *grpc.Server) error { } blackDB := relation.NewBlackGorm(db) friendDB := relation.NewFriendGorm(db) - userRpcClient := rpcclient.NewUserClient(client) + userRpcClient := rpcclient.NewUserRpcClient(client) notificationSender := notification.NewFriendNotificationSender(client, notification.WithRpcFunc(userRpcClient.GetUsersInfo)) pbfriend.RegisterFriendServer(server, &friendServer{ friendDatabase: controller.NewFriendDatabase(friendDB, relation.NewFriendRequestGorm(db), cache.NewFriendCacheRedis(rdb, friendDB, cache.GetDefaultOpt()), tx.NewGorm(db)), blackDatabase: controller.NewBlackDatabase(blackDB, cache.NewBlackCacheRedis(rdb, blackDB, cache.GetDefaultOpt())), - userRpcClient: userRpcClient, + userRpcClient: &userRpcClient, notificationSender: notificationSender, RegisterCenter: client, }) diff --git a/internal/rpc/msg/server.go b/internal/rpc/msg/server.go index c2b30191e..40e494fb1 100644 --- a/internal/rpc/msg/server.go +++ b/internal/rpc/msg/server.go @@ -22,11 +22,10 @@ type msgServer struct { RegisterCenter discoveryregistry.SvcDiscoveryRegistry MsgDatabase controller.CommonMsgDatabase ExtendMsgDatabase controller.ExtendMsgDatabase - Group *rpcclient.GroupClient - User *rpcclient.UserClient - Conversation *rpcclient.ConversationClient - friend *rpcclient.FriendClient - black *rpcclient.BlackClient + Group *rpcclient.GroupRpcClient + User *rpcclient.UserRpcClient + Conversation *rpcclient.ConversationRpcClient + friend *rpcclient.FriendRpcClient GroupLocalCache *localcache.GroupLocalCache ConversationLocalCache *localcache.ConversationLocalCache MessageLocker MessageLocker @@ -67,17 +66,20 @@ func Start(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) e extendMsgCacheModel := cache.NewExtendMsgSetCacheRedis(rdb, extendMsgModel, cache.GetDefaultOpt()) extendMsgDatabase := controller.NewExtendMsgDatabase(extendMsgModel, extendMsgCacheModel, tx.NewMongo(mongo.GetClient())) msgDatabase := controller.NewCommonMsgDatabase(msgDocModel, cacheModel) + conversationClient := rpcclient.NewConversationRpcClient(client) + userRpcClient := rpcclient.NewUserRpcClient(client) + groupRpcClient := rpcclient.NewGroupRpcClient(client) + friendRpcClient := rpcclient.NewFriendRpcClient(client) s := &msgServer{ - Conversation: rpcclient.NewConversationClient(client), - User: rpcclient.NewUserClient(client), - Group: rpcclient.NewGroupClient(client), + Conversation: &conversationClient, + User: &userRpcClient, + Group: &groupRpcClient, MsgDatabase: msgDatabase, ExtendMsgDatabase: extendMsgDatabase, RegisterCenter: client, GroupLocalCache: localcache.NewGroupLocalCache(client), ConversationLocalCache: localcache.NewConversationLocalCache(client), - black: rpcclient.NewBlackClient(client), - friend: rpcclient.NewFriendClient(client), + friend: &friendRpcClient, MessageLocker: NewLockerMessage(cacheModel), } s.notificationSender = rpcclient.NewNotificationSender(rpcclient.WithLocalSendMsg(s.SendMsg)) diff --git a/internal/rpc/msg/verify.go b/internal/rpc/msg/verify.go index f6903f9f9..d0ebc7baa 100644 --- a/internal/rpc/msg/verify.go +++ b/internal/rpc/msg/verify.go @@ -44,7 +44,7 @@ func (m *msgServer) messageVerification(ctx context.Context, data *msg.SendMsgRe if data.MsgData.ContentType <= constant.NotificationEnd && data.MsgData.ContentType >= constant.NotificationBegin { return nil } - black, err := m.black.IsBlocked(ctx, data.MsgData.SendID, data.MsgData.RecvID) + black, err := m.friend.IsBlocked(ctx, data.MsgData.SendID, data.MsgData.RecvID) if err != nil { return err } diff --git a/internal/rpc/user/user.go b/internal/rpc/user/user.go index 0d717cfd4..bc7be1f47 100644 --- a/internal/rpc/user/user.go +++ b/internal/rpc/user/user.go @@ -28,7 +28,7 @@ import ( type userServer struct { controller.UserDatabase notificationSender *notification.FriendNotificationSender - friendRpcClient *rpcclient.FriendClient + friendRpcClient *rpcclient.FriendRpcClient RegisterCenter registry.SvcDiscoveryRegistry } @@ -54,10 +54,11 @@ func Start(client registry.SvcDiscoveryRegistry, server *grpc.Server) error { userDB := relation.NewUserGorm(db) cache := cache.NewUserCacheRedis(rdb, userDB, cache.GetDefaultOpt()) database := controller.NewUserDatabase(userDB, cache, tx.NewGorm(db)) + friendRpcClient := rpcclient.NewFriendRpcClient(client) u := &userServer{ UserDatabase: database, RegisterCenter: client, - friendRpcClient: rpcclient.NewFriendClient(client), + friendRpcClient: &friendRpcClient, notificationSender: notification.NewFriendNotificationSender(client, notification.WithDBFunc(database.FindWithError)), } pbuser.RegisterUserServer(server, u) diff --git a/pkg/a2r/api2rpc.go b/pkg/a2r/api2rpc.go index 13498f4f9..bb1cb1fac 100644 --- a/pkg/a2r/api2rpc.go +++ b/pkg/a2r/api2rpc.go @@ -12,7 +12,7 @@ import ( func Call[A, B, C any]( rpc func(client C, ctx context.Context, req *A, options ...grpc.CallOption) (*B, error), - client func() C, + client C, c *gin.Context, ) { var req A @@ -28,10 +28,7 @@ func Call[A, B, C any]( return } } - log.ZDebug(c, "gin bind json success", "req", req) - cli := client() - log.ZDebug(c, "get conn success", "req", req) - data, err := rpc(cli, c, &req) + data, err := rpc(client, c, &req) if err != nil { apiresp.GinError(c, err) // RPC调用失败 return diff --git a/pkg/rpcclient/auth.go b/pkg/rpcclient/auth.go new file mode 100644 index 000000000..fe6d8da4b --- /dev/null +++ b/pkg/rpcclient/auth.go @@ -0,0 +1,25 @@ +package rpcclient + +import ( + "context" + + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" + "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" + "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/auth" + "google.golang.org/grpc" +) + +func NewAuth(discov discoveryregistry.SvcDiscoveryRegistry) *Auth { + conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImAuthName) + if err != nil { + panic(err) + } + client := auth.NewAuthClient(conn) + return &Auth{discov: discov, conn: conn, Client: client} +} + +type Auth struct { + conn *grpc.ClientConn + Client auth.AuthClient + discov discoveryregistry.SvcDiscoveryRegistry +} diff --git a/pkg/rpcclient/black.go b/pkg/rpcclient/black.go deleted file mode 100644 index 161adb40c..000000000 --- a/pkg/rpcclient/black.go +++ /dev/null @@ -1,30 +0,0 @@ -package rpcclient - -import ( - "context" - - "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" - discoveryRegistry "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" - "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/friend" -) - -type BlackClient struct { - *MetaClient -} - -func NewBlackClient(zk discoveryRegistry.SvcDiscoveryRegistry) *BlackClient { - return &BlackClient{NewMetaClient(zk, config.Config.RpcRegisterName.OpenImFriendName)} -} - -// possibleBlackUserID是否被userID拉黑,也就是是否在userID的黑名单中 -func (b *BlackClient) IsBlocked(ctx context.Context, possibleBlackUserID, userID string) (bool, error) { - cc, err := b.getConn(ctx) - if err != nil { - return false, err - } - r, err := friend.NewFriendClient(cc).IsBlack(ctx, &friend.IsBlackReq{UserID1: possibleBlackUserID, UserID2: userID}) - if err != nil { - return false, err - } - return r.InUser2Blacks, nil -} diff --git a/pkg/rpcclient/conversation.go b/pkg/rpcclient/conversation.go index c0522f492..a41565558 100644 --- a/pkg/rpcclient/conversation.go +++ b/pkg/rpcclient/conversation.go @@ -5,100 +5,83 @@ import ( "fmt" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" - discoveryRegistry "github.com/OpenIMSDK/Open-IM-Server/pkg/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/conversation" pbConversation "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/conversation" + "google.golang.org/grpc" ) -type ConversationClient struct { - *MetaClient +type Conversation struct { + Client conversation.ConversationClient + conn *grpc.ClientConn + discov discoveryregistry.SvcDiscoveryRegistry } -func NewConversationClient(zk discoveryRegistry.SvcDiscoveryRegistry) *ConversationClient { - return &ConversationClient{NewMetaClient(zk, config.Config.RpcRegisterName.OpenImConversationName)} -} - -func (c *ConversationClient) ModifyConversationField(ctx context.Context, req *pbConversation.ModifyConversationFieldReq) error { - cc, err := c.getConn(ctx) +func NewConversation(discov discoveryregistry.SvcDiscoveryRegistry) *Conversation { + conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImConversationName) if err != nil { - return err + panic(err) } - _, err = pbConversation.NewConversationClient(cc).ModifyConversationField(ctx, req) + client := conversation.NewConversationClient(conn) + return &Conversation{discov: discov, conn: conn, Client: client} +} + +type ConversationRpcClient Conversation + +func NewConversationRpcClient(discov discoveryregistry.SvcDiscoveryRegistry) ConversationRpcClient { + return ConversationRpcClient(*NewConversation(discov)) +} + +func (c *ConversationRpcClient) ModifyConversationField(ctx context.Context, req *pbConversation.ModifyConversationFieldReq) error { + _, err := c.Client.ModifyConversationField(ctx, req) return err } -func (c *ConversationClient) GetSingleConversationRecvMsgOpt(ctx context.Context, userID, conversationID string) (int32, error) { - cc, err := c.getConn(ctx) - if err != nil { - return 0, err - } +func (c *ConversationRpcClient) GetSingleConversationRecvMsgOpt(ctx context.Context, userID, conversationID string) (int32, error) { var req pbConversation.GetConversationReq req.OwnerUserID = userID req.ConversationID = conversationID - conversation, err := pbConversation.NewConversationClient(cc).GetConversation(ctx, &req) + conversation, err := c.Client.GetConversation(ctx, &req) if err != nil { return 0, err } return conversation.GetConversation().RecvMsgOpt, err } -func (c *ConversationClient) SingleChatFirstCreateConversation(ctx context.Context, recvID, sendID string) error { - cc, err := c.getConn(ctx) - if err != nil { - return err - } - _, err = pbConversation.NewConversationClient(cc).CreateSingleChatConversations(ctx, &pbConversation.CreateSingleChatConversationsReq{RecvID: recvID, SendID: sendID}) +func (c *ConversationRpcClient) SingleChatFirstCreateConversation(ctx context.Context, recvID, sendID string) error { + _, err := c.Client.CreateSingleChatConversations(ctx, &pbConversation.CreateSingleChatConversationsReq{RecvID: recvID, SendID: sendID}) return err } -func (c *ConversationClient) GroupChatFirstCreateConversation(ctx context.Context, groupID string, userIDs []string) error { - cc, err := c.getConn(ctx) - if err != nil { - return err - } - _, err = pbConversation.NewConversationClient(cc).CreateGroupChatConversations(ctx, &pbConversation.CreateGroupChatConversationsReq{UserIDs: userIDs, GroupID: groupID}) +func (c *ConversationRpcClient) GroupChatFirstCreateConversation(ctx context.Context, groupID string, userIDs []string) error { + _, err := c.Client.CreateGroupChatConversations(ctx, &pbConversation.CreateGroupChatConversationsReq{UserIDs: userIDs, GroupID: groupID}) return err } -func (c *ConversationClient) SetConversationMaxSeq(ctx context.Context, ownerUserIDs []string, conversationID string, maxSeq int64) error { - cc, err := c.getConn(ctx) - if err != nil { - return err - } - _, err = pbConversation.NewConversationClient(cc).SetConversationMaxSeq(ctx, &pbConversation.SetConversationMaxSeqReq{OwnerUserID: ownerUserIDs, ConversationID: conversationID, MaxSeq: maxSeq}) +func (c *ConversationRpcClient) SetConversationMaxSeq(ctx context.Context, ownerUserIDs []string, conversationID string, maxSeq int64) error { + _, err := c.Client.SetConversationMaxSeq(ctx, &pbConversation.SetConversationMaxSeqReq{OwnerUserID: ownerUserIDs, ConversationID: conversationID, MaxSeq: maxSeq}) return err } -func (c *ConversationClient) GetConversationIDs(ctx context.Context, ownerUserID string) ([]string, error) { - cc, err := c.getConn(ctx) - if err != nil { - return nil, err - } - resp, err := pbConversation.NewConversationClient(cc).GetConversationIDs(ctx, &pbConversation.GetConversationIDsReq{UserID: ownerUserID}) +func (c *ConversationRpcClient) GetConversationIDs(ctx context.Context, ownerUserID string) ([]string, error) { + resp, err := c.Client.GetConversationIDs(ctx, &pbConversation.GetConversationIDsReq{UserID: ownerUserID}) if err != nil { return nil, err } return resp.ConversationIDs, nil } -func (c *ConversationClient) GetConversation(ctx context.Context, ownerUserID, conversationID string) (*pbConversation.Conversation, error) { - cc, err := c.getConn(ctx) - if err != nil { - return nil, err - } - resp, err := pbConversation.NewConversationClient(cc).GetConversation(ctx, &pbConversation.GetConversationReq{OwnerUserID: ownerUserID, ConversationID: conversationID}) +func (c *ConversationRpcClient) GetConversation(ctx context.Context, ownerUserID, conversationID string) (*pbConversation.Conversation, error) { + resp, err := c.Client.GetConversation(ctx, &pbConversation.GetConversationReq{OwnerUserID: ownerUserID, ConversationID: conversationID}) if err != nil { return nil, err } return resp.Conversation, nil } -func (c *ConversationClient) GetConversationsByConversationID(ctx context.Context, conversationIDs []string) ([]*pbConversation.Conversation, error) { - cc, err := c.getConn(ctx) - if err != nil { - return nil, err - } - resp, err := pbConversation.NewConversationClient(cc).GetConversationsByConversationID(ctx, &pbConversation.GetConversationsByConversationIDReq{ConversationIDs: conversationIDs}) +func (c *ConversationRpcClient) GetConversationsByConversationID(ctx context.Context, conversationIDs []string) ([]*pbConversation.Conversation, error) { + resp, err := c.Client.GetConversationsByConversationID(ctx, &pbConversation.GetConversationsByConversationIDReq{ConversationIDs: conversationIDs}) if err != nil { return nil, err } @@ -108,12 +91,8 @@ func (c *ConversationClient) GetConversationsByConversationID(ctx context.Contex return resp.Conversations, nil } -func (c *ConversationClient) GetConversations(ctx context.Context, ownerUserID string, conversationIDs []string) ([]*pbConversation.Conversation, error) { - cc, err := c.getConn(ctx) - if err != nil { - return nil, err - } - resp, err := pbConversation.NewConversationClient(cc).GetConversations(ctx, &pbConversation.GetConversationsReq{OwnerUserID: ownerUserID, ConversationIDs: conversationIDs}) +func (c *ConversationRpcClient) GetConversations(ctx context.Context, ownerUserID string, conversationIDs []string) ([]*pbConversation.Conversation, error) { + resp, err := c.Client.GetConversations(ctx, &pbConversation.GetConversationsReq{OwnerUserID: ownerUserID, ConversationIDs: conversationIDs}) if err != nil { return nil, err } diff --git a/pkg/rpcclient/friend.go b/pkg/rpcclient/friend.go index e4fe1b306..299c93b00 100644 --- a/pkg/rpcclient/friend.go +++ b/pkg/rpcclient/friend.go @@ -4,25 +4,35 @@ import ( "context" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" - discoveryRegistry "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" + "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/friend" sdkws "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws" + "google.golang.org/grpc" ) -type FriendClient struct { - *MetaClient +type Friend struct { + conn *grpc.ClientConn + Client friend.FriendClient + discov discoveryregistry.SvcDiscoveryRegistry } -func NewFriendClient(zk discoveryRegistry.SvcDiscoveryRegistry) *FriendClient { - return &FriendClient{NewMetaClient(zk, config.Config.RpcRegisterName.OpenImFriendName)} -} - -func (f *FriendClient) GetFriendsInfo(ctx context.Context, ownerUserID, friendUserID string) (resp *sdkws.FriendInfo, err error) { - cc, err := f.getConn(ctx) +func NewFriend(discov discoveryregistry.SvcDiscoveryRegistry) *Friend { + conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImFriendName) if err != nil { - return nil, err + panic(err) } - r, err := friend.NewFriendClient(cc).GetDesignatedFriends(ctx, &friend.GetDesignatedFriendsReq{OwnerUserID: ownerUserID, FriendUserIDs: []string{friendUserID}}) + client := friend.NewFriendClient(conn) + return &Friend{discov: discov, conn: conn, Client: client} +} + +type FriendRpcClient Friend + +func NewFriendRpcClient(discov discoveryregistry.SvcDiscoveryRegistry) FriendRpcClient { + return FriendRpcClient(*NewFriend(discov)) +} + +func (f *FriendRpcClient) GetFriendsInfo(ctx context.Context, ownerUserID, friendUserID string) (resp *sdkws.FriendInfo, err error) { + r, err := f.Client.GetDesignatedFriends(ctx, &friend.GetDesignatedFriendsReq{OwnerUserID: ownerUserID, FriendUserIDs: []string{friendUserID}}) if err != nil { return nil, err } @@ -31,12 +41,8 @@ func (f *FriendClient) GetFriendsInfo(ctx context.Context, ownerUserID, friendUs } // possibleFriendUserID是否在userID的好友中 -func (f *FriendClient) IsFriend(ctx context.Context, possibleFriendUserID, userID string) (bool, error) { - cc, err := f.getConn(ctx) - if err != nil { - return false, err - } - resp, err := friend.NewFriendClient(cc).IsFriend(ctx, &friend.IsFriendReq{UserID1: userID, UserID2: possibleFriendUserID}) +func (f *FriendRpcClient) IsFriend(ctx context.Context, possibleFriendUserID, userID string) (bool, error) { + resp, err := f.Client.IsFriend(ctx, &friend.IsFriendReq{UserID1: userID, UserID2: possibleFriendUserID}) if err != nil { return false, err } @@ -44,15 +50,19 @@ func (f *FriendClient) IsFriend(ctx context.Context, possibleFriendUserID, userI } -func (f *FriendClient) GetFriendIDs(ctx context.Context, ownerUserID string) (friendIDs []string, err error) { - cc, err := f.getConn(ctx) - if err != nil { - return nil, err - } +func (f *FriendRpcClient) GetFriendIDs(ctx context.Context, ownerUserID string) (friendIDs []string, err error) { req := friend.GetFriendIDsReq{UserID: ownerUserID} - resp, err := friend.NewFriendClient(cc).GetFriendIDs(ctx, &req) + resp, err := f.Client.GetFriendIDs(ctx, &req) if err != nil { return nil, err } return resp.FriendIDs, nil } + +func (b *FriendRpcClient) IsBlocked(ctx context.Context, possibleBlackUserID, userID string) (bool, error) { + r, err := b.Client.IsBlack(ctx, &friend.IsBlackReq{UserID1: possibleBlackUserID, UserID2: userID}) + if err != nil { + return false, err + } + return r.InUser2Blacks, nil +} diff --git a/pkg/rpcclient/group.go b/pkg/rpcclient/group.go index bd45e993e..f1a6fab73 100644 --- a/pkg/rpcclient/group.go +++ b/pkg/rpcclient/group.go @@ -11,27 +11,32 @@ import ( "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/group" "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws" "github.com/OpenIMSDK/Open-IM-Server/pkg/utils" + "google.golang.org/grpc" ) -type GroupClient struct { - MetaClient +type Group struct { + conn *grpc.ClientConn + Client group.GroupClient + discov discoveryregistry.SvcDiscoveryRegistry } -func NewGroupClient(client discoveryregistry.SvcDiscoveryRegistry) *GroupClient { - return &GroupClient{ - MetaClient: MetaClient{ - client: client, - rpcRegisterName: config.Config.RpcRegisterName.OpenImGroupName, - }, - } -} - -func (g *GroupClient) GetGroupInfos(ctx context.Context, groupIDs []string, complete bool) ([]*sdkws.GroupInfo, error) { - cc, err := g.getConn(ctx) +func NewGroup(discov discoveryregistry.SvcDiscoveryRegistry) *Group { + conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImGroupName) if err != nil { - return nil, err + panic(err) } - resp, err := group.NewGroupClient(cc).GetGroupsInfo(ctx, &group.GetGroupsInfoReq{ + client := group.NewGroupClient(conn) + return &Group{discov: discov, conn: conn, Client: client} +} + +type GroupRpcClient Group + +func NewGroupRpcClient(discov discoveryregistry.SvcDiscoveryRegistry) GroupRpcClient { + return GroupRpcClient(*NewGroup(discov)) +} + +func (g *GroupRpcClient) GetGroupInfos(ctx context.Context, groupIDs []string, complete bool) ([]*sdkws.GroupInfo, error) { + resp, err := g.Client.GetGroupsInfo(ctx, &group.GetGroupsInfoReq{ GroupIDs: groupIDs, }) if err != nil { @@ -47,7 +52,7 @@ func (g *GroupClient) GetGroupInfos(ctx context.Context, groupIDs []string, comp return resp.GroupInfos, nil } -func (g *GroupClient) GetGroupInfo(ctx context.Context, groupID string) (*sdkws.GroupInfo, error) { +func (g *GroupRpcClient) GetGroupInfo(ctx context.Context, groupID string) (*sdkws.GroupInfo, error) { groups, err := g.GetGroupInfos(ctx, []string{groupID}, true) if err != nil { return nil, err @@ -55,7 +60,7 @@ func (g *GroupClient) GetGroupInfo(ctx context.Context, groupID string) (*sdkws. return groups[0], nil } -func (g *GroupClient) GetGroupInfoMap(ctx context.Context, groupIDs []string, complete bool) (map[string]*sdkws.GroupInfo, error) { +func (g *GroupRpcClient) 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 @@ -65,12 +70,8 @@ func (g *GroupClient) GetGroupInfoMap(ctx context.Context, groupIDs []string, co }), nil } -func (g *GroupClient) GetGroupMemberInfos(ctx context.Context, groupID string, userIDs []string, complete bool) ([]*sdkws.GroupMemberFullInfo, error) { - cc, err := g.getConn(ctx) - if err != nil { - return nil, err - } - resp, err := group.NewGroupClient(cc).GetGroupMembersInfo(ctx, &group.GetGroupMembersInfoReq{ +func (g *GroupRpcClient) GetGroupMemberInfos(ctx context.Context, groupID string, userIDs []string, complete bool) ([]*sdkws.GroupMemberFullInfo, error) { + resp, err := g.Client.GetGroupMembersInfo(ctx, &group.GetGroupMembersInfoReq{ GroupID: groupID, UserIDs: userIDs, }) @@ -87,7 +88,7 @@ func (g *GroupClient) GetGroupMemberInfos(ctx context.Context, groupID string, u return resp.Members, nil } -func (g *GroupClient) GetGroupMemberInfo(ctx context.Context, groupID string, userID string) (*sdkws.GroupMemberFullInfo, error) { +func (g *GroupRpcClient) 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 @@ -95,7 +96,7 @@ func (g *GroupClient) GetGroupMemberInfo(ctx context.Context, groupID string, us return members[0], nil } -func (g *GroupClient) GetGroupMemberInfoMap(ctx context.Context, groupID string, userIDs []string, complete bool) (map[string]*sdkws.GroupMemberFullInfo, error) { +func (g *GroupRpcClient) 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 @@ -105,12 +106,8 @@ func (g *GroupClient) GetGroupMemberInfoMap(ctx context.Context, groupID string, }), nil } -func (g *GroupClient) GetOwnerAndAdminInfos(ctx context.Context, groupID string) ([]*sdkws.GroupMemberFullInfo, error) { - cc, err := g.getConn(ctx) - if err != nil { - return nil, err - } - resp, err := group.NewGroupClient(cc).GetGroupMemberRoleLevel(ctx, &group.GetGroupMemberRoleLevelReq{ +func (g *GroupRpcClient) GetOwnerAndAdminInfos(ctx context.Context, groupID string) ([]*sdkws.GroupMemberFullInfo, error) { + resp, err := g.Client.GetGroupMemberRoleLevel(ctx, &group.GetGroupMemberRoleLevelReq{ GroupID: groupID, RoleLevels: []int32{constant.GroupOwner, constant.GroupAdmin}, }) @@ -120,24 +117,16 @@ func (g *GroupClient) GetOwnerAndAdminInfos(ctx context.Context, groupID string) return resp.Members, nil } -func (g *GroupClient) GetOwnerInfo(ctx context.Context, groupID string) (*sdkws.GroupMemberFullInfo, error) { - cc, err := g.getConn(ctx) - if err != nil { - return nil, err - } - resp, err := group.NewGroupClient(cc).GetGroupMemberRoleLevel(ctx, &group.GetGroupMemberRoleLevelReq{ +func (g *GroupRpcClient) GetOwnerInfo(ctx context.Context, groupID string) (*sdkws.GroupMemberFullInfo, error) { + resp, err := g.Client.GetGroupMemberRoleLevel(ctx, &group.GetGroupMemberRoleLevelReq{ GroupID: groupID, RoleLevels: []int32{constant.GroupOwner}, }) return resp.Members[0], err } -func (g *GroupClient) GetGroupMemberIDs(ctx context.Context, groupID string) ([]string, error) { - cc, err := g.getConn(ctx) - if err != nil { - return nil, err - } - resp, err := group.NewGroupClient(cc).GetGroupMemberUserIDs(ctx, &group.GetGroupMemberUserIDsReq{ +func (g *GroupRpcClient) GetGroupMemberIDs(ctx context.Context, groupID string) ([]string, error) { + resp, err := g.Client.GetGroupMemberUserIDs(ctx, &group.GetGroupMemberUserIDsReq{ GroupID: groupID, }) if err != nil { @@ -146,12 +135,8 @@ func (g *GroupClient) GetGroupMemberIDs(ctx context.Context, groupID string) ([] return resp.UserIDs, nil } -func (g *GroupClient) GetGroupInfoCache(ctx context.Context, groupID string) (*sdkws.GroupInfo, error) { - cc, err := g.getConn(ctx) - if err != nil { - return nil, err - } - resp, err := group.NewGroupClient(cc).GetGroupInfoCache(ctx, &group.GetGroupInfoCacheReq{ +func (g *GroupRpcClient) GetGroupInfoCache(ctx context.Context, groupID string) (*sdkws.GroupInfo, error) { + resp, err := g.Client.GetGroupInfoCache(ctx, &group.GetGroupInfoCacheReq{ GroupID: groupID, }) if err != nil { @@ -160,12 +145,8 @@ func (g *GroupClient) GetGroupInfoCache(ctx context.Context, groupID string) (*s return resp.GroupInfo, nil } -func (g *GroupClient) GetGroupMemberCache(ctx context.Context, groupID string, groupMemberID string) (*sdkws.GroupMemberFullInfo, error) { - cc, err := g.getConn(ctx) - if err != nil { - return nil, err - } - resp, err := group.NewGroupClient(cc).GetGroupMemberCache(ctx, &group.GetGroupMemberCacheReq{ +func (g *GroupRpcClient) GetGroupMemberCache(ctx context.Context, groupID string, groupMemberID string) (*sdkws.GroupMemberFullInfo, error) { + resp, err := g.Client.GetGroupMemberCache(ctx, &group.GetGroupMemberCacheReq{ GroupID: groupID, GroupMemberID: groupMemberID, }) diff --git a/pkg/rpcclient/meta.go b/pkg/rpcclient/meta.go deleted file mode 100644 index 00bd81737..000000000 --- a/pkg/rpcclient/meta.go +++ /dev/null @@ -1,31 +0,0 @@ -package rpcclient - -import ( - "context" - - "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" - "google.golang.org/grpc" -) - -type MetaClient struct { - // contains filtered or unexported fields - client discoveryregistry.SvcDiscoveryRegistry - rpcRegisterName string -} - -func NewMetaClient(client discoveryregistry.SvcDiscoveryRegistry, rpcRegisterName string, opts ...MetaClientOptions) *MetaClient { - c := &MetaClient{ - client: client, - rpcRegisterName: rpcRegisterName, - } - for _, opt := range opts { - opt(c) - } - return c -} - -type MetaClientOptions func(*MetaClient) - -func (m *MetaClient) getConn(ctx context.Context) (*grpc.ClientConn, error) { - return m.client.GetConn(ctx, m.rpcRegisterName) -} diff --git a/pkg/rpcclient/msg.go b/pkg/rpcclient/msg.go index 0abe607e9..a592e104d 100644 --- a/pkg/rpcclient/msg.go +++ b/pkg/rpcclient/msg.go @@ -103,48 +103,44 @@ func newSessionTypeConf() map[int32]int32 { } } -type MsgClient struct { - conn *grpc.ClientConn - *MetaClient +type Message struct { + conn *grpc.ClientConn + Client msg.MsgClient + discov discoveryregistry.SvcDiscoveryRegistry } -func NewMsgClient(discov discoveryregistry.SvcDiscoveryRegistry) *MsgClient { - return &MsgClient{MetaClient: NewMetaClient(discov, config.Config.RpcRegisterName.OpenImMsgName)} -} - -func (m *MsgClient) SendMsg(ctx context.Context, req *msg.SendMsgReq) (*msg.SendMsgResp, error) { - cc, err := m.getConn(ctx) +func NewMessage(discov discoveryregistry.SvcDiscoveryRegistry) *Message { + conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImMsgName) if err != nil { - return nil, err + panic(err) } - resp, err := msg.NewMsgClient(cc).SendMsg(ctx, req) + client := msg.NewMsgClient(conn) + return &Message{discov: discov, conn: conn, Client: client} +} + +type MessageRpcClient Message + +func NewMessageRpcClient(discov discoveryregistry.SvcDiscoveryRegistry) MessageRpcClient { + return MessageRpcClient(*NewMessage(discov)) +} + +func (m *MessageRpcClient) SendMsg(ctx context.Context, req *msg.SendMsgReq) (*msg.SendMsgResp, error) { + resp, err := m.Client.SendMsg(ctx, req) return resp, err } -func (m *MsgClient) GetMaxSeq(ctx context.Context, req *sdkws.GetMaxSeqReq) (*sdkws.GetMaxSeqResp, error) { - cc, err := m.getConn(ctx) - if err != nil { - return nil, err - } - resp, err := msg.NewMsgClient(cc).GetMaxSeq(ctx, req) +func (m *MessageRpcClient) GetMaxSeq(ctx context.Context, req *sdkws.GetMaxSeqReq) (*sdkws.GetMaxSeqResp, error) { + resp, err := m.Client.GetMaxSeq(ctx, req) return resp, err } -func (m *MsgClient) PullMessageBySeqList(ctx context.Context, req *sdkws.PullMessageBySeqsReq) (*sdkws.PullMessageBySeqsResp, error) { - cc, err := m.getConn(ctx) - if err != nil { - return nil, err - } - resp, err := msg.NewMsgClient(cc).PullMessageBySeqs(ctx, req) +func (m *MessageRpcClient) PullMessageBySeqList(ctx context.Context, req *sdkws.PullMessageBySeqsReq) (*sdkws.PullMessageBySeqsResp, error) { + resp, err := m.Client.PullMessageBySeqs(ctx, req) return resp, err } -func (m *MsgClient) GetConversationMaxSeq(ctx context.Context, conversationID string) (int64, error) { - cc, err := m.getConn(ctx) - if err != nil { - return 0, err - } - resp, err := msg.NewMsgClient(cc).GetConversationMaxSeq(ctx, &msg.GetConversationMaxSeqReq{ConversationID: conversationID}) +func (m *MessageRpcClient) GetConversationMaxSeq(ctx context.Context, conversationID string) (int64, error) { + resp, err := m.Client.GetConversationMaxSeq(ctx, &msg.GetConversationMaxSeqReq{ConversationID: conversationID}) if err != nil { return 0, err } @@ -167,7 +163,8 @@ func WithLocalSendMsg(sendMsg func(ctx context.Context, req *msg.SendMsgReq) (*m func WithDiscov(discov discoveryregistry.SvcDiscoveryRegistry) NewNotificationSenderOptions { return func(s *NotificationSender) { - s.sendMsg = NewMsgClient(discov).SendMsg + rpcClient := NewMessageRpcClient(discov) + s.sendMsg = rpcClient.SendMsg } } diff --git a/pkg/rpcclient/notification/extend_msg.go b/pkg/rpcclient/notification/extend_msg.go index 1ab1924c8..e71ada3e1 100644 --- a/pkg/rpcclient/notification/extend_msg.go +++ b/pkg/rpcclient/notification/extend_msg.go @@ -16,11 +16,11 @@ import ( ) type ExtendMsgNotificationSender struct { - *rpcclient.MsgClient + *rpcclient.MessageRpcClient } func NewExtendMsgNotificationSender(client discoveryregistry.SvcDiscoveryRegistry) *ExtendMsgNotificationSender { - return &ExtendMsgNotificationSender{rpcclient.NewMsgClient(client)} + return &ExtendMsgNotificationSender{} } func (e *ExtendMsgNotificationSender) ExtendMessageUpdatedNotification(ctx context.Context, sendID string, conversationID string, sessionType int32, diff --git a/pkg/rpcclient/push.go b/pkg/rpcclient/push.go index 53cc2bee8..aae7479b4 100644 --- a/pkg/rpcclient/push.go +++ b/pkg/rpcclient/push.go @@ -8,11 +8,10 @@ import ( "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/push" ) -type PushClient struct { - MetaClient +type Push struct { } -func NewPushClient(client discoveryregistry.SvcDiscoveryRegistry) *PushClient { +func NewPushPush(client discoveryregistry.SvcDiscoveryRegistry) *PushClient { return &PushClient{ MetaClient: MetaClient{ client: client, diff --git a/pkg/rpcclient/third.go b/pkg/rpcclient/third.go new file mode 100644 index 000000000..a59a5adb4 --- /dev/null +++ b/pkg/rpcclient/third.go @@ -0,0 +1,25 @@ +package rpcclient + +import ( + "context" + + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" + "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" + "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/third" + "google.golang.org/grpc" +) + +type Third struct { + conn *grpc.ClientConn + Client third.ThirdClient + discov discoveryregistry.SvcDiscoveryRegistry +} + +func NewThird(discov discoveryregistry.SvcDiscoveryRegistry) *Third { + conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImThirdName) + if err != nil { + panic(err) + } + client := third.NewThirdClient(conn) + return &Third{discov: discov, Client: client, conn: conn} +} diff --git a/pkg/rpcclient/user.go b/pkg/rpcclient/user.go index 6324edd47..d717ba6f3 100644 --- a/pkg/rpcclient/user.go +++ b/pkg/rpcclient/user.go @@ -11,27 +11,32 @@ import ( "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws" "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/user" "github.com/OpenIMSDK/Open-IM-Server/pkg/utils" + "google.golang.org/grpc" ) -type UserClient struct { - MetaClient +type User struct { + conn *grpc.ClientConn + Client user.UserClient + discov discoveryregistry.SvcDiscoveryRegistry } -func NewUserClient(client discoveryregistry.SvcDiscoveryRegistry) *UserClient { - return &UserClient{ - MetaClient: MetaClient{ - client: client, - rpcRegisterName: config.Config.RpcRegisterName.OpenImUserName, - }, - } -} - -func (u *UserClient) GetUsersInfo(ctx context.Context, userIDs []string) ([]*sdkws.UserInfo, error) { - cc, err := u.getConn(ctx) +func NewUser(discov discoveryregistry.SvcDiscoveryRegistry) *User { + conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImUserName) if err != nil { - return nil, err + panic(err) } - resp, err := user.NewUserClient(cc).GetDesignateUsers(ctx, &user.GetDesignateUsersReq{ + client := user.NewUserClient(conn) + return &User{discov: discov, Client: client, conn: conn} +} + +type UserRpcClient User + +func NewUserRpcClient(client discoveryregistry.SvcDiscoveryRegistry) UserRpcClient { + return UserRpcClient(*NewUser(client)) +} + +func (u *UserRpcClient) GetUsersInfo(ctx context.Context, userIDs []string) ([]*sdkws.UserInfo, error) { + resp, err := u.Client.GetDesignateUsers(ctx, &user.GetDesignateUsersReq{ UserIDs: userIDs, }) if err != nil { @@ -45,7 +50,7 @@ func (u *UserClient) GetUsersInfo(ctx context.Context, userIDs []string) ([]*sdk return resp.UsersInfo, nil } -func (u *UserClient) GetUserInfo(ctx context.Context, userID string) (*sdkws.UserInfo, error) { +func (u *UserRpcClient) GetUserInfo(ctx context.Context, userID string) (*sdkws.UserInfo, error) { users, err := u.GetUsersInfo(ctx, []string{userID}) if err != nil { return nil, err @@ -53,7 +58,7 @@ func (u *UserClient) GetUserInfo(ctx context.Context, userID string) (*sdkws.Use return users[0], nil } -func (u *UserClient) GetUsersInfoMap(ctx context.Context, userIDs []string) (map[string]*sdkws.UserInfo, error) { +func (u *UserRpcClient) GetUsersInfoMap(ctx context.Context, userIDs []string) (map[string]*sdkws.UserInfo, error) { users, err := u.GetUsersInfo(ctx, userIDs) if err != nil { return nil, err @@ -63,7 +68,7 @@ func (u *UserClient) GetUsersInfoMap(ctx context.Context, userIDs []string) (map }), nil } -func (u *UserClient) GetPublicUserInfos(ctx context.Context, userIDs []string, complete bool) ([]*sdkws.PublicUserInfo, error) { +func (u *UserRpcClient) GetPublicUserInfos(ctx context.Context, userIDs []string, complete bool) ([]*sdkws.PublicUserInfo, error) { users, err := u.GetUsersInfo(ctx, userIDs) if err != nil { return nil, err @@ -78,7 +83,7 @@ func (u *UserClient) GetPublicUserInfos(ctx context.Context, userIDs []string, c }), nil } -func (u *UserClient) GetPublicUserInfo(ctx context.Context, userID string) (*sdkws.PublicUserInfo, error) { +func (u *UserRpcClient) GetPublicUserInfo(ctx context.Context, userID string) (*sdkws.PublicUserInfo, error) { users, err := u.GetPublicUserInfos(ctx, []string{userID}, true) if err != nil { return nil, err @@ -86,7 +91,7 @@ func (u *UserClient) GetPublicUserInfo(ctx context.Context, userID string) (*sdk return users[0], nil } -func (u *UserClient) GetPublicUserInfoMap(ctx context.Context, userIDs []string, complete bool) (map[string]*sdkws.PublicUserInfo, error) { +func (u *UserRpcClient) GetPublicUserInfoMap(ctx context.Context, userIDs []string, complete bool) (map[string]*sdkws.PublicUserInfo, error) { users, err := u.GetPublicUserInfos(ctx, userIDs, complete) if err != nil { return nil, err @@ -96,12 +101,8 @@ func (u *UserClient) GetPublicUserInfoMap(ctx context.Context, userIDs []string, }), nil } -func (u *UserClient) GetUserGlobalMsgRecvOpt(ctx context.Context, userID string) (int32, error) { - cc, err := u.getConn(ctx) - if err != nil { - return 0, err - } - resp, err := user.NewUserClient(cc).GetGlobalRecvMessageOpt(ctx, &user.GetGlobalRecvMessageOptReq{ +func (u *UserRpcClient) GetUserGlobalMsgRecvOpt(ctx context.Context, userID string) (int32, error) { + resp, err := u.Client.GetGlobalRecvMessageOpt(ctx, &user.GetGlobalRecvMessageOptReq{ UserID: userID, }) if err != nil { @@ -110,7 +111,7 @@ func (u *UserClient) GetUserGlobalMsgRecvOpt(ctx context.Context, userID string) return resp.GlobalRecvMsgOpt, err } -func (u *UserClient) Access(ctx context.Context, ownerUserID string) error { +func (u *UserRpcClient) Access(ctx context.Context, ownerUserID string) error { _, err := u.GetUserInfo(ctx, ownerUserID) if err != nil { return err