Skip to content

Commit

Permalink
fix: cherry-pick
Browse files Browse the repository at this point in the history
  • Loading branch information
icey-yu committed Oct 15, 2024
1 parent 066db46 commit a16776f
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 172 deletions.
1 change: 0 additions & 1 deletion internal/api/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/openimsdk/chat/internal/api/util"
"github.com/openimsdk/chat/pkg/common/apistruct"
"github.com/openimsdk/chat/pkg/common/config"
chatConstant "github.com/openimsdk/chat/pkg/common/constant"
"github.com/openimsdk/chat/pkg/common/imapi"
"github.com/openimsdk/chat/pkg/common/mctx"
"github.com/openimsdk/chat/pkg/common/xlsx"
Expand Down
19 changes: 5 additions & 14 deletions internal/rpc/chat/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,6 @@ func (o *chatSvr) RegisterUser(ctx context.Context, req *chat.RegisterUserReq) (
if !o.AllowRegister {
return nil, errs.ErrNoPermission.WrapMsg("register user is disabled")
}
if req.User.UserType != constant.CommonUser {
return nil, errs.ErrNoPermission.WrapMsg("can only register common user")
}
if req.User.UserID != "" {
return nil, errs.ErrNoPermission.WrapMsg("only admin can set user id")
}
Expand Down Expand Up @@ -284,9 +281,8 @@ func (o *chatSvr) RegisterUser(ctx context.Context, req *chat.RegisterUserReq) (
}
}
var (
credentials []*chatdb.Credential
allowChangeRule = datautil.If(req.User.UserType == constant.CommonUser, true, false)
registerType int32
credentials []*chatdb.Credential
registerType int32
)

if req.User.PhoneNumber != "" {
Expand All @@ -295,7 +291,7 @@ func (o *chatSvr) RegisterUser(ctx context.Context, req *chat.RegisterUserReq) (
UserID: req.User.UserID,
Account: BuildCredentialPhone(req.User.AreaCode, req.User.PhoneNumber),
Type: constant.CredentialPhone,
AllowChange: allowChangeRule,
AllowChange: true,
})
}

Expand All @@ -304,7 +300,7 @@ func (o *chatSvr) RegisterUser(ctx context.Context, req *chat.RegisterUserReq) (
UserID: req.User.UserID,
Account: req.User.Account,
Type: constant.CredentialAccount,
AllowChange: allowChangeRule,
AllowChange: true,
})
registerType = constant.AccountRegister
}
Expand All @@ -315,7 +311,7 @@ func (o *chatSvr) RegisterUser(ctx context.Context, req *chat.RegisterUserReq) (
UserID: req.User.UserID,
Account: req.User.Email,
Type: constant.CredentialEmail,
AllowChange: allowChangeRule,
AllowChange: true,
})
}
register := &chatdb.Register{
Expand Down Expand Up @@ -352,11 +348,6 @@ func (o *chatSvr) RegisterUser(ctx context.Context, req *chat.RegisterUserReq) (
AllowAddFriend: constant.DefaultAllowAddFriend,
RegisterType: registerType,
}
if req.User.UserType == constant.OrgUser {
attribute.EnglishName = datautil.ToPtr(req.User.EnglishName.GetValue())
attribute.Station = datautil.ToPtr(req.User.Station.GetValue())
attribute.Telephone = datautil.ToPtr(req.User.Telephone.GetValue())
}
if err := o.Database.RegisterUser(ctx, register, account, attribute, credentials); err != nil {
return nil, err
}
Expand Down
55 changes: 55 additions & 0 deletions internal/rpc/chat/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package chat

import (
"github.com/openimsdk/chat/pkg/common/constant"
chatdb "github.com/openimsdk/chat/pkg/common/db/table/chat"
"time"

"github.com/openimsdk/tools/errs"
Expand Down Expand Up @@ -68,3 +70,56 @@ func ToDBAttributeUpdate(req *chat.UpdateUserInfoReq) (map[string]any, error) {
//}
return update, nil
}

func ToDBCredentialUpdate(req *chat.UpdateUserInfoReq, allowChange bool) ([]*chatdb.Credential, []*chatdb.Credential, error) {
update := make([]*chatdb.Credential, 0)
del := make([]*chatdb.Credential, 0)
if req.Account != nil {
if req.Account.GetValue() == "" {
del = append(del, &chatdb.Credential{
UserID: req.UserID,
Type: constant.CredentialAccount,
})
} else {
update = append(update, &chatdb.Credential{
UserID: req.UserID,
Account: req.Account.GetValue(),
Type: constant.CredentialAccount,
AllowChange: allowChange,
})
}
}

if req.Email != nil {
if req.Email.GetValue() == "" {
del = append(del, &chatdb.Credential{
UserID: req.UserID,
Type: constant.CredentialEmail,
})
} else {
update = append(update, &chatdb.Credential{
UserID: req.UserID,
Account: req.Account.GetValue(),
Type: constant.CredentialEmail,
AllowChange: allowChange,
})
}
}
if req.PhoneNumber != nil {
if req.PhoneNumber.GetValue() == "" {
del = append(del, &chatdb.Credential{
UserID: req.UserID,
Type: constant.CredentialPhone,
})
} else {
update = append(update, &chatdb.Credential{
UserID: req.UserID,
Account: BuildCredentialPhone(req.AreaCode.GetValue(), req.PhoneNumber.GetValue()),
Type: constant.CredentialPhone,
AllowChange: allowChange,
})
}
}

return update, del, nil
}
80 changes: 6 additions & 74 deletions internal/rpc/chat/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"errors"
"github.com/openimsdk/chat/pkg/eerrs"
"github.com/openimsdk/protocol/wrapperspb"
"github.com/openimsdk/tools/utils/datautil"
"github.com/openimsdk/tools/utils/stringutil"
"strconv"
"strings"
Expand Down Expand Up @@ -162,52 +161,8 @@ func (o *chatSvr) UpdateUserInfo(ctx context.Context, req *chat.UpdateUserInfoRe
return nil, err
}

isOrgUser, err := o.Database.IsOrgUser(ctx, req.UserID)
if err != nil {
return nil, err
}

switch userType {
case constant.NormalUser:
if isOrgUser {
if req.AreaCode != nil {
return nil, errs.ErrNoPermission.WrapMsg("areaCode can not be updated")
}
if req.PhoneNumber != nil {
return nil, errs.ErrNoPermission.WrapMsg("phoneNumber can not be updated")
}
if req.Account != nil {
return nil, errs.ErrNoPermission.WrapMsg("account can not be updated")
}
if req.Email != nil {
return nil, errs.ErrNoPermission.WrapMsg("email can not be updated")
}
if req.Level != nil {
return nil, errs.ErrNoPermission.WrapMsg("level can not be updated")
}

if req.Nickname != nil {
return nil, errs.ErrNoPermission.WrapMsg("nickname can not be updated")
}
if req.FaceURL != nil {
return nil, errs.ErrNoPermission.WrapMsg("faceURL can not be updated")
}
if req.Gender != nil {
return nil, errs.ErrNoPermission.WrapMsg("gender can not be updated")
}
if req.Birth != nil {
return nil, errs.ErrNoPermission.WrapMsg("birth can not be updated")
}
if req.EnglishName != nil {
return nil, errs.ErrNoPermission.WrapMsg("englishName can not be updated")
}
if req.Station != nil {
return nil, errs.ErrNoPermission.WrapMsg("station can not be updated")
}
if req.Telephone != nil {
return nil, errs.ErrNoPermission.WrapMsg("telephone can not be updated")
}
}
if req.RegisterType != nil {
return nil, errs.ErrNoPermission.WrapMsg("registerType can not be updated")
}
Expand All @@ -220,11 +175,11 @@ func (o *chatSvr) UpdateUserInfo(ctx context.Context, req *chat.UpdateUserInfoRe
return nil, errs.ErrNoPermission.WrapMsg("user type error")
}

update, err := ToDBAttributeUpdate(req, isOrgUser)
update, err := ToDBAttributeUpdate(req)
if err != nil {
return nil, err
}
credUpdate, credDel, err := ToDBCredentialUpdate(req, !isOrgUser)
credUpdate, credDel, err := ToDBCredentialUpdate(req, true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -284,16 +239,15 @@ func (o *chatSvr) AddUserAccount(ctx context.Context, req *chat.AddUserAccountRe
}

var (
credentials []*chatdb.Credential
allowChangeRule = datautil.If(req.User.UserType == constant.CommonUser, true, false)
credentials []*chatdb.Credential
)

if req.User.PhoneNumber != "" {
credentials = append(credentials, &chatdb.Credential{
UserID: req.User.UserID,
Account: BuildCredentialPhone(req.User.AreaCode, req.User.PhoneNumber),
Type: constant.CredentialPhone,
AllowChange: allowChangeRule,
AllowChange: true,
})
}

Expand All @@ -302,7 +256,7 @@ func (o *chatSvr) AddUserAccount(ctx context.Context, req *chat.AddUserAccountRe
UserID: req.User.UserID,
Account: req.User.Account,
Type: constant.CredentialAccount,
AllowChange: allowChangeRule,
AllowChange: true,
})
}

Expand All @@ -311,7 +265,7 @@ func (o *chatSvr) AddUserAccount(ctx context.Context, req *chat.AddUserAccountRe
UserID: req.User.UserID,
Account: req.User.Email,
Type: constant.CredentialEmail,
AllowChange: allowChangeRule,
AllowChange: true,
})
}

Expand Down Expand Up @@ -348,11 +302,6 @@ func (o *chatSvr) AddUserAccount(ctx context.Context, req *chat.AddUserAccountRe
AllowAddFriend: constant.DefaultAllowAddFriend,
}

if req.User.UserType == constant.OrgUser {
attribute.EnglishName = datautil.ToPtr(req.User.EnglishName.GetValue())
attribute.Station = datautil.ToPtr(req.User.Station.GetValue())
attribute.Telephone = datautil.ToPtr(req.User.Telephone.GetValue())
}
if err := o.Database.RegisterUser(ctx, register, account, attribute, credentials); err != nil {
return nil, err
}
Expand All @@ -373,23 +322,6 @@ func (o *chatSvr) SearchUserPublicInfo(ctx context.Context, req *chat.SearchUser
}, nil
}

func (o *chatSvr) SearchUserID(ctx context.Context, req *chat.SearchUserIDReq) (*chat.SearchUserIDResp, error) {
if req.Pagination == nil {
return nil, errs.ErrArgs.WrapMsg("pagination is nil")
}
if _, _, err := mctx.Check(ctx); err != nil {
return nil, err
}
total, userIDs, err := o.Database.SearchID(ctx, req.Keyword, req.OrUserIDs, req.Pagination)
if err != nil {
return nil, err
}
return &chat.SearchUserIDResp{
Total: uint32(total),
UserIDs: userIDs,
}, nil
}

func (o *chatSvr) FindUserFullInfo(ctx context.Context, req *chat.FindUserFullInfoReq) (*chat.FindUserFullInfoResp, error) {
if _, _, err := mctx.Check(ctx); err != nil {
return nil, err
Expand Down
5 changes: 0 additions & 5 deletions internal/rpc/chat/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/openimsdk/chat/pkg/eerrs"
"github.com/openimsdk/chat/pkg/protocol/chat"
"github.com/openimsdk/chat/pkg/protocol/common"
"github.com/openimsdk/protocol/wrapperspb"
"github.com/openimsdk/tools/errs"
"github.com/openimsdk/tools/utils/datautil"
"github.com/openimsdk/tools/utils/stringutil"
Expand Down Expand Up @@ -52,10 +51,6 @@ func DbToPbUserFullInfo(attribute *table.Attribute) *common.UserFullInfo {
AllowVibration: attribute.AllowVibration,
GlobalRecvMsgOpt: attribute.GlobalRecvMsgOpt,
RegisterType: attribute.RegisterType,

EnglishName: wrapperspb.StringPtr(attribute.EnglishName),
Station: wrapperspb.StringPtr(attribute.Station),
Telephone: wrapperspb.StringPtr(attribute.Telephone),
}
}

Expand Down
7 changes: 7 additions & 0 deletions pkg/common/constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,10 @@ const (
GenderMale = 1 // male
GenderUnknown = 2 // unknown
)

// Credential Type
const (
CredentialAccount = iota
CredentialPhone
CredentialEmail
)
19 changes: 16 additions & 3 deletions pkg/common/db/database/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (

type ChatDatabaseInterface interface {
GetUser(ctx context.Context, userID string) (account *chatdb.Account, err error)
UpdateUseInfo(ctx context.Context, userID string, attribute map[string]any) (err error)
UpdateUseInfo(ctx context.Context, userID string, attribute map[string]any, updateCred, delCred []*chatdb.Credential) (err error)
FindAttribute(ctx context.Context, userIDs []string) ([]*chatdb.Attribute, error)
FindAttributeByAccount(ctx context.Context, accounts []string) ([]*chatdb.Attribute, error)
TakeAttributeByPhone(ctx context.Context, areaCode string, phoneNumber string) (*chatdb.Attribute, error)
Expand Down Expand Up @@ -114,8 +114,21 @@ func (o *ChatDatabase) GetUser(ctx context.Context, userID string) (account *cha
return o.account.Take(ctx, userID)
}

func (o *ChatDatabase) UpdateUseInfo(ctx context.Context, userID string, attribute map[string]any) (err error) {
return o.attribute.Update(ctx, userID, attribute)
func (o *ChatDatabase) UpdateUseInfo(ctx context.Context, userID string, attribute map[string]any, updateCred, delCred []*chatdb.Credential) (err error) {
return o.tx.Transaction(ctx, func(ctx context.Context) error {
if err = o.attribute.Update(ctx, userID, attribute); err != nil {
return err
}
for _, credential := range updateCred {
if err = o.credential.CreateOrUpdateAccount(ctx, credential); err != nil {
return err
}
}
if err = o.credential.DeleteByUserIDType(ctx, delCred...); err != nil {
return err
}
return nil
})
}

func (o *ChatDatabase) FindAttribute(ctx context.Context, userIDs []string) ([]*chatdb.Attribute, error) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/common/db/table/chat/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func (Credential) TableName() string {

type CredentialInterface interface {
Create(ctx context.Context, credential ...*Credential) error
CreateOrUpdateAccount(ctx context.Context, credential *Credential) error
Update(ctx context.Context, userID string, data map[string]any) error
Find(ctx context.Context, userID string) ([]*Credential, error)
FindAccount(ctx context.Context, accounts []string) ([]*Credential, error)
Expand All @@ -27,4 +28,5 @@ type CredentialInterface interface {
SearchNormalUser(ctx context.Context, keyword string, forbiddenID []string, pagination pagination.Pagination) (int64, []*Credential, error)
SearchUser(ctx context.Context, keyword string, userIDs []string, pagination pagination.Pagination) (int64, []*Credential, error)
Delete(ctx context.Context, userIDs []string) error
DeleteByUserIDType(ctx context.Context, credentials ...*Credential) error
}
Loading

0 comments on commit a16776f

Please sign in to comment.