You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/internal/rpc/friend/friend.go

260 lines
9.3 KiB

3 years ago
package friend
import (
"context"
2 years ago
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/controller"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/relation"
tablerelation "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/tx"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/tokenverify"
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"
2 years ago
"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"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
3 years ago
"google.golang.org/grpc"
3 years ago
)
type friendServer struct {
controller.FriendDatabase
controller.BlackDatabase
notification *notification.Check
userCheck *check.UserCheck
RegisterCenter registry.SvcDiscoveryRegistry
3 years ago
}
2 years ago
func Start(client registry.SvcDiscoveryRegistry, server *grpc.Server) error {
db, err := relation.NewGormDB()
if err != nil {
return err
}
if err := db.AutoMigrate(&tablerelation.FriendModel{}, &tablerelation.FriendRequestModel{}, &tablerelation.BlackModel{}); err != nil {
return err
3 years ago
}
2 years ago
rdb, err := cache.NewRedis()
if err != nil {
return err
}
blackDB := relation.NewBlackGorm(db)
friendDB := relation.NewFriendGorm(db)
pbfriend.RegisterFriendServer(server, &friendServer{
2 years ago
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),
RegisterCenter: client,
})
return nil
3 years ago
}
// ok
func (s *friendServer) ApplyToAddFriend(ctx context.Context, req *pbfriend.ApplyToAddFriendReq) (resp *pbfriend.ApplyToAddFriendResp, err error) {
resp = &pbfriend.ApplyToAddFriendResp{}
2 years ago
if err := tokenverify.CheckAccessV3(ctx, req.FromUserID); err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
if err := CallbackBeforeAddFriend(ctx, req); err != nil && err != errs.ErrCallbackContinue {
2 years ago
return nil, err
}
if req.ToUserID == req.FromUserID {
2 years ago
return nil, errs.ErrCanNotAddYourself.Wrap()
}
if _, err := s.userCheck.GetUsersInfoMap(ctx, []string{req.ToUserID, req.FromUserID}, true); err != nil {
2 years ago
return nil, err
2 years ago
}
in1, in2, err := s.FriendDatabase.CheckIn(ctx, req.FromUserID, req.ToUserID)
2 years ago
if err != nil {
2 years ago
return nil, err
2 years ago
}
if in1 && in2 {
2 years ago
return nil, errs.ErrRelationshipAlready.Wrap()
3 years ago
}
if err = s.FriendDatabase.AddFriendRequest(ctx, req.FromUserID, req.ToUserID, req.ReqMsg, req.Ex); err != nil {
return nil, err
3 years ago
}
s.notification.FriendApplicationAddNotification(ctx, req)
2 years ago
return resp, nil
3 years ago
}
// ok
func (s *friendServer) ImportFriends(ctx context.Context, req *pbfriend.ImportFriendReq) (resp *pbfriend.ImportFriendResp, err error) {
2 years ago
if err := tokenverify.CheckAdmin(ctx); err != nil {
2 years ago
return nil, err
2 years ago
}
if _, err := s.userCheck.GetUsersInfos(ctx, append([]string{req.OwnerUserID}, req.FriendUserIDs...), true); err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
if utils.Contain(req.OwnerUserID, req.FriendUserIDs...) {
2 years ago
return nil, errs.ErrCanNotAddYourself.Wrap()
2 years ago
}
if utils.Duplicate(req.FriendUserIDs) {
2 years ago
return nil, errs.ErrArgs.Wrap("friend userID repeated")
}
2 years ago
if err := s.FriendDatabase.BecomeFriends(ctx, req.OwnerUserID, req.FriendUserIDs, constant.BecomeFriendByImport); err != nil {
return nil, err
3 years ago
}
2 years ago
return &pbfriend.ImportFriendResp{}, nil
3 years ago
}
// ok
func (s *friendServer) RespondFriendApply(ctx context.Context, req *pbfriend.RespondFriendApplyReq) (resp *pbfriend.RespondFriendApplyResp, err error) {
resp = &pbfriend.RespondFriendApplyResp{}
if err := s.userCheck.Access(ctx, req.ToUserID); err != nil {
2 years ago
return nil, err
3 years ago
}
friendRequest := tablerelation.FriendRequestModel{FromUserID: req.FromUserID, ToUserID: req.ToUserID, HandleMsg: req.HandleMsg, HandleResult: req.HandleResult}
if req.HandleResult == constant.FriendResponseAgree {
err := s.AgreeFriendRequest(ctx, &friendRequest)
if err != nil {
2 years ago
return nil, err
3 years ago
}
s.notification.FriendApplicationAgreedNotification(ctx, req)
return resp, nil
}
if req.HandleResult == constant.FriendResponseRefuse {
err := s.RefuseFriendRequest(ctx, &friendRequest)
if err != nil {
return nil, err
}
s.notification.FriendApplicationRefusedNotification(ctx, req)
return resp, nil
3 years ago
}
2 years ago
return nil, errs.ErrArgs.Wrap("req.HandleResult != -1/1")
3 years ago
}
// ok
func (s *friendServer) DeleteFriend(ctx context.Context, req *pbfriend.DeleteFriendReq) (resp *pbfriend.DeleteFriendResp, err error) {
resp = &pbfriend.DeleteFriendResp{}
if err := s.userCheck.Access(ctx, req.OwnerUserID); err != nil {
2 years ago
return nil, err
3 years ago
}
_, err = s.FindFriendsWithError(ctx, req.OwnerUserID, []string{req.FriendUserID})
if err != nil {
return nil, err
}
if err := s.FriendDatabase.Delete(ctx, req.OwnerUserID, []string{req.FriendUserID}); err != nil {
2 years ago
return nil, err
3 years ago
}
s.notification.FriendDeletedNotification(ctx, req)
2 years ago
return resp, nil
3 years ago
}
// ok
func (s *friendServer) SetFriendRemark(ctx context.Context, req *pbfriend.SetFriendRemarkReq) (resp *pbfriend.SetFriendRemarkResp, err error) {
resp = &pbfriend.SetFriendRemarkResp{}
if err := s.userCheck.Access(ctx, req.OwnerUserID); err != nil {
2 years ago
return nil, err
3 years ago
}
_, err = s.FindFriendsWithError(ctx, req.OwnerUserID, []string{req.FriendUserID})
if err != nil {
return nil, err
}
if err := s.FriendDatabase.UpdateRemark(ctx, req.OwnerUserID, req.FriendUserID, req.Remark); err != nil {
2 years ago
return nil, err
3 years ago
}
s.notification.FriendRemarkSetNotification(ctx, req.OwnerUserID, req.FriendUserID)
2 years ago
return resp, nil
3 years ago
}
// ok
func (s *friendServer) GetDesignatedFriends(ctx context.Context, req *pbfriend.GetDesignatedFriendsReq) (resp *pbfriend.GetDesignatedFriendsResp, err error) {
resp = &pbfriend.GetDesignatedFriendsResp{}
if utils.Duplicate(req.FriendUserIDs) {
2 years ago
return nil, errs.ErrArgs.Wrap("friend userID repeated")
3 years ago
}
friends, err := s.FriendDatabase.FindFriendsWithError(ctx, req.OwnerUserID, req.FriendUserIDs)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
if resp.FriendsInfo, err = (*convert.NewDBFriend(nil, s.RegisterCenter)).DB2PB(ctx, friends); err != nil {
return nil, err
3 years ago
}
2 years ago
return resp, nil
3 years ago
}
// ok 获取接收到的好友申请(即别人主动申请的)
func (s *friendServer) GetPaginationFriendsApplyTo(ctx context.Context, req *pbfriend.GetPaginationFriendsApplyToReq) (resp *pbfriend.GetPaginationFriendsApplyToResp, err error) {
resp = &pbfriend.GetPaginationFriendsApplyToResp{}
if err := s.userCheck.Access(ctx, req.UserID); err != nil {
2 years ago
return nil, err
}
2 years ago
pageNumber, showNumber := utils.GetPage(req.Pagination)
friendRequests, total, err := s.FriendDatabase.PageFriendRequestToMe(ctx, req.UserID, pageNumber, showNumber)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
resp.FriendRequests, err = (*convert.NewDBFriendRequest(nil, s.RegisterCenter)).DB2PB(ctx, friendRequests)
if err != nil {
return nil, err
3 years ago
}
resp.Total = int32(total)
2 years ago
return resp, nil
3 years ago
}
// ok 获取主动发出去的好友申请列表
func (s *friendServer) GetPaginationFriendsApplyFrom(ctx context.Context, req *pbfriend.GetPaginationFriendsApplyFromReq) (resp *pbfriend.GetPaginationFriendsApplyFromResp, err error) {
resp = &pbfriend.GetPaginationFriendsApplyFromResp{}
if err := s.userCheck.Access(ctx, req.UserID); err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
pageNumber, showNumber := utils.GetPage(req.Pagination)
2 years ago
friendRequests, total, err := s.FriendDatabase.PageFriendRequestFromMe(ctx, req.UserID, pageNumber, showNumber)
2 years ago
if err != nil {
return nil, err
}
resp.FriendRequests, err = (*convert.NewDBFriendRequest(nil, s.RegisterCenter)).DB2PB(ctx, friendRequests)
if err != nil {
return nil, err
3 years ago
}
resp.Total = int32(total)
2 years ago
return resp, nil
3 years ago
}
// ok
func (s *friendServer) IsFriend(ctx context.Context, req *pbfriend.IsFriendReq) (resp *pbfriend.IsFriendResp, err error) {
resp = &pbfriend.IsFriendResp{}
resp.InUser1Friends, resp.InUser2Friends, err = s.FriendDatabase.CheckIn(ctx, req.UserID1, req.UserID2)
if err != nil {
return nil, err
}
return resp, nil
}
// ok
func (s *friendServer) GetPaginationFriends(ctx context.Context, req *pbfriend.GetPaginationFriendsReq) (resp *pbfriend.GetPaginationFriendsResp, err error) {
if err := s.userCheck.Access(ctx, req.UserID); err != nil {
return nil, err
}
2 years ago
pageNumber, showNumber := utils.GetPage(req.Pagination)
friends, total, err := s.FriendDatabase.PageOwnerFriends(ctx, req.UserID, pageNumber, showNumber)
if err != nil {
return nil, err
}
2 years ago
resp = &pbfriend.GetPaginationFriendsResp{}
resp.FriendsInfo, err = (*convert.NewDBFriend(nil, s.RegisterCenter)).DB2PB(ctx, friends)
if err != nil {
return nil, err
}
resp.Total = int32(total)
return resp, nil
}
2 years ago
func (s *friendServer) GetFriendIDs(ctx context.Context, req *pbfriend.GetFriendIDsReq) (resp *pbfriend.GetFriendIDsResp, err error) {
if err := s.userCheck.Access(ctx, req.UserID); err != nil {
return nil, err
}
resp = &pbfriend.GetFriendIDsResp{}
2 years ago
resp.FriendIDs, err = s.FriendDatabase.FindFriendUserIDs(ctx, req.UserID)
2 years ago
if err != nil {
return nil, err
}
return resp, nil
}