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

194 lines
5.9 KiB
Go
Raw Normal View History

2021-12-27 16:48:05 +08:00
package user
import (
2023-02-14 11:33:10 +08:00
"Open_IM/internal/common/check"
2023-01-31 15:45:24 +08:00
"Open_IM/internal/common/convert"
2023-02-14 11:33:10 +08:00
"Open_IM/internal/common/notification"
2023-02-21 16:48:10 +08:00
"Open_IM/pkg/common/config"
2021-12-27 16:48:05 +08:00
"Open_IM/pkg/common/constant"
2023-01-29 14:47:21 +08:00
"Open_IM/pkg/common/db/controller"
"Open_IM/pkg/common/db/relation"
2023-02-10 12:53:46 +08:00
tablerelation "Open_IM/pkg/common/db/table/relation"
2023-02-09 14:40:49 +08:00
"Open_IM/pkg/common/tokenverify"
2023-01-31 15:45:24 +08:00
"Open_IM/pkg/common/tracelog"
2023-02-20 17:13:15 +08:00
registry "Open_IM/pkg/discoveryregistry"
2023-02-10 12:53:46 +08:00
"Open_IM/pkg/proto/sdkws"
pbuser "Open_IM/pkg/proto/user"
2021-12-27 16:48:05 +08:00
"Open_IM/pkg/utils"
"context"
2023-02-14 19:34:19 +08:00
"github.com/OpenIMSDK/openKeeper"
2022-01-27 01:08:02 +08:00
"google.golang.org/grpc"
2021-12-27 16:48:05 +08:00
)
type userServer struct {
2023-01-29 14:47:21 +08:00
controller.UserInterface
2023-02-14 19:34:19 +08:00
notification *notification.Check
userCheck *check.UserCheck
ConversationChecker *check.ConversationChecker
2023-02-20 17:13:15 +08:00
RegisterCenter registry.SvcDiscoveryRegistry
friendCheck *check.FriendChecker
2021-12-27 16:48:05 +08:00
}
2023-02-14 19:34:19 +08:00
func Start(client *openKeeper.ZkClient, server *grpc.Server) error {
2023-02-21 10:29:09 +08:00
gormDB, err := relation.NewGormDB()
2023-01-29 14:47:21 +08:00
if err != nil {
2023-02-14 19:34:19 +08:00
return err
2023-01-29 14:47:21 +08:00
}
2023-02-21 10:29:09 +08:00
if err := gormDB.AutoMigrate(&tablerelation.UserModel{}); err != nil {
2023-02-14 19:34:19 +08:00
return err
2023-01-29 14:47:21 +08:00
}
2023-02-21 16:48:10 +08:00
u := &userServer{
2023-02-21 10:29:09 +08:00
UserInterface: controller.NewUserController(controller.NewUserDatabase(relation.NewUserGorm(gormDB))),
2023-02-14 19:34:19 +08:00
notification: notification.NewCheck(client),
userCheck: check.NewUserCheck(client),
RegisterCenter: client,
2023-02-21 16:48:10 +08:00
}
pbuser.RegisterUserServer(server, u)
users := make([]*tablerelation.UserModel, 0)
if len(config.Config.Manager.AppManagerUid) != len(config.Config.Manager.Nickname) {
return constant.ErrConfig.Wrap("len(config.Config.Manager.AppManagerUid) != len(config.Config.Manager.Nickname)")
}
for k, v := range config.Config.Manager.AppManagerUid {
users = append(users, &tablerelation.UserModel{UserID: v, Nickname: config.Config.Manager.Nickname[k]})
}
u.UserInterface.InitOnce(context.Background(), users)
2023-02-14 19:34:19 +08:00
return nil
2021-12-28 15:33:47 +08:00
}
2023-02-07 20:28:34 +08:00
// ok
2023-02-10 12:53:46 +08:00
func (s *userServer) GetDesignateUsers(ctx context.Context, req *pbuser.GetDesignateUsersReq) (resp *pbuser.GetDesignateUsersResp, err error) {
resp = &pbuser.GetDesignateUsersResp{}
2023-02-09 10:58:30 +08:00
users, err := s.FindWithError(ctx, req.UserIDs)
2023-01-29 16:30:33 +08:00
if err != nil {
return nil, err
}
2023-01-31 20:33:33 +08:00
resp.UsersInfo, err = (*convert.DBUser)(nil).DB2PB(users)
if err != nil {
return nil, err
2023-01-29 16:30:33 +08:00
}
return resp, nil
2021-12-27 16:48:05 +08:00
}
2023-02-09 10:58:30 +08:00
// ok
2023-02-10 12:53:46 +08:00
func (s *userServer) UpdateUserInfo(ctx context.Context, req *pbuser.UpdateUserInfoReq) (resp *pbuser.UpdateUserInfoResp, err error) {
resp = &pbuser.UpdateUserInfoResp{}
2023-02-09 14:40:49 +08:00
err = tokenverify.CheckAccessV3(ctx, req.UserInfo.UserID)
2023-01-29 14:47:21 +08:00
if err != nil {
return nil, err
2021-12-27 16:48:05 +08:00
}
2023-01-31 15:45:24 +08:00
user, err := convert.NewPBUser(req.UserInfo).Convert()
2022-06-16 19:26:09 +08:00
if err != nil {
2023-01-29 14:47:21 +08:00
return nil, err
2021-12-27 16:48:05 +08:00
}
2023-02-10 12:53:46 +08:00
err = s.Update(ctx, []*tablerelation.UserModel{user})
2023-01-29 14:47:21 +08:00
if err != nil {
return nil, err
}
2023-02-20 17:13:15 +08:00
friends, err := s.friendCheck.GetAllPageFriends(ctx, req.UserInfo.UserID)
2021-12-27 16:48:05 +08:00
if err != nil {
2023-01-30 10:40:27 +08:00
return nil, err
2022-07-14 12:08:28 +08:00
}
2023-01-30 10:40:27 +08:00
go func() {
2023-02-09 10:58:30 +08:00
for _, v := range friends {
2023-02-14 11:33:10 +08:00
s.notification.FriendInfoUpdatedNotification(ctx, req.UserInfo.UserID, v.FriendUser.UserID, tracelog.GetOpUserID(ctx))
2023-01-30 10:40:27 +08:00
}
}()
2023-02-14 11:33:10 +08:00
s.notification.UserInfoUpdatedNotification(ctx, tracelog.GetOpUserID(ctx), req.UserInfo.UserID)
2023-02-09 10:58:30 +08:00
return resp, nil
2021-12-27 16:48:05 +08:00
}
2022-01-24 01:40:49 +08:00
2023-02-09 10:58:30 +08:00
// ok
2023-02-10 12:53:46 +08:00
func (s *userServer) SetGlobalRecvMessageOpt(ctx context.Context, req *pbuser.SetGlobalRecvMessageOptReq) (resp *pbuser.SetGlobalRecvMessageOptResp, err error) {
resp = &pbuser.SetGlobalRecvMessageOptResp{}
2023-02-09 10:58:30 +08:00
if _, err := s.FindWithError(ctx, []string{req.UserID}); err != nil {
2023-02-07 20:28:34 +08:00
return nil, err
}
2022-06-16 19:33:35 +08:00
m := make(map[string]interface{}, 1)
m["global_recv_msg_opt"] = req.GlobalRecvMsgOpt
2023-02-07 20:28:34 +08:00
if err := s.UpdateByMap(ctx, req.UserID, m); err != nil {
2023-01-29 16:30:33 +08:00
return nil, err
2022-08-11 20:04:15 +08:00
}
2023-02-14 11:33:10 +08:00
s.notification.UserInfoUpdatedNotification(ctx, req.UserID, req.UserID)
2023-02-07 20:28:34 +08:00
return resp, nil
2022-06-16 19:33:35 +08:00
}
2023-02-09 10:58:30 +08:00
// ok
2023-02-10 12:53:46 +08:00
func (s *userServer) AccountCheck(ctx context.Context, req *pbuser.AccountCheckReq) (resp *pbuser.AccountCheckResp, err error) {
resp = &pbuser.AccountCheckResp{}
2023-02-09 10:58:30 +08:00
if utils.Duplicate(req.CheckUserIDs) {
return nil, constant.ErrArgs.Wrap("userID repeated")
}
2023-02-09 14:40:49 +08:00
err = tokenverify.CheckAdmin(ctx)
2022-05-12 18:00:23 +08:00
if err != nil {
2023-01-29 16:30:33 +08:00
return nil, err
2022-05-12 18:00:23 +08:00
}
2023-02-09 10:58:30 +08:00
users, err := s.Find(ctx, req.CheckUserIDs)
if err != nil {
2023-01-29 16:30:33 +08:00
return nil, err
}
2023-02-07 20:28:34 +08:00
userIDs := make(map[string]interface{}, 0)
2023-02-09 10:58:30 +08:00
for _, v := range users {
2023-02-07 20:28:34 +08:00
userIDs[v.UserID] = nil
2023-01-29 16:30:33 +08:00
}
for _, v := range req.CheckUserIDs {
2023-02-10 12:53:46 +08:00
temp := &pbuser.AccountCheckRespSingleUserStatus{UserID: v}
2023-02-07 20:28:34 +08:00
if _, ok := userIDs[v]; ok {
2023-01-29 16:30:33 +08:00
temp.AccountStatus = constant.Registered
} else {
temp.AccountStatus = constant.UnRegistered
}
2023-02-07 20:28:34 +08:00
resp.Results = append(resp.Results, temp)
}
2023-02-07 20:28:34 +08:00
return resp, nil
}
2023-02-09 10:58:30 +08:00
// ok
2023-02-10 12:53:46 +08:00
func (s *userServer) GetPaginationUsers(ctx context.Context, req *pbuser.GetPaginationUsersReq) (resp *pbuser.GetPaginationUsersResp, err error) {
resp = &pbuser.GetPaginationUsersResp{}
2023-02-09 10:58:30 +08:00
usersDB, total, err := s.Page(ctx, req.Pagination.PageNumber, req.Pagination.ShowNumber)
2023-01-29 17:46:11 +08:00
if err != nil {
2023-01-29 17:54:01 +08:00
return nil, err
2022-09-06 20:35:32 +08:00
}
2023-01-29 17:54:01 +08:00
resp.Total = int32(total)
2023-02-09 10:58:30 +08:00
resp.Users, err = (*convert.DBUser)(nil).DB2PB(usersDB)
2023-02-07 20:39:12 +08:00
return resp, nil
2022-01-24 01:40:49 +08:00
}
2023-01-31 20:33:33 +08:00
2023-02-09 10:58:30 +08:00
// ok
2023-02-10 12:53:46 +08:00
func (s *userServer) UserRegister(ctx context.Context, req *pbuser.UserRegisterReq) (resp *pbuser.UserRegisterResp, err error) {
resp = &pbuser.UserRegisterResp{}
2023-02-09 20:36:34 +08:00
if utils.DuplicateAny(req.Users, func(e *sdkws.UserInfo) string { return e.UserID }) {
2023-02-07 20:28:34 +08:00
return nil, constant.ErrArgs.Wrap("userID repeated")
}
2023-01-31 20:33:33 +08:00
userIDs := make([]string, 0)
for _, v := range req.Users {
userIDs = append(userIDs, v.UserID)
}
exist, err := s.IsExist(ctx, userIDs)
if err != nil {
return nil, err
}
if exist {
2023-02-09 10:58:30 +08:00
return nil, constant.ErrRegisteredAlready.Wrap("userID registered already")
2023-01-31 20:33:33 +08:00
}
users, err := (*convert.PBUser)(nil).PB2DB(req.Users)
if err != nil {
return nil, err
}
err = s.Create(ctx, users)
if err != nil {
return nil, err
}
2023-02-07 20:28:34 +08:00
return resp, nil
2023-01-31 20:33:33 +08:00
}
2023-02-20 11:45:11 +08:00
func (s *userServer) GetGlobalRecvMessageOpt(ctx context.Context, req *pbuser.GetGlobalRecvMessageOptReq) (resp *pbuser.GetGlobalRecvMessageOptResp, err error) {
resp = &pbuser.GetGlobalRecvMessageOptResp{}
user, err := s.FindWithError(ctx, []string{req.UserID})
if err != nil {
return nil, err
}
resp.GlobalRecvMsgOpt = user[0].GlobalRecvMsgOpt
return resp, nil
}