Skip to content

Commit

Permalink
feat: implement checkPhone method in Chat and rewrite register user…
Browse files Browse the repository at this point in the history
… logic.
  • Loading branch information
mo3et committed Jun 14, 2024
1 parent 1664551 commit 68da59a
Show file tree
Hide file tree
Showing 10 changed files with 459 additions and 258 deletions.
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,6 @@ github.com/openimsdk/gomake v0.0.9 h1:ouf25ygN2PMQ68Gfgns/EQRPiLPnp+77SIr68GfE+n
github.com/openimsdk/gomake v0.0.9/go.mod h1:PndCozNc2IsQIciyn9mvEblYWZwJmAI+06z94EY+csI=
github.com/openimsdk/protocol v0.0.63 h1:9DnweZe9nEYDFa4fGTbC9Cqi0gLUdtBhRo1NRP2X3WQ=
github.com/openimsdk/protocol v0.0.63/go.mod h1:OZQA9FR55lseYoN2Ql1XAHYKHJGu7OMNkUbuekrKCM8=
github.com/openimsdk/tools v0.0.49-alpha.18 h1:ARQeCiRmExvtB6XYItegThuV63JGOTxddwhSLHYXd78=
github.com/openimsdk/tools v0.0.49-alpha.18/go.mod h1:g7mkHXYUPi0/8aAX8VPMHpnb3hqdV69Jph+bXOGvvNM=
github.com/openimsdk/tools v0.0.49-alpha.24 h1:lJsqnjTPujnr91LRQ6QmcTliMIa4fMOBSTri6rFz2ek=
github.com/openimsdk/tools v0.0.49-alpha.24/go.mod h1:g7mkHXYUPi0/8aAX8VPMHpnb3hqdV69Jph+bXOGvvNM=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
Expand Down
46 changes: 24 additions & 22 deletions internal/api/chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"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/eerrs"
"github.com/openimsdk/chat/pkg/protocol/admin"
chatpb "github.com/openimsdk/chat/pkg/protocol/chat"
constantpb "github.com/openimsdk/protocol/constant"
Expand Down Expand Up @@ -90,41 +89,44 @@ func (o *Api) RegisterUser(c *gin.Context) {
}
req.Ip = ip

userInfo := &sdkws.UserInfo{
UserID: req.User.UserID,
Nickname: req.User.Nickname,
FaceURL: req.User.FaceURL,
CreateTime: time.Now().UnixMilli(),
}

chatUser, err := o.chatClient.FindUserAccount(c, &chatpb.FindUserAccountReq{UserIDs: []string{req.User.UserID}})
if _, isChatUserExist := chatUser.UserAccountMap[req.User.UserID]; isChatUserExist {
apiresp.GinError(c, eerrs.ErrAccountAlreadyRegister.Wrap())
return
}

isUserNotExist, err := o.imApiCaller.AccountCheckSingle(c, req.User.UserID)
imToken, err := o.imApiCaller.ImAdminTokenWithDefaultAdmin(c)
if err != nil {
apiresp.GinError(c, err)
return
}
apiCtx := mctx.WithApiToken(c, imToken)
rpcCtx := o.WithAdminUser(c)

if isUserNotExist {
if err = o.imApiCaller.RegisterUser(c, []*sdkws.UserInfo{userInfo}); err != nil {
// if phone exist, don't return err, just a condition.
checkResp, err := o.chatClient.CheckPhoneNumberExist(rpcCtx, &chatpb.CheckPhoneNumberExistReq{PhoneNumber: req.User.PhoneNumber})
if err != nil {
isUserNotExist, err := o.imApiCaller.AccountCheckSingle(apiCtx, checkResp.Userid)
if err != nil {
apiresp.GinError(c, err)
return
}
// if User is not exist in SDK server. You need delete this user and register new user again.
if isUserNotExist {
_, err := o.adminClient.DelAdminAccount(rpcCtx, &admin.DelAdminAccountReq{UserIDs: []string{checkResp.Userid}})
if err != nil {
apiresp.GinError(c, err)
return
}
}
}

imToken, err := o.imApiCaller.ImAdminTokenWithDefaultAdmin(c)
respRegisterUser, err := o.chatClient.RegisterUser(c, req)
if err != nil {
apiresp.GinError(c, err)
return
}
apiCtx := mctx.WithApiToken(c, imToken)
rpcCtx := o.WithAdminUser(c)

respRegisterUser, err := o.chatClient.RegisterUser(c, req)
userInfo := &sdkws.UserInfo{
UserID: respRegisterUser.UserID,
Nickname: req.User.Nickname,
FaceURL: req.User.FaceURL,
CreateTime: time.Now().UnixMilli(),
}
err = o.imApiCaller.RegisterUser(c, []*sdkws.UserInfo{userInfo})
if err != nil {
apiresp.GinError(c, err)
return
Expand Down
15 changes: 12 additions & 3 deletions internal/rpc/chat/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/openimsdk/chat/pkg/common/db/dbutil"
chatdb "github.com/openimsdk/chat/pkg/common/db/table/chat"
constantpb "github.com/openimsdk/protocol/constant"
"github.com/openimsdk/tools/log"
"github.com/openimsdk/tools/mcontext"

"github.com/openimsdk/chat/pkg/common/constant"
Expand Down Expand Up @@ -229,9 +230,7 @@ func (o *chatSvr) FindUserAccount(ctx context.Context, req *chat.FindUserAccount
if len(req.UserIDs) == 0 {
return nil, errs.ErrArgs.WrapMsg("user id list must be set")
}
if _, _, err := mctx.CheckAdminOrUser(ctx); err != nil {
return nil, err
}

attributes, err := o.Database.FindAttribute(ctx, req.UserIDs)
if err != nil {
return nil, err
Expand Down Expand Up @@ -293,3 +292,13 @@ func (o *chatSvr) checkTheUniqueness(ctx context.Context, req *chat.AddUserAccou
}
return nil
}

func (o *chatSvr) CheckPhoneNumberExist(ctx context.Context, req *chat.CheckPhoneNumberExistReq) (resp *chat.CheckPhoneNumberExistResp, err error) {
attribute, err := o.Database.FindAttributeByPhone(ctx, []string{req.PhoneNumber})
log.ZDebug(ctx, "Check Number is ", attribute[0].PhoneNumber)
log.ZDebug(ctx, "Check userID is ", attribute[0].UserID)
if attribute[0].PhoneNumber == req.PhoneNumber {
return &chat.CheckPhoneNumberExistResp{Userid: attribute[0].UserID}, eerrs.ErrAccountAlreadyRegister.Wrap()
}
return &chat.CheckPhoneNumberExistResp{}, nil
}
5 changes: 5 additions & 0 deletions pkg/common/db/database/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type ChatDatabaseInterface interface {
UpdateUseInfo(ctx context.Context, userID string, attribute map[string]any) (err error)
FindAttribute(ctx context.Context, userIDs []string) ([]*chatdb.Attribute, error)
FindAttributeByAccount(ctx context.Context, accounts []string) ([]*chatdb.Attribute, error)
FindAttributeByPhone(ctx context.Context, phoneNumbers []string) ([]*chatdb.Attribute, error)
TakeAttributeByPhone(ctx context.Context, areaCode string, phoneNumber string) (*chatdb.Attribute, error)
TakeAttributeByEmail(ctx context.Context, Email string) (*chatdb.Attribute, error)
TakeAttributeByAccount(ctx context.Context, account string) (*chatdb.Attribute, error)
Expand Down Expand Up @@ -121,6 +122,10 @@ func (o *ChatDatabase) FindAttributeByAccount(ctx context.Context, accounts []st
return o.attribute.FindAccount(ctx, accounts)
}

func (o *ChatDatabase) FindAttributeByPhone(ctx context.Context, phoneNumbers []string) ([]*chatdb.Attribute, error) {
return o.attribute.FindPhone(ctx, phoneNumbers)
}

func (o *ChatDatabase) TakeAttributeByPhone(ctx context.Context, areaCode string, phoneNumber string) (*chatdb.Attribute, error) {
return o.attribute.TakePhone(ctx, areaCode, phoneNumber)
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/common/db/model/chat/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package chat

import (
"context"

"github.com/openimsdk/tools/db/mongoutil"
"github.com/openimsdk/tools/db/pagination"
"github.com/openimsdk/tools/errs"
Expand Down Expand Up @@ -81,6 +82,10 @@ func (o *Attribute) FindAccount(ctx context.Context, accounts []string) ([]*chat
return mongoutil.Find[*chat.Attribute](ctx, o.coll, bson.M{"account": bson.M{"$in": accounts}})
}

func (o *Attribute) FindPhone(ctx context.Context, phoneNumbers []string) ([]*chat.Attribute, error) {
return mongoutil.Find[*chat.Attribute](ctx, o.coll, bson.M{"phone_number": bson.M{"$in": phoneNumbers}})
}

func (o *Attribute) Search(ctx context.Context, keyword string, genders []int32, pagination pagination.Pagination) (int64, []*chat.Attribute, error) {
filter := bson.M{}
if len(genders) > 0 {
Expand Down
6 changes: 4 additions & 2 deletions pkg/common/db/table/chat/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ package chat

import (
"context"
"github.com/openimsdk/tools/db/pagination"
"time"

"github.com/openimsdk/tools/db/pagination"
)

type Attribute struct {
Expand Down Expand Up @@ -45,11 +46,12 @@ func (Attribute) TableName() string {
}

type AttributeInterface interface {
//NewTx(tx any) AttributeInterface
// NewTx(tx any) AttributeInterface
Create(ctx context.Context, attribute ...*Attribute) error
Update(ctx context.Context, userID string, data map[string]any) error
Find(ctx context.Context, userIds []string) ([]*Attribute, error)
FindAccount(ctx context.Context, accounts []string) ([]*Attribute, error)
FindPhone(ctx context.Context, phoneNumber []string) ([]*Attribute, error)
Search(ctx context.Context, keyword string, genders []int32, pagination pagination.Pagination) (int64, []*Attribute, error)
TakePhone(ctx context.Context, areaCode string, phoneNumber string) (*Attribute, error)
TakeEmail(ctx context.Context, email string) (*Attribute, error)
Expand Down
3 changes: 2 additions & 1 deletion pkg/common/imapi/caller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"sync"
"time"

"github.com/openimsdk/chat/pkg/eerrs"
"github.com/openimsdk/tools/log"

"github.com/openimsdk/protocol/auth"
Expand Down Expand Up @@ -174,7 +175,7 @@ func (c *Caller) AccountCheckSingle(ctx context.Context, userID string) (bool, e
return false, err
}
if resp.Results[0].AccountStatus != "unregistered" {
return false, nil
return false, eerrs.ErrAccountAlreadyRegister.Wrap()
}
return true, nil
}
Loading

0 comments on commit 68da59a

Please sign in to comment.