From 3d427a8549614aa5ca88b98a88b87b4b435a37ed Mon Sep 17 00:00:00 2001 From: AndrewZuo01 Date: Thu, 30 Nov 2023 11:16:29 +0800 Subject: [PATCH] update check --- go.mod | 3 +++ internal/api/friend.go | 3 +++ internal/api/route.go | 1 + internal/api/user.go | 8 ++++++++ internal/rpc/friend/friend.go | 28 ++++++++++++++++++++++++++ pkg/common/db/controller/friend.go | 9 +++++++++ pkg/common/db/table/relation/friend.go | 1 + 7 files changed, 53 insertions(+) diff --git a/go.mod b/go.mod index 48217d09a..a5f867d92 100644 --- a/go.mod +++ b/go.mod @@ -156,3 +156,6 @@ require ( golang.org/x/crypto v0.14.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect ) +replace ( + github.com/OpenIMSDK/protocol v0.0.31 => github.com/AndrewZuo01/protocol v0.0.0-20231129091747-d1871b8baaa5 +) diff --git a/internal/api/friend.go b/internal/api/friend.go index 23f337a9f..7f2453341 100644 --- a/internal/api/friend.go +++ b/internal/api/friend.go @@ -64,6 +64,9 @@ func (o *FriendApi) GetDesignatedFriends(c *gin.Context) { func (o *FriendApi) SetFriendRemark(c *gin.Context) { a2r.Call(friend.FriendClient.SetFriendRemark, o.Client, c) } +func (o *FriendApi) SetPinFriends(c *gin.Context) { + a2r.Call(friend.FriendClient.PinFriends, o.Client, c) +} func (o *FriendApi) AddBlack(c *gin.Context) { a2r.Call(friend.FriendClient.AddBlack, 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/api/user.go b/internal/api/user.go index 86b7c0b0b..b74b411c2 100644 --- a/internal/api/user.go +++ b/internal/api/user.go @@ -70,6 +70,10 @@ func (u *UserApi) GetUsersOnlineStatus(c *gin.Context) { apiresp.GinError(c, errs.ErrArgs.WithDetail(err.Error()).Wrap()) return } + if len(req.UserIDs) == 0 { + apiresp.GinError(c, errs.ErrArgs.WithDetail("UserIDs array is empty").Wrap()) + return + } conns, err := u.Discov.GetConns(c, config.Config.RpcRegisterName.OpenImMessageGatewayName) if err != nil { apiresp.GinError(c, err) @@ -134,6 +138,10 @@ func (u *UserApi) GetUsersOnlineTokenDetail(c *gin.Context) { apiresp.GinError(c, errs.ErrArgs.WithDetail(err.Error()).Wrap()) return } + if len(req.UserIDs) == 0 { + apiresp.GinError(c, errs.ErrArgs.WithDetail("UserIDs array is empty").Wrap()) + return + } conns, err := u.Discov.GetConns(c, config.Config.RpcRegisterName.OpenImMessageGatewayName) if err != nil { apiresp.GinError(c, err) diff --git a/internal/rpc/friend/friend.go b/internal/rpc/friend/friend.go index 6e6b6d377..9510c1bc4 100644 --- a/internal/rpc/friend/friend.go +++ b/internal/rpc/friend/friend.go @@ -332,6 +332,34 @@ func (s *friendServer) GetPaginationFriendsApplyFrom( resp.Total = int32(total) 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") + } + + //檢查是不是在好友列表裏 + _, 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); err != nil { + return nil, err + } + } + + resp := &pbfriend.PinFriendsResp{} + return resp, nil +} // ok. func (s *friendServer) IsFriend( diff --git a/pkg/common/db/controller/friend.go b/pkg/common/db/controller/friend.go index 7816ef935..2e4320d37 100644 --- a/pkg/common/db/controller/friend.go +++ b/pkg/common/db/controller/friend.go @@ -78,6 +78,7 @@ type FriendDatabase interface { ) (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) (err error) } type friendDatabase struct { @@ -372,3 +373,11 @@ func (f *friendDatabase) FindFriendUserIDs( 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) (err error) { + if err := f.friend.UpdatePinStatus(ctx, ownerUserID, friendUserID); err != nil { + return err + } + return f.cache.DelFriend(ownerUserID, friendUserID).ExecDel(ctx) +} diff --git a/pkg/common/db/table/relation/friend.go b/pkg/common/db/table/relation/friend.go index 58d8d1d34..a59336c67 100644 --- a/pkg/common/db/table/relation/friend.go +++ b/pkg/common/db/table/relation/friend.go @@ -75,4 +75,5 @@ type FriendModelInterface interface { // 获取好友UserID列表 FindFriendUserIDs(ctx context.Context, ownerUserID string) (friendUserIDs []string, err error) NewTx(tx any) FriendModelInterface + //UpdatePinStatus(ctx context.Context, ownerUserID string, friendUserID string) (err error) }