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/internal/common/check/user.go

105 lines
2.8 KiB

package check
import (
2 years ago
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant"
discoveryRegistry "Open_IM/pkg/discoveryregistry"
2 years ago
sdkws "Open_IM/pkg/proto/sdkws"
2 years ago
"Open_IM/pkg/proto/user"
"Open_IM/pkg/utils"
"context"
2 years ago
"google.golang.org/grpc"
"strings"
)
2 years ago
//func GetUsersInfo(ctx context.Context, args ...interface{}) ([]*sdkws.UserInfo, error) {
// return nil, errors.New("TODO:GetUserInfo")
//}
2 years ago
func NewUserCheck(zk discoveryRegistry.SvcDiscoveryRegistry) *UserCheck {
return &UserCheck{
zk: zk,
}
2 years ago
}
2 years ago
type UserCheck struct {
zk discoveryRegistry.SvcDiscoveryRegistry
}
func (u *UserCheck) getConn() (*grpc.ClientConn, error) {
return u.zk.GetConn(config.Config.RpcRegisterName.OpenImUserName)
}
func (u *UserCheck) GetUsersInfos(ctx context.Context, userIDs []string, complete bool) ([]*sdkws.UserInfo, error) {
cc, err := u.getConn()
if err != nil {
return nil, err
}
resp, err := user.NewUserClient(cc).GetDesignateUsers(ctx, &user.GetDesignateUsersReq{
UserIDs: userIDs,
})
if err != nil {
return nil, err
}
if complete {
if ids := utils.Single(userIDs, utils.Slice(resp.UsersInfo, func(e *sdkws.UserInfo) string {
return e.UserID
})); len(ids) > 0 {
return nil, constant.ErrUserIDNotFound.Wrap(strings.Join(ids, ","))
}
}
return resp.UsersInfo, nil
}
2 years ago
2 years ago
func (u *UserCheck) GetUsersInfo(ctx context.Context, userID string) (*sdkws.UserInfo, error) {
users, err := u.GetUsersInfos(ctx, []string{userID}, true)
if err != nil {
return nil, err
}
return users[0], nil
2 years ago
}
2 years ago
func (u *UserCheck) GetUsersInfoMap(ctx context.Context, userIDs []string, complete bool) (map[string]*sdkws.UserInfo, error) {
users, err := u.GetUsersInfos(ctx, userIDs, complete)
if err != nil {
return nil, err
}
return utils.SliceToMap(users, func(e *sdkws.UserInfo) string {
return e.UserID
}), nil
2 years ago
}
2 years ago
func (u *UserCheck) GetPublicUserInfos(ctx context.Context, userIDs []string, complete bool) ([]*sdkws.PublicUserInfo, error) {
users, err := u.GetUsersInfos(ctx, userIDs, complete)
if err != nil {
return nil, err
}
return utils.Slice(users, func(e *sdkws.UserInfo) *sdkws.PublicUserInfo {
return &sdkws.PublicUserInfo{
UserID: e.UserID,
Nickname: e.Nickname,
FaceURL: e.FaceURL,
Gender: e.Gender,
Ex: e.Ex,
}
}), nil
2 years ago
}
2 years ago
func (u *UserCheck) GetPublicUserInfo(ctx context.Context, userID string) (*sdkws.PublicUserInfo, error) {
users, err := u.GetPublicUserInfos(ctx, []string{userID}, true)
if err != nil {
return nil, err
}
return users[0], nil
2 years ago
}
2 years ago
func (u *UserCheck) GetPublicUserInfoMap(ctx context.Context, userIDs []string, complete bool) (map[string]*sdkws.PublicUserInfo, error) {
users, err := u.GetPublicUserInfos(ctx, userIDs, complete)
if err != nil {
return nil, err
}
return utils.SliceToMap(users, func(e *sdkws.PublicUserInfo) string {
return e.UserID
}), nil
}