diff --git a/internal/rpc/chat/login.go b/internal/rpc/chat/login.go index f1f2474b..59990302 100644 --- a/internal/rpc/chat/login.go +++ b/internal/rpc/chat/login.go @@ -240,8 +240,6 @@ func (o *chatSvr) genVerifyCode() string { } func (o *chatSvr) RegisterUser(ctx context.Context, req *chat.RegisterUserReq) (*chat.RegisterUserResp, error) { - resp := &chat.RegisterUserResp{} - isAdmin, err := o.Admin.CheckNilOrAdmin(ctx) ctx = o.WithAdminUser(ctx) if err != nil { @@ -389,6 +387,7 @@ func (o *chatSvr) RegisterUser(ctx context.Context, req *chat.RegisterUserReq) ( log.ZError(ctx, "UseInvitationCode", err, "userID", req.User.UserID, "invitationCode", req.InvitationCode) } } + var resp chat.RegisterUserResp if req.AutoLogin { chatToken, err := o.Admin.CreateToken(ctx, req.User.UserID, constant.NormalUser) if err == nil { @@ -398,7 +397,7 @@ func (o *chatSvr) RegisterUser(ctx context.Context, req *chat.RegisterUserReq) ( } } resp.UserID = req.User.UserID - return resp, nil + return &resp, nil } func (o *chatSvr) Login(ctx context.Context, req *chat.LoginReq) (*chat.LoginResp, error) { diff --git a/internal/rpc/chat/update.go b/internal/rpc/chat/update.go index 169694ff..4579f1c3 100644 --- a/internal/rpc/chat/update.go +++ b/internal/rpc/chat/update.go @@ -63,8 +63,8 @@ func ToDBAttributeUpdate(req *chat.UpdateUserInfoReq) (map[string]any, error) { if req.GlobalRecvMsgOpt != nil { update["global_recv_msg_opt"] = req.GlobalRecvMsgOpt.Value } - if len(update) == 0 { - return nil, errs.ErrArgs.WrapMsg("no update info") - } + //if len(update) == 0 { + // return nil, errs.ErrArgs.WrapMsg("no update info") + //} return update, nil } diff --git a/internal/rpc/chat/user.go b/internal/rpc/chat/user.go index ff53c917..ce789613 100644 --- a/internal/rpc/chat/user.go +++ b/internal/rpc/chat/user.go @@ -32,8 +32,95 @@ import ( "github.com/openimsdk/tools/errs" ) +func (o *chatSvr) checkUpdateInfo(ctx context.Context, req *chat.UpdateUserInfoReq) error { + attribute, err := o.Database.TakeAttributeByUserID(ctx, req.UserID) + if err != nil { + return err + } + checkEmail := func() error { + if req.Email == nil { + return nil + } + if req.Email.Value == attribute.Email { + req.Email = nil + return nil + } + if req.Email.Value == "" { + if !(attribute.Account != "" || (attribute.AreaCode != "" && attribute.PhoneNumber != "")) { + return errs.ErrArgs.WrapMsg("a login method must exist") + } + return nil + } else { + if _, err := o.Database.GetAttributeByEmail(ctx, req.Email.Value); err == nil { + return errs.ErrDuplicateKey.WrapMsg("email already exists") + } else if !dbutil.IsDBNotFound(err) { + return err + } + } + return nil + } + checkPhone := func() error { + if req.AreaCode == nil { + return nil + } + if req.AreaCode.Value == attribute.AreaCode && req.PhoneNumber.Value == attribute.PhoneNumber { + req.AreaCode = nil + req.PhoneNumber = nil + return nil + } + if req.AreaCode.Value == "" || req.PhoneNumber.Value == "" { + if attribute.Email == "" || attribute.Account == "" { + return errs.ErrArgs.WrapMsg("a login method must exist") + } + } else { + if _, err := o.Database.GetAttributeByPhone(ctx, req.AreaCode.Value, req.PhoneNumber.Value); err == nil { + return errs.ErrDuplicateKey.WrapMsg("phone number already exists") + } else if !dbutil.IsDBNotFound(err) { + return err + } + } + return nil + } + checkAccount := func() error { + if req.Account == nil { + return nil + } + if req.Account.Value == attribute.Account { + req.Account = nil + return nil + } + if req.Account.Value == "" { + if !(attribute.Email == "" && (attribute.AreaCode == "" || attribute.PhoneNumber == "")) { + return errs.ErrArgs.WrapMsg("a login method must exist") + } + } else { + if _, err := o.Database.GetAttributeByAccount(ctx, req.Account.Value); err == nil { + return errs.ErrDuplicateKey.WrapMsg("account already exists") + } else if !dbutil.IsDBNotFound(err) { + return err + } + } + return nil + } + for _, fn := range []func() error{checkEmail, checkPhone, checkAccount} { + if err := fn(); err != nil { + return err + } + } + return nil +} + func (o *chatSvr) UpdateUserInfo(ctx context.Context, req *chat.UpdateUserInfoReq) (*chat.UpdateUserInfoResp, error) { - resp := &chat.UpdateUserInfoResp{} + if req.AreaCode != nil || req.PhoneNumber != nil { + if !(req.AreaCode != nil && req.PhoneNumber != nil) { + return nil, errs.ErrArgs.WrapMsg("areaCode and phoneNumber must be set together") + } + if req.AreaCode.Value == "" || req.PhoneNumber.Value == "" { + if req.AreaCode.Value != req.PhoneNumber.Value { + return nil, errs.ErrArgs.WrapMsg("areaCode and phoneNumber must be set together") + } + } + } opUserID, userType, err := mctx.Check(ctx) if err != nil { return nil, err @@ -65,44 +152,19 @@ func (o *chatSvr) UpdateUserInfo(ctx context.Context, req *chat.UpdateUserInfoRe default: return nil, errs.ErrNoPermission.WrapMsg("user type error") } - update, err := ToDBAttributeUpdate(req) - if err != nil { + if err := o.checkUpdateInfo(ctx, req); err != nil { return nil, err } - attribute, err := o.Database.TakeAttributeByUserID(ctx, req.UserID) + update, err := ToDBAttributeUpdate(req) if err != nil { return nil, err } - if req.Account != nil && req.Account.Value != attribute.Account { - _, err := o.Database.TakeAttributeByAccount(ctx, req.Account.Value) - if err == nil { - return nil, eerrs.ErrAccountAlreadyRegister.Wrap() - } else if !dbutil.IsDBNotFound(err) { + if len(update) > 0 { + if err := o.Database.UpdateUseInfo(ctx, req.UserID, update); err != nil { return nil, err } } - if req.AreaCode != nil || req.PhoneNumber != nil { - areaCode := attribute.AreaCode - phoneNumber := attribute.PhoneNumber - if req.AreaCode != nil { - areaCode = req.AreaCode.Value - } - if req.PhoneNumber != nil { - phoneNumber = req.PhoneNumber.Value - } - if attribute.AreaCode != areaCode || attribute.PhoneNumber != phoneNumber { - _, err := o.Database.TakeAttributeByPhone(ctx, areaCode, phoneNumber) - if err == nil { - return nil, eerrs.ErrAccountAlreadyRegister.Wrap() - } else if !dbutil.IsDBNotFound(err) { - return nil, err - } - } - } - if err := o.Database.UpdateUseInfo(ctx, req.UserID, update); err != nil { - return nil, err - } - return resp, nil + return &chat.UpdateUserInfoResp{}, nil } func (o *chatSvr) FindUserPublicInfo(ctx context.Context, req *chat.FindUserPublicInfoReq) (*chat.FindUserPublicInfoResp, error) {