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

387 lines
12 KiB

3 years ago
package friend
import (
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/tracelog"
"Open_IM/pkg/common/db/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"
2 years ago
"Open_IM/pkg/getcdv3"
3 years ago
pbFriend "Open_IM/pkg/proto/friend"
3 years ago
sdkws "Open_IM/pkg/proto/sdk_ws"
3 years ago
"Open_IM/pkg/utils"
"context"
grpcPrometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
3 years ago
"net"
"strconv"
"strings"
2 years ago
"Open_IM/internal/common/check"
3 years ago
"google.golang.org/grpc"
3 years ago
)
type friendServer struct {
rpcPort int
rpcRegisterName string
etcdSchema string
etcdAddr []string
controller.FriendInterface
controller.BlackInterface
3 years ago
}
func NewFriendServer(port int) *friendServer {
3 years ago
log.NewPrivateLog(constant.LogFileName)
f := friendServer{
2 years ago
rpcPort: port,
rpcRegisterName: config.Config.RpcRegisterName.OpenImFriendName,
etcdSchema: config.Config.Etcd.EtcdSchema,
etcdAddr: config.Config.Etcd.EtcdAddr,
3 years ago
}
//mysql init
var mysql relation.Mysql
var model relation.Friend
err := mysql.InitConn().AutoMigrateModel(&model)
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")
}
f.FriendInterface = controller.NewFriendController(model.DB)
f.BlackInterface = controller.NewBlackController(model.DB)
return &f
3 years ago
}
func (s *friendServer) Run() {
3 years ago
log.NewInfo("0", "friendServer run...")
3 years ago
3 years ago
listenIP := ""
if config.Config.ListenIP == "" {
listenIP = "0.0.0.0"
} else {
listenIP = config.Config.ListenIP
}
address := listenIP + ":" + strconv.Itoa(s.rpcPort)
3 years ago
//listener network
3 years ago
listener, err := net.Listen("tcp", address)
3 years ago
if err != nil {
3 years ago
panic("listening err:" + err.Error() + s.rpcRegisterName)
3 years ago
}
3 years ago
log.NewInfo("0", "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()
//User friend related services register to etcd
pbFriend.RegisterFriendServer(srv, s)
2 years ago
rpcRegisterIP := config.Config.RpcRegisterIP
3 years ago
if config.Config.RpcRegisterIP == "" {
rpcRegisterIP, err = utils.GetLocalIP()
if err != nil {
log.Error("", "GetLocalIP failed ", err.Error())
}
}
3 years ago
log.NewInfo("", "rpcRegisterIP", rpcRegisterIP)
err = getcdv3.RegisterEtcd(s.etcdSchema, strings.Join(s.etcdAddr, ","), rpcRegisterIP, s.rpcPort, s.rpcRegisterName, 10, "")
3 years ago
if err != nil {
3 years ago
log.NewError("0", "RegisterEtcd failed ", err.Error(), s.etcdSchema, strings.Join(s.etcdAddr, ","), rpcRegisterIP, s.rpcPort, s.rpcRegisterName)
panic(utils.Wrap(err, "register friend module rpc to etcd err"))
3 years ago
}
err = srv.Serve(listener)
if err != nil {
3 years ago
log.NewError("0", "Serve failed ", err.Error(), listener)
3 years ago
return
}
}
func (s *friendServer) AddBlack(ctx context.Context, req *pbFriend.AddBlackReq) (*pbFriend.AddBlackResp, error) {
resp := &pbFriend.AddBlackResp{}
if err := token_verify.CheckAccessV3(ctx, req.OwnerUserID); err != nil {
2 years ago
return nil, err
3 years ago
}
black := relation.Black{OwnerUserID: req.OwnerUserID, BlockUserID: req.BlackUserID, OperatorUserID: tracelog.OpUserID(ctx)}
if err := s.BlackInterface.Create(ctx, []*relation.Black{&black}); err != nil {
2 years ago
return nil, err
}
3 years ago
chat.BlackAddedNotification(req)
2 years ago
return resp, nil
3 years ago
}
3 years ago
func (s *friendServer) AddFriend(ctx context.Context, req *pbFriend.AddFriendReq) (*pbFriend.AddFriendResp, error) {
2 years ago
resp := &pbFriend.AddFriendResp{}
2 years ago
if err := token_verify.CheckAccessV3(ctx, req.FromUserID); err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
if err := callbackBeforeAddFriendV1(req); err != nil {
return nil, err
}
//检查toUserID fromUserID是否存在
if _, err := check.GetUsersInfo(ctx, req.ToUserID, req.FromUserID); err != nil {
2 years ago
return nil, err
2 years ago
}
//from是否在to的好友列表里面
err, in1, in2 := 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.FriendApplicationNotification(req)
2 years ago
return resp, nil
3 years ago
}
func (s *friendServer) ImportFriend(ctx context.Context, req *pbFriend.ImportFriendReq) (*pbFriend.ImportFriendResp, error) {
2 years ago
resp := &pbFriend.ImportFriendResp{}
2 years ago
if err := token_verify.CheckAdmin(ctx); err != nil {
return nil, err
2 years ago
}
if _, err := GetUsersInfo(ctx, req.OwnerUserID); err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
2 years ago
var friends []*relation.Friend
for _, userID := range utils.RemoveDuplicateElement(req.FriendUserIDs) {
friends = append(friends, &relation.Friend{OwnerUserID: userID, FriendUserID: req.OwnerUserID, AddSource: constant.BecomeFriendByImport, OperatorUserID: tools.OpUserID(ctx)})
2 years ago
}
if len(friends) > 0 {
if err := s.FriendInterface.BecomeFriend(ctx, friends); err != nil {
2 years ago
return nil, err
3 years ago
}
}
2 years ago
return resp, nil
3 years ago
}
2 years ago
// process Friend application
func (s *friendServer) RespondFriendApply(ctx context.Context, req *pbFriend.RespondFriendApplyReq) (*pbFriend.RespondFriendApplyResp, error) {
resp := &pbFriend.RespondFriendApplyResp{}
if err := token_verify.CheckAccessV3(ctx, req.ToUserID); err != nil {
2 years ago
return nil, err
3 years ago
}
friendRequest := relation.FriendRequest{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
}
3 years ago
chat.FriendApplicationApprovedNotification(req)
return resp, nil
}
if req.HandleResult == constant.FriendResponseRefuse {
err := s.RefuseFriendRequest(ctx, &friendRequest)
if err != nil {
return nil, err
}
3 years ago
chat.FriendApplicationRejectedNotification(req)
return resp, nil
3 years ago
}
return nil, constant.ErrArgs.Wrap("req.HandleResult != -1/1")
3 years ago
}
3 years ago
func (s *friendServer) DeleteFriend(ctx context.Context, req *pbFriend.DeleteFriendReq) (*pbFriend.DeleteFriendResp, error) {
2 years ago
resp := &pbFriend.DeleteFriendResp{}
if err := token_verify.CheckAccessV3(ctx, req.OwnerUserID); err != nil {
2 years ago
return nil, err
3 years ago
}
if err := s.FriendInterface.Delete(ctx, req.OwnerUserID, req.FriendUserID); err != nil {
2 years ago
return nil, err
3 years ago
}
chat.FriendDeletedNotification(req)
2 years ago
return resp, nil
3 years ago
}
3 years ago
func (s *friendServer) SetFriendRemark(ctx context.Context, req *pbFriend.SetFriendRemarkReq) (*pbFriend.SetFriendRemarkResp, error) {
2 years ago
resp := &pbFriend.SetFriendRemarkResp{}
if err := token_verify.CheckAccessV3(ctx, req.OwnerUserID); err != nil {
2 years ago
return nil, err
3 years ago
}
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(tools.OperationID(ctx), tools.OpUserID(ctx), req.OwnerUserID, req.FriendUserID)
2 years ago
return resp, nil
3 years ago
}
func (s *friendServer) RemoveBlacklist(ctx context.Context, req *pbFriend.RemoveBlackReq) (*pbFriend.RemoveBlackResp, error) {
2 years ago
resp := &pbFriend.RemoveBlacklistResp{}
3 years ago
//Parse token, to find current user information
2 years ago
if err := token_verify.CheckAccessV3(ctx, req.FromUserID); err != nil {
2 years ago
return nil, err
3 years ago
}
if err := s.BlackInterface.Delete(ctx, []*relation.Black{{OwnerUserID: req.FromUserID, BlockUserID: req.ToUserID}}); err != nil {
2 years ago
return nil, err
}
3 years ago
chat.BlackDeletedNotification(req)
2 years ago
return resp, nil
3 years ago
}
func (s *friendServer) GetFriendList(ctx context.Context, req *pbFriend.GetFriendListReq) (*pbFriend.GetFriendListResp, error) {
2 years ago
resp := &pbFriend.GetFriendListResp{}
2 years ago
if err := token_verify.CheckAccessV3(ctx, req.FromUserID); err != nil {
2 years ago
return nil, err
3 years ago
}
friends, err := s.FriendInterface.FindOwnerUserID(ctx, req.FromUserID)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
userIDList := make([]string, 0, len(friends))
for _, f := range friends {
userIDList = append(userIDList, f.FriendUserID)
}
users, err := GetUsersInfo(ctx, userIDList)
2 years ago
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)
3 years ago
}
2 years ago
return resp, nil
3 years ago
}
2 years ago
// received
3 years ago
func (s *friendServer) GetFriendApplyList(ctx context.Context, req *pbFriend.GetFriendApplyListReq) (*pbFriend.GetFriendApplyListResp, error) {
2 years ago
resp := &pbFriend.GetFriendApplyListResp{}
3 years ago
//Parse token, to find current user information
2 years ago
if err := token_verify.CheckAccessV3(ctx, req.FromUserID); err != nil {
2 years ago
return nil, err
3 years ago
}
// Find the current user friend applications received
2 years ago
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)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
userMap := make(map[string]*sdkws.PublicUserInfo)
for i, user := range users {
userMap[user.UserID] = users[i]
}
for _, friendRequest := range friendRequests {
3 years ago
var userInfo sdkws.FriendRequest
2 years ago
if u, ok := userMap[friendRequest.FromUserID]; ok {
utils.CopyStructFields(&userInfo, u)
}
utils.CopyStructFields(&userInfo, friendRequest)
2 years ago
resp.FriendRequestList = append(resp.FriendRequestList, &userInfo)
3 years ago
}
2 years ago
return resp, nil
3 years ago
}
3 years ago
func (s *friendServer) GetSelfApplyList(ctx context.Context, req *pbFriend.GetSelfApplyListReq) (*pbFriend.GetSelfApplyListResp, error) {
2 years ago
resp := &pbFriend.GetSelfApplyListResp{}
3 years ago
//Parse token, to find current user information
2 years ago
if err := token_verify.CheckAccessV3(ctx, req.FromUserID); err != nil {
2 years ago
return nil, err
3 years ago
}
// Find the self add other userinfo
friendRequests, err := s.FriendRequestInterface.FindFromUserID(ctx, req.FromUserID)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
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)
2 years ago
resp.FriendRequestList = append(resp.FriendRequestList, &userInfo)
3 years ago
}
2 years ago
return resp, nil
3 years ago
}
func (s *friendServer) GetBlacklist(ctx context.Context, req *pbFriend.GetBlacklistReq) (*pbFriend.GetBlacklistResp, error) {
resp := &pbFriend.GetBlacklistResp{}
if err := token_verify.CheckAccessV3(ctx, req.FromUserID); err != nil {
return nil, err
}
blacks, err := s.BlackInterface.FindByOwnerUserID(ctx, req.FromUserID)
if err != nil {
return nil, err
}
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
}
func (s *friendServer) IsInBlackList(ctx context.Context, req *pbFriend.IsInBlackListReq) (*pbFriend.IsInBlackListResp, error) {
resp := &pbFriend.IsInBlackListResp{}
if err := token_verify.CheckAccessV3(ctx, req.FromUserID); err != nil {
return nil, err
}
exist, err := s.BlackInterface.IsExist(ctx, req.FromUserID, req.ToUserID)
if err != nil {
return nil, err
}
resp.Response = exist
return resp, nil
}
func (s *friendServer) IsFriend(ctx context.Context, req *pbFriend.IsFriendReq) (*pbFriend.IsFriendResp, error) {
resp := &pbFriend.IsFriendResp{}
if err := token_verify.CheckAccessV3(ctx, req.FromUserID); err != nil {
return nil, err
}
exist, err := s.FriendInterface.IsExist(ctx, req.FromUserID, req.ToUserID)
if err != nil {
return nil, err
}
resp.Response = exist
return resp, nil
}