pull/2336/head
withchao 1 year ago
parent 10315e9f86
commit eb362daaf2

@ -1,12 +1,12 @@
package friend package friend
import ( import (
relationtb "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation" "github.com/openimsdk/open-im-server/v3/pkg/common/storage/model"
"github.com/openimsdk/protocol/friend" "github.com/openimsdk/protocol/friend"
"github.com/openimsdk/tools/utils/datautil" "github.com/openimsdk/tools/utils/datautil"
) )
func friendDB2PB(db *relationtb.FriendModel) *friend.FriendInfo { func friendDB2PB(db *model.Friend) *friend.FriendInfo {
return &friend.FriendInfo{ return &friend.FriendInfo{
OwnerUserID: db.OwnerUserID, OwnerUserID: db.OwnerUserID,
FriendUserID: db.FriendUserID, FriendUserID: db.FriendUserID,
@ -21,6 +21,6 @@ func friendDB2PB(db *relationtb.FriendModel) *friend.FriendInfo {
} }
} }
func friendsDB2PB(db []*relationtb.FriendModel) []*friend.FriendInfo { func friendsDB2PB(db []*model.Friend) []*friend.FriendInfo {
return datautil.Slice(db, friendDB2PB) return datautil.Slice(db, friendDB2PB)
} }

@ -6,7 +6,7 @@ import (
"encoding/binary" "encoding/binary"
"encoding/json" "encoding/json"
"github.com/openimsdk/open-im-server/v3/pkg/authverify" "github.com/openimsdk/open-im-server/v3/pkg/authverify"
"github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation" "github.com/openimsdk/open-im-server/v3/pkg/common/storage/model"
pbfriend "github.com/openimsdk/protocol/friend" pbfriend "github.com/openimsdk/protocol/friend"
"github.com/openimsdk/tools/errs" "github.com/openimsdk/tools/errs"
) )
@ -89,7 +89,7 @@ func (s *friendServer) GetIncrementalFriends(ctx context.Context, req *pbfriend.
} else { } else {
deleteUserIDs, changeUserIDs = incrVer.DeleteAndChangeIDs() deleteUserIDs, changeUserIDs = incrVer.DeleteAndChangeIDs()
} }
var friends []*relation.FriendModel var friends []*model.Friend
if len(changeUserIDs) > 0 { if len(changeUserIDs) > 0 {
friends, err = s.friendDatabase.FindFriendsWithError(ctx, req.UserID, changeUserIDs) friends, err = s.friendDatabase.FindFriendsWithError(ctx, req.UserID, changeUserIDs)
if err != nil { if err != nil {

@ -695,7 +695,7 @@ func (s *userServer) userModelToResp(users []*tablerelation.User, pagination pag
return &pbuser.SearchNotificationAccountResp{Total: total, NotificationAccounts: notificationAccounts} return &pbuser.SearchNotificationAccountResp{Total: total, NotificationAccounts: notificationAccounts}
} }
func (s *userServer) NotificationUserInfoUpdate(ctx context.Context, userID string, oldUser *relation.UserModel) error { func (s *userServer) NotificationUserInfoUpdate(ctx context.Context, userID string, oldUser *tablerelation.User) error {
user, err := s.db.GetUserByID(ctx, userID) user, err := s.db.GetUserByID(ctx, userID)
if err != nil { if err != nil {
return err return err

@ -34,7 +34,7 @@ func UserDB2Pb(user *relationtb.User) *sdkws.UserInfo {
} }
} }
func UsersDB2Pb(users []*relationtb.UserModel) []*sdkws.UserInfo { func UsersDB2Pb(users []*relationtb.User) []*sdkws.UserInfo {
return datautil.Slice(users, UserDB2Pb) return datautil.Slice(users, UserDB2Pb)
} }

@ -16,6 +16,7 @@ package cache
import ( import (
"context" "context"
"github.com/openimsdk/open-im-server/v3/pkg/common/db/dataver"
relationtb "github.com/openimsdk/open-im-server/v3/pkg/common/storage/model" relationtb "github.com/openimsdk/open-im-server/v3/pkg/common/storage/model"
) )
@ -32,4 +33,12 @@ type FriendCache interface {
DelFriend(ownerUserID, friendUserID string) FriendCache DelFriend(ownerUserID, friendUserID string) FriendCache
// Delete friends when friends' info changed // Delete friends when friends' info changed
DelFriends(ownerUserID string, friendUserIDs []string) FriendCache DelFriends(ownerUserID string, friendUserIDs []string) FriendCache
DelOwner(friendUserID string, ownerUserIDs []string) FriendCache
DelSortFriendUserIDs(ownerUserIDs ...string) FriendCache
FindSortFriendUserIDs(ctx context.Context, ownerUserID string) ([]string, error)
FindFriendIncrVersion(ctx context.Context, ownerUserID string, version uint, limit int) (*dataver.WriteLog, error)
} }

@ -28,37 +28,12 @@ import (
"github.com/openimsdk/tools/log" "github.com/openimsdk/tools/log"
"github.com/openimsdk/tools/utils/datautil" "github.com/openimsdk/tools/utils/datautil"
"github.com/redis/go-redis/v9" "github.com/redis/go-redis/v9"
"time"
) )
const ( const (
friendExpireTime = time.Second * 60 * 60 * 12 friendExpireTime = time.Second * 60 * 60 * 12
) )
//// FriendCache is an interface for caching friend-related data.
//type FriendCache interface {
// metaCache
// NewCache() FriendCache
// GetFriendIDs(ctx context.Context, ownerUserID string) (friendIDs []string, err error)
// // Called when friendID list changed
// DelFriendIDs(ownerUserID ...string) FriendCache
//
// DelSortFriendUserIDs(ownerUserIDs ...string) FriendCache
//
// // Get single friendInfo from the cache
// GetFriend(ctx context.Context, ownerUserID, friendUserID string) (friend *relationtb.FriendModel, err error)
// // Delete friend when friend info changed
// DelFriend(ownerUserID, friendUserID string) FriendCache
// // Delete friends when friends' info changed
// DelFriends(ownerUserID string, friendUserIDs []string) FriendCache
//
// DelOwner(friendUserID string, ownerUserIDs []string) FriendCache
//
// FindSortFriendUserIDs(ctx context.Context, ownerUserID string) ([]string, error)
//
// FindFriendIncrVersion(ctx context.Context, ownerUserID string, version uint, limit int) (*dataver.WriteLog, error)
//}
// FriendCacheRedis is an implementation of the FriendCache interface using Redis. // FriendCacheRedis is an implementation of the FriendCache interface using Redis.
type FriendCacheRedis struct { type FriendCacheRedis struct {
cache.BatchDeleter cache.BatchDeleter
@ -129,8 +104,8 @@ func (f *FriendCacheRedis) DelFriendIDs(ownerUserIDs ...string) cache.FriendCach
return newFriendCache return newFriendCache
} }
func (f *FriendCacheRedis) DelSortFriendUserIDs(ownerUserIDs ...string) FriendCache { func (f *FriendCacheRedis) DelSortFriendUserIDs(ownerUserIDs ...string) cache.FriendCache {
newGroupCache := f.NewCache() newGroupCache := f.CloneFriendCache()
keys := make([]string, 0, len(ownerUserIDs)) keys := make([]string, 0, len(ownerUserIDs))
for _, userID := range ownerUserIDs { for _, userID := range ownerUserIDs {
keys = append(keys, f.getFriendSyncSortUserIDsKey(userID)) keys = append(keys, f.getFriendSyncSortUserIDsKey(userID))
@ -194,7 +169,7 @@ func (f *FriendCacheRedis) DelFriends(ownerUserID string, friendUserIDs []string
return newFriendCache return newFriendCache
} }
func (f *FriendCacheRedis) DelOwner(friendUserID string, ownerUserIDs []string) FriendCache { func (f *FriendCacheRedis) DelOwner(friendUserID string, ownerUserIDs []string) cache.FriendCache {
newFriendCache := f.CloneFriendCache() newFriendCache := f.CloneFriendCache()
for _, ownerUserID := range ownerUserIDs { for _, ownerUserID := range ownerUserIDs {

@ -87,7 +87,7 @@ type FriendDatabase interface {
UpdateFriendUserInfo(ctx context.Context, friendUserID string, ownerUserID []string, nickname string, faceURL string) error UpdateFriendUserInfo(ctx context.Context, friendUserID string, ownerUserID []string, nickname string, faceURL string) error
SearchFriend(ctx context.Context, ownerUserID, keyword string, pagination pagination.Pagination) (int64, []*relation.FriendModel, error) SearchFriend(ctx context.Context, ownerUserID, keyword string, pagination pagination.Pagination) (int64, []*model.Friend, error)
} }
type friendDatabase struct { type friendDatabase struct {
@ -289,7 +289,7 @@ func (f *friendDatabase) AgreeFriendRequest(ctx context.Context, friendRequest *
return err return err
} }
} }
return f.cache.DelFriendIDs(friendRequest.ToUserID, friendRequest.FromUserID).DelSortFriendUserIDs(friendRequest.ToUserID, friendRequest.FromUserID).ExecDel(ctx) return f.cache.DelFriendIDs(friendRequest.ToUserID, friendRequest.FromUserID).DelSortFriendUserIDs(friendRequest.ToUserID, friendRequest.FromUserID).ChainExecDel(ctx)
}) })
} }
@ -374,9 +374,9 @@ func (f *friendDatabase) UpdateFriendUserInfo(ctx context.Context, friendUserID
if err := f.friend.UpdateFriendUserInfo(ctx, friendUserID, nickname, faceURL); err != nil { if err := f.friend.UpdateFriendUserInfo(ctx, friendUserID, nickname, faceURL); err != nil {
return err return err
} }
return f.cache.DelOwner(friendUserID, ownerUserIDs).ExecDel(ctx) return f.cache.DelOwner(friendUserID, ownerUserIDs).ChainExecDel(ctx)
} }
func (f *friendDatabase) SearchFriend(ctx context.Context, ownerUserID, keyword string, pagination pagination.Pagination) (int64, []*relation.FriendModel, error) { func (f *friendDatabase) SearchFriend(ctx context.Context, ownerUserID, keyword string, pagination pagination.Pagination) (int64, []*model.Friend, error) {
return f.friend.SearchFriend(ctx, ownerUserID, keyword, pagination) return f.friend.SearchFriend(ctx, ownerUserID, keyword, pagination)
} }

@ -17,28 +17,13 @@ package database
import ( import (
"context" "context"
"github.com/openimsdk/open-im-server/v3/pkg/common/db/dataver" "github.com/openimsdk/open-im-server/v3/pkg/common/db/dataver"
"time"
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/model" "github.com/openimsdk/open-im-server/v3/pkg/common/storage/model"
"github.com/openimsdk/tools/db/pagination" "github.com/openimsdk/tools/db/pagination"
) )
//// FriendModel represents the data structure for a friend relationship in MongoDB.
//type FriendModel struct {
// OwnerUserID string `bson:"owner_user_id"`
// FriendUserID string `bson:"friend_user_id"`
// FriendNickname string `bson:"friend_nickname"`
// FriendFaceURL string `bson:"friend_face_url"`
// Remark string `bson:"remark"`
// CreateTime time.Time `bson:"create_time"`
// AddSource int32 `bson:"add_source"`
// OperatorUserID string `bson:"operator_user_id"`
// Ex string `bson:"ex"`
// IsPinned bool `bson:"is_pinned"`
//}
// Friend defines the operations for managing friends in MongoDB. // Friend defines the operations for managing friends in MongoDB.
type FriendModelInterface interface { type Friend interface {
// Create inserts multiple friend records. // Create inserts multiple friend records.
Create(ctx context.Context, friends []*model.Friend) (err error) Create(ctx context.Context, friends []*model.Friend) (err error)
// Delete removes specified friends of the owner user. // Delete removes specified friends of the owner user.
@ -71,4 +56,6 @@ type FriendModelInterface interface {
UpdateFriendUserInfo(ctx context.Context, friendUserID string, nickname string, faceURL string) error UpdateFriendUserInfo(ctx context.Context, friendUserID string, nickname string, faceURL string) error
SearchFriend(ctx context.Context, ownerUserID, keyword string, pagination pagination.Pagination) (int64, []*model.Friend, error) SearchFriend(ctx context.Context, ownerUserID, keyword string, pagination pagination.Pagination) (int64, []*model.Friend, error)
FindOwnerFriendUserIds(ctx context.Context, ownerUserID string, limit int) ([]string, error)
} }

@ -225,7 +225,7 @@ func (f *FriendMgo) UpdateFriendUserInfo(ctx context.Context, friendUserID strin
return err return err
} }
func (f *FriendMgo) SearchFriend(ctx context.Context, ownerUserID, keyword string, pagination pagination.Pagination) (int64, []*relation.FriendModel, error) { func (f *FriendMgo) SearchFriend(ctx context.Context, ownerUserID, keyword string, pagination pagination.Pagination) (int64, []*model.Friend, error) {
//where := bson.M{ //where := bson.M{
// "owner_user_id": ownerUserID, // "owner_user_id": ownerUserID,
// "$or": []bson.M{ // "$or": []bson.M{

@ -22,6 +22,8 @@ import (
type Friend struct { type Friend struct {
OwnerUserID string `bson:"owner_user_id"` OwnerUserID string `bson:"owner_user_id"`
FriendUserID string `bson:"friend_user_id"` FriendUserID string `bson:"friend_user_id"`
FriendNickname string `bson:"friend_nickname"`
FriendFaceURL string `bson:"friend_face_url"`
Remark string `bson:"remark"` Remark string `bson:"remark"`
CreateTime time.Time `bson:"create_time"` CreateTime time.Time `bson:"create_time"`
AddSource int32 `bson:"add_source"` AddSource int32 `bson:"add_source"`

Loading…
Cancel
Save