Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: firebase token and user status update. #381

Merged
merged 16 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
156 changes: 0 additions & 156 deletions internal/cache/cache.go

This file was deleted.

72 changes: 72 additions & 0 deletions internal/cache/cahe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package cache

import "sync"

// Cache is a Generic sync.Map structure.
type Cache[K comparable, V any] struct {
m sync.Map
}

func NewCache[K comparable, V any]() *Cache[K, V] {
return &Cache[K, V]{}
}

// Load returns the value stored in the map for a key, or nil if no value is present.
func (c *Cache[K, V]) Load(key K) (value V, ok bool) {
rawValue, ok := c.m.Load(key)
if !ok {
return
}
return rawValue.(V), ok
}

// Store sets the value for a key.
func (c *Cache[K, V]) Store(key K, value V) {
c.m.Store(key, value)
}

// StoreAll sets all value by f's key.
func (c *Cache[K, V]) StoreAll(f func(value V) K, values []V) {
for _, v := range values {
c.m.Store(f(v), v)
}
}

// LoadOrStore returns the existing value for the key if present.
func (c *Cache[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) {
rawValue, loaded := c.m.LoadOrStore(key, value)
return rawValue.(V), loaded
}

// Delete deletes the value for a key.
func (c *Cache[K, V]) Delete(key K) {
c.m.Delete(key)
}

// DeleteAll deletes all values.
func (c *Cache[K, V]) DeleteAll() {
c.m.Range(func(key, value interface{}) bool {
c.m.Delete(key)
return true
})
}

// RangeAll returns all values in the map.
func (c *Cache[K, V]) RangeAll() (values []V) {
c.m.Range(func(rawKey, rawValue interface{}) bool {
values = append(values, rawValue.(V))
return true
})
return values
}

// RangeCon returns values in the map that satisfy condition f.
func (c *Cache[K, V]) RangeCon(f func(key K, value V) bool) (values []V) {
c.m.Range(func(rawKey, rawValue interface{}) bool {
if f(rawKey.(K), rawValue.(V)) {
values = append(values, rawValue.(V))
}
return true
})
return values
}
85 changes: 80 additions & 5 deletions internal/conversation_msg/conversation_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"encoding/json"
"errors"
utils2 "github.com/OpenIMSDK/tools/utils"
"github.com/openimsdk/openim-sdk-core/v3/internal/business"
"github.com/openimsdk/openim-sdk-core/v3/internal/cache"
"github.com/openimsdk/openim-sdk-core/v3/internal/file"
Expand All @@ -33,6 +34,7 @@ import (
"github.com/openimsdk/openim-sdk-core/v3/pkg/db/db_interface"
"github.com/openimsdk/openim-sdk-core/v3/pkg/db/model_struct"
sdk "github.com/openimsdk/openim-sdk-core/v3/pkg/sdk_params_callback"
"github.com/openimsdk/openim-sdk-core/v3/pkg/sdkerrs"
"github.com/openimsdk/openim-sdk-core/v3/pkg/syncer"

"github.com/OpenIMSDK/tools/log"
Expand Down Expand Up @@ -65,7 +67,7 @@ type Conversation struct {
file *file.File
business *business.Business
messageController *MessageController
cache *cache.Cache
cache *cache.Cache[string, *model_struct.LocalConversation]
full *full.Full
maxSeqRecorder MaxSeqRecorder
IsExternalExtensions bool
Expand Down Expand Up @@ -106,7 +108,7 @@ func NewConversation(ctx context.Context, longConnMgr *interaction.LongConnMgr,
ch chan common.Cmd2Value,
friend *friend.Friend, group *group.Group, user *user.User,
conversationListener open_im_sdk_callback.OnConversationListener,
msgListener open_im_sdk_callback.OnAdvancedMsgListener, business *business.Business, cache *cache.Cache, full *full.Full, file *file.File) *Conversation {
msgListener open_im_sdk_callback.OnAdvancedMsgListener, business *business.Business, full *full.Full, file *file.File) *Conversation {
info := ccontext.Info(ctx)
n := &Conversation{db: db,
LongConnMgr: longConnMgr,
Expand All @@ -127,7 +129,7 @@ func NewConversation(ctx context.Context, longConnMgr *interaction.LongConnMgr,
n.SetMsgListener(msgListener)
n.SetConversationListener(conversationListener)
n.initSyncer()
n.cache = cache
n.cache = cache.NewCache[string, *model_struct.LocalConversation]()
return n
}

Expand Down Expand Up @@ -915,7 +917,7 @@ func mapConversationToList(m map[string]*model_struct.LocalConversation) (cs []*
func (c *Conversation) addFaceURLAndName(ctx context.Context, lc *model_struct.LocalConversation) error {
switch lc.ConversationType {
case constant.SingleChatType, constant.NotificationChatType:
faceUrl, name, err := c.cache.GetUserNameAndFaceURL(ctx, lc.UserID)
faceUrl, name, err := c.getUserNameAndFaceURL(ctx, lc.UserID)
if err != nil {
return err
}
Expand Down Expand Up @@ -943,7 +945,7 @@ func (c *Conversation) batchAddFaceURLAndName(ctx context.Context, conversations
groupIDs = append(groupIDs, conversation.GroupID)
}
}
users, err := c.cache.BatchGetUserNameAndFaceURL(ctx, userIDs...)
users, err := c.batchGetUserNameAndFaceURL(ctx, userIDs...)
if err != nil {
return err
}
Expand Down Expand Up @@ -974,3 +976,76 @@ func (c *Conversation) batchAddFaceURLAndName(ctx context.Context, conversations
}
return nil
}
func (c *Conversation) batchGetUserNameAndFaceURL(ctx context.Context, userIDs ...string) (map[string]*user.BasicInfo,
error) {
m := make(map[string]*user.BasicInfo)
var notCachedUserIDs []string
var notInFriend []string

friendList, err := c.friend.Db().GetFriendInfoList(ctx, userIDs)
if err != nil {
log.ZWarn(ctx, "BatchGetUserNameAndFaceURL", err, "userIDs", userIDs)
notInFriend = userIDs
} else {
notInFriend = utils2.SliceSub(userIDs, utils2.Slice(friendList, func(e *model_struct.LocalFriend) string {
return e.FriendUserID
}))
}
for _, localFriend := range friendList {
userInfo := &user.BasicInfo{FaceURL: localFriend.FaceURL}
if localFriend.Remark != "" {
userInfo.Nickname = localFriend.Remark
} else {
userInfo.Nickname = localFriend.Nickname
}
m[localFriend.FriendUserID] = userInfo
}

for _, userID := range notInFriend {
if value, ok := c.user.UserBasicCache.Load(userID); ok {
m[userID] = value
} else {
notCachedUserIDs = append(notCachedUserIDs, userID)
}
}

if len(notCachedUserIDs) > 0 {
users, err := c.user.GetServerUserInfo(ctx, notCachedUserIDs)
if err != nil {
return nil, err
}
for _, u := range users {
userInfo := &user.BasicInfo{FaceURL: u.FaceURL, Nickname: u.Nickname}
m[u.UserID] = userInfo
c.user.UserBasicCache.Store(u.UserID, userInfo)
}
}
return m, nil
}
func (c *Conversation) getUserNameAndFaceURL(ctx context.Context, userID string) (faceURL, name string, err error) {
//find in cache
if value, ok := c.user.UserBasicCache.Load(userID); ok {
return value.FaceURL, value.Nickname, nil
}
//get from local db
friendInfo, err := c.friend.Db().GetFriendInfoByFriendUserID(ctx, userID)
if err == nil {
faceURL = friendInfo.FaceURL
if friendInfo.Remark != "" {
name = friendInfo.Remark
} else {
name = friendInfo.Nickname
}
return faceURL, name, nil
}
//get from server db
users, err := c.user.GetServerUserInfo(ctx, []string{userID})
if err != nil {
return "", "", err
}
if len(users) == 0 {
return "", "", sdkerrs.ErrUserIDNotFound.Wrap(userID)
}
c.user.UserBasicCache.Store(userID, &user.BasicInfo{FaceURL: users[0].FaceURL, Nickname: users[0].Nickname})
return users[0].FaceURL, users[0].Nickname, nil
}