Skip to content

Commit

Permalink
fix: firebase token and user status update. (#381)
Browse files Browse the repository at this point in the history
* fix: message unread count remove when user not in group.

Signed-off-by: Gordon <[email protected]>

* fix: group member delete trigger and close ws conn when user logout.

Signed-off-by: Gordon <[email protected]>

* fix: group member delete trigger and close ws conn when user logout.

Signed-off-by: Gordon <[email protected]>

* fix: group member delete trigger and close ws conn when user logout.

Signed-off-by: Gordon <[email protected]>

* fix: group member delete trigger and close ws conn when user logout.

Signed-off-by: Gordon <[email protected]>

* fix: remove send time and void client messages sort incorrect.

Signed-off-by: Gordon <[email protected]>

* fix: add avatar and nickname when sessionType is notification.

Signed-off-by: Gordon <[email protected]>

* fix: add avatar and nickname when sessionType is notification.

Signed-off-by: Gordon <[email protected]>

* fix: add avatar and nickname when sessionType is notification.

Signed-off-by: Gordon <[email protected]>

* fix: add avatar and nickname when sessionType is notification.

Signed-off-by: Gordon <[email protected]>

* fix: some function remove not be used.

Signed-off-by: Gordon <[email protected]>

* fix: firebase token and user status update.

Signed-off-by: Gordon <[email protected]>

---------

Signed-off-by: Gordon <[email protected]>
  • Loading branch information
FGadvancer authored Oct 23, 2023
1 parent 53aa556 commit 7332710
Show file tree
Hide file tree
Showing 29 changed files with 357 additions and 514 deletions.
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
}
Loading

0 comments on commit 7332710

Please sign in to comment.