From 0e87b03fb5acaed637918b405f7d612413e8fedf Mon Sep 17 00:00:00 2001 From: icey-yu <1186114839@qq.com> Date: Fri, 18 Oct 2024 12:19:59 +0800 Subject: [PATCH] fix: add user --- internal/api/admin/admin.go | 9 +++++++-- pkg/common/imapi/caller.go | 25 +++++++++++++------------ 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/internal/api/admin/admin.go b/internal/api/admin/admin.go index 6bc4b921..bafb5cdc 100644 --- a/internal/api/admin/admin.go +++ b/internal/api/admin/admin.go @@ -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 @@ -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) { diff --git a/pkg/common/imapi/caller.go b/pkg/common/imapi/caller.go index 24b9d545..88f8f914 100644 --- a/pkg/common/imapi/caller.go +++ b/pkg/common/imapi/caller.go @@ -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" @@ -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 }