From 6ab9cff3c3d43965d198e9f0b94092a3a98074a0 Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Tue, 17 Jan 2023 17:31:22 +0800 Subject: [PATCH 1/2] 1 --- internal/rpc/friend/friend.go | 224 ++++++++---------- internal/rpc/friend/other.go | 15 +- pkg/common/db/model/black.go | 40 ++++ pkg/common/db/model/friend.go | 60 +++++ pkg/common/db/model/friend_request.go | 44 ++++ .../{user_black_list_model.go => black.go} | 2 +- pkg/common/db/mysql/friend_model_k.go | 9 +- pkg/common/db/mysql/friend_request_model.go | 12 +- 8 files changed, 264 insertions(+), 142 deletions(-) create mode 100644 pkg/common/db/model/black.go create mode 100644 pkg/common/db/model/friend.go create mode 100644 pkg/common/db/model/friend_request.go rename pkg/common/db/mysql/{user_black_list_model.go => black.go} (97%) diff --git a/internal/rpc/friend/friend.go b/internal/rpc/friend/friend.go index 9f5f6540e..443ba2516 100644 --- a/internal/rpc/friend/friend.go +++ b/internal/rpc/friend/friend.go @@ -4,6 +4,8 @@ import ( chat "Open_IM/internal/rpc/msg" "Open_IM/pkg/common/config" "Open_IM/pkg/common/constant" + rocksCache "Open_IM/pkg/common/db/cache" + "Open_IM/pkg/common/db/model" "Open_IM/pkg/common/db/mysql" "Open_IM/pkg/common/log" "Open_IM/pkg/common/middleware" @@ -11,9 +13,7 @@ import ( "Open_IM/pkg/common/token_verify" "Open_IM/pkg/common/tools" "Open_IM/pkg/common/trace_log" - cp "Open_IM/pkg/common/utils" "Open_IM/pkg/getcdv3" - pbCache "Open_IM/pkg/proto/cache" pbFriend "Open_IM/pkg/proto/friend" sdkws "Open_IM/pkg/proto/sdk_ws" "Open_IM/pkg/utils" @@ -35,9 +35,9 @@ type friendServer struct { rpcRegisterName string etcdSchema string etcdAddr []string - friendModel *mysql.Friend - friendRequestModel *mysql.FriendRequest - blackModel *mysql.Black + friendModel *model.FriendModel + friendRequestModel *model.FriendRequestModel + blackModel *model.BlackModel } func NewFriendServer(port int) *friendServer { @@ -52,9 +52,9 @@ func NewFriendServer(port int) *friendServer { func (s *friendServer) Run() { db := mysql.ConnectToDB() - s.friendModel = mysql.NewFriend(db) - s.friendRequestModel = mysql.NewFriendRequest(db) - s.blackModel = mysql.NewBlack(db) + //s.friendModel = mysql.NewFriend(db) + //s.friendRequestModel = mysql.NewFriendRequest(db) + //s.blackModel = mysql.NewBlack(db) log.NewInfo("0", "friendServer run...") @@ -115,16 +115,8 @@ func (s *friendServer) AddBlacklist(ctx context.Context, req *pbFriend.AddBlackl if err := token_verify.CheckAccessV3(ctx, req.FromUserID); err != nil { return nil, err } - black := imdb.Black{OwnerUserID: req.FromUserID, BlockUserID: req.ToUserID, OperatorUserID: tools.OpUserID(ctx)} - if err := s.blackModel.Create(ctx, []*imdb.Black{&black}); err != nil { - return nil, err - } - etcdConn, err := getcdv3.GetConn(ctx, config.Config.RpcRegisterName.OpenImCacheName) - if err != nil { - return nil, err - } - _, err = pbCache.NewCacheClient(etcdConn).DelBlackIDListFromCache(ctx, &pbCache.DelBlackIDListFromCacheReq{UserID: req.FromUserID}) - if err != nil { + black := mysql.Black{OwnerUserID: req.FromUserID, BlockUserID: req.ToUserID, OperatorUserID: tools.OpUserID(ctx)} + if err := s.blackModel.Create(ctx, []*mysql.Black{&black}); err != nil { return nil, err } chat.BlackAddedNotification(req) @@ -139,19 +131,19 @@ func (s *friendServer) AddFriend(ctx context.Context, req *pbFriend.AddFriendReq if err := callbackBeforeAddFriendV1(req); err != nil { return nil, err } - userIDList, err := rocksCache.GetFriendIDListFromCache(ctx, req.ToUserID) + friends1, err := s.friendModel.FindOwnerUserID(ctx, req.ToUserID) if err != nil { return nil, err } - userIDList2, err := rocksCache.GetFriendIDListFromCache(ctx, req.FromUserID) + friends2, err := s.friendModel.FindOwnerUserID(ctx, req.FromUserID) if err != nil { return nil, err } var isSend = true - for _, v := range userIDList { - if v == req.FromUserID { - for _, v2 := range userIDList2 { - if v2 == req.ToUserID { + for _, v1 := range friends1 { + if v1.FriendUserID == req.FromUserID { + for _, v2 := range friends2 { + if v2.FriendUserID == req.ToUserID { isSend = false break } @@ -159,21 +151,19 @@ func (s *friendServer) AddFriend(ctx context.Context, req *pbFriend.AddFriendReq break } } - //Cannot add non-existent users - if isSend { if _, err := GetUserInfo(ctx, req.ToUserID); err != nil { return nil, err } - friendRequest := imdb.FriendRequest{ + friendRequest := mysql.FriendRequest{ FromUserID: req.FromUserID, ToUserID: req.ToUserID, HandleResult: 0, ReqMsg: req.ReqMsg, CreateTime: time.Now(), } - if err := s.friendRequestModel.Create(ctx, []*imdb.FriendRequest{&friendRequest}); err != nil { + if err := s.friendRequestModel.Create(ctx, []*mysql.FriendRequest{&friendRequest}); err != nil { return nil, err } chat.FriendApplicationNotification(req) @@ -189,7 +179,9 @@ func (s *friendServer) ImportFriend(ctx context.Context, req *pbFriend.ImportFri if _, err := GetUserInfo(ctx, req.FromUserID); err != nil { return nil, err } - for _, userID := range req.FriendUserIDList { + + var friends []*mysql.Friend + for _, userID := range utils.RemoveDuplicateElement(req.FriendUserIDList) { if _, err := GetUserInfo(ctx, userID); err != nil { return nil, err } @@ -197,42 +189,24 @@ func (s *friendServer) ImportFriend(ctx context.Context, req *pbFriend.ImportFri if err != nil { return nil, err } - var friends []*imdb.Friend switch len(fs) { case 1: if fs[0].OwnerUserID == req.FromUserID { - friends = append(friends, &imdb.Friend{OwnerUserID: userID, FriendUserID: req.FromUserID}) + friends = append(friends, &mysql.Friend{OwnerUserID: userID, FriendUserID: req.FromUserID}) } else { - friends = append(friends, &imdb.Friend{OwnerUserID: req.FromUserID, FriendUserID: userID}) + friends = append(friends, &mysql.Friend{OwnerUserID: req.FromUserID, FriendUserID: userID}) } case 0: - friends = append(friends, &imdb.Friend{OwnerUserID: userID, FriendUserID: req.FromUserID}, &imdb.Friend{OwnerUserID: req.FromUserID, FriendUserID: userID}) + friends = append(friends, &mysql.Friend{OwnerUserID: userID, FriendUserID: req.FromUserID}, &mysql.Friend{OwnerUserID: req.FromUserID, FriendUserID: userID}) default: continue } + } + if len(friends) > 0 { if err := s.friendModel.Create(ctx, friends); err != nil { return nil, err } } - etcdConn, err := getcdv3.GetConn(ctx, config.Config.RpcRegisterName.OpenImCacheName) - if err != nil { - return nil, err - } - cacheClient := pbCache.NewCacheClient(etcdConn) - if _, err := cacheClient.DelFriendIDListFromCache(ctx, &pbCache.DelFriendIDListFromCacheReq{UserID: req.FromUserID}); err != nil { - return nil, err - } - if err := rocksCache.DelAllFriendsInfoFromCache(ctx, req.FromUserID); err != nil { - trace_log.SetCtxInfo(ctx, "DelAllFriendsInfoFromCache", err, "userID", req.FromUserID) - } - for _, userID := range req.FriendUserIDList { - if _, err = cacheClient.DelFriendIDListFromCache(ctx, &pbCache.DelFriendIDListFromCacheReq{UserID: userID}); err != nil { - return nil, err - } - if err := rocksCache.DelAllFriendsInfoFromCache(ctx, userID); err != nil { - trace_log.SetCtxInfo(ctx, "DelAllFriendsInfoFromCache", err, "userID", userID) - } - } return resp, nil } @@ -250,47 +224,23 @@ func (s *friendServer) AddFriendResponse(ctx context.Context, req *pbFriend.AddF friendRequest.HandleTime = time.Now() friendRequest.HandleMsg = req.HandleMsg friendRequest.HandlerUserID = tools.OpUserID(ctx) - err = imdb.UpdateFriendApplication(friendRequest) + err = mysql.UpdateFriendApplication(friendRequest) if err != nil { return nil, err } //Change the status of the friend request form if req.HandleResult == constant.FriendFlag { - var isInsert bool //Establish friendship after find friend relationship not exists _, err := s.friendModel.Take(ctx, req.FromUserID, req.ToUserID) if errors.Is(err, gorm.ErrRecordNotFound) { - if err := s.friendModel.Create(ctx, []*imdb.Friend{{OwnerUserID: req.FromUserID, FriendUserID: req.ToUserID, OperatorUserID: tools.OpUserID(ctx)}}); err != nil { + if err := s.friendModel.Create(ctx, []*mysql.Friend{{OwnerUserID: req.FromUserID, FriendUserID: req.ToUserID, OperatorUserID: tools.OpUserID(ctx)}}); err != nil { return nil, err } - isInsert = true + chat.FriendAddedNotification(tools.OperationID(ctx), tools.OpUserID(ctx), req.FromUserID, req.ToUserID) } else if err != nil { return nil, err } - - // cache rpc - if isInsert { - etcdConn, err := getcdv3.GetConn(ctx, config.Config.RpcRegisterName.OpenImCacheName) - if err != nil { - return nil, err - } - client := pbCache.NewCacheClient(etcdConn) - - if _, err := client.DelFriendIDListFromCache(context.Background(), &pbCache.DelFriendIDListFromCacheReq{UserID: req.ToUserID}); err != nil { - return nil, err - } - if _, err := client.DelFriendIDListFromCache(context.Background(), &pbCache.DelFriendIDListFromCacheReq{UserID: req.FromUserID}); err != nil { - return nil, err - } - if err := rocksCache.DelAllFriendsInfoFromCache(ctx, req.ToUserID); err != nil { - trace_log.SetCtxInfo(ctx, "DelAllFriendsInfoFromCache", err, "userID", req.ToUserID) - } - if err := rocksCache.DelAllFriendsInfoFromCache(ctx, req.FromUserID); err != nil { - trace_log.SetCtxInfo(ctx, "DelAllFriendsInfoFromCache", err, "userID", req.FromUserID) - } - chat.FriendAddedNotification(tools.OperationID(ctx), tools.OpUserID(ctx), req.FromUserID, req.ToUserID) - } } if req.HandleResult == constant.FriendResponseAgree { @@ -311,20 +261,6 @@ func (s *friendServer) DeleteFriend(ctx context.Context, req *pbFriend.DeleteFri if err := s.friendModel.Delete(ctx, req.FromUserID, req.ToUserID); err != nil { return nil, err } - etcdConn, err := getcdv3.GetConn(ctx, config.Config.RpcRegisterName.OpenImCacheName) - if err != nil { - return nil, err - } - _, err = pbCache.NewCacheClient(etcdConn).DelFriendIDListFromCache(context.Background(), &pbCache.DelFriendIDListFromCacheReq{UserID: req.FromUserID}) - if err != nil { - return nil, err - } - if err := rocksCache.DelAllFriendsInfoFromCache(ctx, req.FromUserID); err != nil { - trace_log.SetCtxInfo(ctx, "DelAllFriendsInfoFromCache", err, "DelAllFriendsInfoFromCache", req.FromUserID) - } - if err := rocksCache.DelAllFriendsInfoFromCache(ctx, req.ToUserID); err != nil { - trace_log.SetCtxInfo(ctx, "DelAllFriendsInfoFromCache", err, "DelAllFriendsInfoFromCache", req.ToUserID) - } chat.FriendDeletedNotification(req) return resp, nil } @@ -334,19 +270,17 @@ func (s *friendServer) GetBlacklist(ctx context.Context, req *pbFriend.GetBlackl if err := token_verify.CheckAccessV3(ctx, req.FromUserID); err != nil { return nil, err } - blackIDList, err := rocksCache.GetBlackListFromCache(ctx, req.FromUserID) + blacks, err := s.blackModel.FindByOwnerUserID(ctx, req.FromUserID) if err != nil { return nil, err } - for _, userID := range blackIDList { - user, err := rocksCache.GetUserInfoFromCache(ctx, userID) - if err != nil { - trace_log.SetCtxInfo(ctx, "GetUserInfoFromCache", err, "userID", userID) - continue - } - var blackUserInfo sdkws.PublicUserInfo - utils.CopyStructFields(&blackUserInfo, user) - resp.BlackUserInfoList = append(resp.BlackUserInfoList, &blackUserInfo) + blackIDList := make([]string, 0, len(blacks)) + for _, black := range blacks { + blackIDList = append(blackIDList, black.BlockUserID) + } + resp.BlackUserInfoList, err = GetPublicUserInfoBatch(ctx, blackIDList) + if err != nil { + return nil, err } return resp, nil } @@ -359,9 +293,6 @@ func (s *friendServer) SetFriendRemark(ctx context.Context, req *pbFriend.SetFri if err := s.friendModel.UpdateRemark(ctx, req.FromUserID, req.ToUserID, req.Remark); err != nil { return nil, err } - if err := rocksCache.DelAllFriendsInfoFromCache(ctx, req.FromUserID); err != nil { - return nil, err - } chat.FriendRemarkSetNotification(tools.OperationID(ctx), tools.OpUserID(ctx), req.FromUserID, req.ToUserID) return resp, nil } @@ -372,15 +303,7 @@ func (s *friendServer) RemoveBlacklist(ctx context.Context, req *pbFriend.Remove if err := token_verify.CheckAccessV3(ctx, req.FromUserID); err != nil { return nil, err } - if err := s.blackModel.Delete(ctx, []*imdb.Black{{OwnerUserID: req.FromUserID, BlockUserID: req.ToUserID}}); err != nil { - return nil, err - } - etcdConn, err := getcdv3.GetConn(ctx, config.Config.RpcRegisterName.OpenImCacheName) - if err != nil { - return nil, err - } - _, err = pbCache.NewCacheClient(etcdConn).DelBlackIDListFromCache(context.Background(), &pbCache.DelBlackIDListFromCacheReq{UserID: req.FromUserID}) - if err != nil { + if err := s.blackModel.Delete(ctx, []*mysql.Black{{OwnerUserID: req.FromUserID, BlockUserID: req.ToUserID}}); err != nil { return nil, err } chat.BlackDeletedNotification(req) @@ -405,11 +328,11 @@ func (s *friendServer) IsFriend(ctx context.Context, req *pbFriend.IsFriendReq) if err := token_verify.CheckAccessV3(ctx, req.FromUserID); err != nil { return nil, err } - friendIDList, err := rocksCache.GetFriendIDListFromCache(ctx, req.FromUserID) + exist, err := s.friendModel.IsExist(ctx, req.FromUserID, req.ToUserID) if err != nil { return nil, err } - resp.Response = utils.IsContain(req.ToUserID, friendIDList) + resp.Response = exist return resp, nil } @@ -418,15 +341,26 @@ func (s *friendServer) GetFriendList(ctx context.Context, req *pbFriend.GetFrien if err := token_verify.CheckAccessV3(ctx, req.FromUserID); err != nil { return nil, err } - friendList, err := rocksCache.GetAllFriendsInfoFromCache(ctx, req.FromUserID) + friends, err := s.friendModel.FindOwnerUserID(ctx, req.FromUserID) if err != nil { return nil, err } - var userInfoList []*sdkws.FriendInfo - for _, friendUser := range friendList { - friendUserInfo := sdkws.FriendInfo{FriendUser: &sdkws.UserInfo{}} - cp.FriendDBCopyOpenIM(&friendUserInfo, friendUser) - userInfoList = append(userInfoList, &friendUserInfo) + userIDList := make([]string, 0, len(friends)) + for _, f := range friends { + userIDList = append(userIDList, f.FriendUserID) + } + users, err := GetUserInfoList(ctx, userIDList) + if err != nil { + return nil, err + } + userMap := make(map[string]*sdkws.UserInfo) + for i, user := range users { + userMap[user.UserID] = users[i] + } + for _, friendUser := range friends { + friendUserInfo := sdkws.FriendInfo{FriendUser: userMap[friendUser.FriendUserID]} + utils.CopyStructFields(&friendUserInfo, friendUser) + resp.FriendInfoList = append(resp.FriendInfoList, &friendUserInfo) } return resp, nil } @@ -439,13 +373,28 @@ func (s *friendServer) GetFriendApplyList(ctx context.Context, req *pbFriend.Get return nil, err } // Find the current user friend applications received - applyUsersInfo, err := s.friendRequestModel.FindToUserID(ctx, req.FromUserID) + friendRequests, err := s.friendRequestModel.FindToUserID(ctx, req.FromUserID) + if err != nil { + return nil, err + } + userIDList := make([]string, 0, len(friendRequests)) + for _, f := range friendRequests { + userIDList = append(userIDList, f.FromUserID) + } + users, err := GetPublicUserInfoBatch(ctx, userIDList) if err != nil { return nil, err } - for _, applyUserInfo := range applyUsersInfo { + userMap := make(map[string]*sdkws.PublicUserInfo) + for i, user := range users { + userMap[user.UserID] = users[i] + } + for _, friendRequest := range friendRequests { var userInfo sdkws.FriendRequest - cp.FriendRequestDBCopyOpenIM(&userInfo, applyUserInfo) + if u, ok := userMap[friendRequest.FromUserID]; ok { + utils.CopyStructFields(&userInfo, u) + } + utils.CopyStructFields(&userInfo, friendRequest) resp.FriendRequestList = append(resp.FriendRequestList, &userInfo) } return resp, nil @@ -458,13 +407,28 @@ func (s *friendServer) GetSelfApplyList(ctx context.Context, req *pbFriend.GetSe return nil, err } // Find the self add other userinfo - usersInfo, err := s.friendRequestModel.FindFromUserID(ctx, req.FromUserID) + friendRequests, err := s.friendRequestModel.FindFromUserID(ctx, req.FromUserID) if err != nil { return nil, err } - for _, selfApplyOtherUserInfo := range usersInfo { - var userInfo sdkws.FriendRequest // pbFriend.ApplyUserInfo - cp.FriendRequestDBCopyOpenIM(&userInfo, selfApplyOtherUserInfo) + userIDList := make([]string, 0, len(friendRequests)) + for _, f := range friendRequests { + userIDList = append(userIDList, f.ToUserID) + } + users, err := GetPublicUserInfoBatch(ctx, userIDList) + if err != nil { + return nil, err + } + userMap := make(map[string]*sdkws.PublicUserInfo) + for i, user := range users { + userMap[user.UserID] = users[i] + } + for _, friendRequest := range friendRequests { + var userInfo sdkws.FriendRequest + if u, ok := userMap[friendRequest.ToUserID]; ok { + utils.CopyStructFields(&userInfo, u) + } + utils.CopyStructFields(&userInfo, friendRequest) resp.FriendRequestList = append(resp.FriendRequestList, &userInfo) } return resp, nil diff --git a/internal/rpc/friend/other.go b/internal/rpc/friend/other.go index 3beebfada..18a41f56b 100644 --- a/internal/rpc/friend/other.go +++ b/internal/rpc/friend/other.go @@ -1,14 +1,25 @@ package friend import ( + server_api_params "Open_IM/pkg/proto/sdk_ws" "context" "errors" ) -func GetUserInfo(ctx context.Context, userID string) (interface{}, error) { +func GetUserInfo(ctx context.Context, userID string) (*server_api_params.PublicUserInfo, error) { return nil, errors.New("TODO:GetUserInfo") } -func GetUserInfoBatch(ctx context.Context, userIDs []string) (interface{}, error) { +func GetPublicUserInfoBatch(ctx context.Context, userIDs []string) ([]*server_api_params.PublicUserInfo, error) { + if len(userIDs) == 0 { + return []*server_api_params.PublicUserInfo{}, nil + } + return nil, errors.New("TODO:GetUserInfo") +} + +func GetUserInfoList(ctx context.Context, userIDs []string) ([]*server_api_params.UserInfo, error) { + if len(userIDs) == 0 { + return []*server_api_params.UserInfo{}, nil + } return nil, errors.New("TODO:GetUserInfo") } diff --git a/pkg/common/db/model/black.go b/pkg/common/db/model/black.go new file mode 100644 index 000000000..920f517a2 --- /dev/null +++ b/pkg/common/db/model/black.go @@ -0,0 +1,40 @@ +package model + +import ( + "Open_IM/pkg/common/db/cache" + "Open_IM/pkg/common/db/mysql" + "context" +) + +type BlackModel struct { + db *mysql.Black + cache *cache.GroupCache +} + +func (b *BlackModel) Create(ctx context.Context, blacks []*mysql.Black) (err error) { + return b.db.Create(ctx, blacks) +} + +func (b *BlackModel) Delete(ctx context.Context, blacks []*mysql.Black) (err error) { + return b.db.Delete(ctx, blacks) +} + +func (b *BlackModel) UpdateByMap(ctx context.Context, ownerUserID, blockUserID string, args map[string]interface{}) (err error) { + return b.db.UpdateByMap(ctx, ownerUserID, blockUserID, args) +} + +func (b *BlackModel) Update(ctx context.Context, blacks []*mysql.Black) (err error) { + return b.db.Update(ctx, blacks) +} + +func (b *BlackModel) Find(ctx context.Context, blacks []*mysql.Black) (blackList []*mysql.Black, err error) { + return b.db.Find(ctx, blacks) +} + +func (b *BlackModel) Take(ctx context.Context, blackID string) (black *mysql.Black, err error) { + return b.db.Take(ctx, blackID) +} + +func (b *BlackModel) FindByOwnerUserID(ctx context.Context, ownerUserID string) (blackList []*mysql.Black, err error) { + return b.db.FindByOwnerUserID(ctx, ownerUserID) +} diff --git a/pkg/common/db/model/friend.go b/pkg/common/db/model/friend.go new file mode 100644 index 000000000..32c4fdb75 --- /dev/null +++ b/pkg/common/db/model/friend.go @@ -0,0 +1,60 @@ +package model + +import ( + "Open_IM/pkg/common/db/cache" + "Open_IM/pkg/common/db/mysql" + "context" + "errors" + "gorm.io/gorm" +) + +type FriendModel struct { + db *mysql.Friend + cache *cache.GroupCache +} + +func (f *FriendModel) Create(ctx context.Context, friends []*mysql.Friend) (err error) { + return f.db.Create(ctx, friends) +} + +func (f *FriendModel) Delete(ctx context.Context, ownerUserID string, friendUserIDs string) (err error) { + return f.db.Delete(ctx, ownerUserID, friendUserIDs) +} + +func (f *FriendModel) UpdateByMap(ctx context.Context, ownerUserID string, args map[string]interface{}) (err error) { + return f.db.UpdateByMap(ctx, ownerUserID, args) +} + +func (f *FriendModel) Update(ctx context.Context, friends []*mysql.Friend) (err error) { + return f.db.Update(ctx, friends) +} + +func (f *FriendModel) UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error) { + return f.db.UpdateRemark(ctx, ownerUserID, friendUserID, remark) +} + +func (f *FriendModel) FindOwnerUserID(ctx context.Context, ownerUserID string) (friends []*mysql.Friend, err error) { + return f.db.FindOwnerUserID(ctx, ownerUserID) +} + +func (f *FriendModel) FindFriendUserID(ctx context.Context, friendUserID string) (friends []*mysql.Friend, err error) { + return f.db.FindFriendUserID(ctx, friendUserID) +} + +func (f *FriendModel) Take(ctx context.Context, ownerUserID, friendUserID string) (friend *mysql.Friend, err error) { + return f.db.Take(ctx, ownerUserID, friendUserID) +} + +func (f *FriendModel) FindUserState(ctx context.Context, userID1, userID2 string) (friends []*mysql.Friend, err error) { + return f.db.FindUserState(ctx, userID1, userID2) +} + +func (f *FriendModel) IsExist(ctx context.Context, ownerUserID, friendUserID string) (bool, error) { + if _, err := f.Take(ctx, ownerUserID, friendUserID); err == nil { + return true, nil + } else if errors.Is(err, gorm.ErrRecordNotFound) { + return false, nil + } else { + return false, err + } +} diff --git a/pkg/common/db/model/friend_request.go b/pkg/common/db/model/friend_request.go new file mode 100644 index 000000000..55e5422c0 --- /dev/null +++ b/pkg/common/db/model/friend_request.go @@ -0,0 +1,44 @@ +package model + +import ( + "Open_IM/pkg/common/db/cache" + "Open_IM/pkg/common/db/mysql" + "context" +) + +type FriendRequestModel struct { + db *mysql.FriendRequest + cache *cache.GroupCache +} + +func (f *FriendRequestModel) Create(ctx context.Context, friends []*mysql.FriendRequest) (err error) { + return f.db.Create(ctx, friends) +} + +func (f *FriendRequestModel) Delete(ctx context.Context, fromUserID, toUserID string) (err error) { + return f.db.Delete(ctx, fromUserID, toUserID) +} + +func (f *FriendRequestModel) UpdateByMap(ctx context.Context, ownerUserID string, args map[string]interface{}) (err error) { + return f.db.UpdateByMap(ctx, ownerUserID, args) +} + +func (f *FriendRequestModel) Update(ctx context.Context, friends []*mysql.FriendRequest) (err error) { + return f.db.Update(ctx, friends) +} + +func (f *FriendRequestModel) Find(ctx context.Context, ownerUserID string) (friends []*mysql.FriendRequest, err error) { + return f.db.Find(ctx, ownerUserID) +} + +func (f *FriendRequestModel) Take(ctx context.Context, fromUserID, toUserID string) (friend *mysql.FriendRequest, err error) { + return f.db.Take(ctx, fromUserID, toUserID) +} + +func (f *FriendRequestModel) FindToUserID(ctx context.Context, toUserID string) (friends []*mysql.FriendRequest, err error) { + return f.db.FindToUserID(ctx, toUserID) +} + +func (f *FriendRequestModel) FindFromUserID(ctx context.Context, fromUserID string) (friends []*mysql.FriendRequest, err error) { + return f.db.FindFromUserID(ctx, fromUserID) +} diff --git a/pkg/common/db/mysql/user_black_list_model.go b/pkg/common/db/mysql/black.go similarity index 97% rename from pkg/common/db/mysql/user_black_list_model.go rename to pkg/common/db/mysql/black.go index bb3194645..108e9fd72 100644 --- a/pkg/common/db/mysql/user_black_list_model.go +++ b/pkg/common/db/mysql/black.go @@ -52,7 +52,7 @@ func (b *Black) Update(ctx context.Context, blacks []*Black) (err error) { return utils.Wrap(b.DB.Updates(&blacks).Error, "") } -func (b *Black) Find(ctx context.Context, blacks []Black) (blackList []*Black, err error) { +func (b *Black) Find(ctx context.Context, blacks []*Black) (blackList []*Black, err error) { defer func() { trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "blacks", blacks, "blackList", blackList) }() diff --git a/pkg/common/db/mysql/friend_model_k.go b/pkg/common/db/mysql/friend_model_k.go index 9e56fa050..af62c3b0b 100644 --- a/pkg/common/db/mysql/friend_model_k.go +++ b/pkg/common/db/mysql/friend_model_k.go @@ -61,13 +61,20 @@ func (f *Friend) UpdateRemark(ctx context.Context, ownerUserID, friendUserID, re return utils.Wrap(f.DB.Model(f).Where("owner_user_id = ? and friend_user_id = ?", ownerUserID, friendUserID).Update("remark", remark).Error, "") } -func (f *Friend) Find(ctx context.Context, ownerUserID string) (friends []*Friend, err error) { +func (f *Friend) FindOwnerUserID(ctx context.Context, ownerUserID string) (friends []*Friend, err error) { defer func() { trace_log.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "friends", friends) }() return friends, utils.Wrap(f.DB.Where("owner_user_id = ?", ownerUserID).Find(&friends).Error, "") } +func (f *Friend) FindFriendUserID(ctx context.Context, friendUserID string) (friends []*Friend, err error) { + defer func() { + trace_log.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friendUserID", friendUserID, "friends", friends) + }() + return friends, utils.Wrap(f.DB.Where("friend_user_id = ?", friendUserID).Find(&friends).Error, "") +} + func (f *Friend) Take(ctx context.Context, ownerUserID, friendUserID string) (friend *Friend, err error) { friend = &Friend{} defer trace_log.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "friendUserID", friendUserID, "friend", friend) diff --git a/pkg/common/db/mysql/friend_request_model.go b/pkg/common/db/mysql/friend_request_model.go index 7f1257147..6db3e5f38 100644 --- a/pkg/common/db/mysql/friend_request_model.go +++ b/pkg/common/db/mysql/friend_request_model.go @@ -33,16 +33,14 @@ func (f *FriendRequest) Create(ctx context.Context, friends []*FriendRequest) (e defer func() { trace_log.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friends", friends) }() - err = utils.Wrap(f.DB.Create(&friends).Error, "") - return err + return utils.Wrap(f.DB.Create(&friends).Error, "") } func (f *FriendRequest) Delete(ctx context.Context, fromUserID, toUserID string) (err error) { defer func() { trace_log.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "fromUserID", fromUserID, "toUserID", toUserID) }() - err = utils.Wrap(f.DB.Where("from_user_id = ? and to_user_id = ?", fromUserID, toUserID).Delete(&FriendRequest{}).Error, "") - return err + return utils.Wrap(f.DB.Where("from_user_id = ? and to_user_id = ?", fromUserID, toUserID).Delete(&FriendRequest{}).Error, "") } func (f *FriendRequest) UpdateByMap(ctx context.Context, ownerUserID string, args map[string]interface{}) (err error) { @@ -63,15 +61,13 @@ func (f *FriendRequest) Find(ctx context.Context, ownerUserID string) (friends [ defer func() { trace_log.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "friends", friends) }() - err = utils.Wrap(f.DB.Where("owner_user_id = ?", ownerUserID).Find(&friends).Error, "") - return friends, err + return friends, utils.Wrap(f.DB.Where("owner_user_id = ?", ownerUserID).Find(&friends).Error, "") } func (f *FriendRequest) Take(ctx context.Context, fromUserID, toUserID string) (friend *FriendRequest, err error) { friend = &FriendRequest{} defer trace_log.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "fromUserID", fromUserID, "toUserID", toUserID, "friend", friend) - err = utils.Wrap(f.DB.Where("from_user_id = ? and to_user_id", fromUserID, toUserID).Take(friend).Error, "") - return friend, err + return friend, utils.Wrap(f.DB.Where("from_user_id = ? and to_user_id", fromUserID, toUserID).Take(friend).Error, "") } func (f *FriendRequest) FindToUserID(ctx context.Context, toUserID string) (friends []*FriendRequest, err error) { From 8e47b55ab477593d3e8d081fe736064da0f394dc Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Tue, 17 Jan 2023 17:43:30 +0800 Subject: [PATCH 2/2] 1 --- internal/rpc/friend/friend.go | 9 ++++----- pkg/common/db/model/black.go | 16 ++++++++++++++-- pkg/common/db/mysql/black.go | 6 +++--- pkg/common/token_verify/jwt_token.go | 7 +++++++ 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/internal/rpc/friend/friend.go b/internal/rpc/friend/friend.go index 443ba2516..4d8244719 100644 --- a/internal/rpc/friend/friend.go +++ b/internal/rpc/friend/friend.go @@ -4,7 +4,6 @@ import ( chat "Open_IM/internal/rpc/msg" "Open_IM/pkg/common/config" "Open_IM/pkg/common/constant" - rocksCache "Open_IM/pkg/common/db/cache" "Open_IM/pkg/common/db/model" "Open_IM/pkg/common/db/mysql" "Open_IM/pkg/common/log" @@ -173,8 +172,8 @@ func (s *friendServer) AddFriend(ctx context.Context, req *pbFriend.AddFriendReq func (s *friendServer) ImportFriend(ctx context.Context, req *pbFriend.ImportFriendReq) (*pbFriend.ImportFriendResp, error) { resp := &pbFriend.ImportFriendResp{} - if !utils.IsContain(tools.OpUserID(ctx), config.Config.Manager.AppManagerUid) { - return nil, constant.ErrNoPermission.Wrap() + if err := token_verify.CheckAdmin(ctx); err != nil { + return nil, err } if _, err := GetUserInfo(ctx, req.FromUserID); err != nil { return nil, err @@ -315,11 +314,11 @@ func (s *friendServer) IsInBlackList(ctx context.Context, req *pbFriend.IsInBlac if err := token_verify.CheckAccessV3(ctx, req.FromUserID); err != nil { return nil, err } - blackIDList, err := rocksCache.GetBlackListFromCache(ctx, req.FromUserID) + exist, err := s.blackModel.IsExist(ctx, req.FromUserID, req.ToUserID) if err != nil { return nil, err } - resp.Response = utils.IsContain(req.ToUserID, blackIDList) + resp.Response = exist return resp, nil } diff --git a/pkg/common/db/model/black.go b/pkg/common/db/model/black.go index 920f517a2..29ef1753e 100644 --- a/pkg/common/db/model/black.go +++ b/pkg/common/db/model/black.go @@ -4,6 +4,8 @@ import ( "Open_IM/pkg/common/db/cache" "Open_IM/pkg/common/db/mysql" "context" + "errors" + "gorm.io/gorm" ) type BlackModel struct { @@ -31,10 +33,20 @@ func (b *BlackModel) Find(ctx context.Context, blacks []*mysql.Black) (blackList return b.db.Find(ctx, blacks) } -func (b *BlackModel) Take(ctx context.Context, blackID string) (black *mysql.Black, err error) { - return b.db.Take(ctx, blackID) +func (b *BlackModel) Take(ctx context.Context, ownerUserID, blockUserID string) (black *mysql.Black, err error) { + return b.db.Take(ctx, ownerUserID, blockUserID) } func (b *BlackModel) FindByOwnerUserID(ctx context.Context, ownerUserID string) (blackList []*mysql.Black, err error) { return b.db.FindByOwnerUserID(ctx, ownerUserID) } + +func (b *BlackModel) IsExist(ctx context.Context, ownerUserID, blockUserID string) (bool, error) { + if _, err := b.Take(ctx, ownerUserID, blockUserID); err == nil { + return true, nil + } else if errors.Is(err, gorm.ErrRecordNotFound) { + return false, nil + } else { + return false, err + } +} diff --git a/pkg/common/db/mysql/black.go b/pkg/common/db/mysql/black.go index 108e9fd72..a7e712767 100644 --- a/pkg/common/db/mysql/black.go +++ b/pkg/common/db/mysql/black.go @@ -63,12 +63,12 @@ func (b *Black) Find(ctx context.Context, blacks []*Black) (blackList []*Black, return blackList, utils.Wrap(GroupMemberDB.Where("(owner_user_id, block_user_id) in ?", where).Find(&blackList).Error, "") } -func (b *Black) Take(ctx context.Context, blackID string) (black *Black, err error) { +func (b *Black) Take(ctx context.Context, ownerUserID, blockUserID string) (black *Black, err error) { black = &Black{} defer func() { - trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "blackID", blackID, "black", *black) + trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "blockUserID", blockUserID, "black", *black) }() - return black, utils.Wrap(b.DB.Where("black_id = ?", blackID).Take(black).Error, "") + return black, utils.Wrap(b.DB.Where("owner_user_id = ? and block_user_id = ?", ownerUserID, blockUserID).Take(black).Error, "") } func (b *Black) FindByOwnerUserID(ctx context.Context, ownerUserID string) (blackList []*Black, err error) { diff --git a/pkg/common/token_verify/jwt_token.go b/pkg/common/token_verify/jwt_token.go index e5daef0e4..0b40012a8 100644 --- a/pkg/common/token_verify/jwt_token.go +++ b/pkg/common/token_verify/jwt_token.go @@ -173,6 +173,13 @@ func CheckAccessV3(ctx context.Context, OwnerUserID string) (err error) { return constant.ErrIdentity.Wrap(utils.GetSelfFuncName()) } +func CheckAdmin(ctx context.Context) error { + if utils.IsContain(tools.OpUserID(ctx), config.Config.Manager.AppManagerUid) { + return nil + } + return constant.ErrIdentity.Wrap() +} + func GetUserIDFromToken(token string, operationID string) (bool, string, string) { claims, err := ParseToken(token, operationID) if err != nil {