Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
41 changes: 34 additions & 7 deletions internal/api/chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"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 @@ -88,6 +89,38 @@ func (o *Api) RegisterUser(c *gin.Context) {
return
}
req.Ip = ip

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

checkResp, err := o.chatClient.CheckUserExist(rpcCtx, &chatpb.CheckUserExistReq{User: req.User})
if err != nil {
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
}
}
}

respRegisterUser, err := o.chatClient.RegisterUser(c, req)
if err != nil {
apiresp.GinError(c, err)
Expand All @@ -104,13 +137,7 @@ func (o *Api) RegisterUser(c *gin.Context) {
apiresp.GinError(c, err)
return
}
imToken, err := o.imApiCaller.ImAdminTokenWithDefaultAdmin(c)
if err != nil {
apiresp.GinError(c, err)
return
}
apiCtx := mctx.WithApiToken(c, imToken)
rpcCtx := o.WithAdminUser(c)

if resp, err := o.adminClient.FindDefaultFriend(rpcCtx, &admin.FindDefaultFriendReq{}); err == nil {
_ = o.imApiCaller.ImportFriend(apiCtx, respRegisterUser.UserID, resp.UserIDs)
}
Expand Down
41 changes: 41 additions & 0 deletions internal/rpc/chat/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ 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"
"go.mongodb.org/mongo-driver/mongo"

"github.com/openimsdk/chat/pkg/common/constant"
"github.com/openimsdk/chat/pkg/common/mctx"
Expand Down Expand Up @@ -293,3 +295,42 @@ func (o *chatSvr) checkTheUniqueness(ctx context.Context, req *chat.AddUserAccou
}
return nil
}

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 User
if err != nil && errs.Unwrap(err) != mongo.ErrNoDocuments {
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
}
}
} 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}})
}
12 changes: 12 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 Expand Up @@ -162,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
}
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,7 +46,7 @@ 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)
Expand All @@ -57,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
}
1 change: 1 addition & 0 deletions pkg/common/imapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +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]("/user/account_check")
)
14 changes: 14 additions & 0 deletions 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 All @@ -40,6 +41,7 @@ type CallerInterface interface {
FindGroupInfo(ctx context.Context, groupIDs []string) ([]*sdkws.GroupInfo, error)
UserRegisterCount(ctx context.Context, start int64, end int64) (map[string]int64, int64, error)
FriendUserIDs(ctx context.Context, userID string) ([]string, error)
AccountCheckSingle(ctx context.Context, userID string) (bool, error)
}

type Caller struct {
Expand Down Expand Up @@ -165,3 +167,15 @@ func (c *Caller) FriendUserIDs(ctx context.Context, userID string) ([]string, er
}
return resp.FriendIDs, nil
}

// return true when isUserNotExist.
func (c *Caller) AccountCheckSingle(ctx context.Context, userID string) (bool, error) {
resp, err := accountCheck.Call(ctx, c.imApi, &user.AccountCheckReq{CheckUserIDs: []string{userID}})
if err != nil {
return false, err
}
if resp.Results[0].AccountStatus == "registered" {
return false, eerrs.ErrAccountAlreadyRegister.Wrap()
}
return true, nil
}
Loading