Files
open-im-server/internal/rpc/user/user.go
T

702 lines
23 KiB
Go
Raw Normal View History

2023-07-13 17:07:42 +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-29 22:35:31 +08:00
package user
import (
"context"
"errors"
"math/rand"
"strings"
"sync"
"time"
"github.com/openimsdk/open-im-server/v3/internal/rpc/relation"
"github.com/openimsdk/open-im-server/v3/pkg/authverify"
2024-04-19 22:23:08 +08:00
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
"github.com/openimsdk/open-im-server/v3/pkg/common/convert"
2024-07-19 16:08:39 +08:00
"github.com/openimsdk/open-im-server/v3/pkg/common/prommetrics"
"github.com/openimsdk/open-im-server/v3/pkg/common/servererrs"
2024-07-16 10:46:21 +08:00
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache"
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache/redis"
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/controller"
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/database/mgo"
tablerelation "github.com/openimsdk/open-im-server/v3/pkg/common/storage/model"
2024-04-19 22:23:08 +08:00
"github.com/openimsdk/open-im-server/v3/pkg/common/webhook"
"github.com/openimsdk/open-im-server/v3/pkg/dbbuild"
"github.com/openimsdk/open-im-server/v3/pkg/localcache"
"github.com/openimsdk/open-im-server/v3/pkg/rpcli"
"github.com/openimsdk/protocol/constant"
"github.com/openimsdk/protocol/group"
friendpb "github.com/openimsdk/protocol/relation"
2024-04-19 22:23:08 +08:00
"github.com/openimsdk/protocol/sdkws"
pbuser "github.com/openimsdk/protocol/user"
"github.com/openimsdk/tools/db/pagination"
"github.com/openimsdk/tools/discovery"
2024-04-19 22:23:08 +08:00
"github.com/openimsdk/tools/errs"
"github.com/openimsdk/tools/utils/datautil"
2024-03-05 10:51:55 +08:00
"google.golang.org/grpc"
2023-06-29 22:35:31 +08:00
)
type userServer struct {
pbuser.UnimplementedUserServer
2024-07-16 10:46:21 +08:00
online cache.OnlineCache
2024-04-19 22:23:08 +08:00
db controller.UserDatabase
friendNotificationSender *relation.FriendNotificationSender
2024-04-19 22:23:08 +08:00
userNotificationSender *UserNotificationSender
RegisterCenter discovery.Conn
2024-04-19 22:23:08 +08:00
config *Config
webhookClient *webhook.Client
2024-12-24 10:51:38 +08:00
groupClient *rpcli.GroupClient
relationClient *rpcli.RelationClient
2023-06-29 22:35:31 +08:00
}
2024-04-19 22:23:08 +08:00
type Config struct {
RpcConfig config.User
RedisConfig config.Redis
MongodbConfig config.Mongo
KafkaConfig config.Kafka
NotificationConfig config.Notification
Share config.Share
WebhooksConfig config.Webhooks
LocalCacheConfig config.LocalCache
2024-05-14 18:21:36 +08:00
Discovery config.Discovery
}
func Start(ctx context.Context, config *Config, client discovery.Conn, server grpc.ServiceRegistrar) error {
dbb := dbbuild.NewBuilder(&config.MongodbConfig, &config.RedisConfig)
mgocli, err := dbb.Mongo(ctx)
2023-06-29 22:35:31 +08:00
if err != nil {
2023-06-30 09:45:02 +08:00
return err
2023-06-29 22:35:31 +08:00
}
rdb, err := dbb.Redis(ctx)
if err != nil {
return err
}
users := make([]*tablerelation.User, 0)
2024-04-19 22:23:08 +08:00
for _, v := range config.Share.IMAdminUserID {
users = append(users, &tablerelation.User{UserID: v, Nickname: v, AppMangerLevel: constant.AppNotificationAdmin})
2023-12-26 10:15:15 +08:00
}
2024-04-19 22:23:08 +08:00
userDB, err := mgo.NewUserMongo(mgocli.GetDB())
if err != nil {
return err
}
2024-12-24 10:51:38 +08:00
msgConn, err := client.GetConn(ctx, config.Discovery.RpcService.Msg)
if err != nil {
return err
}
groupConn, err := client.GetConn(ctx, config.Discovery.RpcService.Group)
if err != nil {
return err
}
friendConn, err := client.GetConn(ctx, config.Discovery.RpcService.Friend)
if err != nil {
return err
}
msgClient := rpcli.NewMsgClient(msgConn)
userCache := redis.NewUserCacheRedis(rdb, &config.LocalCacheConfig, userDB, redis.GetRocksCacheOptions())
database := controller.NewUserDatabase(userDB, userCache, mgocli.GetTx())
localcache.InitLocalCache(&config.LocalCacheConfig)
2023-06-30 09:45:02 +08:00
u := &userServer{
2024-07-16 10:46:21 +08:00
online: redis.NewUserOnline(rdb),
2024-04-19 22:23:08 +08:00
db: database,
2023-08-15 19:56:41 +08:00
RegisterCenter: client,
2024-12-24 10:51:38 +08:00
friendNotificationSender: relation.NewFriendNotificationSender(&config.NotificationConfig, msgClient, relation.WithDBFunc(database.FindWithError)),
userNotificationSender: NewUserNotificationSender(config, msgClient, WithUserFunc(database.FindWithError)),
config: config,
2024-04-19 22:23:08 +08:00
webhookClient: webhook.NewWebhookClient(config.WebhooksConfig.URL),
2024-12-24 10:51:38 +08:00
groupClient: rpcli.NewGroupClient(groupConn),
relationClient: rpcli.NewRelationClient(friendConn),
2023-06-29 22:35:31 +08:00
}
2023-06-30 09:45:02 +08:00
pbuser.RegisterUserServer(server, u)
2024-04-19 22:23:08 +08:00
return u.db.InitOnce(context.Background(), users)
2023-06-29 22:35:31 +08:00
}
2023-07-13 16:51:52 +08:00
func (s *userServer) GetDesignateUsers(ctx context.Context, req *pbuser.GetDesignateUsersReq) (resp *pbuser.GetDesignateUsersResp, err error) {
2023-06-30 09:45:02 +08:00
resp = &pbuser.GetDesignateUsersResp{}
2024-10-25 16:23:21 +08:00
users, err := s.db.Find(ctx, req.UserIDs)
2023-06-29 22:35:31 +08:00
if err != nil {
2023-06-30 09:45:02 +08:00
return nil, err
2023-06-29 22:35:31 +08:00
}
2024-10-25 16:23:21 +08:00
2023-06-30 09:45:02 +08:00
resp.UsersInfo = convert.UsersDB2Pb(users)
2023-06-29 22:35:31 +08:00
return resp, nil
}
2024-04-19 22:23:08 +08:00
// deprecated:
2024-10-25 16:23:21 +08:00
// UpdateUserInfo
2023-07-13 16:51:52 +08:00
func (s *userServer) UpdateUserInfo(ctx context.Context, req *pbuser.UpdateUserInfoReq) (resp *pbuser.UpdateUserInfoResp, err error) {
2023-06-30 09:45:02 +08:00
resp = &pbuser.UpdateUserInfoResp{}
2024-04-19 22:23:08 +08:00
err = authverify.CheckAccessV3(ctx, req.UserInfo.UserID, s.config.Share.IMAdminUserID)
2023-06-29 22:35:31 +08:00
if err != nil {
2023-06-30 09:45:02 +08:00
return nil, err
2023-06-29 22:35:31 +08:00
}
2024-04-19 22:23:08 +08:00
if err := s.webhookBeforeUpdateUserInfo(ctx, &s.config.WebhooksConfig.BeforeUpdateUserInfo, req); err != nil {
return nil, err
}
data := convert.UserPb2DBMap(req.UserInfo)
oldUser, err := s.db.GetUserByID(ctx, req.UserInfo.UserID)
2023-06-29 22:35:31 +08:00
if err != nil {
2023-06-30 09:45:02 +08:00
return nil, err
2023-06-29 22:35:31 +08:00
}
if err := s.db.UpdateByMap(ctx, req.UserInfo.UserID, data); err != nil {
return nil, err
2023-06-29 22:35:31 +08:00
}
s.friendNotificationSender.UserInfoUpdatedNotification(ctx, req.UserInfo.UserID)
2024-04-19 22:23:08 +08:00
s.webhookAfterUpdateUserInfo(ctx, &s.config.WebhooksConfig.AfterUpdateUserInfo, req)
if err = s.NotificationUserInfoUpdate(ctx, req.UserInfo.UserID, oldUser); err != nil {
return nil, err
}
2023-06-30 09:45:02 +08:00
return resp, nil
2023-06-29 22:35:31 +08:00
}
2023-12-28 14:45:27 +08:00
func (s *userServer) UpdateUserInfoEx(ctx context.Context, req *pbuser.UpdateUserInfoExReq) (resp *pbuser.UpdateUserInfoExResp, err error) {
resp = &pbuser.UpdateUserInfoExResp{}
2024-04-19 22:23:08 +08:00
err = authverify.CheckAccessV3(ctx, req.UserInfo.UserID, s.config.Share.IMAdminUserID)
2023-12-28 14:45:27 +08:00
if err != nil {
return nil, err
}
2024-04-19 22:23:08 +08:00
if err = s.webhookBeforeUpdateUserInfoEx(ctx, &s.config.WebhooksConfig.BeforeUpdateUserInfoEx, req); err != nil {
2023-12-28 14:45:27 +08:00
return nil, err
}
oldUser, err := s.db.GetUserByID(ctx, req.UserInfo.UserID)
if err != nil {
return nil, err
}
2023-12-28 14:45:27 +08:00
data := convert.UserPb2DBMapEx(req.UserInfo)
2024-04-19 22:23:08 +08:00
if err = s.db.UpdateByMap(ctx, req.UserInfo.UserID, data); err != nil {
2023-12-28 14:45:27 +08:00
return nil, err
}
2024-04-19 22:23:08 +08:00
s.friendNotificationSender.UserInfoUpdatedNotification(ctx, req.UserInfo.UserID)
//friends, err := s.friendRpcClient.GetFriendIDs(ctx, req.UserInfo.UserID)
//if err != nil {
// return nil, err
//}
//if req.UserInfo.Nickname != nil || req.UserInfo.FaceURL != nil {
// if err := s.NotificationUserInfoUpdate(ctx, req.UserInfo.UserID); err != nil {
// return nil, err
// }
//}
//for _, friendID := range friends {
// s.friendNotificationSender.FriendInfoUpdatedNotification(ctx, req.UserInfo.UserID, friendID)
//}
2024-04-19 22:23:08 +08:00
s.webhookAfterUpdateUserInfoEx(ctx, &s.config.WebhooksConfig.AfterUpdateUserInfoEx, req)
if err := s.NotificationUserInfoUpdate(ctx, req.UserInfo.UserID, oldUser); err != nil {
return nil, err
2023-12-28 14:45:27 +08:00
}
2023-12-28 14:45:27 +08:00
return resp, nil
}
2023-07-13 16:51:52 +08:00
func (s *userServer) SetGlobalRecvMessageOpt(ctx context.Context, req *pbuser.SetGlobalRecvMessageOptReq) (resp *pbuser.SetGlobalRecvMessageOptResp, err error) {
2023-06-30 09:45:02 +08:00
resp = &pbuser.SetGlobalRecvMessageOptResp{}
2024-04-19 22:23:08 +08:00
if _, err := s.db.FindWithError(ctx, []string{req.UserID}); err != nil {
2023-06-30 09:45:02 +08:00
return nil, err
2023-06-29 22:35:31 +08:00
}
m := make(map[string]any, 1)
2023-06-30 09:45:02 +08:00
m["global_recv_msg_opt"] = req.GlobalRecvMsgOpt
2024-04-19 22:23:08 +08:00
if err := s.db.UpdateByMap(ctx, req.UserID, m); err != nil {
2023-06-30 09:45:02 +08:00
return nil, err
2023-06-29 22:35:31 +08:00
}
2023-08-15 19:56:41 +08:00
s.friendNotificationSender.UserInfoUpdatedNotification(ctx, req.UserID)
2023-06-29 22:35:31 +08:00
return resp, nil
}
2023-07-13 16:51:52 +08:00
func (s *userServer) AccountCheck(ctx context.Context, req *pbuser.AccountCheckReq) (resp *pbuser.AccountCheckResp, err error) {
2023-06-30 09:45:02 +08:00
resp = &pbuser.AccountCheckResp{}
2024-04-19 22:23:08 +08:00
if datautil.Duplicate(req.CheckUserIDs) {
return nil, errs.ErrArgs.WrapMsg("userID repeated")
2023-06-29 22:35:31 +08:00
}
2024-04-19 22:23:08 +08:00
err = authverify.CheckAdmin(ctx, s.config.Share.IMAdminUserID)
2023-06-29 22:35:31 +08:00
if err != nil {
2023-06-30 09:45:02 +08:00
return nil, err
2023-06-29 22:35:31 +08:00
}
2024-04-19 22:23:08 +08:00
users, err := s.db.Find(ctx, req.CheckUserIDs)
2023-06-29 22:35:31 +08:00
if err != nil {
2023-06-30 09:45:02 +08:00
return nil, err
2023-06-29 22:35:31 +08:00
}
userIDs := make(map[string]any, 0)
2023-06-29 22:35:31 +08:00
for _, v := range users {
2023-06-30 09:45:02 +08:00
userIDs[v.UserID] = nil
}
for _, v := range req.CheckUserIDs {
temp := &pbuser.AccountCheckRespSingleUserStatus{UserID: v}
if _, ok := userIDs[v]; ok {
temp.AccountStatus = constant.Registered
} else {
temp.AccountStatus = constant.UnRegistered
2023-06-29 22:35:31 +08:00
}
2023-06-30 09:45:02 +08:00
resp.Results = append(resp.Results, temp)
2023-06-29 22:35:31 +08:00
}
return resp, nil
}
2023-07-13 16:51:52 +08:00
func (s *userServer) GetPaginationUsers(ctx context.Context, req *pbuser.GetPaginationUsersReq) (resp *pbuser.GetPaginationUsersResp, err error) {
if req.UserID == "" && req.NickName == "" {
2024-04-19 22:23:08 +08:00
total, users, err := s.db.PageFindUser(ctx, constant.IMOrdinaryUser, constant.AppOrdinaryUsers, req.Pagination)
if err != nil {
return nil, err
}
return &pbuser.GetPaginationUsersResp{Total: int32(total), Users: convert.UsersDB2Pb(users)}, err
} else {
2024-04-19 22:23:08 +08:00
total, users, err := s.db.PageFindUserWithKeyword(ctx, constant.IMOrdinaryUser, constant.AppOrdinaryUsers, req.UserID, req.NickName, req.Pagination)
if err != nil {
return nil, err
}
return &pbuser.GetPaginationUsersResp{Total: int32(total), Users: convert.UsersDB2Pb(users)}, err
2023-06-29 22:35:31 +08:00
}
2023-06-29 22:35:31 +08:00
}
2023-07-13 16:51:52 +08:00
func (s *userServer) UserRegister(ctx context.Context, req *pbuser.UserRegisterReq) (resp *pbuser.UserRegisterResp, err error) {
2023-06-30 09:45:02 +08:00
resp = &pbuser.UserRegisterResp{}
if len(req.Users) == 0 {
2024-04-19 22:23:08 +08:00
return nil, errs.ErrArgs.WrapMsg("users is empty")
2023-06-29 22:35:31 +08:00
}
if err = authverify.CheckAdmin(ctx, s.config.Share.IMAdminUserID); err != nil {
return nil, err
2023-06-30 23:04:28 +08:00
}
2024-04-19 22:23:08 +08:00
if datautil.DuplicateAny(req.Users, func(e *sdkws.UserInfo) string { return e.UserID }) {
return nil, errs.ErrArgs.WrapMsg("userID repeated")
2023-06-29 22:35:31 +08:00
}
2023-06-30 09:45:02 +08:00
userIDs := make([]string, 0)
for _, user := range req.Users {
if user.UserID == "" {
2024-04-19 22:23:08 +08:00
return nil, errs.ErrArgs.WrapMsg("userID is empty")
2023-06-30 09:45:02 +08:00
}
if strings.Contains(user.UserID, ":") {
2024-04-19 22:23:08 +08:00
return nil, errs.ErrArgs.WrapMsg("userID contains ':' is invalid userID")
2023-06-30 09:45:02 +08:00
}
userIDs = append(userIDs, user.UserID)
}
2024-04-19 22:23:08 +08:00
exist, err := s.db.IsExist(ctx, userIDs)
2023-06-30 09:45:02 +08:00
if err != nil {
return nil, err
}
if exist {
2024-04-19 22:23:08 +08:00
return nil, servererrs.ErrRegisteredAlready.WrapMsg("userID registered already")
2023-06-30 09:45:02 +08:00
}
2024-04-19 22:23:08 +08:00
if err := s.webhookBeforeUserRegister(ctx, &s.config.WebhooksConfig.BeforeUserRegister, req); err != nil {
2023-11-28 15:26:46 +08:00
return nil, err
}
2023-06-30 09:45:02 +08:00
now := time.Now()
users := make([]*tablerelation.User, 0, len(req.Users))
2023-06-30 09:45:02 +08:00
for _, user := range req.Users {
users = append(users, &tablerelation.User{
2023-06-30 09:45:02 +08:00
UserID: user.UserID,
Nickname: user.Nickname,
FaceURL: user.FaceURL,
Ex: user.Ex,
CreateTime: now,
AppMangerLevel: user.AppMangerLevel,
GlobalRecvMsgOpt: user.GlobalRecvMsgOpt,
2023-06-29 22:35:31 +08:00
})
}
2024-04-19 22:23:08 +08:00
if err := s.db.Create(ctx, users); err != nil {
2023-06-30 09:45:02 +08:00
return nil, err
2023-06-29 22:35:31 +08:00
}
2023-11-28 15:26:46 +08:00
2024-07-19 16:08:39 +08:00
prommetrics.UserRegisterCounter.Add(float64(len(users)))
2024-04-19 22:23:08 +08:00
s.webhookAfterUserRegister(ctx, &s.config.WebhooksConfig.AfterUserRegister, req)
2023-06-29 22:35:31 +08:00
return resp, nil
}
2023-07-13 16:51:52 +08:00
func (s *userServer) GetGlobalRecvMessageOpt(ctx context.Context, req *pbuser.GetGlobalRecvMessageOptReq) (resp *pbuser.GetGlobalRecvMessageOptResp, err error) {
2024-04-19 22:23:08 +08:00
user, err := s.db.FindWithError(ctx, []string{req.UserID})
2023-06-29 22:35:31 +08:00
if err != nil {
2023-06-30 09:45:02 +08:00
return nil, err
2023-06-29 22:35:31 +08:00
}
2023-06-30 09:45:02 +08:00
return &pbuser.GetGlobalRecvMessageOptResp{GlobalRecvMsgOpt: user[0].GlobalRecvMsgOpt}, nil
2023-06-29 22:35:31 +08:00
}
// GetAllUserID Get user account by page.
2023-07-13 16:51:52 +08:00
func (s *userServer) GetAllUserID(ctx context.Context, req *pbuser.GetAllUserIDReq) (resp *pbuser.GetAllUserIDResp, err error) {
2024-04-19 22:23:08 +08:00
total, userIDs, err := s.db.GetAllUserID(ctx, req.Pagination)
2023-06-30 09:45:02 +08:00
if err != nil {
return nil, err
2023-06-29 22:35:31 +08:00
}
return &pbuser.GetAllUserIDResp{Total: int32(total), UserIDs: userIDs}, nil
2023-06-29 22:35:31 +08:00
}
2023-07-27 12:32:08 +08:00
// ProcessUserCommandAdd user general function add.
func (s *userServer) ProcessUserCommandAdd(ctx context.Context, req *pbuser.ProcessUserCommandAddReq) (*pbuser.ProcessUserCommandAddResp, error) {
2024-04-19 22:23:08 +08:00
err := authverify.CheckAccessV3(ctx, req.UserID, s.config.Share.IMAdminUserID)
2024-01-10 10:27:03 +08:00
if err != nil {
return nil, err
}
2024-01-10 10:27:03 +08:00
var value string
if req.Value != nil {
value = req.Value.Value
}
var ex string
if req.Ex != nil {
value = req.Ex.Value
}
// Assuming you have a method in s.storage to add a user command
2024-04-19 22:23:08 +08:00
err = s.db.AddUserCommand(ctx, req.UserID, req.Type, req.Uuid, value, ex)
2024-01-10 10:27:03 +08:00
if err != nil {
return nil, err
}
tips := &sdkws.UserCommandAddTips{
FromUserID: req.UserID,
ToUserID: req.UserID,
}
2024-04-19 22:23:08 +08:00
s.userNotificationSender.UserCommandAddNotification(ctx, tips)
return &pbuser.ProcessUserCommandAddResp{}, nil
}
// ProcessUserCommandDelete user general function delete.
func (s *userServer) ProcessUserCommandDelete(ctx context.Context, req *pbuser.ProcessUserCommandDeleteReq) (*pbuser.ProcessUserCommandDeleteResp, error) {
2024-04-19 22:23:08 +08:00
err := authverify.CheckAccessV3(ctx, req.UserID, s.config.Share.IMAdminUserID)
if err != nil {
return nil, err
}
2024-04-19 22:23:08 +08:00
err = s.db.DeleteUserCommand(ctx, req.UserID, req.Type, req.Uuid)
2024-01-10 10:27:03 +08:00
if err != nil {
return nil, err
}
tips := &sdkws.UserCommandDeleteTips{
FromUserID: req.UserID,
ToUserID: req.UserID,
}
2024-04-19 22:23:08 +08:00
s.userNotificationSender.UserCommandDeleteNotification(ctx, tips)
return &pbuser.ProcessUserCommandDeleteResp{}, nil
}
// ProcessUserCommandUpdate user general function update.
func (s *userServer) ProcessUserCommandUpdate(ctx context.Context, req *pbuser.ProcessUserCommandUpdateReq) (*pbuser.ProcessUserCommandUpdateResp, error) {
2024-04-19 22:23:08 +08:00
err := authverify.CheckAccessV3(ctx, req.UserID, s.config.Share.IMAdminUserID)
2024-01-10 10:27:03 +08:00
if err != nil {
return nil, err
}
val := make(map[string]any)
2024-01-10 10:27:03 +08:00
// Map fields from eax to val
if req.Value != nil {
val["value"] = req.Value.Value
}
if req.Ex != nil {
val["ex"] = req.Ex.Value
}
// Assuming you have a method in s.storage to update a user command
2024-04-19 22:23:08 +08:00
err = s.db.UpdateUserCommand(ctx, req.UserID, req.Type, req.Uuid, val)
2024-01-10 10:27:03 +08:00
if err != nil {
return nil, err
}
tips := &sdkws.UserCommandUpdateTips{
FromUserID: req.UserID,
ToUserID: req.UserID,
}
2024-04-19 22:23:08 +08:00
s.userNotificationSender.UserCommandUpdateNotification(ctx, tips)
return &pbuser.ProcessUserCommandUpdateResp{}, nil
}
func (s *userServer) ProcessUserCommandGet(ctx context.Context, req *pbuser.ProcessUserCommandGetReq) (*pbuser.ProcessUserCommandGetResp, error) {
2024-04-19 22:23:08 +08:00
err := authverify.CheckAccessV3(ctx, req.UserID, s.config.Share.IMAdminUserID)
2024-01-10 10:27:03 +08:00
if err != nil {
return nil, err
}
// Fetch user commands from the database
2024-04-19 22:23:08 +08:00
commands, err := s.db.GetUserCommands(ctx, req.UserID, req.Type)
if err != nil {
return nil, err
}
// Initialize commandInfoSlice as an empty slice
commandInfoSlice := make([]*pbuser.CommandInfoResp, 0, len(commands))
for _, command := range commands {
// No need to use index since command is already a pointer
commandInfoSlice = append(commandInfoSlice, &pbuser.CommandInfoResp{
2024-01-10 10:27:03 +08:00
Type: command.Type,
Uuid: command.Uuid,
Value: command.Value,
CreateTime: command.CreateTime,
Ex: command.Ex,
})
}
// Return the response with the slice
return &pbuser.ProcessUserCommandGetResp{CommandResp: commandInfoSlice}, nil
}
func (s *userServer) ProcessUserCommandGetAll(ctx context.Context, req *pbuser.ProcessUserCommandGetAllReq) (*pbuser.ProcessUserCommandGetAllResp, error) {
2024-04-19 22:23:08 +08:00
err := authverify.CheckAccessV3(ctx, req.UserID, s.config.Share.IMAdminUserID)
2024-01-10 10:27:03 +08:00
if err != nil {
return nil, err
}
// Fetch user commands from the database
2024-04-19 22:23:08 +08:00
commands, err := s.db.GetAllUserCommands(ctx, req.UserID)
2024-01-10 10:27:03 +08:00
if err != nil {
return nil, err
}
// Initialize commandInfoSlice as an empty slice
commandInfoSlice := make([]*pbuser.AllCommandInfoResp, 0, len(commands))
for _, command := range commands {
// No need to use index since command is already a pointer
commandInfoSlice = append(commandInfoSlice, &pbuser.AllCommandInfoResp{
Type: command.Type,
Uuid: command.Uuid,
Value: command.Value,
CreateTime: command.CreateTime,
2024-01-10 10:27:03 +08:00
Ex: command.Ex,
})
}
// Return the response with the slice
2024-01-10 10:27:03 +08:00
return &pbuser.ProcessUserCommandGetAllResp{CommandResp: commandInfoSlice}, nil
}
2023-12-26 10:15:15 +08:00
func (s *userServer) AddNotificationAccount(ctx context.Context, req *pbuser.AddNotificationAccountReq) (*pbuser.AddNotificationAccountResp, error) {
2024-04-19 22:23:08 +08:00
if err := authverify.CheckAdmin(ctx, s.config.Share.IMAdminUserID); err != nil {
2023-12-26 10:15:15 +08:00
return nil, err
}
if req.AppMangerLevel < constant.AppNotificationAdmin {
return nil, errs.ErrArgs.WithDetail("app level not supported")
}
2024-01-08 21:38:48 +08:00
if req.UserID == "" {
for i := 0; i < 20; i++ {
userId := s.genUserID()
2024-04-19 22:23:08 +08:00
_, err := s.db.FindWithError(ctx, []string{userId})
2024-01-08 21:38:48 +08:00
if err == nil {
continue
}
req.UserID = userId
break
}
if req.UserID == "" {
2024-04-19 22:23:08 +08:00
return nil, errs.ErrInternalServer.WrapMsg("gen user id failed")
2023-12-26 10:15:15 +08:00
}
} else {
2024-04-19 22:23:08 +08:00
_, err := s.db.FindWithError(ctx, []string{req.UserID})
if err == nil {
2024-04-19 22:23:08 +08:00
return nil, errs.ErrArgs.WrapMsg("userID is used")
}
2023-12-26 10:15:15 +08:00
}
user := &tablerelation.User{
2024-01-08 21:38:48 +08:00
UserID: req.UserID,
2023-12-26 10:15:15 +08:00
Nickname: req.NickName,
FaceURL: req.FaceURL,
CreateTime: time.Now(),
AppMangerLevel: req.AppMangerLevel,
2023-12-26 10:15:15 +08:00
}
if err := s.db.Create(ctx, []*tablerelation.User{user}); err != nil {
2023-12-26 10:15:15 +08:00
return nil, err
}
2024-01-08 21:38:48 +08:00
return &pbuser.AddNotificationAccountResp{
UserID: req.UserID,
NickName: req.NickName,
FaceURL: req.FaceURL,
AppMangerLevel: req.AppMangerLevel,
2024-01-08 21:38:48 +08:00
}, nil
2023-12-26 10:15:15 +08:00
}
func (s *userServer) UpdateNotificationAccountInfo(ctx context.Context, req *pbuser.UpdateNotificationAccountInfoReq) (*pbuser.UpdateNotificationAccountInfoResp, error) {
2024-04-19 22:23:08 +08:00
if err := authverify.CheckAdmin(ctx, s.config.Share.IMAdminUserID); err != nil {
2023-12-26 10:15:15 +08:00
return nil, err
}
2024-04-19 22:23:08 +08:00
if _, err := s.db.FindWithError(ctx, []string{req.UserID}); err != nil {
2023-12-26 10:15:15 +08:00
return nil, errs.ErrArgs.Wrap()
}
user := map[string]interface{}{}
if req.NickName != "" {
user["nickname"] = req.NickName
}
if req.FaceURL != "" {
user["face_url"] = req.FaceURL
}
2024-04-19 22:23:08 +08:00
if err := s.db.UpdateByMap(ctx, req.UserID, user); err != nil {
2023-12-26 10:15:15 +08:00
return nil, err
}
return &pbuser.UpdateNotificationAccountInfoResp{}, nil
}
func (s *userServer) SearchNotificationAccount(ctx context.Context, req *pbuser.SearchNotificationAccountReq) (*pbuser.SearchNotificationAccountResp, error) {
// Check if user is an admin
2024-04-19 22:23:08 +08:00
if err := authverify.CheckAdmin(ctx, s.config.Share.IMAdminUserID); err != nil {
2023-12-26 10:15:15 +08:00
return nil, err
}
var users []*tablerelation.User
2024-01-08 21:38:48 +08:00
var err error
// If a keyword is provided in the request
2024-01-08 21:38:48 +08:00
if req.Keyword != "" {
// Find users by keyword
2024-04-19 22:23:08 +08:00
users, err = s.db.Find(ctx, []string{req.Keyword})
if err != nil {
return nil, err
}
// Convert users to response format
resp := s.userModelToResp(users, req.Pagination, req.AppManagerLevel)
2024-01-08 21:38:48 +08:00
if resp.Total != 0 {
return resp, nil
}
// Find users by nickname if no users found by keyword
2024-04-19 22:23:08 +08:00
users, err = s.db.FindByNickname(ctx, req.Keyword)
if err != nil {
return nil, err
}
resp = s.userModelToResp(users, req.Pagination, req.AppManagerLevel)
2024-01-08 21:38:48 +08:00
return resp, nil
}
// If no keyword, find users with notification settings
if req.AppManagerLevel != nil {
users, err = s.db.FindNotification(ctx, int64(*req.AppManagerLevel))
if err != nil {
return nil, err
}
} else {
users, err = s.db.FindSystemAccount(ctx)
if err != nil {
return nil, err
}
2023-12-26 10:15:15 +08:00
}
resp := s.userModelToResp(users, req.Pagination, req.AppManagerLevel)
return resp, nil
}
2023-12-26 10:15:15 +08:00
func (s *userServer) GetNotificationAccount(ctx context.Context, req *pbuser.GetNotificationAccountReq) (*pbuser.GetNotificationAccountResp, error) {
if req.UserID == "" {
2024-04-19 22:23:08 +08:00
return nil, errs.ErrArgs.WrapMsg("userID is empty")
2023-12-26 10:15:15 +08:00
}
2024-04-19 22:23:08 +08:00
user, err := s.db.GetUserByID(ctx, req.UserID)
2023-12-26 10:15:15 +08:00
if err != nil {
2024-04-19 22:23:08 +08:00
return nil, servererrs.ErrUserIDNotFound.Wrap()
2023-12-26 10:15:15 +08:00
}
if user.AppMangerLevel == constant.AppAdmin || user.AppMangerLevel >= constant.AppNotificationAdmin {
return &pbuser.GetNotificationAccountResp{Account: &pbuser.NotificationAccountInfo{
UserID: user.UserID,
FaceURL: user.FaceURL,
NickName: user.Nickname,
AppMangerLevel: user.AppMangerLevel,
}}, nil
2023-12-26 10:15:15 +08:00
}
2024-04-19 22:23:08 +08:00
return nil, errs.ErrNoPermission.WrapMsg("notification messages cannot be sent for this ID")
2023-12-26 10:15:15 +08:00
}
func (s *userServer) genUserID() string {
const l = 10
data := make([]byte, l)
rand.Read(data)
chars := []byte("0123456789")
for i := 0; i < len(data); i++ {
if i == 0 {
data[i] = chars[1:][data[i]%9]
} else {
data[i] = chars[data[i]%10]
}
}
return string(data)
}
func (s *userServer) userModelToResp(users []*tablerelation.User, pagination pagination.Pagination, appManagerLevel *int32) *pbuser.SearchNotificationAccountResp {
accounts := make([]*pbuser.NotificationAccountInfo, 0)
var total int64
for _, v := range users {
if v.AppMangerLevel >= constant.AppNotificationAdmin && !datautil.Contain(v.UserID, s.config.Share.IMAdminUserID...) {
if appManagerLevel != nil {
if v.AppMangerLevel != *appManagerLevel {
continue
}
}
temp := &pbuser.NotificationAccountInfo{
UserID: v.UserID,
FaceURL: v.FaceURL,
NickName: v.Nickname,
AppMangerLevel: v.AppMangerLevel,
}
accounts = append(accounts, temp)
total += 1
}
}
2024-01-08 21:38:48 +08:00
2024-04-19 22:23:08 +08:00
notificationAccounts := datautil.Paginate(accounts, int(pagination.GetPageNumber()), int(pagination.GetShowNumber()))
2024-01-08 21:38:48 +08:00
return &pbuser.SearchNotificationAccountResp{Total: total, NotificationAccounts: notificationAccounts}
}
func (s *userServer) NotificationUserInfoUpdate(ctx context.Context, userID string, oldUser *tablerelation.User) error {
user, err := s.db.GetUserByID(ctx, userID)
if err != nil {
return err
}
if user.Nickname == oldUser.Nickname && user.FaceURL == oldUser.FaceURL {
return nil
}
oldUserInfo := convert.UserDB2Pb(oldUser)
newUserInfo := convert.UserDB2Pb(user)
var wg sync.WaitGroup
var es [2]error
wg.Add(len(es))
go func() {
defer wg.Done()
2024-12-24 10:51:38 +08:00
_, es[0] = s.groupClient.NotificationUserInfoUpdate(ctx, &group.NotificationUserInfoUpdateReq{
UserID: userID,
OldUserInfo: oldUserInfo,
NewUserInfo: newUserInfo,
})
}()
go func() {
defer wg.Done()
2024-12-24 10:51:38 +08:00
_, es[1] = s.relationClient.NotificationUserInfoUpdate(ctx, &friendpb.NotificationUserInfoUpdateReq{
UserID: userID,
OldUserInfo: oldUserInfo,
NewUserInfo: newUserInfo,
})
}()
wg.Wait()
return errors.Join(es[:]...)
}
func (s *userServer) SortQuery(ctx context.Context, req *pbuser.SortQueryReq) (*pbuser.SortQueryResp, error) {
users, err := s.db.SortQuery(ctx, req.UserIDName, req.Asc)
if err != nil {
return nil, err
}
return &pbuser.SortQueryResp{Users: convert.UsersDB2Pb(users)}, nil
}