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

473 lines
16 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
"Open_IM/pkg/common/db"
3 years ago
imdb "Open_IM/pkg/common/db/mysql_model/im_mysql_model"
2 years ago
rocksCache "Open_IM/pkg/common/db/rocks_cache"
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"
3 years ago
cp "Open_IM/pkg/common/utils"
2 years ago
"Open_IM/pkg/getcdv3"
3 years ago
pbCache "Open_IM/pkg/proto/cache"
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
friendModel *imdb.Friend
friendRequestModel *imdb.FriendRequest
blackModel *imdb.Black
3 years ago
}
func NewFriendServer(port int) *friendServer {
3 years ago
log.NewPrivateLog(constant.LogFileName)
2 years ago
DB := db.DB.MysqlDB.DefaultGormDB()
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,
2 years ago
friendModel: imdb.NewFriend(DB),
friendRequestModel: imdb.NewFriendRequest(DB),
blackModel: imdb.NewBlack(DB),
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
}
}
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 := imdb.Black{OwnerUserID: req.FromUserID, BlockUserID: req.ToUserID, OperatorUserID: tools.OpUserID(ctx)}
2 years ago
if err := s.blackModel.Create(ctx, []*imdb.Black{&black}); err != nil {
return nil, err
}
2 years ago
etcdConn, err := getcdv3.GetConn(ctx, config.Config.RpcRegisterName.OpenImCacheName)
if err != nil {
2 years ago
return nil, err
}
2 years ago
_, err = pbCache.NewCacheClient(etcdConn).DelBlackIDListFromCache(ctx, &pbCache.DelBlackIDListFromCacheReq{UserID: req.FromUserID})
2 years ago
if err != nil {
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
userIDList, err := rocksCache.GetFriendIDListFromCache(ctx, req.ToUserID)
2 years ago
if err != nil {
2 years ago
return nil, err
2 years ago
}
2 years ago
userIDList2, err := rocksCache.GetFriendIDListFromCache(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 _, v := range userIDList {
2 years ago
if v == req.FromUserID {
2 years ago
for _, v2 := range userIDList2 {
2 years ago
if v2 == 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 := imdb.FriendRequest{
2 years ago
FromUserID: req.FromUserID,
ToUserID: req.ToUserID,
2 years ago
HandleResult: 0,
ReqMsg: req.ReqMsg,
CreateTime: time.Now(),
}
if err := s.friendRequestModel.Create(ctx, []*imdb.FriendRequest{&friendRequest}); err != nil {
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
for _, userID := range 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
}
var friends []*imdb.Friend
switch len(fs) {
case 1:
if fs[0].OwnerUserID == req.FromUserID {
friends = append(friends, &imdb.Friend{OwnerUserID: userID, FriendUserID: req.FromUserID})
3 years ago
} else {
2 years ago
friends = append(friends, &imdb.Friend{OwnerUserID: req.FromUserID, FriendUserID: userID})
3 years ago
}
2 years ago
case 0:
friends = append(friends, &imdb.Friend{OwnerUserID: userID, FriendUserID: req.FromUserID}, &imdb.Friend{OwnerUserID: req.FromUserID, FriendUserID: userID})
default:
continue
}
if err := s.friendModel.Create(ctx, friends); err != nil {
return nil, err
3 years ago
}
}
2 years ago
etcdConn, err := getcdv3.GetConn(ctx, config.Config.RpcRegisterName.OpenImCacheName)
2 years ago
if err != nil {
2 years ago
return nil, err
2 years ago
}
2 years ago
cacheClient := pbCache.NewCacheClient(etcdConn)
if _, err := cacheClient.DelFriendIDListFromCache(ctx, &pbCache.DelFriendIDListFromCacheReq{UserID: req.FromUserID}); err != nil {
return nil, err
2 years ago
}
2 years ago
if err := rocksCache.DelAllFriendsInfoFromCache(ctx, req.FromUserID); err != nil {
2 years ago
trace_log.SetCtxInfo(ctx, "DelAllFriendsInfoFromCache", err, "userID", req.FromUserID)
}
for _, userID := range req.FriendUserIDList {
2 years ago
if _, err = cacheClient.DelFriendIDListFromCache(ctx, &pbCache.DelFriendIDListFromCacheReq{UserID: userID}); err != nil {
return nil, err
}
2 years ago
if err := rocksCache.DelAllFriendsInfoFromCache(ctx, userID); err != nil {
2 years ago
trace_log.SetCtxInfo(ctx, "DelAllFriendsInfoFromCache", err, "userID", userID)
}
}
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)
3 years ago
err = imdb.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 {
2 years ago
var isInsert bool
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, []*imdb.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
isInsert = true
} else if err != nil {
return nil, err
3 years ago
}
2 years ago
// cache rpc
2 years ago
if isInsert {
etcdConn, err := getcdv3.GetConn(ctx, config.Config.RpcRegisterName.OpenImCacheName)
if err != nil {
2 years ago
return nil, err
}
2 years ago
client := pbCache.NewCacheClient(etcdConn)
2 years ago
if _, err := client.DelFriendIDListFromCache(context.Background(), &pbCache.DelFriendIDListFromCacheReq{UserID: req.ToUserID}); err != nil {
2 years ago
return nil, err
}
2 years ago
if _, err := client.DelFriendIDListFromCache(context.Background(), &pbCache.DelFriendIDListFromCacheReq{UserID: req.FromUserID}); err != nil {
2 years ago
return nil, err
}
2 years ago
if err := rocksCache.DelAllFriendsInfoFromCache(ctx, req.ToUserID); err != nil {
trace_log.SetCtxInfo(ctx, "DelAllFriendsInfoFromCache", err, "userID", req.ToUserID)
}
2 years ago
if err := rocksCache.DelAllFriendsInfoFromCache(ctx, req.FromUserID); err != nil {
trace_log.SetCtxInfo(ctx, "DelAllFriendsInfoFromCache", err, "userID", req.FromUserID)
}
2 years ago
chat.FriendAddedNotification(tools.OperationID(ctx), tools.OpUserID(ctx), req.FromUserID, req.ToUserID)
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
}
2 years ago
etcdConn, err := getcdv3.GetConn(ctx, config.Config.RpcRegisterName.OpenImCacheName)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
_, err = pbCache.NewCacheClient(etcdConn).DelFriendIDListFromCache(context.Background(), &pbCache.DelFriendIDListFromCacheReq{UserID: req.FromUserID})
2 years ago
if err != nil {
return nil, err
3 years ago
}
2 years ago
if err := rocksCache.DelAllFriendsInfoFromCache(ctx, req.FromUserID); err != nil {
trace_log.SetCtxInfo(ctx, "DelAllFriendsInfoFromCache", err, "DelAllFriendsInfoFromCache", req.FromUserID)
}
2 years ago
if err := rocksCache.DelAllFriendsInfoFromCache(ctx, req.ToUserID); err != nil {
trace_log.SetCtxInfo(ctx, "DelAllFriendsInfoFromCache", err, "DelAllFriendsInfoFromCache", req.ToUserID)
}
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
blackIDList, err := rocksCache.GetBlackListFromCache(ctx, req.FromUserID)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
for _, userID := range blackIDList {
2 years ago
user, err := rocksCache.GetUserInfoFromCache(ctx, userID)
3 years ago
if err != nil {
2 years ago
trace_log.SetCtxInfo(ctx, "GetUserInfoFromCache", err, "userID", userID)
3 years ago
continue
}
2 years ago
var blackUserInfo sdkws.PublicUserInfo
utils.CopyStructFields(&blackUserInfo, user)
2 years ago
resp.BlackUserInfoList = append(resp.BlackUserInfoList, &blackUserInfo)
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
if err := rocksCache.DelAllFriendsInfoFromCache(ctx, req.FromUserID); err != nil {
2 years ago
return nil, err
2 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, []*imdb.Black{{OwnerUserID: req.FromUserID, BlockUserID: req.ToUserID}}); err != nil {
2 years ago
return nil, err
}
2 years ago
etcdConn, err := getcdv3.GetConn(ctx, config.Config.RpcRegisterName.OpenImCacheName)
if err != nil {
2 years ago
return nil, err
}
2 years ago
_, err = pbCache.NewCacheClient(etcdConn).DelBlackIDListFromCache(context.Background(), &pbCache.DelBlackIDListFromCacheReq{UserID: req.FromUserID})
2 years ago
if err != nil {
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
friendIDList, err := rocksCache.GetFriendIDListFromCache(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, friendIDList)
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
friendList, err := rocksCache.GetAllFriendsInfoFromCache(ctx, req.FromUserID)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
3 years ago
var userInfoList []*sdkws.FriendInfo
2 years ago
for _, friendUser := range friendList {
3 years ago
friendUserInfo := sdkws.FriendInfo{FriendUser: &sdkws.UserInfo{}}
2 years ago
cp.FriendDBCopyOpenIM(&friendUserInfo, friendUser)
3 years ago
userInfoList = append(userInfoList, &friendUserInfo)
}
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
applyUsersInfo, err := s.friendRequestModel.FindToUserID(ctx, req.FromUserID)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
for _, applyUserInfo := range applyUsersInfo {
3 years ago
var userInfo sdkws.FriendRequest
2 years ago
cp.FriendRequestDBCopyOpenIM(&userInfo, applyUserInfo)
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
usersInfo, err := s.friendRequestModel.FindFromUserID(ctx, req.FromUserID)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
for _, selfApplyOtherUserInfo := range usersInfo {
3 years ago
var userInfo sdkws.FriendRequest // pbFriend.ApplyUserInfo
2 years ago
cp.FriendRequestDBCopyOpenIM(&userInfo, selfApplyOtherUserInfo)
resp.FriendRequestList = append(resp.FriendRequestList, &userInfo)
3 years ago
}
2 years ago
return resp, nil
3 years ago
}