|
|
|
@ -2,7 +2,9 @@ package friend
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
|
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
|
|
|
|
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient"
|
|
|
|
|
|
|
|
|
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
|
|
|
|
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache"
|
|
|
|
@ -14,9 +16,8 @@ import (
|
|
|
|
|
registry "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
|
|
|
|
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
|
|
|
|
|
pbfriend "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/friend"
|
|
|
|
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient/check"
|
|
|
|
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient/convert"
|
|
|
|
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient/notification"
|
|
|
|
|
notification "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient/notification2"
|
|
|
|
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
)
|
|
|
|
@ -24,8 +25,8 @@ import (
|
|
|
|
|
type friendServer struct {
|
|
|
|
|
friendDatabase controller.FriendDatabase
|
|
|
|
|
blackDatabase controller.BlackDatabase
|
|
|
|
|
notification *notification.Check
|
|
|
|
|
userCheck *check.UserCheck
|
|
|
|
|
userRpcClient *rpcclient.UserClient
|
|
|
|
|
notificationSender *notification.FriendNotificationSender
|
|
|
|
|
RegisterCenter registry.SvcDiscoveryRegistry
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -43,11 +44,13 @@ func Start(client registry.SvcDiscoveryRegistry, server *grpc.Server) error {
|
|
|
|
|
}
|
|
|
|
|
blackDB := relation.NewBlackGorm(db)
|
|
|
|
|
friendDB := relation.NewFriendGorm(db)
|
|
|
|
|
userRpcClient := rpcclient.NewUserClient(client)
|
|
|
|
|
notificationSender := notification.NewFriendNotificationSender(client, notification.WithRpcFunc(userRpcClient.GetUsersInfo))
|
|
|
|
|
pbfriend.RegisterFriendServer(server, &friendServer{
|
|
|
|
|
friendDatabase: controller.NewFriendDatabase(friendDB, relation.NewFriendRequestGorm(db), cache.NewFriendCacheRedis(rdb, friendDB, cache.GetDefaultOpt()), tx.NewGorm(db)),
|
|
|
|
|
blackDatabase: controller.NewBlackDatabase(blackDB, cache.NewBlackCacheRedis(rdb, blackDB, cache.GetDefaultOpt())),
|
|
|
|
|
notification: notification.NewCheck(client),
|
|
|
|
|
userCheck: check.NewUserCheck(client),
|
|
|
|
|
userRpcClient: userRpcClient,
|
|
|
|
|
notificationSender: notificationSender,
|
|
|
|
|
RegisterCenter: client,
|
|
|
|
|
})
|
|
|
|
|
return nil
|
|
|
|
@ -66,7 +69,7 @@ func (s *friendServer) ApplyToAddFriend(ctx context.Context, req *pbfriend.Apply
|
|
|
|
|
if req.ToUserID == req.FromUserID {
|
|
|
|
|
return nil, errs.ErrCanNotAddYourself.Wrap()
|
|
|
|
|
}
|
|
|
|
|
if _, err := s.userCheck.GetUsersInfoMap(ctx, []string{req.ToUserID, req.FromUserID}, true); err != nil {
|
|
|
|
|
if _, err := s.userRpcClient.GetUsersInfoMap(ctx, []string{req.ToUserID, req.FromUserID}); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
in1, in2, err := s.friendDatabase.CheckIn(ctx, req.FromUserID, req.ToUserID)
|
|
|
|
@ -79,7 +82,7 @@ func (s *friendServer) ApplyToAddFriend(ctx context.Context, req *pbfriend.Apply
|
|
|
|
|
if err = s.friendDatabase.AddFriendRequest(ctx, req.FromUserID, req.ToUserID, req.ReqMsg, req.Ex); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
s.notification.FriendApplicationAddNotification(ctx, req)
|
|
|
|
|
s.notificationSender.FriendApplicationAddNotification(ctx, req)
|
|
|
|
|
return resp, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -89,7 +92,7 @@ func (s *friendServer) ImportFriends(ctx context.Context, req *pbfriend.ImportFr
|
|
|
|
|
if err := tokenverify.CheckAdmin(ctx); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if _, err := s.userCheck.GetUsersInfos(ctx, append([]string{req.OwnerUserID}, req.FriendUserIDs...), true); err != nil {
|
|
|
|
|
if _, err := s.userRpcClient.GetUsersInfo(ctx, append([]string{req.OwnerUserID}, req.FriendUserIDs...)); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -120,7 +123,7 @@ func (s *friendServer) RespondFriendApply(ctx context.Context, req *pbfriend.Res
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
s.notification.FriendApplicationAgreedNotification(ctx, req)
|
|
|
|
|
s.notificationSender.FriendApplicationAgreedNotification(ctx, req)
|
|
|
|
|
return resp, nil
|
|
|
|
|
}
|
|
|
|
|
if req.HandleResult == constant.FriendResponseRefuse {
|
|
|
|
@ -128,7 +131,7 @@ func (s *friendServer) RespondFriendApply(ctx context.Context, req *pbfriend.Res
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
s.notification.FriendApplicationRefusedNotification(ctx, req)
|
|
|
|
|
s.notificationSender.FriendApplicationRefusedNotification(ctx, req)
|
|
|
|
|
return resp, nil
|
|
|
|
|
}
|
|
|
|
|
return nil, errs.ErrArgs.Wrap("req.HandleResult != -1/1")
|
|
|
|
@ -138,7 +141,7 @@ func (s *friendServer) RespondFriendApply(ctx context.Context, req *pbfriend.Res
|
|
|
|
|
func (s *friendServer) DeleteFriend(ctx context.Context, req *pbfriend.DeleteFriendReq) (resp *pbfriend.DeleteFriendResp, err error) {
|
|
|
|
|
defer log.ZInfo(ctx, utils.GetFuncName()+" Return")
|
|
|
|
|
resp = &pbfriend.DeleteFriendResp{}
|
|
|
|
|
if err := s.userCheck.Access(ctx, req.OwnerUserID); err != nil {
|
|
|
|
|
if err := s.userRpcClient.Access(ctx, req.OwnerUserID); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
_, err = s.friendDatabase.FindFriendsWithError(ctx, req.OwnerUserID, []string{req.FriendUserID})
|
|
|
|
@ -148,7 +151,7 @@ func (s *friendServer) DeleteFriend(ctx context.Context, req *pbfriend.DeleteFri
|
|
|
|
|
if err := s.friendDatabase.Delete(ctx, req.OwnerUserID, []string{req.FriendUserID}); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
s.notification.FriendDeletedNotification(ctx, req)
|
|
|
|
|
s.notificationSender.FriendDeletedNotification(ctx, req)
|
|
|
|
|
return resp, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -156,7 +159,7 @@ func (s *friendServer) DeleteFriend(ctx context.Context, req *pbfriend.DeleteFri
|
|
|
|
|
func (s *friendServer) SetFriendRemark(ctx context.Context, req *pbfriend.SetFriendRemarkReq) (resp *pbfriend.SetFriendRemarkResp, err error) {
|
|
|
|
|
defer log.ZInfo(ctx, utils.GetFuncName()+" Return")
|
|
|
|
|
resp = &pbfriend.SetFriendRemarkResp{}
|
|
|
|
|
if err := s.userCheck.Access(ctx, req.OwnerUserID); err != nil {
|
|
|
|
|
if err := s.userRpcClient.Access(ctx, req.OwnerUserID); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
_, err = s.friendDatabase.FindFriendsWithError(ctx, req.OwnerUserID, []string{req.FriendUserID})
|
|
|
|
@ -166,7 +169,7 @@ func (s *friendServer) SetFriendRemark(ctx context.Context, req *pbfriend.SetFri
|
|
|
|
|
if err := s.friendDatabase.UpdateRemark(ctx, req.OwnerUserID, req.FriendUserID, req.Remark); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
s.notification.FriendRemarkSetNotification(ctx, req.OwnerUserID, req.FriendUserID)
|
|
|
|
|
s.notificationSender.FriendRemarkSetNotification(ctx, req.OwnerUserID, req.FriendUserID)
|
|
|
|
|
return resp, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -181,7 +184,7 @@ func (s *friendServer) GetDesignatedFriends(ctx context.Context, req *pbfriend.G
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if resp.FriendsInfo, err = (*convert.NewDBFriend(nil, s.RegisterCenter)).DB2PB(ctx, friends); err != nil {
|
|
|
|
|
if resp.FriendsInfo, err = convert.FriendsDB2Pb(ctx, friends, s.userRpcClient.GetUsersInfoMap); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return resp, nil
|
|
|
|
@ -191,7 +194,7 @@ func (s *friendServer) GetDesignatedFriends(ctx context.Context, req *pbfriend.G
|
|
|
|
|
func (s *friendServer) GetPaginationFriendsApplyTo(ctx context.Context, req *pbfriend.GetPaginationFriendsApplyToReq) (resp *pbfriend.GetPaginationFriendsApplyToResp, err error) {
|
|
|
|
|
defer log.ZInfo(ctx, utils.GetFuncName()+" Return")
|
|
|
|
|
resp = &pbfriend.GetPaginationFriendsApplyToResp{}
|
|
|
|
|
if err := s.userCheck.Access(ctx, req.UserID); err != nil {
|
|
|
|
|
if err := s.userRpcClient.Access(ctx, req.UserID); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
pageNumber, showNumber := utils.GetPage(req.Pagination)
|
|
|
|
@ -199,7 +202,7 @@ func (s *friendServer) GetPaginationFriendsApplyTo(ctx context.Context, req *pbf
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
resp.FriendRequests, err = (*convert.NewDBFriendRequest(nil, s.RegisterCenter)).DB2PB(ctx, friendRequests)
|
|
|
|
|
resp.FriendRequests, err = convert.FriendRequestDB2Pb(ctx, friendRequests, s.userRpcClient.GetUsersInfoMap)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
@ -211,7 +214,7 @@ func (s *friendServer) GetPaginationFriendsApplyTo(ctx context.Context, req *pbf
|
|
|
|
|
func (s *friendServer) GetPaginationFriendsApplyFrom(ctx context.Context, req *pbfriend.GetPaginationFriendsApplyFromReq) (resp *pbfriend.GetPaginationFriendsApplyFromResp, err error) {
|
|
|
|
|
defer log.ZInfo(ctx, utils.GetFuncName()+" Return")
|
|
|
|
|
resp = &pbfriend.GetPaginationFriendsApplyFromResp{}
|
|
|
|
|
if err := s.userCheck.Access(ctx, req.UserID); err != nil {
|
|
|
|
|
if err := s.userRpcClient.Access(ctx, req.UserID); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
pageNumber, showNumber := utils.GetPage(req.Pagination)
|
|
|
|
@ -219,7 +222,7 @@ func (s *friendServer) GetPaginationFriendsApplyFrom(ctx context.Context, req *p
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
resp.FriendRequests, err = (*convert.NewDBFriendRequest(nil, s.RegisterCenter)).DB2PB(ctx, friendRequests)
|
|
|
|
|
resp.FriendRequests, err = convert.FriendRequestDB2Pb(ctx, friendRequests, s.userRpcClient.GetUsersInfoMap)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
@ -241,7 +244,7 @@ func (s *friendServer) IsFriend(ctx context.Context, req *pbfriend.IsFriendReq)
|
|
|
|
|
// ok
|
|
|
|
|
func (s *friendServer) GetPaginationFriends(ctx context.Context, req *pbfriend.GetPaginationFriendsReq) (resp *pbfriend.GetPaginationFriendsResp, err error) {
|
|
|
|
|
defer log.ZInfo(ctx, utils.GetFuncName()+" Return")
|
|
|
|
|
if err := s.userCheck.Access(ctx, req.UserID); err != nil {
|
|
|
|
|
if err := s.userRpcClient.Access(ctx, req.UserID); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
pageNumber, showNumber := utils.GetPage(req.Pagination)
|
|
|
|
@ -250,7 +253,7 @@ func (s *friendServer) GetPaginationFriends(ctx context.Context, req *pbfriend.G
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
resp = &pbfriend.GetPaginationFriendsResp{}
|
|
|
|
|
resp.FriendsInfo, err = (*convert.NewDBFriend(nil, s.RegisterCenter)).DB2PB(ctx, friends)
|
|
|
|
|
resp.FriendsInfo, err = convert.FriendsDB2Pb(ctx, friends, s.userRpcClient.GetUsersInfoMap)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
@ -260,7 +263,7 @@ func (s *friendServer) GetPaginationFriends(ctx context.Context, req *pbfriend.G
|
|
|
|
|
|
|
|
|
|
func (s *friendServer) GetFriendIDs(ctx context.Context, req *pbfriend.GetFriendIDsReq) (resp *pbfriend.GetFriendIDsResp, err error) {
|
|
|
|
|
defer log.ZInfo(ctx, utils.GetFuncName()+" Return")
|
|
|
|
|
if err := s.userCheck.Access(ctx, req.UserID); err != nil {
|
|
|
|
|
if err := s.userRpcClient.Access(ctx, req.UserID); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
resp = &pbfriend.GetFriendIDsResp{}
|
|
|
|
|