Skip to content

Commit

Permalink
feat: implement correct is registered logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
mo3et committed Jun 14, 2024
1 parent fab0a02 commit 9e75658
Show file tree
Hide file tree
Showing 14 changed files with 444 additions and 208 deletions.
30 changes: 15 additions & 15 deletions internal/api/chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ 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"
"github.com/openimsdk/protocol/sdkws"
"github.com/openimsdk/tools/a2r"
"github.com/openimsdk/tools/apiresp"
"github.com/openimsdk/tools/errs"
"github.com/openimsdk/tools/log"
)

func New(chatClient chatpb.ChatClient, adminClient admin.AdminClient, imApiCaller imapi.CallerInterface, api *util.Api) *Api {
Expand Down Expand Up @@ -98,26 +98,26 @@ func (o *Api) RegisterUser(c *gin.Context) {
apiCtx := mctx.WithApiToken(c, imToken)
rpcCtx := o.WithAdminUser(c)

// if User exist, don't return err, just a condition.
checkResp, err := o.chatClient.CheckUserExist(rpcCtx, &chatpb.CheckUserExistReq{User: req.User})
if err != nil {
if err == eerrs.ErrAccountAlreadyRegister.Wrap() {
isUserNotExist, err := o.imApiCaller.AccountCheckSingle(apiCtx, checkResp.Userid)
log.ZDebug(rpcCtx, "Not else", errs.Unwrap(err))
apiresp.GinError(c, err)
return
}
if checkResp.IsRegistered {
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.chatClient.DelUserAccount(rpcCtx, &chatpb.DelUserAccountReq{UserIDs: []string{checkResp.Userid}})
log.ZDebug(c, "Delete Succsssss", 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
}
}
} else {
apiresp.GinError(c, err)
return
}
}

Expand Down
44 changes: 29 additions & 15 deletions internal/rpc/chat/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
constantpb "github.com/openimsdk/protocol/constant"
"github.com/openimsdk/tools/log"
"github.com/openimsdk/tools/mcontext"
"go.mongodb.org/mongo-driver/mongo"

"github.com/openimsdk/chat/pkg/common/constant"
"github.com/openimsdk/chat/pkg/common/mctx"
Expand Down Expand Up @@ -298,25 +299,38 @@ func (o *chatSvr) checkTheUniqueness(ctx context.Context, req *chat.AddUserAccou
func (o *chatSvr) CheckUserExist(ctx context.Context, req *chat.CheckUserExistReq) (resp *chat.CheckUserExistResp, err error) {
if req.User.PhoneNumber != "" {
attributeByPhone, err := o.Database.TakeAttributeByPhone(ctx, req.User.AreaCode, req.User.PhoneNumber)
// err != nil is not found
if err != nil {
// err != nil is not found User
if err != nil && errs.Unwrap(err) != mongo.ErrNoDocuments {
return nil, err
}
log.ZDebug(ctx, "Check Number is ", attributeByPhone.PhoneNumber)
log.ZDebug(ctx, "Check userID is ", attributeByPhone.UserID)
if attributeByPhone.PhoneNumber == req.User.PhoneNumber {
return &chat.CheckUserExistResp{Userid: attributeByPhone.UserID}, eerrs.ErrAccountAlreadyRegister.Wrap()
}
} else if req.User.Email != "" {
attributeByEmail, err := o.Database.TakeAttributeByEmail(ctx, req.User.Email)
if err != nil {
return nil, err
if attributeByPhone != nil {
log.ZDebug(ctx, "Check Number is ", attributeByPhone.PhoneNumber)
log.ZDebug(ctx, "Check userID is ", attributeByPhone.UserID)
if attributeByPhone.PhoneNumber == req.User.PhoneNumber {
return &chat.CheckUserExistResp{Userid: attributeByPhone.UserID, IsRegistered: true}, nil
}
}
log.ZDebug(ctx, "Check email is ", attributeByEmail.Email)
log.ZDebug(ctx, "Check userID is ", attributeByEmail.UserID)
if attributeByEmail.Email == req.User.Email {
return &chat.CheckUserExistResp{Userid: attributeByEmail.UserID}, eerrs.ErrAccountAlreadyRegister.Wrap()
} else {
if req.User.Email != "" {
attributeByEmail, err := o.Database.TakeAttributeByEmail(ctx, req.User.Email)
if err != nil && errs.Unwrap(err) != mongo.ErrNoDocuments {
return nil, err
}
if attributeByEmail != nil {
log.ZDebug(ctx, "Check email is ", attributeByEmail.Email)
log.ZDebug(ctx, "Check userID is ", attributeByEmail.UserID)
if attributeByEmail.Email == req.User.Email {
return &chat.CheckUserExistResp{Userid: attributeByEmail.UserID, IsRegistered: true}, nil
}
}
}
}
return nil, nil
}

func (o *chatSvr) DelUserAccount(ctx context.Context, req *chat.DelUserAccountReq) (resp *chat.DelUserAccountResp, err error) {
if err := o.Database.DelUserAccount(ctx, req.UserIDs); err != nil && errs.Unwrap(err) != mongo.ErrNoDocuments {
return nil, err
}
return nil, nil
}
16 changes: 16 additions & 0 deletions pkg/common/db/database/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type ChatDatabaseInterface interface {
NewUserCountTotal(ctx context.Context, before *time.Time) (int64, error)
UserLoginCountTotal(ctx context.Context, before *time.Time) (int64, error)
UserLoginCountRangeEverydayTotal(ctx context.Context, start *time.Time, end *time.Time) (map[string]int64, int64, error)
DelUserAccount(ctx context.Context, userIDs []string) error
}

func NewChatDatabase(cli *mongoutil.Client) (ChatDatabaseInterface, error) {
Expand Down Expand Up @@ -260,3 +261,18 @@ func (o *ChatDatabase) UserLoginCountTotal(ctx context.Context, before *time.Tim
func (o *ChatDatabase) UserLoginCountRangeEverydayTotal(ctx context.Context, start *time.Time, end *time.Time) (map[string]int64, int64, error) {
return o.userLoginRecord.CountRangeEverydayTotal(ctx, start, end)
}

func (o *ChatDatabase) DelUserAccount(ctx context.Context, userIDs []string) error {
return o.tx.Transaction(ctx, func(ctx context.Context) error {
if err := o.register.Delete(ctx, userIDs); err != nil {
return err
}
if err := o.account.Delete(ctx, userIDs); err != nil {
return err
}
if err := o.attribute.Delete(ctx, userIDs); err != nil {
return err
}
return nil
})
}
10 changes: 9 additions & 1 deletion pkg/common/db/model/chat/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ package chat

import (
"context"
"time"

"github.com/openimsdk/tools/db/mongoutil"
"github.com/openimsdk/tools/errs"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"time"

"github.com/openimsdk/chat/pkg/common/db/table/chat"
)
Expand Down Expand Up @@ -62,3 +63,10 @@ func (o *Account) Update(ctx context.Context, userID string, data map[string]any
func (o *Account) UpdatePassword(ctx context.Context, userId string, password string) error {
return mongoutil.UpdateOne(ctx, o.coll, bson.M{"user_id": userId}, bson.M{"$set": bson.M{"password": password, "change_time": time.Now()}}, false)
}

func (o *Account) Delete(ctx context.Context, userIDs []string) error {
if len(userIDs) == 0 {
return nil
}
return mongoutil.DeleteMany(ctx, o.coll, bson.M{"user_id": bson.M{"$in": userIDs}})
}
7 changes: 7 additions & 0 deletions pkg/common/db/model/chat/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,10 @@ func (o *Attribute) SearchUser(ctx context.Context, keyword string, userIDs []st
}
return mongoutil.FindPage[*chat.Attribute](ctx, o.coll, filter, pagination)
}

func (o *Attribute) Delete(ctx context.Context, userIDs []string) error {
if len(userIDs) == 0 {
return nil
}
return mongoutil.DeleteMany(ctx, o.coll, bson.M{"user_id": bson.M{"$in": userIDs}})
}
10 changes: 9 additions & 1 deletion pkg/common/db/model/chat/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ package chat

import (
"context"
"time"

"github.com/openimsdk/tools/db/mongoutil"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"time"

"github.com/openimsdk/chat/pkg/common/db/table/chat"
"github.com/openimsdk/tools/errs"
Expand Down Expand Up @@ -57,3 +58,10 @@ func (o *Register) CountTotal(ctx context.Context, before *time.Time) (int64, er
}
return mongoutil.Count(ctx, o.coll, filter)
}

func (o *Register) Delete(ctx context.Context, userIDs []string) error {
if len(userIDs) == 0 {
return nil
}
return mongoutil.DeleteMany(ctx, o.coll, bson.M{"user_id": bson.M{"$in": userIDs}})
}
1 change: 1 addition & 0 deletions pkg/common/db/table/chat/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ type AccountInterface interface {
Take(ctx context.Context, userId string) (*Account, error)
Update(ctx context.Context, userID string, data map[string]any) error
UpdatePassword(ctx context.Context, userId string, password string) error
Delete(ctx context.Context, userIDs []string) error
}
1 change: 1 addition & 0 deletions pkg/common/db/table/chat/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ type AttributeInterface interface {
Take(ctx context.Context, userID string) (*Attribute, error)
SearchNormalUser(ctx context.Context, keyword string, forbiddenID []string, gender int32, pagination pagination.Pagination) (int64, []*Attribute, error)
SearchUser(ctx context.Context, keyword string, userIDs []string, genders []int32, pagination pagination.Pagination) (int64, []*Attribute, error)
Delete(ctx context.Context, userIDs []string) error
}
3 changes: 2 additions & 1 deletion pkg/common/db/table/chat/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ func (Register) TableName() string {
}

type RegisterInterface interface {
//NewTx(tx any) RegisterInterface
// NewTx(tx any) RegisterInterface
Create(ctx context.Context, registers ...*Register) error
CountTotal(ctx context.Context, before *time.Time) (int64, error)
Delete(ctx context.Context, userIDs []string) error
}
2 changes: 1 addition & 1 deletion pkg/common/imapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ var (
getGroupsInfo = NewApiCaller[group.GetGroupsInfoReq, group.GetGroupsInfoResp]("/group/get_groups_info")
registerUserCount = NewApiCaller[user.UserRegisterCountReq, user.UserRegisterCountResp]("/statistics/user/register")
friendUserIDs = NewApiCaller[friend.GetFriendIDsReq, friend.GetFriendIDsResp]("/friend/get_friend_id")
accountCheck = NewApiCaller[user.AccountCheckReq, user.AccountCheckResp]("/account_check")
accountCheck = NewApiCaller[user.AccountCheckReq, user.AccountCheckResp]("/user/account_check")
)
2 changes: 1 addition & 1 deletion pkg/common/imapi/caller.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (c *Caller) AccountCheckSingle(ctx context.Context, userID string) (bool, e
if err != nil {
return false, err
}
if resp.Results[0].AccountStatus != "unregistered" {
if resp.Results[0].AccountStatus == "registered" {
return false, eerrs.ErrAccountAlreadyRegister.Wrap()
}
return true, nil
Expand Down
Loading

0 comments on commit 9e75658

Please sign in to comment.