Skip to content

Commit

Permalink
Merge branch 'main' into fix-set-token-expire
Browse files Browse the repository at this point in the history
  • Loading branch information
skiffer-git authored Aug 2, 2024
2 parents e254bdf + 8581365 commit 106d74c
Show file tree
Hide file tree
Showing 19 changed files with 751 additions and 263 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ COPY --from=builder $SERVER_DIR/start-config.yml $SERVER_DIR/
COPY --from=builder $SERVER_DIR/go.mod $SERVER_DIR/
COPY --from=builder $SERVER_DIR/go.sum $SERVER_DIR/

RUN go get github.com/openimsdk/gomake@v0.0.9
RUN go get github.com/openimsdk/v0.0.14-alpha.5

# Set the command to run when the container starts
ENTRYPOINT ["sh", "-c", "mage start && tail -f /dev/null"]
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ require (
require (
github.com/livekit/protocol v1.10.1
github.com/mitchellh/mapstructure v1.5.0
github.com/openimsdk/gomake v0.0.9
github.com/openimsdk/gomake v0.0.14-alpha.5
github.com/openimsdk/protocol v0.0.63
github.com/openimsdk/tools v0.0.49-alpha.24
github.com/redis/go-redis/v9 v9.5.1
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,10 @@ github.com/nats-io/nkeys v0.4.6/go.mod h1:4DxZNzenSVd1cYQoAa8948QY3QDjrHfcfVADym
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/openimsdk/gomake v0.0.9 h1:ouf25ygN2PMQ68Gfgns/EQRPiLPnp+77SIr68GfE+n4=
github.com/openimsdk/gomake v0.0.9/go.mod h1:PndCozNc2IsQIciyn9mvEblYWZwJmAI+06z94EY+csI=
github.com/openimsdk/gomake v0.0.14-alpha.5 h1:VY9c5x515lTfmdhhPjMvR3BBRrRquAUCFsz7t7vbv7Y=
github.com/openimsdk/gomake v0.0.14-alpha.5/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
}
2 changes: 1 addition & 1 deletion livekit/livekit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ rtc:
use_external_ip: true
enable_loopback_candidate: false
keys:
APIftrpEkL9x2pa: 23ztfSqsfQ8hKkHzHTl3Z4bvaxro0snjk5jwbp5p6Q3
APIGPW3gnFTzqHH: 23ztfSqsfQ8hKkHzHTl3Z4bvaxro0snjk5jwbp5p6Q3
2 changes: 1 addition & 1 deletion pkg/common/config/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.7.0
v1.8.0
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

0 comments on commit 106d74c

Please sign in to comment.