From eb362daaf28e37502f1bd595a2c4cfd30bff5a33 Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Thu, 30 May 2024 15:07:42 +0800 Subject: [PATCH] mage --- internal/rpc/friend/convert.go | 6 ++-- internal/rpc/friend/sync.go | 4 +-- internal/rpc/user/user.go | 2 +- pkg/common/convert/user.go | 2 +- pkg/common/storage/cache/friend.go | 9 ++++++ pkg/common/storage/cache/redis/friend.go | 31 ++----------------- pkg/common/storage/controller/friend.go | 8 ++--- pkg/common/storage/database/friend.go | 19 ++---------- pkg/common/storage/database/mgo/friend.go | 2 +- pkg/common/storage/database/mgo/mongo_test.go | 0 pkg/common/storage/model/friend.go | 2 ++ 11 files changed, 29 insertions(+), 56 deletions(-) delete mode 100644 pkg/common/storage/database/mgo/mongo_test.go diff --git a/internal/rpc/friend/convert.go b/internal/rpc/friend/convert.go index aa6895284..0730df529 100644 --- a/internal/rpc/friend/convert.go +++ b/internal/rpc/friend/convert.go @@ -1,12 +1,12 @@ package friend 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/tools/utils/datautil" ) -func friendDB2PB(db *relationtb.FriendModel) *friend.FriendInfo { +func friendDB2PB(db *model.Friend) *friend.FriendInfo { return &friend.FriendInfo{ OwnerUserID: db.OwnerUserID, 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) } diff --git a/internal/rpc/friend/sync.go b/internal/rpc/friend/sync.go index dd6e49250..bfa7e3a57 100644 --- a/internal/rpc/friend/sync.go +++ b/internal/rpc/friend/sync.go @@ -6,7 +6,7 @@ import ( "encoding/binary" "encoding/json" "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" "github.com/openimsdk/tools/errs" ) @@ -89,7 +89,7 @@ func (s *friendServer) GetIncrementalFriends(ctx context.Context, req *pbfriend. } else { deleteUserIDs, changeUserIDs = incrVer.DeleteAndChangeIDs() } - var friends []*relation.FriendModel + var friends []*model.Friend if len(changeUserIDs) > 0 { friends, err = s.friendDatabase.FindFriendsWithError(ctx, req.UserID, changeUserIDs) if err != nil { diff --git a/internal/rpc/user/user.go b/internal/rpc/user/user.go index 14ce09ed5..504c08a2c 100644 --- a/internal/rpc/user/user.go +++ b/internal/rpc/user/user.go @@ -695,7 +695,7 @@ func (s *userServer) userModelToResp(users []*tablerelation.User, pagination pag 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) if err != nil { return err diff --git a/pkg/common/convert/user.go b/pkg/common/convert/user.go index 5595e8fe5..d824fa68e 100644 --- a/pkg/common/convert/user.go +++ b/pkg/common/convert/user.go @@ -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) } diff --git a/pkg/common/storage/cache/friend.go b/pkg/common/storage/cache/friend.go index acff829f8..9dec3d5ab 100644 --- a/pkg/common/storage/cache/friend.go +++ b/pkg/common/storage/cache/friend.go @@ -16,6 +16,7 @@ package cache import ( "context" + "github.com/openimsdk/open-im-server/v3/pkg/common/db/dataver" relationtb "github.com/openimsdk/open-im-server/v3/pkg/common/storage/model" ) @@ -32,4 +33,12 @@ type FriendCache interface { DelFriend(ownerUserID, friendUserID string) FriendCache // Delete friends when friends' info changed 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) } diff --git a/pkg/common/storage/cache/redis/friend.go b/pkg/common/storage/cache/redis/friend.go index 7b93c74ac..d6754d7bf 100644 --- a/pkg/common/storage/cache/redis/friend.go +++ b/pkg/common/storage/cache/redis/friend.go @@ -28,37 +28,12 @@ import ( "github.com/openimsdk/tools/log" "github.com/openimsdk/tools/utils/datautil" "github.com/redis/go-redis/v9" - "time" ) const ( 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. type FriendCacheRedis struct { cache.BatchDeleter @@ -129,8 +104,8 @@ func (f *FriendCacheRedis) DelFriendIDs(ownerUserIDs ...string) cache.FriendCach return newFriendCache } -func (f *FriendCacheRedis) DelSortFriendUserIDs(ownerUserIDs ...string) FriendCache { - newGroupCache := f.NewCache() +func (f *FriendCacheRedis) DelSortFriendUserIDs(ownerUserIDs ...string) cache.FriendCache { + newGroupCache := f.CloneFriendCache() keys := make([]string, 0, len(ownerUserIDs)) for _, userID := range ownerUserIDs { keys = append(keys, f.getFriendSyncSortUserIDsKey(userID)) @@ -194,7 +169,7 @@ func (f *FriendCacheRedis) DelFriends(ownerUserID string, friendUserIDs []string return newFriendCache } -func (f *FriendCacheRedis) DelOwner(friendUserID string, ownerUserIDs []string) FriendCache { +func (f *FriendCacheRedis) DelOwner(friendUserID string, ownerUserIDs []string) cache.FriendCache { newFriendCache := f.CloneFriendCache() for _, ownerUserID := range ownerUserIDs { diff --git a/pkg/common/storage/controller/friend.go b/pkg/common/storage/controller/friend.go index 9b177dc45..0c83930b1 100644 --- a/pkg/common/storage/controller/friend.go +++ b/pkg/common/storage/controller/friend.go @@ -87,7 +87,7 @@ type FriendDatabase interface { 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 { @@ -289,7 +289,7 @@ func (f *friendDatabase) AgreeFriendRequest(ctx context.Context, friendRequest * 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 { 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) } diff --git a/pkg/common/storage/database/friend.go b/pkg/common/storage/database/friend.go index 728e19593..f5a999485 100644 --- a/pkg/common/storage/database/friend.go +++ b/pkg/common/storage/database/friend.go @@ -17,28 +17,13 @@ package database import ( "context" "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/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. -type FriendModelInterface interface { +type Friend interface { // Create inserts multiple friend records. Create(ctx context.Context, friends []*model.Friend) (err error) // 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 SearchFriend(ctx context.Context, ownerUserID, keyword string, pagination pagination.Pagination) (int64, []*model.Friend, error) + + FindOwnerFriendUserIds(ctx context.Context, ownerUserID string, limit int) ([]string, error) } diff --git a/pkg/common/storage/database/mgo/friend.go b/pkg/common/storage/database/mgo/friend.go index dae5f2103..8993ee829 100644 --- a/pkg/common/storage/database/mgo/friend.go +++ b/pkg/common/storage/database/mgo/friend.go @@ -225,7 +225,7 @@ func (f *FriendMgo) UpdateFriendUserInfo(ctx context.Context, friendUserID strin 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{ // "owner_user_id": ownerUserID, // "$or": []bson.M{ diff --git a/pkg/common/storage/database/mgo/mongo_test.go b/pkg/common/storage/database/mgo/mongo_test.go deleted file mode 100644 index e69de29bb..000000000 diff --git a/pkg/common/storage/model/friend.go b/pkg/common/storage/model/friend.go index 60a40d9c2..c7675e466 100644 --- a/pkg/common/storage/model/friend.go +++ b/pkg/common/storage/model/friend.go @@ -22,6 +22,8 @@ import ( type Friend 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"`