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

294 lines
9.6 KiB

3 years ago
package friend
import (
"Open_IM/internal/common/check"
"Open_IM/internal/common/convert"
"Open_IM/internal/common/network"
"Open_IM/internal/common/rpc_server"
3 years ago
chat "Open_IM/internal/rpc/msg"
3 years ago
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant"
"Open_IM/pkg/common/db/controller"
"Open_IM/pkg/common/db/relation"
2 years ago
relationTb "Open_IM/pkg/common/db/table/relation"
3 years ago
"Open_IM/pkg/common/log"
2 years ago
"Open_IM/pkg/common/middleware"
2 years ago
promePkg "Open_IM/pkg/common/prometheus"
3 years ago
"Open_IM/pkg/common/token_verify"
"Open_IM/pkg/common/tracelog"
discoveryRegistry "Open_IM/pkg/discovery_registry"
3 years ago
pbFriend "Open_IM/pkg/proto/friend"
"Open_IM/pkg/utils"
"context"
"github.com/OpenIMSDK/openKeeper"
grpcPrometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"google.golang.org/grpc"
3 years ago
"net"
"strconv"
)
type friendServer struct {
*rpc_server.RpcServer
controller.FriendInterface
controller.BlackInterface
3 years ago
}
func NewFriendServer(port int) *friendServer {
r, err := rpc_server.NewRpcServer(config.Config.RpcRegisterIP, port, config.Config.RpcRegisterName.OpenImAuthName, config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema)
if err != nil {
panic(err)
}
//mysql init
var mysql relation.Mysql
2 years ago
var model relation.FriendGorm
2 years ago
err = mysql.InitConn().AutoMigrateModel(&relationTb.FriendModel{})
if err != nil {
panic("db init err:" + err.Error())
}
2 years ago
err = mysql.InitConn().AutoMigrateModel(&relationTb.FriendRequestModel{})
if err != nil {
panic("db init err:" + err.Error())
}
2 years ago
err = mysql.InitConn().AutoMigrateModel(&relationTb.BlackModel{})
if err != nil {
panic("db init err:" + err.Error())
}
if mysql.GormConn() != nil {
model.DB = mysql.GormConn()
} else {
panic("db init err:" + "conn is nil")
}
return &friendServer{
RpcServer: r,
FriendInterface : controller.NewFriendController(model.DB),
BlackInterface : controller.NewBlackController(model.DB)
}
3 years ago
}
func (s *friendServer) Run() {
operationID := utils.OperationIDGenerator()
log.NewInfo(operationID, "friendServer run...")
listener, address, err := rpc_server.GetTcpListen(config.Config.ListenIP, s.Port)
3 years ago
if err != nil {
panic(err)
3 years ago
}
log.NewInfo(operationID, "listen ok ", address)
3 years ago
defer listener.Close()
//grpc server
2 years ago
var grpcOpts []grpc.ServerOption
2 years ago
grpcOpts = append(grpcOpts, grpc.UnaryInterceptor(middleware.RpcServerInterceptor))
2 years ago
if config.Config.Prometheus.Enable {
2 years ago
promePkg.NewGrpcRequestCounter()
promePkg.NewGrpcRequestFailedCounter()
promePkg.NewGrpcRequestSuccessCounter()
2 years ago
grpcOpts = append(grpcOpts, []grpc.ServerOption{
2 years ago
// grpc.UnaryInterceptor(promePkg.UnaryServerInterceptorProme),
2 years ago
grpc.StreamInterceptor(grpcPrometheus.StreamServerInterceptor),
grpc.UnaryInterceptor(grpcPrometheus.UnaryServerInterceptor),
}...)
2 years ago
}
srv := grpc.NewServer(grpcOpts...)
3 years ago
defer srv.GracefulStop()
pbFriend.RegisterFriendServer(srv, s)
err = srv.Serve(listener)
if err != nil {
log.NewError(operationID, "Serve failed ", err.Error(), listener)
3 years ago
return
}
}
// ok
func (s *friendServer) ApplyToAddFriend(ctx context.Context, req *pbFriend.ApplyToAddFriendReq) (resp *pbFriend.ApplyToAddFriendResp, err error) {
resp = &pbFriend.ApplyToAddFriendResp{}
2 years ago
if err := token_verify.CheckAccessV3(ctx, req.FromUserID); err != nil {
2 years ago
return nil, err
3 years ago
}
if err := callbackBeforeAddFriendV1(ctx, req); err != nil {
2 years ago
return nil, err
}
if req.ToUserID == req.FromUserID {
return nil, constant.ErrCanNotAddYourself.Wrap()
}
if _, err := check.GetUsersInfo(ctx, req.ToUserID, req.FromUserID); err != nil {
2 years ago
return nil, err
2 years ago
}
in1, in2, err := s.FriendInterface.CheckIn(ctx, req.FromUserID, req.ToUserID)
2 years ago
if err != nil {
2 years ago
return nil, err
2 years ago
}
if in1 && in2 {
return nil, constant.ErrRelationshipAlready.Wrap()
3 years ago
}
if err = s.FriendInterface.AddFriendRequest(ctx, req.FromUserID, req.ToUserID, req.ReqMsg, req.Ex); err != nil {
return nil, err
3 years ago
}
chat.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) {
resp = &pbFriend.ImportFriendResp{}
2 years ago
if err := token_verify.CheckAdmin(ctx); err != nil {
return nil, err
2 years ago
}
if _, err := check.GetUsersInfo(ctx, req.OwnerUserID, req.FriendUserIDs); err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
if utils.Contain(req.OwnerUserID, req.FriendUserIDs...) {
return nil, constant.ErrCanNotAddYourself.Wrap()
2 years ago
}
if utils.Duplicate(req.FriendUserIDs) {
return nil, constant.ErrArgs.Wrap("friend userID repeated")
}
if err := s.FriendInterface.BecomeFriends(ctx, req.OwnerUserID, req.FriendUserIDs, constant.BecomeFriendByImport, tracelog.GetOpUserID(ctx)); err != nil {
return nil, err
3 years ago
}
2 years ago
return resp, 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 := check.Access(ctx, req.ToUserID); err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
friendRequest := relationTb.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
}
chat.FriendApplicationAgreedNotification(ctx, req)
return resp, nil
}
if req.HandleResult == constant.FriendResponseRefuse {
err := s.RefuseFriendRequest(ctx, &friendRequest)
if err != nil {
return nil, err
}
chat.FriendApplicationRefusedNotification(ctx, req)
return resp, nil
3 years ago
}
return nil, constant.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 := check.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.FriendInterface.Delete(ctx, req.OwnerUserID, []string{req.FriendUserID}); err != nil {
2 years ago
return nil, err
3 years ago
}
chat.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 := check.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.FriendInterface.UpdateRemark(ctx, req.OwnerUserID, req.FriendUserID, req.Remark); err != nil {
2 years ago
return nil, err
3 years ago
}
chat.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 err := check.Access(ctx, req.UserID); err != nil {
2 years ago
return nil, err
3 years ago
}
friends, total, err := s.FriendInterface.PageOwnerFriends(ctx, req.UserID, req.Pagination.PageNumber, req.Pagination.ShowNumber)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
resp.FriendsInfo, err = (*convert.DBFriend)(nil).DB2PB(friends)
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) GetPaginationFriendsApplyTo(ctx context.Context, req *pbFriend.GetPaginationFriendsApplyToReq) (resp *pbFriend.GetPaginationFriendsApplyToResp, err error) {
resp = &pbFriend.GetPaginationFriendsApplyToResp{}
if err := check.Access(ctx, req.UserID); err != nil {
2 years ago
return nil, err
}
friendRequests, total, err := s.FriendInterface.PageFriendRequestToMe(ctx, req.UserID, req.Pagination.PageNumber, req.Pagination.ShowNumber)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
resp.FriendRequests, err = (*convert.DBFriendRequest)(nil).DB2PB(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 := check.Access(ctx, req.UserID); err != nil {
2 years ago
return nil, err
3 years ago
}
friendRequests, total, err := s.FriendInterface.PageFriendRequestFromMe(ctx, req.UserID, req.Pagination.PageNumber, req.Pagination.ShowNumber)
2 years ago
if err != nil {
return nil, err
}
resp.FriendRequests, err = (*convert.DBFriendRequest)(nil).DB2PB(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.FriendInterface.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) {
resp = &pbFriend.GetPaginationFriendsResp{}
if utils.Duplicate(req.FriendUserIDs) {
return nil, constant.ErrArgs.Wrap("friend userID repeated")
}
friends, err := s.FriendInterface.FindFriendsWithError(ctx, req.OwnerUserID, req.FriendUserIDs)
if err != nil {
return nil, err
}
if resp.FriendsInfo, err = (*convert.DBFriend)(nil).DB2PB(friends); err != nil {
return nil, err
}
return resp, nil
}