Skip to content

Commit

Permalink
refactor: move user
Browse files Browse the repository at this point in the history
  • Loading branch information
icey-yu committed Sep 14, 2024
1 parent 72d4d44 commit e3b0ee1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 65 deletions.
55 changes: 54 additions & 1 deletion internal/user/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,25 @@ func (u *User) UserOnlineStatusChange(users map[string][]int32) {
}

func (u *User) GetSelfUserInfo(ctx context.Context) (*model_struct.LocalUser, error) {
return u.getSelfUserInfo(ctx)
userInfo, errLocal := u.GetLoginUser(ctx, u.loginUserID)
if errLocal == nil {
return userInfo, nil
}

userInfoFromServer, errServer := u.GetUserInfoFromServer(ctx, []string{u.loginUserID})
if errServer != nil {
return nil, errServer
}

if len(userInfoFromServer) == 0 {
return nil, sdkerrs.ErrUserIDNotFound
}

if err := u.InsertLoginUser(ctx, userInfoFromServer[0]); err != nil {
return nil, err
}

return userInfoFromServer[0], nil
}

func (u *User) SetSelfInfo(ctx context.Context, userInfo *sdkws.UserInfoWithEx) error {
Expand Down Expand Up @@ -151,3 +169,38 @@ func (u *User) GetUsersInfo(ctx context.Context, userIDs []string) ([]*sdk_struc
}
return res, nil
}

// GetUsersInfoFromSvr retrieves user information from the server.
func (u *User) GetUsersInfoFromSvr(ctx context.Context, userIDs []string) ([]*model_struct.LocalUser, error) {
users, err := u.getUsersInfo(ctx, userIDs)
if err != nil {
return nil, sdkerrs.WrapMsg(err, "GetUsersInfoFromSvr failed")
}
return datautil.Batch(ServerUserToLocalUser, users), nil
}

func (u *User) GetUserInfoWithCache(ctx context.Context, cacheKey string) (*model_struct.LocalUser, error) {
return u.UserCache.FetchGet(ctx, cacheKey)
}

func (u *User) GetUserInfoWithCacheFunc(ctx context.Context, cacheKey string, fetchFunc func(ctx context.Context, key string) (*model_struct.LocalUser, error)) (*model_struct.LocalUser, error) {
if userInfo, ok := u.UserCache.Load(cacheKey); ok {
return userInfo, nil
}

fetchedData, err := fetchFunc(ctx, cacheKey)
if err != nil {
return nil, err
}

u.UserCache.Store(cacheKey, fetchedData)
return fetchedData, nil
}

func (u *User) GetUsersInfoWithCache(ctx context.Context, cacheKeys []string) ([]*model_struct.LocalUser, error) {
m, err := u.UserCache.MultiFetchGet(ctx, cacheKeys)
if err != nil {
return nil, err
}
return datautil.MapToSlice(m), nil
}
67 changes: 3 additions & 64 deletions internal/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@ import (
"context"
"fmt"

"github.com/openimsdk/openim-sdk-core/v3/open_im_sdk_callback"
"github.com/openimsdk/openim-sdk-core/v3/pkg/cache"
"github.com/openimsdk/openim-sdk-core/v3/pkg/common"
"github.com/openimsdk/openim-sdk-core/v3/pkg/constant"
"github.com/openimsdk/openim-sdk-core/v3/pkg/db/db_interface"
"github.com/openimsdk/openim-sdk-core/v3/pkg/db/model_struct"
"github.com/openimsdk/openim-sdk-core/v3/pkg/sdkerrs"
"github.com/openimsdk/openim-sdk-core/v3/pkg/syncer"
"github.com/openimsdk/tools/utils/datautil"

"github.com/openimsdk/openim-sdk-core/v3/open_im_sdk_callback"
"github.com/openimsdk/openim-sdk-core/v3/pkg/common"
"github.com/openimsdk/openim-sdk-core/v3/pkg/constant"
"github.com/openimsdk/openim-sdk-core/v3/pkg/utils"
)

Expand Down Expand Up @@ -135,61 +132,3 @@ func (u *User) initSyncer() {
},
)
}

// GetUsersInfoFromSvr retrieves user information from the server.
func (u *User) GetUsersInfoFromSvr(ctx context.Context, userIDs []string) ([]*model_struct.LocalUser, error) {
users, err := u.getUsersInfo(ctx, userIDs)
if err != nil {
return nil, sdkerrs.WrapMsg(err, "GetUsersInfoFromSvr failed")
}
return datautil.Batch(ServerUserToLocalUser, users), nil
}

// getSelfUserInfo retrieves the user's information.
func (u *User) getSelfUserInfo(ctx context.Context) (*model_struct.LocalUser, error) {
userInfo, errLocal := u.GetLoginUser(ctx, u.loginUserID)
if errLocal == nil {
return userInfo, nil
}

userInfoFromServer, errServer := u.GetUserInfoFromServer(ctx, []string{u.loginUserID})
if errServer != nil {
return nil, errServer
}

if len(userInfoFromServer) == 0 {
return nil, sdkerrs.ErrUserIDNotFound
}

if err := u.InsertLoginUser(ctx, userInfoFromServer[0]); err != nil {
return nil, err
}

return userInfoFromServer[0], nil
}

func (u *User) GetUserInfoWithCache(ctx context.Context, cacheKey string) (*model_struct.LocalUser, error) {
return u.UserCache.FetchGet(ctx, cacheKey)
}

func (u *User) GetUserInfoWithCacheFunc(ctx context.Context, cacheKey string, fetchFunc func(ctx context.Context, key string) (*model_struct.LocalUser, error)) (*model_struct.LocalUser, error) {
if userInfo, ok := u.UserCache.Load(cacheKey); ok {
return userInfo, nil
}

fetchedData, err := fetchFunc(ctx, cacheKey)
if err != nil {
return nil, err
}

u.UserCache.Store(cacheKey, fetchedData)
return fetchedData, nil
}

func (u *User) GetUsersInfoWithCache(ctx context.Context, cacheKeys []string) ([]*model_struct.LocalUser, error) {
m, err := u.UserCache.MultiFetchGet(ctx, cacheKeys)
if err != nil {
return nil, err
}
return datautil.MapToSlice(m), nil
}

0 comments on commit e3b0ee1

Please sign in to comment.