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

436 lines
14 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"
2 years ago
rocksCache "Open_IM/pkg/common/db/cache"
"Open_IM/pkg/common/db/model"
2 years ago
"Open_IM/pkg/common/db/mysql"
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/common/tools"
2 years ago
"Open_IM/pkg/common/trace_log"
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"
2 years ago
"errors"
"gorm.io/gorm"
3 years ago
"net"
"strconv"
"strings"
3 years ago
"time"
3 years ago
2 years ago
grpcPrometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
3 years ago
"google.golang.org/grpc"
3 years ago
)
type friendServer struct {
2 years ago
rpcPort int
rpcRegisterName string
etcdSchema string
etcdAddr []string
2 years ago
friendModel *model.FriendModel
friendRequestModel *model.FriendRequestModel
blackModel *model.BlackModel
3 years ago
}
func NewFriendServer(port int) *friendServer {
3 years ago
log.NewPrivateLog(constant.LogFileName)
3 years ago
return &friendServer{
2 years ago
rpcPort: port,
rpcRegisterName: config.Config.RpcRegisterName.OpenImFriendName,
etcdSchema: config.Config.Etcd.EtcdSchema,
etcdAddr: config.Config.Etcd.EtcdAddr,
3 years ago
}
}
func (s *friendServer) Run() {
2 years ago
db := mysql.ConnectToDB()
2 years ago
//s.friendModel = mysql.NewFriend(db)
//s.friendRequestModel = mysql.NewFriendRequest(db)
//s.blackModel = mysql.NewBlack(db)
2 years ago
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
}
}
3 years ago
func (s *friendServer) AddBlacklist(ctx context.Context, req *pbFriend.AddBlacklistReq) (*pbFriend.AddBlacklistResp, error) {
2 years ago
resp := &pbFriend.AddBlacklistResp{}
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
black := mysql.Black{OwnerUserID: req.FromUserID, BlockUserID: req.ToUserID, OperatorUserID: tools.OpUserID(ctx)}
if err := s.blackModel.Create(ctx, []*mysql.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
}
2 years ago
friends1, err := s.friendModel.FindOwnerUserID(ctx, req.ToUserID)
2 years ago
if err != nil {
2 years ago
return nil, err
2 years ago
}
2 years ago
friends2, err := s.friendModel.FindOwnerUserID(ctx, req.FromUserID)
2 years ago
if err != nil {
2 years ago
return nil, err
2 years ago
}
2 years ago
var isSend = true
2 years ago
for _, v1 := range friends1 {
if v1.FriendUserID == req.FromUserID {
for _, v2 := range friends2 {
if v2.FriendUserID == req.ToUserID {
2 years ago
isSend = false
2 years ago
break
2 years ago
}
}
2 years ago
break
2 years ago
}
3 years ago
}
2 years ago
//Cannot add non-existent users
if isSend {
2 years ago
if _, err := GetUserInfo(ctx, req.ToUserID); err != nil {
2 years ago
return nil, err
2 years ago
}
2 years ago
friendRequest := mysql.FriendRequest{
2 years ago
FromUserID: req.FromUserID,
ToUserID: req.ToUserID,
2 years ago
HandleResult: 0,
ReqMsg: req.ReqMsg,
CreateTime: time.Now(),
}
2 years ago
if err := s.friendRequestModel.Create(ctx, []*mysql.FriendRequest{&friendRequest}); err != nil {
2 years ago
return nil, err
2 years ago
}
chat.FriendApplicationNotification(req)
3 years ago
}
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{}
if !utils.IsContain(tools.OpUserID(ctx), config.Config.Manager.AppManagerUid) {
return nil, constant.ErrNoPermission.Wrap()
}
if _, err := GetUserInfo(ctx, req.FromUserID); err != nil {
return nil, err
3 years ago
}
2 years ago
var friends []*mysql.Friend
for _, userID := range utils.RemoveDuplicateElement(req.FriendUserIDList) {
2 years ago
if _, err := GetUserInfo(ctx, userID); err != nil {
return nil, err
}
fs, err := s.friendModel.FindUserState(ctx, req.FromUserID, userID)
if err != nil {
return nil, err
}
switch len(fs) {
case 1:
if fs[0].OwnerUserID == req.FromUserID {
2 years ago
friends = append(friends, &mysql.Friend{OwnerUserID: userID, FriendUserID: req.FromUserID})
3 years ago
} else {
2 years ago
friends = append(friends, &mysql.Friend{OwnerUserID: req.FromUserID, FriendUserID: userID})
3 years ago
}
2 years ago
case 0:
2 years ago
friends = append(friends, &mysql.Friend{OwnerUserID: userID, FriendUserID: req.FromUserID}, &mysql.Friend{OwnerUserID: req.FromUserID, FriendUserID: userID})
2 years ago
default:
continue
}
2 years ago
}
if len(friends) > 0 {
2 years ago
if err := s.friendModel.Create(ctx, friends); err != nil {
return nil, err
3 years ago
}
}
2 years ago
return resp, nil
3 years ago
}
2 years ago
// process Friend application
3 years ago
func (s *friendServer) AddFriendResponse(ctx context.Context, req *pbFriend.AddFriendResponseReq) (*pbFriend.AddFriendResponseResp, error) {
2 years ago
resp := &pbFriend.AddFriendResponseResp{}
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
friendRequest, err := s.friendRequestModel.Take(ctx, req.ToUserID, req.FromUserID)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
3 years ago
friendRequest.HandleResult = req.HandleResult
3 years ago
friendRequest.HandleTime = time.Now()
friendRequest.HandleMsg = req.HandleMsg
2 years ago
friendRequest.HandlerUserID = tools.OpUserID(ctx)
2 years ago
err = mysql.UpdateFriendApplication(friendRequest)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
3 years ago
3 years ago
//Change the status of the friend request form
3 years ago
if req.HandleResult == constant.FriendFlag {
3 years ago
//Establish friendship after find friend relationship not exists
2 years ago
_, err := s.friendModel.Take(ctx, req.FromUserID, req.ToUserID)
2 years ago
if errors.Is(err, gorm.ErrRecordNotFound) {
2 years ago
if err := s.friendModel.Create(ctx, []*mysql.Friend{{OwnerUserID: req.FromUserID, FriendUserID: req.ToUserID, OperatorUserID: tools.OpUserID(ctx)}}); err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
chat.FriendAddedNotification(tools.OperationID(ctx), tools.OpUserID(ctx), req.FromUserID, req.ToUserID)
2 years ago
} else if err != nil {
return nil, err
3 years ago
}
}
3 years ago
if req.HandleResult == constant.FriendResponseAgree {
chat.FriendApplicationApprovedNotification(req)
} else if req.HandleResult == constant.FriendResponseRefuse {
chat.FriendApplicationRejectedNotification(req)
} else {
2 years ago
trace_log.SetCtxInfo(ctx, utils.GetSelfFuncName(), nil, "handleResult", req.HandleResult)
3 years ago
}
2 years ago
return resp, nil
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{}
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 := s.friendModel.Delete(ctx, req.FromUserID, req.ToUserID); err != nil {
2 years ago
return nil, err
3 years ago
}
chat.FriendDeletedNotification(req)
2 years ago
return resp, nil
3 years ago
}
func (s *friendServer) GetBlacklist(ctx context.Context, req *pbFriend.GetBlacklistReq) (*pbFriend.GetBlacklistResp, error) {
2 years ago
resp := &pbFriend.GetBlacklistResp{}
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
blacks, err := s.blackModel.FindByOwnerUserID(ctx, req.FromUserID)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
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
3 years ago
}
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{}
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 := s.friendModel.UpdateRemark(ctx, req.FromUserID, req.ToUserID, req.Remark); err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
chat.FriendRemarkSetNotification(tools.OperationID(ctx), tools.OpUserID(ctx), req.FromUserID, req.ToUserID)
2 years ago
return resp, nil
3 years ago
}
3 years ago
func (s *friendServer) RemoveBlacklist(ctx context.Context, req *pbFriend.RemoveBlacklistReq) (*pbFriend.RemoveBlacklistResp, 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
}
2 years ago
if err := s.blackModel.Delete(ctx, []*mysql.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) IsInBlackList(ctx context.Context, req *pbFriend.IsInBlackListReq) (*pbFriend.IsInBlackListResp, error) {
2 years ago
resp := &pbFriend.IsInBlackListResp{}
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
blackIDList, err := rocksCache.GetBlackListFromCache(ctx, req.FromUserID)
2 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
resp.Response = utils.IsContain(req.ToUserID, blackIDList)
2 years ago
return resp, nil
3 years ago
}
func (s *friendServer) IsFriend(ctx context.Context, req *pbFriend.IsFriendReq) (*pbFriend.IsFriendResp, error) {
2 years ago
resp := &pbFriend.IsFriendResp{}
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
exist, err := s.friendModel.IsExist(ctx, req.FromUserID, req.ToUserID)
2 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
resp.Response = exist
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
}
2 years ago
friends, err := s.friendModel.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 := GetUserInfoList(ctx, userIDList)
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
2 years ago
friendRequests, err := s.friendRequestModel.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
}