You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/pkg/rpcclient/meta.go

94 lines
2.4 KiB

2 years ago
package rpcclient
import (
2 years ago
"context"
"fmt"
relationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
sdkws "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
2 years ago
"google.golang.org/grpc"
)
type MetaClient struct {
// contains filtered or unexported fields
client discoveryregistry.SvcDiscoveryRegistry
rpcRegisterName string
2 years ago
getUsersInfo func(ctx context.Context, userIDs []string) ([]CommonUser, error)
2 years ago
}
2 years ago
func NewMetaClient(client discoveryregistry.SvcDiscoveryRegistry, rpcRegisterName string, opts ...MetaClientOptions) *MetaClient {
c := &MetaClient{
2 years ago
client: client,
rpcRegisterName: rpcRegisterName,
}
2 years ago
for _, opt := range opts {
opt(c)
}
return c
2 years ago
}
2 years ago
type MetaClientOptions func(*MetaClient)
func WithDBFunc(fn func(ctx context.Context, userIDs []string) (users []*relationTb.UserModel, err error)) MetaClientOptions {
return func(s *MetaClient) {
f := func(ctx context.Context, userIDs []string) (result []CommonUser, err error) {
users, err := fn(ctx, userIDs)
if err != nil {
return nil, err
}
for _, user := range users {
result = append(result, user)
}
return result, nil
}
s.getUsersInfo = f
}
2 years ago
}
2 years ago
func WithRpcFunc(fn func(ctx context.Context, userIDs []string) ([]*sdkws.UserInfo, error)) MetaClientOptions {
return func(s *MetaClient) {
f := func(ctx context.Context, userIDs []string) (result []CommonUser, err error) {
users, err := fn(ctx, userIDs)
if err != nil {
return nil, err
}
for _, user := range users {
result = append(result, user)
}
return result, err
}
s.getUsersInfo = f
}
}
func (m *MetaClient) getFaceURLAndName(userID string) (faceURL, nickname string, err error) {
users, err := m.getUsersInfo(context.Background(), []string{userID})
if err != nil {
return "", "", err
}
if len(users) == 0 {
return "", "", errs.ErrRecordNotFound.Wrap(fmt.Sprintf("notification user %s not found", userID))
}
return users[0].GetFaceURL(), users[0].GetNickname(), nil
}
2 years ago
func (m *MetaClient) getConn(ctx context.Context) (*grpc.ClientConn, error) {
return m.client.GetConn(ctx, m.rpcRegisterName)
2 years ago
}
type CommonUser interface {
GetNickname() string
GetFaceURL() string
GetUserID() string
GetEx() string
}
2 years ago
type CommonGroup interface {
GetNickname() string
GetFaceURL() string
GetGroupID() string
GetEx() string
}