Files
open-im-server/pkg/rpcclient/user.go
T

110 lines
3.2 KiB
Go
Raw Normal View History

2023-07-04 11:15:20 +08:00
// Copyright © 2023 OpenIM. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2023-06-30 09:45:02 +08:00
package rpcclient
import (
"context"
"strings"
2024-04-19 22:23:08 +08:00
"github.com/openimsdk/open-im-server/v3/pkg/common/servererrs"
"github.com/openimsdk/protocol/sdkws"
"github.com/openimsdk/protocol/user"
"github.com/openimsdk/tools/utils/datautil"
2023-06-30 09:45:02 +08:00
)
// GetUsersInfo retrieves information for multiple users based on their user IDs.
func GetUsersInfo(ctx context.Context, userIDs []string) ([]*sdkws.UserInfo, error) {
2024-01-03 17:24:02 +08:00
if len(userIDs) == 0 {
return []*sdkws.UserInfo{}, nil
}
resp, err := user.GetDesignateUsersCaller.Invoke(ctx, &user.GetDesignateUsersReq{
2023-06-30 09:45:02 +08:00
UserIDs: userIDs,
})
if err != nil {
return nil, err
}
2024-04-19 22:23:08 +08:00
if ids := datautil.Single(userIDs, datautil.Slice(resp.UsersInfo, func(e *sdkws.UserInfo) string {
2023-06-30 09:45:02 +08:00
return e.UserID
})); len(ids) > 0 {
2024-04-19 22:23:08 +08:00
return nil, servererrs.ErrUserIDNotFound.WrapMsg(strings.Join(ids, ","))
2023-06-30 09:45:02 +08:00
}
return resp.UsersInfo, nil
}
// GetUserInfo retrieves information for a single user based on the provided user ID.
func GetUserInfo(ctx context.Context, userID string) (*sdkws.UserInfo, error) {
users, err := GetUsersInfo(ctx, []string{userID})
2023-06-30 09:45:02 +08:00
if err != nil {
return nil, err
}
return users[0], nil
}
// GetUsersInfoMap retrieves a map of user information indexed by their user IDs.
func GetUsersInfoMap(ctx context.Context, userIDs []string) (map[string]*sdkws.UserInfo, error) {
users, err := GetUsersInfo(ctx, userIDs)
2023-06-30 09:45:02 +08:00
if err != nil {
return nil, err
}
2024-04-19 22:23:08 +08:00
return datautil.SliceToMap(users, func(e *sdkws.UserInfo) string {
2023-06-30 09:45:02 +08:00
return e.UserID
}), nil
}
// GetPublicUserInfos retrieves public information for multiple users based on their user IDs.
func GetPublicUserInfos(
2023-07-03 16:29:22 +08:00
ctx context.Context,
userIDs []string,
) ([]*sdkws.PublicUserInfo, error) {
users, err := GetUsersInfo(ctx, userIDs)
2023-06-30 09:45:02 +08:00
if err != nil {
return nil, err
}
2024-04-19 22:23:08 +08:00
return datautil.Slice(users, func(e *sdkws.UserInfo) *sdkws.PublicUserInfo {
2023-06-30 09:45:02 +08:00
return &sdkws.PublicUserInfo{
UserID: e.UserID,
Nickname: e.Nickname,
FaceURL: e.FaceURL,
Ex: e.Ex,
}
}), nil
}
// GetPublicUserInfo retrieves public information for a single user based on the provided user ID.
func GetPublicUserInfo(ctx context.Context, userID string) (*sdkws.PublicUserInfo, error) {
users, err := GetPublicUserInfos(ctx, []string{userID})
2023-06-30 09:45:02 +08:00
if err != nil {
return nil, err
}
2023-06-30 09:45:02 +08:00
return users[0], nil
}
// GetPublicUserInfoMap retrieves a map of public user information indexed by their user IDs.
func GetPublicUserInfoMap(
2023-07-03 16:29:22 +08:00
ctx context.Context,
userIDs []string,
) (map[string]*sdkws.PublicUserInfo, error) {
users, err := GetPublicUserInfos(ctx, userIDs)
2023-06-30 09:45:02 +08:00
if err != nil {
return nil, err
}
2024-04-19 22:23:08 +08:00
return datautil.SliceToMap(users, func(e *sdkws.PublicUserInfo) string {
2023-06-30 09:45:02 +08:00
return e.UserID
}), nil
}