From 738bb8a925ab7f935777c3a00fe0b045d1df30ee Mon Sep 17 00:00:00 2001 From: AndrewZuo01 Date: Wed, 6 Dec 2023 15:12:39 +0800 Subject: [PATCH] update set pin friends --- internal/api/friend.go | 3 +++ internal/api/route.go | 1 + internal/rpc/friend/friend.go | 34 ++++++++++++++++++++++++++ pkg/common/db/controller/friend.go | 7 ++++++ pkg/common/db/mgo/friend.go | 15 ++++++++++++ pkg/common/db/table/relation/friend.go | 2 ++ 6 files changed, 62 insertions(+) diff --git a/internal/api/friend.go b/internal/api/friend.go index 23f337a9f..58e7398dd 100644 --- a/internal/api/friend.go +++ b/internal/api/friend.go @@ -92,3 +92,6 @@ func (o *FriendApi) GetFriendIDs(c *gin.Context) { func (o *FriendApi) GetSpecifiedFriendsInfo(c *gin.Context) { a2r.Call(friend.FriendClient.GetSpecifiedFriendsInfo, o.Client, c) } +func (o *FriendApi) SetPinFriends(c *gin.Context) { + a2r.Call(friend.FriendClient.PinFriends, o.Client, c) +} diff --git a/internal/api/route.go b/internal/api/route.go index 7a331d643..42d7a7744 100644 --- a/internal/api/route.go +++ b/internal/api/route.go @@ -98,6 +98,7 @@ func NewGinRouter(discov discoveryregistry.SvcDiscoveryRegistry, rdb redis.Unive friendRouterGroup.POST("/is_friend", f.IsFriend) friendRouterGroup.POST("/get_friend_id", f.GetFriendIDs) friendRouterGroup.POST("/get_specified_friends_info", f.GetSpecifiedFriendsInfo) + friendRouterGroup.POST("/set_pin_friend", f.SetPinFriends) } g := NewGroupApi(*groupRpc) groupRouterGroup := r.Group("/group", ParseToken) diff --git a/internal/rpc/friend/friend.go b/internal/rpc/friend/friend.go index 7f22c9d0d..c41559614 100644 --- a/internal/rpc/friend/friend.go +++ b/internal/rpc/friend/friend.go @@ -434,3 +434,37 @@ func (s *friendServer) GetSpecifiedFriendsInfo(ctx context.Context, req *pbfrien } return resp, nil } +func (s *friendServer) PinFriends( + ctx context.Context, + req *pbfriend.PinFriendsReq, +) (*pbfriend.PinFriendsResp, error) { + if len(req.FriendUserIDs) == 0 { + return nil, errs.ErrArgs.Wrap("friendIDList is empty") + } + if utils.Duplicate(req.FriendUserIDs) { + return nil, errs.ErrArgs.Wrap("friendIDList repeated") + } + var isPinned bool + if req.IsPinned != nil { + isPinned = req.IsPinned.Value + } else { + return nil, errs.ErrArgs.Wrap("isPinned is nil") + } + + //檢查是不是在好友列表裏 + _, err := s.friendDatabase.FindFriendsWithError(ctx, req.OwnerUserID, req.FriendUserIDs) + if err != nil { + return nil, err + } + + //全部置頂 + //把所有friendslist的isPinned都設置為true + for _, friendID := range req.FriendUserIDs { + if err := s.friendDatabase.UpdateFriendPinStatus(ctx, req.OwnerUserID, friendID, isPinned); err != nil { + return nil, err + } + } + + resp := &pbfriend.PinFriendsResp{} + return resp, nil +} diff --git a/pkg/common/db/controller/friend.go b/pkg/common/db/controller/friend.go index 29b2ef9b1..34ce22295 100644 --- a/pkg/common/db/controller/friend.go +++ b/pkg/common/db/controller/friend.go @@ -58,6 +58,7 @@ type FriendDatabase interface { FindFriendsWithError(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*relation.FriendModel, err error) FindFriendUserIDs(ctx context.Context, ownerUserID string) (friendUserIDs []string, err error) FindBothFriendRequests(ctx context.Context, fromUserID, toUserID string) (friends []*relation.FriendRequestModel, err error) + UpdateFriendPinStatus(ctx context.Context, ownerUserID string, friendUserID string, isPinned bool) (err error) } type friendDatabase struct { @@ -298,3 +299,9 @@ func (f *friendDatabase) FindFriendUserIDs(ctx context.Context, ownerUserID stri func (f *friendDatabase) FindBothFriendRequests(ctx context.Context, fromUserID, toUserID string) (friends []*relation.FriendRequestModel, err error) { return f.friendRequest.FindBothFriendRequests(ctx, fromUserID, toUserID) } +func (f *friendDatabase) UpdateFriendPinStatus(ctx context.Context, ownerUserID string, friendUserID string, isPinned bool) (err error) { + if err := f.friend.UpdatePinStatus(ctx, ownerUserID, friendUserID, isPinned); err != nil { + return err + } + return f.cache.DelFriend(ownerUserID, friendUserID).ExecDel(ctx) +} diff --git a/pkg/common/db/mgo/friend.go b/pkg/common/db/mgo/friend.go index aa9cb0301..f0312e02e 100644 --- a/pkg/common/db/mgo/friend.go +++ b/pkg/common/db/mgo/friend.go @@ -2,6 +2,7 @@ package mgo import ( "context" + "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/mgoutil" "github.com/OpenIMSDK/tools/pagination" @@ -129,3 +130,17 @@ func (f *FriendMgo) FindFriendUserIDs(ctx context.Context, ownerUserID string) ( filter := bson.M{"owner_user_id": ownerUserID} return mgoutil.Find[string](ctx, f.coll, filter, options.Find().SetProjection(bson.M{"_id": 0, "friend_user_id": 1})) } + +// UpdatePinStatus update friend's pin status +func (f *FriendMgo) UpdatePinStatus(ctx context.Context, ownerUserID string, friendUserID string, isPinned bool) (err error) { + + filter := bson.M{"owner_user_id": ownerUserID, "friend_user_id": friendUserID} + update := bson.M{"$set": bson.M{"is_pinned": isPinned}} + + _, err = f.coll.UpdateOne(ctx, filter, update) + if err != nil { + return errs.Wrap(err, "update pin error") + } + + return nil +} diff --git a/pkg/common/db/table/relation/friend.go b/pkg/common/db/table/relation/friend.go index 75dbea850..dfe9d036d 100644 --- a/pkg/common/db/table/relation/friend.go +++ b/pkg/common/db/table/relation/friend.go @@ -56,4 +56,6 @@ type FriendModelInterface interface { FindInWhoseFriends(ctx context.Context, friendUserID string, pagination pagination.Pagination) (total int64, friends []*FriendModel, err error) // FindFriendUserIDs retrieves a list of friend user IDs for a given owner. FindFriendUserIDs(ctx context.Context, ownerUserID string) (friendUserIDs []string, err error) + // UpdatePinStatus update friend's pin status + UpdatePinStatus(ctx context.Context, ownerUserID string, friendUserID string, isPinned bool) (err error) }