|
|
@ -2,7 +2,9 @@ package friend
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"context"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
|
|
|
|
"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/constant"
|
|
|
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache"
|
|
|
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache"
|
|
|
@ -14,19 +16,18 @@ import (
|
|
|
|
registry "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
|
|
|
|
registry "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
|
|
|
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
|
|
|
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
|
|
|
|
pbfriend "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/friend"
|
|
|
|
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/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"
|
|
|
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type friendServer struct {
|
|
|
|
type friendServer struct {
|
|
|
|
friendDatabase controller.FriendDatabase
|
|
|
|
friendDatabase controller.FriendDatabase
|
|
|
|
blackDatabase controller.BlackDatabase
|
|
|
|
blackDatabase controller.BlackDatabase
|
|
|
|
notification *notification.Check
|
|
|
|
userRpcClient *rpcclient.UserClient
|
|
|
|
userCheck *check.UserCheck
|
|
|
|
notificationSender *notification.FriendNotificationSender
|
|
|
|
RegisterCenter registry.SvcDiscoveryRegistry
|
|
|
|
RegisterCenter registry.SvcDiscoveryRegistry
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func Start(client registry.SvcDiscoveryRegistry, server *grpc.Server) error {
|
|
|
|
func Start(client registry.SvcDiscoveryRegistry, server *grpc.Server) error {
|
|
|
@ -43,12 +44,14 @@ func Start(client registry.SvcDiscoveryRegistry, server *grpc.Server) error {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
blackDB := relation.NewBlackGorm(db)
|
|
|
|
blackDB := relation.NewBlackGorm(db)
|
|
|
|
friendDB := relation.NewFriendGorm(db)
|
|
|
|
friendDB := relation.NewFriendGorm(db)
|
|
|
|
|
|
|
|
userRpcClient := rpcclient.NewUserClient(client)
|
|
|
|
|
|
|
|
notificationSender := notification.NewFriendNotificationSender(client, notification.WithRpcFunc(userRpcClient.GetUsersInfo))
|
|
|
|
pbfriend.RegisterFriendServer(server, &friendServer{
|
|
|
|
pbfriend.RegisterFriendServer(server, &friendServer{
|
|
|
|
friendDatabase: controller.NewFriendDatabase(friendDB, relation.NewFriendRequestGorm(db), cache.NewFriendCacheRedis(rdb, friendDB, cache.GetDefaultOpt()), tx.NewGorm(db)),
|
|
|
|
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())),
|
|
|
|
blackDatabase: controller.NewBlackDatabase(blackDB, cache.NewBlackCacheRedis(rdb, blackDB, cache.GetDefaultOpt())),
|
|
|
|
notification: notification.NewCheck(client),
|
|
|
|
userRpcClient: userRpcClient,
|
|
|
|
userCheck: check.NewUserCheck(client),
|
|
|
|
notificationSender: notificationSender,
|
|
|
|
RegisterCenter: client,
|
|
|
|
RegisterCenter: client,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -66,7 +69,7 @@ func (s *friendServer) ApplyToAddFriend(ctx context.Context, req *pbfriend.Apply
|
|
|
|
if req.ToUserID == req.FromUserID {
|
|
|
|
if req.ToUserID == req.FromUserID {
|
|
|
|
return nil, errs.ErrCanNotAddYourself.Wrap()
|
|
|
|
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
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
in1, in2, err := s.friendDatabase.CheckIn(ctx, req.FromUserID, req.ToUserID)
|
|
|
|
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 {
|
|
|
|
if err = s.friendDatabase.AddFriendRequest(ctx, req.FromUserID, req.ToUserID, req.ReqMsg, req.Ex); err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.notification.FriendApplicationAddNotification(ctx, req)
|
|
|
|
s.notificationSender.FriendApplicationAddNotification(ctx, req)
|
|
|
|
return resp, nil
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -89,7 +92,7 @@ func (s *friendServer) ImportFriends(ctx context.Context, req *pbfriend.ImportFr
|
|
|
|
if err := tokenverify.CheckAdmin(ctx); err != nil {
|
|
|
|
if err := tokenverify.CheckAdmin(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
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
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -120,7 +123,7 @@ func (s *friendServer) RespondFriendApply(ctx context.Context, req *pbfriend.Res
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.notification.FriendApplicationAgreedNotification(ctx, req)
|
|
|
|
s.notificationSender.FriendApplicationAgreedNotification(ctx, req)
|
|
|
|
return resp, nil
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if req.HandleResult == constant.FriendResponseRefuse {
|
|
|
|
if req.HandleResult == constant.FriendResponseRefuse {
|
|
|
@ -128,7 +131,7 @@ func (s *friendServer) RespondFriendApply(ctx context.Context, req *pbfriend.Res
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.notification.FriendApplicationRefusedNotification(ctx, req)
|
|
|
|
s.notificationSender.FriendApplicationRefusedNotification(ctx, req)
|
|
|
|
return resp, nil
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, errs.ErrArgs.Wrap("req.HandleResult != -1/1")
|
|
|
|
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) {
|
|
|
|
func (s *friendServer) DeleteFriend(ctx context.Context, req *pbfriend.DeleteFriendReq) (resp *pbfriend.DeleteFriendResp, err error) {
|
|
|
|
defer log.ZInfo(ctx, utils.GetFuncName()+" Return")
|
|
|
|
defer log.ZInfo(ctx, utils.GetFuncName()+" Return")
|
|
|
|
resp = &pbfriend.DeleteFriendResp{}
|
|
|
|
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
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, err = s.friendDatabase.FindFriendsWithError(ctx, req.OwnerUserID, []string{req.FriendUserID})
|
|
|
|
_, 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 {
|
|
|
|
if err := s.friendDatabase.Delete(ctx, req.OwnerUserID, []string{req.FriendUserID}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.notification.FriendDeletedNotification(ctx, req)
|
|
|
|
s.notificationSender.FriendDeletedNotification(ctx, req)
|
|
|
|
return resp, nil
|
|
|
|
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) {
|
|
|
|
func (s *friendServer) SetFriendRemark(ctx context.Context, req *pbfriend.SetFriendRemarkReq) (resp *pbfriend.SetFriendRemarkResp, err error) {
|
|
|
|
defer log.ZInfo(ctx, utils.GetFuncName()+" Return")
|
|
|
|
defer log.ZInfo(ctx, utils.GetFuncName()+" Return")
|
|
|
|
resp = &pbfriend.SetFriendRemarkResp{}
|
|
|
|
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
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, err = s.friendDatabase.FindFriendsWithError(ctx, req.OwnerUserID, []string{req.FriendUserID})
|
|
|
|
_, 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 {
|
|
|
|
if err := s.friendDatabase.UpdateRemark(ctx, req.OwnerUserID, req.FriendUserID, req.Remark); err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.notification.FriendRemarkSetNotification(ctx, req.OwnerUserID, req.FriendUserID)
|
|
|
|
s.notificationSender.FriendRemarkSetNotification(ctx, req.OwnerUserID, req.FriendUserID)
|
|
|
|
return resp, nil
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -181,7 +184,7 @@ func (s *friendServer) GetDesignatedFriends(ctx context.Context, req *pbfriend.G
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
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 nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
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) {
|
|
|
|
func (s *friendServer) GetPaginationFriendsApplyTo(ctx context.Context, req *pbfriend.GetPaginationFriendsApplyToReq) (resp *pbfriend.GetPaginationFriendsApplyToResp, err error) {
|
|
|
|
defer log.ZInfo(ctx, utils.GetFuncName()+" Return")
|
|
|
|
defer log.ZInfo(ctx, utils.GetFuncName()+" Return")
|
|
|
|
resp = &pbfriend.GetPaginationFriendsApplyToResp{}
|
|
|
|
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
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pageNumber, showNumber := utils.GetPage(req.Pagination)
|
|
|
|
pageNumber, showNumber := utils.GetPage(req.Pagination)
|
|
|
@ -199,7 +202,7 @@ func (s *friendServer) GetPaginationFriendsApplyTo(ctx context.Context, req *pbf
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
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 {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
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) {
|
|
|
|
func (s *friendServer) GetPaginationFriendsApplyFrom(ctx context.Context, req *pbfriend.GetPaginationFriendsApplyFromReq) (resp *pbfriend.GetPaginationFriendsApplyFromResp, err error) {
|
|
|
|
defer log.ZInfo(ctx, utils.GetFuncName()+" Return")
|
|
|
|
defer log.ZInfo(ctx, utils.GetFuncName()+" Return")
|
|
|
|
resp = &pbfriend.GetPaginationFriendsApplyFromResp{}
|
|
|
|
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
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pageNumber, showNumber := utils.GetPage(req.Pagination)
|
|
|
|
pageNumber, showNumber := utils.GetPage(req.Pagination)
|
|
|
@ -219,7 +222,7 @@ func (s *friendServer) GetPaginationFriendsApplyFrom(ctx context.Context, req *p
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
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 {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -241,7 +244,7 @@ func (s *friendServer) IsFriend(ctx context.Context, req *pbfriend.IsFriendReq)
|
|
|
|
// ok
|
|
|
|
// ok
|
|
|
|
func (s *friendServer) GetPaginationFriends(ctx context.Context, req *pbfriend.GetPaginationFriendsReq) (resp *pbfriend.GetPaginationFriendsResp, err error) {
|
|
|
|
func (s *friendServer) GetPaginationFriends(ctx context.Context, req *pbfriend.GetPaginationFriendsReq) (resp *pbfriend.GetPaginationFriendsResp, err error) {
|
|
|
|
defer log.ZInfo(ctx, utils.GetFuncName()+" Return")
|
|
|
|
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
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pageNumber, showNumber := utils.GetPage(req.Pagination)
|
|
|
|
pageNumber, showNumber := utils.GetPage(req.Pagination)
|
|
|
@ -250,7 +253,7 @@ func (s *friendServer) GetPaginationFriends(ctx context.Context, req *pbfriend.G
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resp = &pbfriend.GetPaginationFriendsResp{}
|
|
|
|
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 {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
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) {
|
|
|
|
func (s *friendServer) GetFriendIDs(ctx context.Context, req *pbfriend.GetFriendIDsReq) (resp *pbfriend.GetFriendIDsResp, err error) {
|
|
|
|
defer log.ZInfo(ctx, utils.GetFuncName()+" Return")
|
|
|
|
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
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resp = &pbfriend.GetFriendIDsResp{}
|
|
|
|
resp = &pbfriend.GetFriendIDsResp{}
|
|
|
|