Merge branch 'errcode' into v3

pull/454/head
wangchuxiao 2 years ago
commit 45421de2ca

@ -5,11 +5,16 @@ import (
"context" "context"
"fmt" "fmt"
"net" "net"
"os"
"runtime"
"strconv" "strconv"
"time" "time"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"net/http"
_ "net/http/pprof"
"github.com/OpenIMSDK/Open-IM-Server/internal/api" "github.com/OpenIMSDK/Open-IM-Server/internal/api"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/cmd" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/cmd"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
@ -29,6 +34,16 @@ func main() {
} }
} }
func startPprof() {
runtime.GOMAXPROCS(1)
runtime.SetMutexProfileFraction(1)
runtime.SetBlockProfileRate(1)
if err := http.ListenAndServe(":6060", nil); err != nil {
panic(err)
}
os.Exit(0)
}
func run(port int) error { func run(port int) error {
if port == 0 { if port == 0 {
port = config.Config.Api.GinPort[0] port = config.Config.Api.GinPort[0]
@ -66,6 +81,7 @@ func run(port int) error {
} }
fmt.Println("start api server, address: ", address, ", OpenIM version: ", config.Version) fmt.Println("start api server, address: ", address, ", OpenIM version: ", config.Version)
log.ZInfo(context.Background(), "start server success", "address", address, "version", config.Version) log.ZInfo(context.Background(), "start server success", "address", address, "version", config.Version)
go startPprof()
err = router.Run(address) err = router.Run(address)
if err != nil { if err != nil {
log.ZError(context.Background(), "api run failed ", err, "address", address) log.ZError(context.Background(), "api run failed ", err, "address", address)

@ -1,49 +1,27 @@
package api package api
import ( import (
"context"
"github.com/OpenIMSDK/Open-IM-Server/pkg/a2r" "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/discoveryregistry"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/auth" "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/auth"
"github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"google.golang.org/grpc"
) )
func NewAuth(discov discoveryregistry.SvcDiscoveryRegistry) *Auth { type AuthApi rpcclient.Auth
// conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImAuthName)
// if err != nil {
// panic(err)
// }
return &Auth{discov: discov}
}
type Auth struct {
conn *grpc.ClientConn
discov discoveryregistry.SvcDiscoveryRegistry
}
func (o *Auth) client(ctx context.Context) (auth.AuthClient, error) {
c, err := o.discov.GetConn(ctx, config.Config.RpcRegisterName.OpenImAuthName)
if err != nil {
return nil, err
}
return auth.NewAuthClient(c), nil
}
func (o *Auth) UserRegister(c *gin.Context) { func NewAuthApi(discov discoveryregistry.SvcDiscoveryRegistry) AuthApi {
//a2r.Call(auth.AuthClient.UserRegister, o.userClient, c) // todo return AuthApi(*rpcclient.NewAuth(discov))
} }
func (o *Auth) UserToken(c *gin.Context) { func (o *AuthApi) UserToken(c *gin.Context) {
a2r.Call(auth.AuthClient.UserToken, o.client, c) 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) 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) a2r.Call(auth.AuthClient.ForceLogout, o.Client, c)
} }

@ -1,67 +1,49 @@
package api package api
import ( import (
"context"
"github.com/OpenIMSDK/Open-IM-Server/pkg/a2r" "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/discoveryregistry"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/conversation" "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/conversation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"google.golang.org/grpc"
) )
func NewConversation(discov discoveryregistry.SvcDiscoveryRegistry) *Conversation { type ConversationApi rpcclient.Conversation
// conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImConversationName)
// if err != nil {
// panic(err)
// }
return &Conversation{discov: discov}
}
type Conversation struct { func NewConversationApi(discov discoveryregistry.SvcDiscoveryRegistry) ConversationApi {
conn *grpc.ClientConn return ConversationApi(*rpcclient.NewConversation(discov))
discov discoveryregistry.SvcDiscoveryRegistry
}
func (o *Conversation) client(ctx context.Context) (conversation.ConversationClient, error) {
c, err := o.discov.GetConn(ctx, config.Config.RpcRegisterName.OpenImConversationName)
if err != nil {
return nil, err
}
return conversation.NewConversationClient(c), nil
} }
func (o *Conversation) GetAllConversations(c *gin.Context) { func (o *ConversationApi) GetAllConversations(c *gin.Context) {
a2r.Call(conversation.ConversationClient.GetAllConversations, o.client, c) 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) 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) a2r.Call(conversation.ConversationClient.GetConversations, o.Client, c)
} }
// deprecated // deprecated
func (o *Conversation) SetConversation(c *gin.Context) { func (o *ConversationApi) SetConversation(c *gin.Context) {
a2r.Call(conversation.ConversationClient.SetConversation, o.client, c) a2r.Call(conversation.ConversationClient.SetConversation, o.Client, c)
} }
// deprecated // deprecated
func (o *Conversation) BatchSetConversations(c *gin.Context) { func (o *ConversationApi) BatchSetConversations(c *gin.Context) {
a2r.Call(conversation.ConversationClient.BatchSetConversations, o.client, c) 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) 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) 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) a2r.Call(conversation.ConversationClient.SetConversations, o.Client, c)
} }

@ -1,82 +1,64 @@
package api package api
import ( import (
"context"
"github.com/OpenIMSDK/Open-IM-Server/pkg/a2r" "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/discoveryregistry"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/friend" "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" "github.com/gin-gonic/gin"
) )
func NewFriend(discov discoveryregistry.SvcDiscoveryRegistry) *Friend { type FriendApi rpcclient.Friend
// conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImFriendName)
// if err != nil {
// panic(err)
// }
return &Friend{discov: discov}
}
type Friend struct { func NewFriendApi(discov discoveryregistry.SvcDiscoveryRegistry) FriendApi {
conn *grpc.ClientConn return FriendApi(*rpcclient.NewFriend(discov))
discov discoveryregistry.SvcDiscoveryRegistry
}
func (o *Friend) client(ctx context.Context) (friend.FriendClient, error) {
c, err := o.discov.GetConn(ctx, config.Config.RpcRegisterName.OpenImFriendName)
if err != nil {
return nil, err
}
return friend.NewFriendClient(c), nil
} }
func (o *Friend) ApplyToAddFriend(c *gin.Context) { func (o *FriendApi) ApplyToAddFriend(c *gin.Context) {
a2r.Call(friend.FriendClient.ApplyToAddFriend, o.client, c) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) a2r.Call(friend.FriendClient.IsFriend, o.Client, c)
} }

@ -1,120 +1,102 @@
package api package api
import ( import (
"context"
"github.com/OpenIMSDK/Open-IM-Server/pkg/a2r" "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/discoveryregistry"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/group" "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" "github.com/gin-gonic/gin"
) )
func NewGroup(discov discoveryregistry.SvcDiscoveryRegistry) *Group { type GroupApi rpcclient.Group
// conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImGroupName)
// if err != nil {
// panic(err)
// }
return &Group{discov: discov}
}
type Group struct {
conn *grpc.ClientConn
discov discoveryregistry.SvcDiscoveryRegistry
}
func (o *Group) client(ctx context.Context) (group.GroupClient, error) { func NewGroupApi(discov discoveryregistry.SvcDiscoveryRegistry) GroupApi {
c, err := o.discov.GetConn(ctx, config.Config.RpcRegisterName.OpenImGroupName) return GroupApi(*rpcclient.NewGroup(discov))
if err != nil {
return nil, err
}
return group.NewGroupClient(c), nil
} }
func (o *Group) CreateGroup(c *gin.Context) { func (o *GroupApi) CreateGroup(c *gin.Context) {
a2r.Call(group.GroupClient.CreateGroup, o.client, c) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) a2r.Call(group.GroupClient.GetGroupAbstractInfo, o.Client, c)
} }
//func (g *Group) SetGroupMemberNickname(c *gin.Context) { //func (g *Group) SetGroupMemberNickname(c *gin.Context) {
@ -125,10 +107,10 @@ func (o *Group) GetGroupAbstractInfo(c *gin.Context) {
// a2r.Call(group.GroupClient.GetGroupAllMember, g.userClient, c) // 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) 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) a2r.Call(group.GroupClient.GetSuperGroupsInfo, o.Client, c)
} }

@ -1,48 +1,40 @@
package api package api
import ( import (
"context"
"github.com/OpenIMSDK/Open-IM-Server/pkg/a2r" "github.com/OpenIMSDK/Open-IM-Server/pkg/a2r"
"github.com/OpenIMSDK/Open-IM-Server/pkg/apiresp" "github.com/OpenIMSDK/Open-IM-Server/pkg/apiresp"
"github.com/OpenIMSDK/Open-IM-Server/pkg/apistruct" "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/constant"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
"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/errs"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/msg" "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/proto/sdkws"
"github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils" "github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10" "github.com/go-playground/validator/v10"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
"google.golang.org/grpc"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
) )
func NewMsg(discov discoveryregistry.SvcDiscoveryRegistry) *Message { type MessageApi struct {
// conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImMsgName) rpcclient.Message
// if err != nil { validate *validator.Validate
// panic(err)
// }
return &Message{validate: validator.New(), discov: discov}
} }
type Message struct { func NewMessageApi(discov discoveryregistry.SvcDiscoveryRegistry) MessageApi {
conn *grpc.ClientConn return MessageApi{Message: *rpcclient.NewMessage(discov), validate: validator.New()}
validate *validator.Validate
discov discoveryregistry.SvcDiscoveryRegistry
} }
func (Message) SetOptions(options map[string]bool, value bool) { func (MessageApi) SetOptions(options map[string]bool, value bool) {
utils.SetSwitchFromOptions(options, constant.IsHistory, value) utils.SetSwitchFromOptions(options, constant.IsHistory, value)
utils.SetSwitchFromOptions(options, constant.IsPersistent, value) utils.SetSwitchFromOptions(options, constant.IsPersistent, value)
utils.SetSwitchFromOptions(options, constant.IsSenderSync, value) utils.SetSwitchFromOptions(options, constant.IsSenderSync, value)
utils.SetSwitchFromOptions(options, constant.IsConversationUpdate, 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 newContent string
var err error var err error
switch params.ContentType { switch params.ContentType {
@ -107,79 +99,71 @@ func (m Message) newUserSendMsgReq(c *gin.Context, params *apistruct.ManagementS
return &pbData return &pbData
} }
func (m *Message) client(ctx context.Context) (msg.MsgClient, error) { func (m *MessageApi) GetSeq(c *gin.Context) {
c, err := m.discov.GetConn(ctx, config.Config.RpcRegisterName.OpenImMsgName) a2r.Call(msg.MsgClient.GetMaxSeq, m.Client, c)
if err != nil {
return nil, err
}
return msg.NewMsgClient(c), nil
}
func (m *Message) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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{} params := apistruct.ManagementSendMsgReq{}
if err := c.BindJSON(&params); err != nil { if err := c.BindJSON(&params); err != nil {
apiresp.GinError(c, errs.ErrArgs.WithDetail(err.Error()).Wrap()) apiresp.GinError(c, errs.ErrArgs.WithDetail(err.Error()).Wrap())
@ -226,16 +210,15 @@ func (m *Message) SendMessage(c *gin.Context) {
return return
} }
pbReq := m.newUserSendMsgReq(c, &params) pbReq := m.newUserSendMsgReq(c, &params)
client := msg.NewMsgClient(m.conn)
var status int var status int
respPb, err := client.SendMsg(c, pbReq) respPb, err := m.Client.SendMsg(c, pbReq)
if err != nil { if err != nil {
status = constant.MsgSendFailed status = constant.MsgSendFailed
apiresp.GinError(c, err) apiresp.GinError(c, err)
return return
} }
status = constant.MsgSendSuccessed status = constant.MsgSendSuccessed
_, err = client.SetSendMsgStatus(c, &msg.SetSendMsgStatusReq{ _, err = m.Client.SetSendMsgStatus(c, &msg.SetSendMsgStatusReq{
Status: int32(status), Status: int32(status),
}) })
if err != nil { if err != nil {
@ -244,14 +227,14 @@ func (m *Message) SendMessage(c *gin.Context) {
apiresp.GinSuccess(c, respPb) 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) 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) 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) a2r.Call(msg.MsgClient.GetSendMsgStatus, m.Client, c)
} }

@ -37,7 +37,7 @@ func NewGinRouter(discov discoveryregistry.SvcDiscoveryRegistry, rdb redis.Unive
} }
userRouterGroup := r.Group("/user") userRouterGroup := r.Group("/user")
{ {
u := NewUser(discov) u := NewUserApi(discov)
userRouterGroupChild := mw.NewRouterGroup(userRouterGroup, "") userRouterGroupChild := mw.NewRouterGroup(userRouterGroup, "")
userRouterGroupChildToken := mw.NewRouterGroup(userRouterGroup, "", mw.WithGinParseToken(rdb)) userRouterGroupChildToken := mw.NewRouterGroup(userRouterGroup, "", mw.WithGinParseToken(rdb))
userRouterGroupChild.POST("/user_register", u.UserRegister) userRouterGroupChild.POST("/user_register", u.UserRegister)
@ -52,7 +52,7 @@ func NewGinRouter(discov discoveryregistry.SvcDiscoveryRegistry, rdb redis.Unive
//friend routing group //friend routing group
friendRouterGroup := r.Group("/friend") friendRouterGroup := r.Group("/friend")
{ {
f := NewFriend(discov) f := NewFriendApi(discov)
friendRouterGroup.Use(mw.GinParseToken(rdb)) friendRouterGroup.Use(mw.GinParseToken(rdb))
friendRouterGroup.POST("/delete_friend", f.DeleteFriend) //1 friendRouterGroup.POST("/delete_friend", f.DeleteFriend) //1
friendRouterGroup.POST("/get_friend_apply_list", f.GetFriendApplyList) //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("/import_friend", f.ImportFriends) //1
friendRouterGroup.POST("/is_friend", f.IsFriend) //1 friendRouterGroup.POST("/is_friend", f.IsFriend) //1
} }
g := NewGroup(discov) g := NewGroupApi(discov)
groupRouterGroup := r.Group("/group") groupRouterGroup := r.Group("/group")
{ {
@ -105,8 +105,8 @@ func NewGinRouter(discov discoveryregistry.SvcDiscoveryRegistry, rdb redis.Unive
////certificate ////certificate
authRouterGroup := r.Group("/auth") authRouterGroup := r.Group("/auth")
{ {
a := NewAuth(discov) a := NewAuthApi(discov)
u := NewUser(discov) u := NewUserApi(discov)
authRouterGroupChild := mw.NewRouterGroup(authRouterGroup, "") authRouterGroupChild := mw.NewRouterGroup(authRouterGroup, "")
authRouterGroupChildToken := mw.NewRouterGroup(authRouterGroup, "", mw.WithGinParseToken(rdb)) authRouterGroupChildToken := mw.NewRouterGroup(authRouterGroup, "", mw.WithGinParseToken(rdb))
authRouterGroupChild.POST("/user_register", u.UserRegister) //1 authRouterGroupChild.POST("/user_register", u.UserRegister) //1
@ -117,7 +117,7 @@ func NewGinRouter(discov discoveryregistry.SvcDiscoveryRegistry, rdb redis.Unive
////Third service ////Third service
thirdGroup := r.Group("/third") thirdGroup := r.Group("/third")
{ {
t := NewThird(discov) t := NewThirdApi(discov)
thirdGroup.Use(mw.GinParseToken(rdb)) thirdGroup.Use(mw.GinParseToken(rdb))
thirdGroup.POST("/fcm_update_token", t.FcmUpdateToken) thirdGroup.POST("/fcm_update_token", t.FcmUpdateToken)
thirdGroup.POST("/set_app_badge", t.SetAppBadge) thirdGroup.POST("/set_app_badge", t.SetAppBadge)
@ -132,7 +132,7 @@ func NewGinRouter(discov discoveryregistry.SvcDiscoveryRegistry, rdb redis.Unive
////Message ////Message
msgGroup := r.Group("/msg") msgGroup := r.Group("/msg")
{ {
m := NewMsg(discov) m := NewMessageApi(discov)
msgGroup.Use(mw.GinParseToken(rdb)) msgGroup.Use(mw.GinParseToken(rdb))
msgGroup.POST("/newest_seq", m.GetSeq) msgGroup.POST("/newest_seq", m.GetSeq)
msgGroup.POST("/send_msg", m.SendMessage) msgGroup.POST("/send_msg", m.SendMessage)
@ -160,7 +160,7 @@ func NewGinRouter(discov discoveryregistry.SvcDiscoveryRegistry, rdb redis.Unive
////Conversation ////Conversation
conversationGroup := r.Group("/conversation") conversationGroup := r.Group("/conversation")
{ {
c := NewConversation(discov) c := NewConversationApi(discov)
conversationGroup.Use(mw.GinParseToken(rdb)) conversationGroup.Use(mw.GinParseToken(rdb))
conversationGroup.POST("/get_all_conversations", c.GetAllConversations) conversationGroup.POST("/get_all_conversations", c.GetAllConversations)
conversationGroup.POST("/get_conversation", c.GetConversation) conversationGroup.POST("/get_conversation", c.GetConversation)
@ -174,10 +174,9 @@ func NewGinRouter(discov discoveryregistry.SvcDiscoveryRegistry, rdb redis.Unive
statisticsGroup := r.Group("/statistics") statisticsGroup := r.Group("/statistics")
{ {
s := NewStatistics(discov) s := NewStatisticsApi(discov)
conversationGroup.Use(mw.GinParseToken(rdb)) conversationGroup.Use(mw.GinParseToken(rdb))
statisticsGroup.POST("/user/register", s.UserRegister) statisticsGroup.POST("/user_register", s.UserRegister)
} }
return r return r
} }

@ -1,30 +1,19 @@
package api package api
import ( import (
"context"
"github.com/OpenIMSDK/Open-IM-Server/pkg/a2r" "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/discoveryregistry"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/user" "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/user"
"github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
func NewStatistics(discov discoveryregistry.SvcDiscoveryRegistry) *Statistics { type StatisticsApi rpcclient.User
return &Statistics{discov: discov}
}
type Statistics struct {
discov discoveryregistry.SvcDiscoveryRegistry
}
func (s *Statistics) userClient(ctx context.Context) (user.UserClient, error) { func NewStatisticsApi(discov discoveryregistry.SvcDiscoveryRegistry) StatisticsApi {
conn, err := s.discov.GetConn(ctx, config.Config.RpcRegisterName.OpenImUserName) return StatisticsApi(*rpcclient.NewUser(discov))
if err != nil {
return nil, err
}
return user.NewUserClient(conn), nil
} }
func (s *Statistics) UserRegister(c *gin.Context) { func (s *StatisticsApi) UserRegister(c *gin.Context) {
a2r.Call(user.UserClient.UserRegisterCount, s.userClient, c) a2r.Call(user.UserClient.UserRegisterCount, s.Client, c)
} }

@ -1,70 +1,53 @@
package api package api
import ( import (
"context"
"math/rand" "math/rand"
"net/http" "net/http"
"strconv" "strconv"
"github.com/OpenIMSDK/Open-IM-Server/pkg/a2r" "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/constant"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext"
"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/errs"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/third" "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/third"
"github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"google.golang.org/grpc"
) )
func NewThird(discov discoveryregistry.SvcDiscoveryRegistry) *Third { type ThirdApi rpcclient.Third
// conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImThirdName)
// if err != nil {
// panic(err)
// }
return &Third{discov: discov}
}
type Third struct {
conn *grpc.ClientConn
discov discoveryregistry.SvcDiscoveryRegistry
}
func (o *Third) client(ctx context.Context) (third.ThirdClient, error) { func NewThirdApi(discov discoveryregistry.SvcDiscoveryRegistry) ThirdApi {
conn, err := o.discov.GetConn(ctx, config.Config.RpcRegisterName.OpenImThirdName) return ThirdApi(*rpcclient.NewThird(discov))
if err != nil {
return nil, err
}
return third.NewThirdClient(conn), nil
} }
func (o *Third) ApplyPut(c *gin.Context) { func (o *ThirdApi) ApplyPut(c *gin.Context) {
a2r.Call(third.ThirdClient.ApplyPut, o.client, c) 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) 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) 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) 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) 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) 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 { if c.Request.Method == http.MethodPost {
a2r.Call(third.ThirdClient.GetUrl, o.client, c) a2r.Call(third.ThirdClient.GetUrl, o.Client, c)
return return
} }
name := c.Query("name") name := c.Query("name")
@ -81,13 +64,8 @@ func (o *Third) GetURL(c *gin.Context) {
expires = 3600 * 1000 expires = 3600 * 1000
} }
attachment, _ := strconv.ParseBool(c.Query("attachment")) attachment, _ := strconv.ParseBool(c.Query("attachment"))
client, err := o.client(c)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
c.Set(constant.OperationID, operationID) c.Set(constant.OperationID, operationID)
resp, err := 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 err != nil {
if errs.ErrArgs.Is(err) { if errs.ErrArgs.Is(err) {
c.String(http.StatusBadRequest, err.Error()) c.String(http.StatusBadRequest, err.Error())

@ -1,68 +1,52 @@
package api package api
import ( import (
"context"
"github.com/OpenIMSDK/Open-IM-Server/pkg/a2r" "github.com/OpenIMSDK/Open-IM-Server/pkg/a2r"
"github.com/OpenIMSDK/Open-IM-Server/pkg/apiresp" "github.com/OpenIMSDK/Open-IM-Server/pkg/apiresp"
"github.com/OpenIMSDK/Open-IM-Server/pkg/apistruct" "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/common/tokenverify"
"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/errs"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/user" "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/user"
"github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
func NewUser(discov discoveryregistry.SvcDiscoveryRegistry) *User { type UserApi rpcclient.User
// conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImUserName)
// if err != nil {
// panic(err)
// }
return &User{discov: discov}
}
type User struct { func NewUserApi(discov discoveryregistry.SvcDiscoveryRegistry) UserApi {
discov discoveryregistry.SvcDiscoveryRegistry return UserApi(*rpcclient.NewUser(discov))
}
func (u *User) client(ctx context.Context) (user.UserClient, error) {
conn, err := u.discov.GetConn(ctx, config.Config.RpcRegisterName.OpenImUserName)
if err != nil {
return nil, err
}
return user.NewUserClient(conn), nil
} }
func (u *User) UserRegister(c *gin.Context) { func (u *UserApi) UserRegister(c *gin.Context) {
a2r.Call(user.UserClient.UserRegister, u.client, c) 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) 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) 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) 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) 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) 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) 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{} params := apistruct.ManagementSendMsgReq{}
if err := c.BindJSON(&params); err != nil { if err := c.BindJSON(&params); err != nil {
apiresp.GinError(c, errs.ErrArgs.WithDetail(err.Error()).Wrap()) apiresp.GinError(c, errs.ErrArgs.WithDetail(err.Error()).Wrap())

@ -52,14 +52,16 @@ type MessageHandler interface {
var _ MessageHandler = (*GrpcHandler)(nil) var _ MessageHandler = (*GrpcHandler)(nil)
type GrpcHandler struct { type GrpcHandler struct {
msgRpcClient *rpcclient.MsgClient msgRpcClient *rpcclient.MessageRpcClient
pushClient *rpcclient.PushClient pushClient *rpcclient.PushRpcClient
validate *validator.Validate validate *validator.Validate
} }
func NewGrpcHandler(validate *validator.Validate, client discoveryregistry.SvcDiscoveryRegistry) *GrpcHandler { func NewGrpcHandler(validate *validator.Validate, client discoveryregistry.SvcDiscoveryRegistry) *GrpcHandler {
return &GrpcHandler{msgRpcClient: rpcclient.NewMsgClient(client), msgRpcClient := rpcclient.NewMessageRpcClient(client)
pushClient: rpcclient.NewPushClient(client), validate: validate} pushRpcClient := rpcclient.NewPushRpcClient(client)
return &GrpcHandler{msgRpcClient: &msgRpcClient,
pushClient: &pushRpcClient, validate: validate}
} }
func (g GrpcHandler) GetSeq(context context.Context, data Req) ([]byte, error) { func (g GrpcHandler) GetSeq(context context.Context, data Req) ([]byte, error) {

@ -61,16 +61,16 @@ func StartTransfer(prometheusPort int) error {
chatLogDatabase := controller.NewChatLogDatabase(relation.NewChatLogGorm(db)) chatLogDatabase := controller.NewChatLogDatabase(relation.NewChatLogGorm(db))
extendMsgDatabase := controller.NewExtendMsgDatabase(extendMsgModel, extendMsgCache, tx.NewMongo(mongo.GetClient())) extendMsgDatabase := controller.NewExtendMsgDatabase(extendMsgModel, extendMsgCache, tx.NewMongo(mongo.GetClient()))
msgDatabase := controller.NewCommonMsgDatabase(msgDocModel, msgModel) msgDatabase := controller.NewCommonMsgDatabase(msgDocModel, msgModel)
conversationRpcClient := rpcclient.NewConversationClient(client) conversationRpcClient := rpcclient.NewConversationRpcClient(client)
groupRpcClient := rpcclient.NewGroupClient(client) groupRpcClient := rpcclient.NewGroupRpcClient(client)
msgTransfer := NewMsgTransfer(chatLogDatabase, extendMsgDatabase, msgDatabase, conversationRpcClient, groupRpcClient) msgTransfer := NewMsgTransfer(chatLogDatabase, extendMsgDatabase, msgDatabase, &conversationRpcClient, &groupRpcClient)
msgTransfer.initPrometheus() msgTransfer.initPrometheus()
return msgTransfer.Start(prometheusPort) return msgTransfer.Start(prometheusPort)
} }
func NewMsgTransfer(chatLogDatabase controller.ChatLogDatabase, func NewMsgTransfer(chatLogDatabase controller.ChatLogDatabase,
extendMsgDatabase controller.ExtendMsgDatabase, msgDatabase controller.CommonMsgDatabase, 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), return &MsgTransfer{persistentCH: NewPersistentConsumerHandler(chatLogDatabase), historyCH: NewOnlineHistoryRedisConsumerHandler(msgDatabase, conversationRpcClient, groupRpcClient),
historyMongoCH: NewOnlineHistoryMongoConsumerHandler(msgDatabase), modifyCH: NewModifyMsgConsumerHandler(extendMsgDatabase)} historyMongoCH: NewOnlineHistoryMongoConsumerHandler(msgDatabase), modifyCH: NewModifyMsgConsumerHandler(extendMsgDatabase)}
} }

@ -59,11 +59,11 @@ type OnlineHistoryRedisConsumerHandler struct {
singleMsgFailedCountMutex sync.Mutex singleMsgFailedCountMutex sync.Mutex
msgDatabase controller.CommonMsgDatabase msgDatabase controller.CommonMsgDatabase
conversationRpcClient *rpcclient.ConversationClient conversationRpcClient *rpcclient.ConversationRpcClient
groupRpcClient *rpcclient.GroupClient 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 var och OnlineHistoryRedisConsumerHandler
och.msgDatabase = database och.msgDatabase = database
och.msgDistributionCh = make(chan Cmd2Value) //no buffer channel och.msgDistributionCh = make(chan Cmd2Value) //no buffer channel

@ -4,6 +4,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"errors" "errors"
"github.com/OpenIMSDK/Open-IM-Server/internal/push/offlinepush" "github.com/OpenIMSDK/Open-IM-Server/internal/push/offlinepush"
"github.com/OpenIMSDK/Open-IM-Server/internal/push/offlinepush/fcm" "github.com/OpenIMSDK/Open-IM-Server/internal/push/offlinepush/fcm"
"github.com/OpenIMSDK/Open-IM-Server/internal/push/offlinepush/getui" "github.com/OpenIMSDK/Open-IM-Server/internal/push/offlinepush/getui"
@ -17,7 +18,6 @@ import (
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/prome" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/prome"
"github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/group"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/msggateway" "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/msggateway"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws" "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"
@ -30,24 +30,27 @@ type Pusher struct {
offlinePusher offlinepush.OfflinePusher offlinePusher offlinepush.OfflinePusher
groupLocalCache *localcache.GroupLocalCache groupLocalCache *localcache.GroupLocalCache
conversationLocalCache *localcache.ConversationLocalCache conversationLocalCache *localcache.ConversationLocalCache
msgClient *rpcclient.MsgClient msgClient *rpcclient.MessageRpcClient
conversationClient *rpcclient.ConversationClient conversationRpcClient *rpcclient.ConversationRpcClient
groupRpcClient *rpcclient.GroupRpcClient
successCount int successCount int
} }
var errNoOfflinePusher = errors.New("no offlinePusher is configured") var errNoOfflinePusher = errors.New("no offlinePusher is configured")
func NewPusher(client discoveryregistry.SvcDiscoveryRegistry, offlinePusher offlinepush.OfflinePusher, database controller.PushDatabase, func NewPusher(discov discoveryregistry.SvcDiscoveryRegistry, offlinePusher offlinepush.OfflinePusher, database controller.PushDatabase,
groupLocalCache *localcache.GroupLocalCache, conversationLocalCache *localcache.ConversationLocalCache) *Pusher { groupLocalCache *localcache.GroupLocalCache, conversationLocalCache *localcache.ConversationLocalCache) *Pusher {
rpcclient.NewGroupClient(client) msgClient := rpcclient.NewMessageRpcClient(discov)
conversationRpcClient := rpcclient.NewConversationRpcClient(discov)
groupRpcClient := rpcclient.NewGroupRpcClient(discov)
return &Pusher{ return &Pusher{
database: database, database: database,
client: client,
offlinePusher: offlinePusher, offlinePusher: offlinePusher,
groupLocalCache: groupLocalCache, groupLocalCache: groupLocalCache,
conversationLocalCache: conversationLocalCache, conversationLocalCache: conversationLocalCache,
msgClient: rpcclient.NewMsgClient(client), msgClient: &msgClient,
conversationClient: rpcclient.NewConversationClient(client), conversationRpcClient: &conversationRpcClient,
groupRpcClient: &groupRpcClient,
} }
} }
@ -65,25 +68,13 @@ func NewOfflinePusher(cache cache.MsgModel) offlinepush.OfflinePusher {
return offlinePusher return offlinePusher
} }
func (p *Pusher) DismissGroup(ctx context.Context, groupID string) error {
cc, err := p.client.GetConn(ctx, config.Config.RpcRegisterName.OpenImGroupName)
if err != nil {
return err
}
_, err = group.NewGroupClient(cc).DismissGroup(ctx, &group.DismissGroupReq{
GroupID: groupID,
DeleteMember: true,
})
return err
}
func (p *Pusher) DeleteMemberAndSetConversationSeq(ctx context.Context, groupID string, userIDs []string) error { func (p *Pusher) DeleteMemberAndSetConversationSeq(ctx context.Context, groupID string, userIDs []string) error {
conevrsationID := utils.GetConversationIDBySessionType(constant.SuperGroupChatType, groupID) conevrsationID := utils.GetConversationIDBySessionType(constant.SuperGroupChatType, groupID)
maxSeq, err := p.msgClient.GetConversationMaxSeq(ctx, conevrsationID) maxSeq, err := p.msgClient.GetConversationMaxSeq(ctx, conevrsationID)
if err != nil { if err != nil {
return err return err
} }
return p.conversationClient.SetConversationMaxSeq(ctx, userIDs, conevrsationID, maxSeq) return p.conversationRpcClient.SetConversationMaxSeq(ctx, userIDs, conevrsationID, maxSeq)
} }
func (p *Pusher) Push2User(ctx context.Context, userIDs []string, msg *sdkws.MsgData) error { func (p *Pusher) Push2User(ctx context.Context, userIDs []string, msg *sdkws.MsgData) error {
@ -176,7 +167,7 @@ func (p *Pusher) Push2SuperGroup(ctx context.Context, groupID string, msg *sdkws
ctx = mcontext.WithOpUserIDContext(ctx, config.Config.Manager.AppManagerUid[0]) ctx = mcontext.WithOpUserIDContext(ctx, config.Config.Manager.AppManagerUid[0])
} }
defer func(groupID string) { defer func(groupID string) {
if err := p.DismissGroup(ctx, groupID); err != nil { if err := p.groupRpcClient.DismissGroup(ctx, groupID); err != nil {
log.ZError(ctx, "DismissGroup Notification clear members", err, "groupID", groupID) log.ZError(ctx, "DismissGroup Notification clear members", err, "groupID", groupID)
} }
}(groupID) }(groupID)
@ -255,6 +246,7 @@ func (p *Pusher) GetConnsAndOnlinePush(ctx context.Context, msg *sdkws.MsgData,
for _, v := range conns { for _, v := range conns {
msgClient := msggateway.NewMsgGatewayClient(v) msgClient := msggateway.NewMsgGatewayClient(v)
reply, err := msgClient.SuperGroupOnlineBatchPushOneMsg(ctx, &msggateway.OnlineBatchPushOneMsgReq{MsgData: msg, PushToUserIDs: pushToUserIDs}) reply, err := msgClient.SuperGroupOnlineBatchPushOneMsg(ctx, &msggateway.OnlineBatchPushOneMsgReq{MsgData: msg, PushToUserIDs: pushToUserIDs})
v.Close()
if err != nil { if err != nil {
continue continue
} }
@ -262,6 +254,7 @@ func (p *Pusher) GetConnsAndOnlinePush(ctx context.Context, msg *sdkws.MsgData,
if reply != nil && reply.SinglePushResult != nil { if reply != nil && reply.SinglePushResult != nil {
wsResults = append(wsResults, reply.SinglePushResult...) wsResults = append(wsResults, reply.SinglePushResult...)
} }
} }
return wsResults, nil return wsResults, nil
} }

@ -20,7 +20,7 @@ import (
type authServer struct { type authServer struct {
authDatabase controller.AuthDatabase authDatabase controller.AuthDatabase
userRpcClient *rpcclient.UserClient userRpcClient *rpcclient.UserRpcClient
RegisterCenter discoveryregistry.SvcDiscoveryRegistry RegisterCenter discoveryregistry.SvcDiscoveryRegistry
} }
@ -29,8 +29,9 @@ func Start(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) e
if err != nil { if err != nil {
return err return err
} }
userRpcClient := rpcclient.NewUserRpcClient(client)
pbAuth.RegisterAuthServer(server, &authServer{ pbAuth.RegisterAuthServer(server, &authServer{
userRpcClient: rpcclient.NewUserClient(client), userRpcClient: &userRpcClient,
RegisterCenter: client, RegisterCenter: client,
authDatabase: controller.NewAuthDatabase(cache.NewMsgCacheModel(rdb), config.Config.TokenPolicy.AccessSecret, config.Config.TokenPolicy.AccessExpire), authDatabase: controller.NewAuthDatabase(cache.NewMsgCacheModel(rdb), config.Config.TokenPolicy.AccessSecret, config.Config.TokenPolicy.AccessExpire),
}) })
@ -108,6 +109,7 @@ func (s *authServer) forceKickOff(ctx context.Context, userID string, platformID
client := msggateway.NewMsgGatewayClient(v) client := msggateway.NewMsgGatewayClient(v)
kickReq := &msggateway.KickUserOfflineReq{KickUserIDList: []string{userID}, PlatformID: platformID} kickReq := &msggateway.KickUserOfflineReq{KickUserIDList: []string{userID}, PlatformID: platformID}
_, err := client.KickUserOffline(ctx, kickReq) _, err := client.KickUserOffline(ctx, kickReq)
v.Close()
return utils.Wrap(err, "") return utils.Wrap(err, "")
} }
return errs.ErrInternalServer.Wrap() return errs.ErrInternalServer.Wrap()

@ -21,10 +21,10 @@ import (
) )
type conversationServer struct { type conversationServer struct {
groupRpcClient *rpcclient.GroupClient groupRpcClient *rpcclient.GroupRpcClient
conversationDatabase controller.ConversationDatabase conversationDatabase controller.ConversationDatabase
conversationNotificationSender *notification.ConversationNotificationSender conversationNotificationSender *notification.ConversationNotificationSender
msgRpcClient *rpcclient.MsgClient msgRpcClient *rpcclient.MessageRpcClient
} }
func Start(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) error { func Start(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) error {
@ -40,10 +40,12 @@ func Start(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) e
return err return err
} }
conversationDB := relation.NewConversationGorm(db) conversationDB := relation.NewConversationGorm(db)
groupRpcClient := rpcclient.NewGroupRpcClient(client)
msgRpcClient := rpcclient.NewMessageRpcClient(client)
pbConversation.RegisterConversationServer(server, &conversationServer{ pbConversation.RegisterConversationServer(server, &conversationServer{
conversationNotificationSender: notification.NewConversationNotificationSender(client), conversationNotificationSender: notification.NewConversationNotificationSender(client),
groupRpcClient: rpcclient.NewGroupClient(client), groupRpcClient: &groupRpcClient,
msgRpcClient: rpcclient.NewMsgClient(client), msgRpcClient: &msgRpcClient,
conversationDatabase: controller.NewConversationDatabase(conversationDB, cache.NewConversationRedis(rdb, cache.GetDefaultOpt(), conversationDB), tx.NewGorm(db)), conversationDatabase: controller.NewConversationDatabase(conversationDB, cache.NewConversationRedis(rdb, cache.GetDefaultOpt(), conversationDB), tx.NewGorm(db)),
}) })
return nil return nil

@ -25,7 +25,7 @@ import (
type friendServer struct { type friendServer struct {
friendDatabase controller.FriendDatabase friendDatabase controller.FriendDatabase
blackDatabase controller.BlackDatabase blackDatabase controller.BlackDatabase
userRpcClient *rpcclient.UserClient userRpcClient *rpcclient.UserRpcClient
notificationSender *notification.FriendNotificationSender notificationSender *notification.FriendNotificationSender
RegisterCenter registry.SvcDiscoveryRegistry RegisterCenter registry.SvcDiscoveryRegistry
} }
@ -44,12 +44,12 @@ func Start(client registry.SvcDiscoveryRegistry, server *grpc.Server) error {
} }
blackDB := relation.NewBlackGorm(db) blackDB := relation.NewBlackGorm(db)
friendDB := relation.NewFriendGorm(db) friendDB := relation.NewFriendGorm(db)
userRpcClient := rpcclient.NewUserClient(client) userRpcClient := rpcclient.NewUserRpcClient(client)
notificationSender := notification.NewFriendNotificationSender(client, notification.WithRpcFunc(userRpcClient.GetUsersInfo)) notificationSender := notification.NewFriendNotificationSender(client, notification.WithRpcFunc(userRpcClient.GetUsersInfo))
pbfriend.RegisterFriendServer(server, &friendServer{ pbfriend.RegisterFriendServer(server, &friendServer{
friendDatabase: controller.NewFriendDatabase(friendDB, relation.NewFriendRequestGorm(db), cache.NewFriendCacheRedis(rdb, friendDB, cache.GetDefaultOpt()), tx.NewGorm(db)), 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())), blackDatabase: controller.NewBlackDatabase(blackDB, cache.NewBlackCacheRedis(rdb, blackDB, cache.GetDefaultOpt())),
userRpcClient: userRpcClient, userRpcClient: &userRpcClient,
notificationSender: notificationSender, notificationSender: notificationSender,
RegisterCenter: client, RegisterCenter: client,
}) })

@ -22,11 +22,10 @@ type msgServer struct {
RegisterCenter discoveryregistry.SvcDiscoveryRegistry RegisterCenter discoveryregistry.SvcDiscoveryRegistry
MsgDatabase controller.CommonMsgDatabase MsgDatabase controller.CommonMsgDatabase
ExtendMsgDatabase controller.ExtendMsgDatabase ExtendMsgDatabase controller.ExtendMsgDatabase
Group *rpcclient.GroupClient Group *rpcclient.GroupRpcClient
User *rpcclient.UserClient User *rpcclient.UserRpcClient
Conversation *rpcclient.ConversationClient Conversation *rpcclient.ConversationRpcClient
friend *rpcclient.FriendClient friend *rpcclient.FriendRpcClient
black *rpcclient.BlackClient
GroupLocalCache *localcache.GroupLocalCache GroupLocalCache *localcache.GroupLocalCache
ConversationLocalCache *localcache.ConversationLocalCache ConversationLocalCache *localcache.ConversationLocalCache
MessageLocker MessageLocker MessageLocker MessageLocker
@ -67,17 +66,20 @@ func Start(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) e
extendMsgCacheModel := cache.NewExtendMsgSetCacheRedis(rdb, extendMsgModel, cache.GetDefaultOpt()) extendMsgCacheModel := cache.NewExtendMsgSetCacheRedis(rdb, extendMsgModel, cache.GetDefaultOpt())
extendMsgDatabase := controller.NewExtendMsgDatabase(extendMsgModel, extendMsgCacheModel, tx.NewMongo(mongo.GetClient())) extendMsgDatabase := controller.NewExtendMsgDatabase(extendMsgModel, extendMsgCacheModel, tx.NewMongo(mongo.GetClient()))
msgDatabase := controller.NewCommonMsgDatabase(msgDocModel, cacheModel) msgDatabase := controller.NewCommonMsgDatabase(msgDocModel, cacheModel)
conversationClient := rpcclient.NewConversationRpcClient(client)
userRpcClient := rpcclient.NewUserRpcClient(client)
groupRpcClient := rpcclient.NewGroupRpcClient(client)
friendRpcClient := rpcclient.NewFriendRpcClient(client)
s := &msgServer{ s := &msgServer{
Conversation: rpcclient.NewConversationClient(client), Conversation: &conversationClient,
User: rpcclient.NewUserClient(client), User: &userRpcClient,
Group: rpcclient.NewGroupClient(client), Group: &groupRpcClient,
MsgDatabase: msgDatabase, MsgDatabase: msgDatabase,
ExtendMsgDatabase: extendMsgDatabase, ExtendMsgDatabase: extendMsgDatabase,
RegisterCenter: client, RegisterCenter: client,
GroupLocalCache: localcache.NewGroupLocalCache(client), GroupLocalCache: localcache.NewGroupLocalCache(client),
ConversationLocalCache: localcache.NewConversationLocalCache(client), ConversationLocalCache: localcache.NewConversationLocalCache(client),
black: rpcclient.NewBlackClient(client), friend: &friendRpcClient,
friend: rpcclient.NewFriendClient(client),
MessageLocker: NewLockerMessage(cacheModel), MessageLocker: NewLockerMessage(cacheModel),
} }
s.notificationSender = rpcclient.NewNotificationSender(rpcclient.WithLocalSendMsg(s.SendMsg)) s.notificationSender = rpcclient.NewNotificationSender(rpcclient.WithLocalSendMsg(s.SendMsg))

@ -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 { if data.MsgData.ContentType <= constant.NotificationEnd && data.MsgData.ContentType >= constant.NotificationBegin {
return nil 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 { if err != nil {
return err return err
} }

@ -28,7 +28,7 @@ import (
type userServer struct { type userServer struct {
controller.UserDatabase controller.UserDatabase
notificationSender *notification.FriendNotificationSender notificationSender *notification.FriendNotificationSender
friendRpcClient *rpcclient.FriendClient friendRpcClient *rpcclient.FriendRpcClient
RegisterCenter registry.SvcDiscoveryRegistry RegisterCenter registry.SvcDiscoveryRegistry
} }
@ -54,10 +54,11 @@ func Start(client registry.SvcDiscoveryRegistry, server *grpc.Server) error {
userDB := relation.NewUserGorm(db) userDB := relation.NewUserGorm(db)
cache := cache.NewUserCacheRedis(rdb, userDB, cache.GetDefaultOpt()) cache := cache.NewUserCacheRedis(rdb, userDB, cache.GetDefaultOpt())
database := controller.NewUserDatabase(userDB, cache, tx.NewGorm(db)) database := controller.NewUserDatabase(userDB, cache, tx.NewGorm(db))
friendRpcClient := rpcclient.NewFriendRpcClient(client)
u := &userServer{ u := &userServer{
UserDatabase: database, UserDatabase: database,
RegisterCenter: client, RegisterCenter: client,
friendRpcClient: rpcclient.NewFriendClient(client), friendRpcClient: &friendRpcClient,
notificationSender: notification.NewFriendNotificationSender(client, notification.WithDBFunc(database.FindWithError)), notificationSender: notification.NewFriendNotificationSender(client, notification.WithDBFunc(database.FindWithError)),
} }
pbuser.RegisterUserServer(server, u) pbuser.RegisterUserServer(server, u)

@ -12,7 +12,7 @@ import (
func Call[A, B, C any]( func Call[A, B, C any](
rpc func(client C, ctx context.Context, req *A, options ...grpc.CallOption) (*B, error), rpc func(client C, ctx context.Context, req *A, options ...grpc.CallOption) (*B, error),
client func(ctx context.Context) (C, error), client C,
c *gin.Context, c *gin.Context,
) { ) {
var req A var req A
@ -28,15 +28,7 @@ func Call[A, B, C any](
return return
} }
} }
log.ZDebug(c, "gin bind json success", "req", req) data, err := rpc(client, c, &req)
cli, err := client(c)
if err != nil {
log.ZError(c, "get conn error", err, "req", req)
apiresp.GinError(c, errs.ErrInternalServer.Wrap(err.Error())) // 获取RPC连接失败
return
}
log.ZDebug(c, "get conn success", "req", req)
data, err := rpc(cli, c, &req)
if err != nil { if err != nil {
apiresp.GinError(c, err) // RPC调用失败 apiresp.GinError(c, err) // RPC调用失败
return return

@ -4,16 +4,16 @@ import (
"context" "context"
"sync" "sync"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/conversation" "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/conversation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient"
) )
type ConversationLocalCache struct { type ConversationLocalCache struct {
lock sync.Mutex lock sync.Mutex
superGroupRecvMsgNotNotifyUserIDs map[string]Hash superGroupRecvMsgNotNotifyUserIDs map[string]Hash
conversationIDs map[string]Hash conversationIDs map[string]Hash
client discoveryregistry.SvcDiscoveryRegistry client *rpcclient.Conversation
} }
type Hash struct { type Hash struct {
@ -21,21 +21,16 @@ type Hash struct {
ids []string ids []string
} }
func NewConversationLocalCache(client discoveryregistry.SvcDiscoveryRegistry) *ConversationLocalCache { func NewConversationLocalCache(discov discoveryregistry.SvcDiscoveryRegistry) *ConversationLocalCache {
return &ConversationLocalCache{ return &ConversationLocalCache{
superGroupRecvMsgNotNotifyUserIDs: make(map[string]Hash), superGroupRecvMsgNotNotifyUserIDs: make(map[string]Hash),
conversationIDs: make(map[string]Hash), conversationIDs: make(map[string]Hash),
client: client, client: rpcclient.NewConversation(discov),
} }
} }
func (g *ConversationLocalCache) GetRecvMsgNotNotifyUserIDs(ctx context.Context, groupID string) ([]string, error) { func (g *ConversationLocalCache) GetRecvMsgNotNotifyUserIDs(ctx context.Context, groupID string) ([]string, error) {
conn, err := g.client.GetConn(ctx, config.Config.RpcRegisterName.OpenImConversationName) resp, err := g.client.Client.GetRecvMsgNotNotifyUserIDs(ctx, &conversation.GetRecvMsgNotNotifyUserIDsReq{
if err != nil {
return nil, err
}
client := conversation.NewConversationClient(conn)
resp, err := client.GetRecvMsgNotNotifyUserIDs(ctx, &conversation.GetRecvMsgNotNotifyUserIDsReq{
GroupID: groupID, GroupID: groupID,
}) })
if err != nil { if err != nil {
@ -45,12 +40,7 @@ func (g *ConversationLocalCache) GetRecvMsgNotNotifyUserIDs(ctx context.Context,
} }
func (g *ConversationLocalCache) GetConversationIDs(ctx context.Context, userID string) ([]string, error) { func (g *ConversationLocalCache) GetConversationIDs(ctx context.Context, userID string) ([]string, error) {
conn, err := g.client.GetConn(ctx, config.Config.RpcRegisterName.OpenImConversationName) resp, err := g.client.Client.GetUserConversationIDsHash(ctx, &conversation.GetUserConversationIDsHashReq{
if err != nil {
return nil, err
}
client := conversation.NewConversationClient(conn)
resp, err := client.GetUserConversationIDsHash(ctx, &conversation.GetUserConversationIDsHashReq{
OwnerUserID: userID, OwnerUserID: userID,
}) })
if err != nil { if err != nil {
@ -60,7 +50,7 @@ func (g *ConversationLocalCache) GetConversationIDs(ctx context.Context, userID
defer g.lock.Unlock() defer g.lock.Unlock()
hash, ok := g.conversationIDs[userID] hash, ok := g.conversationIDs[userID]
if !ok || hash.hash != resp.Hash { if !ok || hash.hash != resp.Hash {
conversationIDsResp, err := client.GetConversationIDs(ctx, &conversation.GetConversationIDsReq{ conversationIDsResp, err := g.client.Client.GetConversationIDs(ctx, &conversation.GetConversationIDsReq{
UserID: userID, UserID: userID,
}) })
if err != nil { if err != nil {

@ -4,17 +4,16 @@ import (
"context" "context"
"sync" "sync"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"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/errs"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/group" "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/group"
"google.golang.org/grpc" "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient"
) )
type GroupLocalCache struct { type GroupLocalCache struct {
lock sync.Mutex lock sync.Mutex
cache map[string]GroupMemberIDsHash cache map[string]GroupMemberIDsHash
conn *grpc.ClientConn client *rpcclient.Group
} }
type GroupMemberIDsHash struct { type GroupMemberIDsHash struct {
@ -22,22 +21,16 @@ type GroupMemberIDsHash struct {
userIDs []string userIDs []string
} }
func NewGroupLocalCache(client discoveryregistry.SvcDiscoveryRegistry) *GroupLocalCache { func NewGroupLocalCache(discov discoveryregistry.SvcDiscoveryRegistry) *GroupLocalCache {
conn, err := client.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImGroupName) client := rpcclient.NewGroup(discov)
if err != nil {
panic(err)
}
return &GroupLocalCache{ return &GroupLocalCache{
cache: make(map[string]GroupMemberIDsHash, 0), cache: make(map[string]GroupMemberIDsHash, 0),
conn: conn, client: client,
} }
} }
func (g *GroupLocalCache) GetGroupMemberIDs(ctx context.Context, groupID string) ([]string, error) { func (g *GroupLocalCache) GetGroupMemberIDs(ctx context.Context, groupID string) ([]string, error) {
g.lock.Lock() resp, err := g.client.Client.GetGroupAbstractInfo(ctx, &group.GetGroupAbstractInfoReq{
defer g.lock.Unlock()
client := group.NewGroupClient(g.conn)
resp, err := client.GetGroupAbstractInfo(ctx, &group.GetGroupAbstractInfoReq{
GroupIDs: []string{groupID}, GroupIDs: []string{groupID},
}) })
if err != nil { if err != nil {
@ -46,11 +39,13 @@ func (g *GroupLocalCache) GetGroupMemberIDs(ctx context.Context, groupID string)
if len(resp.GroupAbstractInfos) < 1 { if len(resp.GroupAbstractInfos) < 1 {
return nil, errs.ErrGroupIDNotFound return nil, errs.ErrGroupIDNotFound
} }
g.lock.Lock()
defer g.lock.Unlock()
localHashInfo, ok := g.cache[groupID] localHashInfo, ok := g.cache[groupID]
if ok && localHashInfo.memberListHash == resp.GroupAbstractInfos[0].GroupMemberListHash { if ok && localHashInfo.memberListHash == resp.GroupAbstractInfos[0].GroupMemberListHash {
return localHashInfo.userIDs, nil return localHashInfo.userIDs, nil
} }
groupMembersResp, err := client.GetGroupMemberUserIDs(ctx, &group.GetGroupMemberUserIDsReq{ groupMembersResp, err := g.client.Client.GetGroupMemberUserIDs(ctx, &group.GetGroupMemberUserIDsReq{
GroupID: groupID, GroupID: groupID,
}) })
if err != nil { if err != nil {

@ -11,6 +11,7 @@ type Conn interface {
GetConns(ctx context.Context, serviceName string, opts ...grpc.DialOption) ([]*grpc.ClientConn, error) GetConns(ctx context.Context, serviceName string, opts ...grpc.DialOption) ([]*grpc.ClientConn, error)
GetConn(ctx context.Context, serviceName string, opts ...grpc.DialOption) (*grpc.ClientConn, error) GetConn(ctx context.Context, serviceName string, opts ...grpc.DialOption) (*grpc.ClientConn, error)
AddOption(opts ...grpc.DialOption) AddOption(opts ...grpc.DialOption)
CloseConn(conn *grpc.ClientConn)
// do not use this method for call rpc // do not use this method for call rpc
GetClientLocalConns() map[string][]resolver.Address GetClientLocalConns() map[string][]resolver.Address
} }

@ -1,51 +0,0 @@
package discoveryregistry
// type DnsDiscoveryRegistry struct {
// opts []grpc.DialOption
// namespace string
// clientset *kubernetes.Clientset
// }
// func NewDnsDiscoveryRegistry(namespace string, opts []grpc.DialOption) (*DnsDiscoveryRegistry, error) {
// config, err := rest.InClusterConfig()
// if err != nil {
// return nil, err
// }
// clientset, err := kubernetes.NewForConfig(config)
// if err != nil {
// return nil, err
// }
// return &DnsDiscoveryRegistry{
// clientset: clientset,
// namespace: namespace,
// opts: opts,
// }, nil
// }
// func (d DnsDiscoveryRegistry) GetConns(ctx context.Context, serviceName string, opts ...grpc.DialOption) ([]*grpc.ClientConn, error) {
// endpoints, err := d.clientset.CoreV1().Endpoints(d.namespace).Get(context.TODO(), serviceName, metav1.GetOptions{})
// if err != nil {
// return nil, err
// }
// var conns []*grpc.ClientConn
// for _, subset := range endpoints.Subsets {
// for _, address := range subset.Addresses {
// for _, port := range subset.Ports {
// conn, err := grpc.DialContext(ctx, net.JoinHostPort(address.IP, string(port.Port)), append(d.opts, opts...)...)
// if err != nil {
// return nil, err
// }
// conns = append(conns, conn)
// }
// }
// }
// return conns, nil
// }
// func (d DnsDiscoveryRegistry) GetConn(ctx context.Context, serviceName string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
// return grpc.DialContext(ctx, fmt.Sprintf("%s.%s.svc.cluster.local", serviceName, d.namespace), append(d.opts, opts...)...)
// }
// func (d *DnsDiscoveryRegistry) AddOption(opts ...grpc.DialOption) {
// d.opts = append(d.opts, opts...)
// }

@ -22,7 +22,6 @@ func (s *ZkClient) watch(wg *sync.WaitGroup) {
switch event.Type { switch event.Type {
case zk.EventSession: case zk.EventSession:
s.logger.Printf("zk session event: %+v", event) s.logger.Printf("zk session event: %+v", event)
case zk.EventNodeCreated:
case zk.EventNodeChildrenChanged: case zk.EventNodeChildrenChanged:
s.logger.Printf("zk event: %s", event.Path) s.logger.Printf("zk event: %s", event.Path)
l := strings.Split(event.Path, "/") l := strings.Split(event.Path, "/")
@ -37,6 +36,7 @@ func (s *ZkClient) watch(wg *sync.WaitGroup) {
} }
s.logger.Printf("zk event handle success: %s", event.Path) s.logger.Printf("zk event handle success: %s", event.Path)
case zk.EventNodeDataChanged: case zk.EventNodeDataChanged:
case zk.EventNodeCreated:
case zk.EventNodeDeleted: case zk.EventNodeDeleted:
case zk.EventNotWatching: case zk.EventNotWatching:
} }
@ -101,24 +101,10 @@ func (s *ZkClient) GetConns(ctx context.Context, serviceName string, opts ...grp
} }
func (s *ZkClient) GetConn(ctx context.Context, serviceName string, opts ...grpc.DialOption) (*grpc.ClientConn, error) { func (s *ZkClient) GetConn(ctx context.Context, serviceName string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
conns, err := s.GetConns(ctx, serviceName, opts...) newOpts := append(s.options, grpc.WithDefaultServiceConfig(fmt.Sprintf(`{"LoadBalancingPolicy": "%s"}`, s.balancerName)))
if err != nil { return grpc.DialContext(ctx, fmt.Sprintf("%s:///%s", s.scheme, serviceName), append(newOpts, opts...)...)
return nil, err
}
if len(conns) == 0 {
return nil, ErrConnIsNil
}
s.logger.Printf("get conn from conns, serviceName: %s", serviceName)
return s.getConnBalance(conns)
} }
func (s *ZkClient) GetFirstConn(ctx context.Context, serviceName string, opts ...grpc.DialOption) (*grpc.ClientConn, error) { func (s *ZkClient) CloseConn(conn *grpc.ClientConn) {
conns, err := s.GetConns(ctx, serviceName, opts...) conn.Close()
if err != nil {
return nil, err
}
if len(conns) == 0 {
return nil, ErrConnIsNil
}
return conns[0], nil
} }

@ -113,7 +113,7 @@ func NewClient(zkServers []string, zkRoot string, options ...ZkOption) (*ZkClien
client.CloseZK() client.CloseZK()
return nil, err return nil, err
} }
// resolver.Register(client) resolver.Register(client)
var wg sync.WaitGroup var wg sync.WaitGroup
go client.refresh(&wg) go client.refresh(&wg)
go client.watch(&wg) go client.watch(&wg)

@ -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
}

@ -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
}

@ -5,100 +5,83 @@ import (
"fmt" "fmt"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "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/errs"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/conversation"
pbConversation "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 { type Conversation struct {
*MetaClient Client conversation.ConversationClient
conn *grpc.ClientConn
discov discoveryregistry.SvcDiscoveryRegistry
} }
func NewConversationClient(zk discoveryRegistry.SvcDiscoveryRegistry) *ConversationClient { func NewConversation(discov discoveryregistry.SvcDiscoveryRegistry) *Conversation {
return &ConversationClient{NewMetaClient(zk, config.Config.RpcRegisterName.OpenImConversationName)} 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}
} }
func (c *ConversationClient) ModifyConversationField(ctx context.Context, req *pbConversation.ModifyConversationFieldReq) error { type ConversationRpcClient Conversation
cc, err := c.getConn(ctx)
if err != nil { func NewConversationRpcClient(discov discoveryregistry.SvcDiscoveryRegistry) ConversationRpcClient {
return err return ConversationRpcClient(*NewConversation(discov))
} }
_, err = pbConversation.NewConversationClient(cc).ModifyConversationField(ctx, req)
func (c *ConversationRpcClient) ModifyConversationField(ctx context.Context, req *pbConversation.ModifyConversationFieldReq) error {
_, err := c.Client.ModifyConversationField(ctx, req)
return err return err
} }
func (c *ConversationClient) GetSingleConversationRecvMsgOpt(ctx context.Context, userID, conversationID string) (int32, error) { func (c *ConversationRpcClient) GetSingleConversationRecvMsgOpt(ctx context.Context, userID, conversationID string) (int32, error) {
cc, err := c.getConn(ctx)
if err != nil {
return 0, err
}
var req pbConversation.GetConversationReq var req pbConversation.GetConversationReq
req.OwnerUserID = userID req.OwnerUserID = userID
req.ConversationID = conversationID req.ConversationID = conversationID
conversation, err := pbConversation.NewConversationClient(cc).GetConversation(ctx, &req) conversation, err := c.Client.GetConversation(ctx, &req)
if err != nil { if err != nil {
return 0, err return 0, err
} }
return conversation.GetConversation().RecvMsgOpt, err return conversation.GetConversation().RecvMsgOpt, err
} }
func (c *ConversationClient) SingleChatFirstCreateConversation(ctx context.Context, recvID, sendID string) error { func (c *ConversationRpcClient) SingleChatFirstCreateConversation(ctx context.Context, recvID, sendID string) error {
cc, err := c.getConn(ctx) _, err := c.Client.CreateSingleChatConversations(ctx, &pbConversation.CreateSingleChatConversationsReq{RecvID: recvID, SendID: sendID})
if err != nil {
return err
}
_, err = pbConversation.NewConversationClient(cc).CreateSingleChatConversations(ctx, &pbConversation.CreateSingleChatConversationsReq{RecvID: recvID, SendID: sendID})
return err return err
} }
func (c *ConversationClient) GroupChatFirstCreateConversation(ctx context.Context, groupID string, userIDs []string) error { func (c *ConversationRpcClient) GroupChatFirstCreateConversation(ctx context.Context, groupID string, userIDs []string) error {
cc, err := c.getConn(ctx) _, err := c.Client.CreateGroupChatConversations(ctx, &pbConversation.CreateGroupChatConversationsReq{UserIDs: userIDs, GroupID: groupID})
if err != nil {
return err
}
_, err = pbConversation.NewConversationClient(cc).CreateGroupChatConversations(ctx, &pbConversation.CreateGroupChatConversationsReq{UserIDs: userIDs, GroupID: groupID})
return err return err
} }
func (c *ConversationClient) SetConversationMaxSeq(ctx context.Context, ownerUserIDs []string, conversationID string, maxSeq int64) error { func (c *ConversationRpcClient) SetConversationMaxSeq(ctx context.Context, ownerUserIDs []string, conversationID string, maxSeq int64) error {
cc, err := c.getConn(ctx) _, err := c.Client.SetConversationMaxSeq(ctx, &pbConversation.SetConversationMaxSeqReq{OwnerUserID: ownerUserIDs, ConversationID: conversationID, MaxSeq: maxSeq})
if err != nil {
return err
}
_, err = pbConversation.NewConversationClient(cc).SetConversationMaxSeq(ctx, &pbConversation.SetConversationMaxSeqReq{OwnerUserID: ownerUserIDs, ConversationID: conversationID, MaxSeq: maxSeq})
return err return err
} }
func (c *ConversationClient) GetConversationIDs(ctx context.Context, ownerUserID string) ([]string, error) { func (c *ConversationRpcClient) GetConversationIDs(ctx context.Context, ownerUserID string) ([]string, error) {
cc, err := c.getConn(ctx) resp, err := c.Client.GetConversationIDs(ctx, &pbConversation.GetConversationIDsReq{UserID: ownerUserID})
if err != nil {
return nil, err
}
resp, err := pbConversation.NewConversationClient(cc).GetConversationIDs(ctx, &pbConversation.GetConversationIDsReq{UserID: ownerUserID})
if err != nil { if err != nil {
return nil, err return nil, err
} }
return resp.ConversationIDs, nil return resp.ConversationIDs, nil
} }
func (c *ConversationClient) GetConversation(ctx context.Context, ownerUserID, conversationID string) (*pbConversation.Conversation, error) { func (c *ConversationRpcClient) GetConversation(ctx context.Context, ownerUserID, conversationID string) (*pbConversation.Conversation, error) {
cc, err := c.getConn(ctx) resp, err := c.Client.GetConversation(ctx, &pbConversation.GetConversationReq{OwnerUserID: ownerUserID, ConversationID: conversationID})
if err != nil {
return nil, err
}
resp, err := pbConversation.NewConversationClient(cc).GetConversation(ctx, &pbConversation.GetConversationReq{OwnerUserID: ownerUserID, ConversationID: conversationID})
if err != nil { if err != nil {
return nil, err return nil, err
} }
return resp.Conversation, nil return resp.Conversation, nil
} }
func (c *ConversationClient) GetConversationsByConversationID(ctx context.Context, conversationIDs []string) ([]*pbConversation.Conversation, error) { func (c *ConversationRpcClient) GetConversationsByConversationID(ctx context.Context, conversationIDs []string) ([]*pbConversation.Conversation, error) {
cc, err := c.getConn(ctx) resp, err := c.Client.GetConversationsByConversationID(ctx, &pbConversation.GetConversationsByConversationIDReq{ConversationIDs: conversationIDs})
if err != nil {
return nil, err
}
resp, err := pbConversation.NewConversationClient(cc).GetConversationsByConversationID(ctx, &pbConversation.GetConversationsByConversationIDReq{ConversationIDs: conversationIDs})
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -108,12 +91,8 @@ func (c *ConversationClient) GetConversationsByConversationID(ctx context.Contex
return resp.Conversations, nil return resp.Conversations, nil
} }
func (c *ConversationClient) GetConversations(ctx context.Context, ownerUserID string, conversationIDs []string) ([]*pbConversation.Conversation, error) { func (c *ConversationRpcClient) GetConversations(ctx context.Context, ownerUserID string, conversationIDs []string) ([]*pbConversation.Conversation, error) {
cc, err := c.getConn(ctx) resp, err := c.Client.GetConversations(ctx, &pbConversation.GetConversationsReq{OwnerUserID: ownerUserID, ConversationIDs: conversationIDs})
if err != nil {
return nil, err
}
resp, err := pbConversation.NewConversationClient(cc).GetConversations(ctx, &pbConversation.GetConversationsReq{OwnerUserID: ownerUserID, ConversationIDs: conversationIDs})
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -4,25 +4,35 @@ import (
"context" "context"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "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" "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/friend"
sdkws "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws" sdkws "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
"google.golang.org/grpc"
) )
type FriendClient struct { type Friend struct {
*MetaClient conn *grpc.ClientConn
Client friend.FriendClient
discov discoveryregistry.SvcDiscoveryRegistry
} }
func NewFriendClient(zk discoveryRegistry.SvcDiscoveryRegistry) *FriendClient { func NewFriend(discov discoveryregistry.SvcDiscoveryRegistry) *Friend {
return &FriendClient{NewMetaClient(zk, config.Config.RpcRegisterName.OpenImFriendName)} 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}
} }
func (f *FriendClient) GetFriendsInfo(ctx context.Context, ownerUserID, friendUserID string) (resp *sdkws.FriendInfo, err error) { type FriendRpcClient Friend
cc, err := f.getConn(ctx)
if err != nil { func NewFriendRpcClient(discov discoveryregistry.SvcDiscoveryRegistry) FriendRpcClient {
return nil, err return FriendRpcClient(*NewFriend(discov))
} }
r, err := friend.NewFriendClient(cc).GetDesignatedFriends(ctx, &friend.GetDesignatedFriendsReq{OwnerUserID: ownerUserID, FriendUserIDs: []string{friendUserID}})
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 { if err != nil {
return nil, err return nil, err
} }
@ -31,12 +41,8 @@ func (f *FriendClient) GetFriendsInfo(ctx context.Context, ownerUserID, friendUs
} }
// possibleFriendUserID是否在userID的好友中 // possibleFriendUserID是否在userID的好友中
func (f *FriendClient) IsFriend(ctx context.Context, possibleFriendUserID, userID string) (bool, error) { func (f *FriendRpcClient) IsFriend(ctx context.Context, possibleFriendUserID, userID string) (bool, error) {
cc, err := f.getConn(ctx) resp, err := f.Client.IsFriend(ctx, &friend.IsFriendReq{UserID1: userID, UserID2: possibleFriendUserID})
if err != nil {
return false, err
}
resp, err := friend.NewFriendClient(cc).IsFriend(ctx, &friend.IsFriendReq{UserID1: userID, UserID2: possibleFriendUserID})
if err != nil { if err != nil {
return false, err 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) { func (f *FriendRpcClient) GetFriendIDs(ctx context.Context, ownerUserID string) (friendIDs []string, err error) {
cc, err := f.getConn(ctx)
if err != nil {
return nil, err
}
req := friend.GetFriendIDsReq{UserID: ownerUserID} req := friend.GetFriendIDsReq{UserID: ownerUserID}
resp, err := friend.NewFriendClient(cc).GetFriendIDs(ctx, &req) resp, err := f.Client.GetFriendIDs(ctx, &req)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return resp.FriendIDs, nil 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
}

@ -11,27 +11,32 @@ import (
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/group" "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/proto/sdkws"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils" "github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
"google.golang.org/grpc"
) )
type GroupClient struct { type Group struct {
MetaClient conn *grpc.ClientConn
Client group.GroupClient
discov discoveryregistry.SvcDiscoveryRegistry
} }
func NewGroupClient(client discoveryregistry.SvcDiscoveryRegistry) *GroupClient { func NewGroup(discov discoveryregistry.SvcDiscoveryRegistry) *Group {
return &GroupClient{ conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImGroupName)
MetaClient: MetaClient{ if err != nil {
client: client, panic(err)
rpcRegisterName: config.Config.RpcRegisterName.OpenImGroupName,
},
} }
client := group.NewGroupClient(conn)
return &Group{discov: discov, conn: conn, Client: client}
} }
func (g *GroupClient) GetGroupInfos(ctx context.Context, groupIDs []string, complete bool) ([]*sdkws.GroupInfo, error) { type GroupRpcClient Group
cc, err := g.getConn(ctx)
if err != nil { func NewGroupRpcClient(discov discoveryregistry.SvcDiscoveryRegistry) GroupRpcClient {
return nil, err return GroupRpcClient(*NewGroup(discov))
} }
resp, err := group.NewGroupClient(cc).GetGroupsInfo(ctx, &group.GetGroupsInfoReq{
func (g *GroupRpcClient) GetGroupInfos(ctx context.Context, groupIDs []string, complete bool) ([]*sdkws.GroupInfo, error) {
resp, err := g.Client.GetGroupsInfo(ctx, &group.GetGroupsInfoReq{
GroupIDs: groupIDs, GroupIDs: groupIDs,
}) })
if err != nil { if err != nil {
@ -47,7 +52,7 @@ func (g *GroupClient) GetGroupInfos(ctx context.Context, groupIDs []string, comp
return resp.GroupInfos, nil 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) groups, err := g.GetGroupInfos(ctx, []string{groupID}, true)
if err != nil { if err != nil {
return nil, err return nil, err
@ -55,7 +60,7 @@ func (g *GroupClient) GetGroupInfo(ctx context.Context, groupID string) (*sdkws.
return groups[0], nil 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) groups, err := g.GetGroupInfos(ctx, groupIDs, complete)
if err != nil { if err != nil {
return nil, err return nil, err
@ -65,12 +70,8 @@ func (g *GroupClient) GetGroupInfoMap(ctx context.Context, groupIDs []string, co
}), nil }), nil
} }
func (g *GroupClient) GetGroupMemberInfos(ctx context.Context, groupID string, userIDs []string, complete bool) ([]*sdkws.GroupMemberFullInfo, error) { func (g *GroupRpcClient) GetGroupMemberInfos(ctx context.Context, groupID string, userIDs []string, complete bool) ([]*sdkws.GroupMemberFullInfo, error) {
cc, err := g.getConn(ctx) resp, err := g.Client.GetGroupMembersInfo(ctx, &group.GetGroupMembersInfoReq{
if err != nil {
return nil, err
}
resp, err := group.NewGroupClient(cc).GetGroupMembersInfo(ctx, &group.GetGroupMembersInfoReq{
GroupID: groupID, GroupID: groupID,
UserIDs: userIDs, UserIDs: userIDs,
}) })
@ -87,7 +88,7 @@ func (g *GroupClient) GetGroupMemberInfos(ctx context.Context, groupID string, u
return resp.Members, nil 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) members, err := g.GetGroupMemberInfos(ctx, groupID, []string{userID}, true)
if err != nil { if err != nil {
return nil, err return nil, err
@ -95,7 +96,7 @@ func (g *GroupClient) GetGroupMemberInfo(ctx context.Context, groupID string, us
return members[0], nil 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) members, err := g.GetGroupMemberInfos(ctx, groupID, userIDs, true)
if err != nil { if err != nil {
return nil, err return nil, err
@ -105,12 +106,8 @@ func (g *GroupClient) GetGroupMemberInfoMap(ctx context.Context, groupID string,
}), nil }), nil
} }
func (g *GroupClient) GetOwnerAndAdminInfos(ctx context.Context, groupID string) ([]*sdkws.GroupMemberFullInfo, error) { func (g *GroupRpcClient) GetOwnerAndAdminInfos(ctx context.Context, groupID string) ([]*sdkws.GroupMemberFullInfo, error) {
cc, err := g.getConn(ctx) resp, err := g.Client.GetGroupMemberRoleLevel(ctx, &group.GetGroupMemberRoleLevelReq{
if err != nil {
return nil, err
}
resp, err := group.NewGroupClient(cc).GetGroupMemberRoleLevel(ctx, &group.GetGroupMemberRoleLevelReq{
GroupID: groupID, GroupID: groupID,
RoleLevels: []int32{constant.GroupOwner, constant.GroupAdmin}, RoleLevels: []int32{constant.GroupOwner, constant.GroupAdmin},
}) })
@ -120,24 +117,16 @@ func (g *GroupClient) GetOwnerAndAdminInfos(ctx context.Context, groupID string)
return resp.Members, nil return resp.Members, nil
} }
func (g *GroupClient) GetOwnerInfo(ctx context.Context, groupID string) (*sdkws.GroupMemberFullInfo, error) { func (g *GroupRpcClient) GetOwnerInfo(ctx context.Context, groupID string) (*sdkws.GroupMemberFullInfo, error) {
cc, err := g.getConn(ctx) resp, err := g.Client.GetGroupMemberRoleLevel(ctx, &group.GetGroupMemberRoleLevelReq{
if err != nil {
return nil, err
}
resp, err := group.NewGroupClient(cc).GetGroupMemberRoleLevel(ctx, &group.GetGroupMemberRoleLevelReq{
GroupID: groupID, GroupID: groupID,
RoleLevels: []int32{constant.GroupOwner}, RoleLevels: []int32{constant.GroupOwner},
}) })
return resp.Members[0], err return resp.Members[0], err
} }
func (g *GroupClient) GetGroupMemberIDs(ctx context.Context, groupID string) ([]string, error) { func (g *GroupRpcClient) GetGroupMemberIDs(ctx context.Context, groupID string) ([]string, error) {
cc, err := g.getConn(ctx) resp, err := g.Client.GetGroupMemberUserIDs(ctx, &group.GetGroupMemberUserIDsReq{
if err != nil {
return nil, err
}
resp, err := group.NewGroupClient(cc).GetGroupMemberUserIDs(ctx, &group.GetGroupMemberUserIDsReq{
GroupID: groupID, GroupID: groupID,
}) })
if err != nil { if err != nil {
@ -146,12 +135,8 @@ func (g *GroupClient) GetGroupMemberIDs(ctx context.Context, groupID string) ([]
return resp.UserIDs, nil return resp.UserIDs, nil
} }
func (g *GroupClient) GetGroupInfoCache(ctx context.Context, groupID string) (*sdkws.GroupInfo, error) { func (g *GroupRpcClient) GetGroupInfoCache(ctx context.Context, groupID string) (*sdkws.GroupInfo, error) {
cc, err := g.getConn(ctx) resp, err := g.Client.GetGroupInfoCache(ctx, &group.GetGroupInfoCacheReq{
if err != nil {
return nil, err
}
resp, err := group.NewGroupClient(cc).GetGroupInfoCache(ctx, &group.GetGroupInfoCacheReq{
GroupID: groupID, GroupID: groupID,
}) })
if err != nil { if err != nil {
@ -160,12 +145,8 @@ func (g *GroupClient) GetGroupInfoCache(ctx context.Context, groupID string) (*s
return resp.GroupInfo, nil return resp.GroupInfo, nil
} }
func (g *GroupClient) GetGroupMemberCache(ctx context.Context, groupID string, groupMemberID string) (*sdkws.GroupMemberFullInfo, error) { func (g *GroupRpcClient) GetGroupMemberCache(ctx context.Context, groupID string, groupMemberID string) (*sdkws.GroupMemberFullInfo, error) {
cc, err := g.getConn(ctx) resp, err := g.Client.GetGroupMemberCache(ctx, &group.GetGroupMemberCacheReq{
if err != nil {
return nil, err
}
resp, err := group.NewGroupClient(cc).GetGroupMemberCache(ctx, &group.GetGroupMemberCacheReq{
GroupID: groupID, GroupID: groupID,
GroupMemberID: groupMemberID, GroupMemberID: groupMemberID,
}) })
@ -174,3 +155,11 @@ func (g *GroupClient) GetGroupMemberCache(ctx context.Context, groupID string, g
} }
return resp.Member, nil return resp.Member, nil
} }
func (g *GroupRpcClient) DismissGroup(ctx context.Context, groupID string) error {
_, err := g.Client.DismissGroup(ctx, &group.DismissGroupReq{
GroupID: groupID,
DeleteMember: true,
})
return err
}

@ -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)
}

@ -103,48 +103,44 @@ func newSessionTypeConf() map[int32]int32 {
} }
} }
type MsgClient struct { type Message struct {
conn *grpc.ClientConn conn *grpc.ClientConn
*MetaClient Client msg.MsgClient
discov discoveryregistry.SvcDiscoveryRegistry
} }
func NewMsgClient(discov discoveryregistry.SvcDiscoveryRegistry) *MsgClient { func NewMessage(discov discoveryregistry.SvcDiscoveryRegistry) *Message {
return &MsgClient{MetaClient: NewMetaClient(discov, config.Config.RpcRegisterName.OpenImMsgName)} conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImMsgName)
}
func (m *MsgClient) SendMsg(ctx context.Context, req *msg.SendMsgReq) (*msg.SendMsgResp, error) {
cc, err := m.getConn(ctx)
if err != nil { if err != nil {
return nil, err panic(err)
} }
resp, err := msg.NewMsgClient(cc).SendMsg(ctx, req) client := msg.NewMsgClient(conn)
return resp, err return &Message{discov: discov, conn: conn, Client: client}
} }
func (m *MsgClient) GetMaxSeq(ctx context.Context, req *sdkws.GetMaxSeqReq) (*sdkws.GetMaxSeqResp, error) { type MessageRpcClient Message
cc, err := m.getConn(ctx)
if err != nil { func NewMessageRpcClient(discov discoveryregistry.SvcDiscoveryRegistry) MessageRpcClient {
return nil, err return MessageRpcClient(*NewMessage(discov))
} }
resp, err := msg.NewMsgClient(cc).GetMaxSeq(ctx, req)
func (m *MessageRpcClient) SendMsg(ctx context.Context, req *msg.SendMsgReq) (*msg.SendMsgResp, error) {
resp, err := m.Client.SendMsg(ctx, req)
return resp, err return resp, err
} }
func (m *MsgClient) PullMessageBySeqList(ctx context.Context, req *sdkws.PullMessageBySeqsReq) (*sdkws.PullMessageBySeqsResp, error) { func (m *MessageRpcClient) GetMaxSeq(ctx context.Context, req *sdkws.GetMaxSeqReq) (*sdkws.GetMaxSeqResp, error) {
cc, err := m.getConn(ctx) resp, err := m.Client.GetMaxSeq(ctx, req)
if err != nil {
return nil, err
}
resp, err := msg.NewMsgClient(cc).PullMessageBySeqs(ctx, req)
return resp, err return resp, err
} }
func (m *MsgClient) GetConversationMaxSeq(ctx context.Context, conversationID string) (int64, error) { func (m *MessageRpcClient) PullMessageBySeqList(ctx context.Context, req *sdkws.PullMessageBySeqsReq) (*sdkws.PullMessageBySeqsResp, error) {
cc, err := m.getConn(ctx) resp, err := m.Client.PullMessageBySeqs(ctx, req)
if err != nil { return resp, err
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 { if err != nil {
return 0, err return 0, err
} }
@ -167,7 +163,8 @@ func WithLocalSendMsg(sendMsg func(ctx context.Context, req *msg.SendMsgReq) (*m
func WithDiscov(discov discoveryregistry.SvcDiscoveryRegistry) NewNotificationSenderOptions { func WithDiscov(discov discoveryregistry.SvcDiscoveryRegistry) NewNotificationSenderOptions {
return func(s *NotificationSender) { return func(s *NotificationSender) {
s.sendMsg = NewMsgClient(discov).SendMsg rpcClient := NewMessageRpcClient(discov)
s.sendMsg = rpcClient.SendMsg
} }
} }

@ -16,11 +16,11 @@ import (
) )
type ExtendMsgNotificationSender struct { type ExtendMsgNotificationSender struct {
*rpcclient.MsgClient *rpcclient.MessageRpcClient
} }
func NewExtendMsgNotificationSender(client discoveryregistry.SvcDiscoveryRegistry) *ExtendMsgNotificationSender { 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, func (e *ExtendMsgNotificationSender) ExtendMessageUpdatedNotification(ctx context.Context, sendID string, conversationID string, sessionType int32,

@ -6,29 +6,33 @@ import (
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/push" "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/push"
"google.golang.org/grpc"
) )
type PushClient struct { type Push struct {
MetaClient conn *grpc.ClientConn
Client push.PushMsgServiceClient
discov discoveryregistry.SvcDiscoveryRegistry
} }
func NewPushClient(client discoveryregistry.SvcDiscoveryRegistry) *PushClient { func NewPush(discov discoveryregistry.SvcDiscoveryRegistry) *Push {
return &PushClient{ conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImMsgName)
MetaClient: MetaClient{ if err != nil {
client: client, panic(err)
rpcRegisterName: config.Config.RpcRegisterName.OpenImPushName,
},
} }
return &Push{
discov: discov,
conn: conn,
Client: push.NewPushMsgServiceClient(conn),
} }
func (p *PushClient) DelUserPushToken(ctx context.Context, req *push.DelUserPushTokenReq) (*push.DelUserPushTokenResp, error) {
cc, err := p.getConn(ctx)
if err != nil {
return nil, err
} }
resp, err := push.NewPushMsgServiceClient(cc).DelUserPushToken(ctx, req)
if err != nil { type PushRpcClient Push
return nil, err
func NewPushRpcClient(discov discoveryregistry.SvcDiscoveryRegistry) PushRpcClient {
return PushRpcClient(*NewPush(discov))
} }
return resp, nil
func (p *PushRpcClient) DelUserPushToken(ctx context.Context, req *push.DelUserPushTokenReq) (*push.DelUserPushTokenResp, error) {
return p.Client.DelUserPushToken(ctx, req)
} }

@ -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}
}

@ -11,27 +11,32 @@ import (
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws" "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/proto/user"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils" "github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
"google.golang.org/grpc"
) )
type UserClient struct { type User struct {
MetaClient conn *grpc.ClientConn
Client user.UserClient
discov discoveryregistry.SvcDiscoveryRegistry
} }
func NewUserClient(client discoveryregistry.SvcDiscoveryRegistry) *UserClient { func NewUser(discov discoveryregistry.SvcDiscoveryRegistry) *User {
return &UserClient{ conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImUserName)
MetaClient: MetaClient{ if err != nil {
client: client, panic(err)
rpcRegisterName: config.Config.RpcRegisterName.OpenImUserName,
},
} }
client := user.NewUserClient(conn)
return &User{discov: discov, Client: client, conn: conn}
} }
func (u *UserClient) GetUsersInfo(ctx context.Context, userIDs []string) ([]*sdkws.UserInfo, error) { type UserRpcClient User
cc, err := u.getConn(ctx)
if err != nil { func NewUserRpcClient(client discoveryregistry.SvcDiscoveryRegistry) UserRpcClient {
return nil, err return UserRpcClient(*NewUser(client))
} }
resp, err := user.NewUserClient(cc).GetDesignateUsers(ctx, &user.GetDesignateUsersReq{
func (u *UserRpcClient) GetUsersInfo(ctx context.Context, userIDs []string) ([]*sdkws.UserInfo, error) {
resp, err := u.Client.GetDesignateUsers(ctx, &user.GetDesignateUsersReq{
UserIDs: userIDs, UserIDs: userIDs,
}) })
if err != nil { if err != nil {
@ -45,7 +50,7 @@ func (u *UserClient) GetUsersInfo(ctx context.Context, userIDs []string) ([]*sdk
return resp.UsersInfo, nil 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}) users, err := u.GetUsersInfo(ctx, []string{userID})
if err != nil { if err != nil {
return nil, err return nil, err
@ -53,7 +58,7 @@ func (u *UserClient) GetUserInfo(ctx context.Context, userID string) (*sdkws.Use
return users[0], nil 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) users, err := u.GetUsersInfo(ctx, userIDs)
if err != nil { if err != nil {
return nil, err return nil, err
@ -63,7 +68,7 @@ func (u *UserClient) GetUsersInfoMap(ctx context.Context, userIDs []string) (map
}), nil }), 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) users, err := u.GetUsersInfo(ctx, userIDs)
if err != nil { if err != nil {
return nil, err return nil, err
@ -78,7 +83,7 @@ func (u *UserClient) GetPublicUserInfos(ctx context.Context, userIDs []string, c
}), nil }), 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) users, err := u.GetPublicUserInfos(ctx, []string{userID}, true)
if err != nil { if err != nil {
return nil, err return nil, err
@ -86,7 +91,7 @@ func (u *UserClient) GetPublicUserInfo(ctx context.Context, userID string) (*sdk
return users[0], nil 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) users, err := u.GetPublicUserInfos(ctx, userIDs, complete)
if err != nil { if err != nil {
return nil, err return nil, err
@ -96,12 +101,8 @@ func (u *UserClient) GetPublicUserInfoMap(ctx context.Context, userIDs []string,
}), nil }), nil
} }
func (u *UserClient) GetUserGlobalMsgRecvOpt(ctx context.Context, userID string) (int32, error) { func (u *UserRpcClient) GetUserGlobalMsgRecvOpt(ctx context.Context, userID string) (int32, error) {
cc, err := u.getConn(ctx) resp, err := u.Client.GetGlobalRecvMessageOpt(ctx, &user.GetGlobalRecvMessageOptReq{
if err != nil {
return 0, err
}
resp, err := user.NewUserClient(cc).GetGlobalRecvMessageOpt(ctx, &user.GetGlobalRecvMessageOptReq{
UserID: userID, UserID: userID,
}) })
if err != nil { if err != nil {
@ -110,7 +111,7 @@ func (u *UserClient) GetUserGlobalMsgRecvOpt(ctx context.Context, userID string)
return resp.GlobalRecvMsgOpt, err 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) _, err := u.GetUserInfo(ctx, ownerUserID)
if err != nil { if err != nil {
return err return err

Loading…
Cancel
Save