Skip to content

Commit

Permalink
fix: add user
Browse files Browse the repository at this point in the history
  • Loading branch information
icey-yu committed Oct 18, 2024
1 parent f34cb46 commit 52878c6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
9 changes: 7 additions & 2 deletions internal/api/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,13 @@ func (o *Api) AddUserAccount(c *gin.Context) {
FaceURL: req.User.FaceURL,
CreateTime: time.Now().UnixMilli(),
}
err = o.imApiCaller.RegisterUser(c, []*sdkws.UserInfo{userInfo})
imToken, err := o.imApiCaller.ImAdminTokenWithDefaultAdmin(c)
if err != nil {
apiresp.GinError(c, err)
return
}
ctx := o.WithAdminUser(mctx.WithApiToken(c, imToken))
err = o.imApiCaller.RegisterUser(ctx, []*sdkws.UserInfo{userInfo})
if err != nil {
apiresp.GinError(c, err)
return
Expand All @@ -166,7 +172,6 @@ func (o *Api) AddUserAccount(c *gin.Context) {
_ = o.imApiCaller.InviteToGroup(c, req.User.UserID, resp.GroupIDs)
}
apiresp.GinSuccess(c, nil)

}

func (o *Api) DelAdminAccount(c *gin.Context) {
Expand Down
25 changes: 13 additions & 12 deletions pkg/common/imapi/caller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package imapi

import (
"context"
"github.com/openimsdk/tools/log"
"sync"
"time"

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

"github.com/openimsdk/protocol/auth"
"github.com/openimsdk/protocol/constant"
constantpb "github.com/openimsdk/protocol/constant"
Expand Down Expand Up @@ -61,18 +60,20 @@ func (c *Caller) ImportFriend(ctx context.Context, ownerUserID string, friendUse
}

func (c *Caller) ImAdminTokenWithDefaultAdmin(ctx context.Context) (string, error) {
c.lock.Lock()
defer c.lock.Unlock()
if c.token == "" || c.timeout.Before(time.Now()) {
userID := c.defaultIMUserID
token, err := c.GetAdminToken(ctx, userID)
if err != nil {
log.ZError(ctx, "get im admin token", err, "userID", userID)
return "", err
c.lock.Lock()
if c.token == "" || c.timeout.Before(time.Now()) {
userID := c.defaultIMUserID
token, err := c.GetAdminToken(ctx, userID)
if err != nil {
log.ZError(ctx, "get im admin token", err, "userID", userID)
return "", err
}
log.ZDebug(ctx, "get im admin token", "userID", userID)
c.token = token
c.timeout = time.Now().Add(time.Minute * 5)
}
log.ZDebug(ctx, "get im admin token", "userID", userID)
c.token = token
c.timeout = time.Now().Add(time.Minute * 5)
c.lock.Unlock()
}
return c.token, nil
}
Expand Down

0 comments on commit 52878c6

Please sign in to comment.