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/firend.go

468 lines
23 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"
imdb "Open_IM/pkg/common/db/mysql_model/im_mysql_model"
"Open_IM/pkg/common/log"
"Open_IM/pkg/common/token_verify"
3 years ago
cp "Open_IM/pkg/common/utils"
3 years ago
"Open_IM/pkg/grpc-etcdv3/getcdv3"
pbFriend "Open_IM/pkg/proto/friend"
3 years ago
sdkws "Open_IM/pkg/proto/sdk_ws"
3 years ago
"Open_IM/pkg/utils"
"context"
"google.golang.org/grpc"
"net"
"strconv"
"strings"
3 years ago
"time"
3 years ago
)
type friendServer struct {
rpcPort int
rpcRegisterName string
etcdSchema string
etcdAddr []string
}
func NewFriendServer(port int) *friendServer {
log.NewPrivateLog("friend")
return &friendServer{
rpcPort: port,
rpcRegisterName: config.Config.RpcRegisterName.OpenImFriendName,
etcdSchema: config.Config.Etcd.EtcdSchema,
etcdAddr: config.Config.Etcd.EtcdAddr,
}
}
func (s *friendServer) Run() {
3 years ago
log.NewInfo("0", "friendServer run...")
3 years ago
ip := utils.ServerIP
registerAddress := ip + ":" + strconv.Itoa(s.rpcPort)
//listener network
listener, err := net.Listen("tcp", registerAddress)
if err != nil {
3 years ago
log.NewError("0", "Listen failed ", err.Error(), registerAddress)
3 years ago
return
}
3 years ago
log.NewInfo("0", "listen ok ", registerAddress)
3 years ago
defer listener.Close()
//grpc server
srv := grpc.NewServer()
defer srv.GracefulStop()
//User friend related services register to etcd
pbFriend.RegisterFriendServer(srv, s)
err = getcdv3.RegisterEtcd(s.etcdSchema, strings.Join(s.etcdAddr, ","), ip, s.rpcPort, s.rpcRegisterName, 10)
if err != nil {
3 years ago
log.NewError("0", "RegisterEtcd failed ", err.Error(), s.etcdSchema, strings.Join(s.etcdAddr, ","), ip, s.rpcPort, s.rpcRegisterName)
3 years ago
return
}
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) GetFriendsInfo(ctx context.Context, req *pbFriend.GetFriendsInfoReq) (*pbFriend.GetFriendInfoResp, error) {
// return nil, nil
//// log.NewInfo(req.CommID.OperationID, "GetFriendsInfo args ", req.String())
//// var (
//// isInBlackList int32
//// // isFriend int32
//// comment string
//// )
////
//// friendShip, err := imdb.FindFriendRelationshipFromFriend(req.CommID.FromUserID, req.CommID.ToUserID)
//// if err != nil {
//// log.NewError(req.CommID.OperationID, "FindFriendRelationshipFromFriend failed ", err.Error())
//// return &pbFriend.GetFriendInfoResp{ErrCode: constant.ErrSearchUserInfo.ErrCode, ErrMsg: constant.ErrSearchUserInfo.ErrMsg}, nil
//// // isFriend = constant.FriendFlag
//// }
//// comment = friendShip.Remark
////
//// friendUserInfo, err := imdb.FindUserByUID(req.CommID.ToUserID)
//// if err != nil {
//// log.NewError(req.CommID.OperationID, "FindUserByUID failed ", err.Error())
//// return &pbFriend.GetFriendInfoResp{ErrCode: constant.ErrSearchUserInfo.ErrCode, ErrMsg: constant.ErrSearchUserInfo.ErrMsg}, nil
//// }
////
//// err = imdb.FindRelationshipFromBlackList(req.CommID.FromUserID, req.CommID.ToUserID)
//// if err == nil {
//// isInBlackList = constant.BlackListFlag
//// }
////
//// resp := pbFriend.GetFriendInfoResp{ErrCode: 0, ErrMsg: "",}
////
//// utils.CopyStructFields(resp.FriendInfoList, friendUserInfo)
//// resp.Data.IsBlack = isInBlackList
//// resp.Data.OwnerUserID = req.CommID.FromUserID
//// resp.Data.Remark = comment
//// resp.Data.CreateTime = friendUserInfo.CreateTime
////
//// log.NewInfo(req.CommID.OperationID, "GetFriendsInfo ok ", resp)
//// return &resp, nil
////
//}
func (s *friendServer) AddBlacklist(ctx context.Context, req *pbFriend.AddBlacklistReq) (*pbFriend.AddBlacklistResp, error) {
3 years ago
log.NewInfo(req.CommID.OperationID, "AddBlacklist args ", req.String())
ok := token_verify.CheckAccess(req.CommID.OpUserID, req.CommID.FromUserID)
if !ok {
3 years ago
log.NewError(req.CommID.OperationID, "CheckAccess false ", req.CommID.OpUserID, req.CommID.FromUserID)
return &pbFriend.AddBlacklistResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil
3 years ago
}
3 years ago
black := imdb.Black{OwnerUserID: req.CommID.FromUserID, BlockUserID: req.CommID.ToUserID, OperatorUserID: req.CommID.OpUserID}
3 years ago
3 years ago
err := imdb.InsertInToUserBlackList(black)
3 years ago
if err != nil {
log.NewError(req.CommID.OperationID, "InsertInToUserBlackList failed ", err.Error())
3 years ago
return &pbFriend.AddBlacklistResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
3 years ago
}
3 years ago
log.NewInfo(req.CommID.OperationID, "AddBlacklist rpc ok ", req.CommID.FromUserID, req.CommID.ToUserID)
3 years ago
chat.BlackAddedNotification(req)
3 years ago
return &pbFriend.AddBlacklistResp{CommonResp: &pbFriend.CommonResp{}}, nil
3 years ago
}
3 years ago
func (s *friendServer) AddFriend(ctx context.Context, req *pbFriend.AddFriendReq) (*pbFriend.AddFriendResp, error) {
3 years ago
log.NewInfo(req.CommID.OperationID, "AddFriend args ", req.String())
ok := token_verify.CheckAccess(req.CommID.OpUserID, req.CommID.FromUserID)
if !ok {
3 years ago
log.NewError(req.CommID.OperationID, "CheckAccess false ", req.CommID.OpUserID, req.CommID.FromUserID)
return &pbFriend.AddFriendResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil
3 years ago
}
//Cannot add non-existent users
3 years ago
if _, err := imdb.GetUserByUserID(req.CommID.ToUserID); err != nil {
log.NewError(req.CommID.OperationID, "GetUserByUserID failed ", err.Error(), req.CommID.ToUserID)
3 years ago
return &pbFriend.AddFriendResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
3 years ago
}
//Establish a latest relationship in the friend request table
3 years ago
friendRequest := imdb.FriendRequest{ReqMsg: req.ReqMsg}
3 years ago
utils.CopyStructFields(&friendRequest, req.CommID)
3 years ago
// {openIM001 openIM002 0 test add friend 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC }]
3 years ago
log.NewDebug(req.CommID.OperationID, "UpdateFriendApplication args ", friendRequest)
3 years ago
err := imdb.UpdateFriendApplication(&friendRequest)
3 years ago
if err != nil {
3 years ago
log.NewError(req.CommID.OperationID, "UpdateFriendApplication failed ", err.Error(), friendRequest)
3 years ago
return &pbFriend.AddFriendResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
3 years ago
}
3 years ago
chat.FriendApplicationAddedNotification(req)
3 years ago
return &pbFriend.AddFriendResp{CommonResp: &pbFriend.CommonResp{}}, nil
3 years ago
}
3 years ago
//todo
3 years ago
func (s *friendServer) ImportFriend(ctx context.Context, req *pbFriend.ImportFriendReq) (*pbFriend.ImportFriendResp, error) {
log.NewInfo(req.OperationID, "ImportFriend failed ", req.String())
var resp pbFriend.ImportFriendResp
var c pbFriend.CommonResp
if !utils.IsContain(req.OpUserID, config.Config.Manager.AppManagerUid) {
3 years ago
log.NewError(req.OperationID, "not authorized", req.OpUserID, config.Config.Manager.AppManagerUid)
3 years ago
c.ErrCode = constant.ErrAccess.ErrCode
c.ErrMsg = constant.ErrAccess.ErrMsg
return &pbFriend.ImportFriendResp{CommonResp: &c, FailedFriendUserIDList: req.FriendUserIDList}, nil
3 years ago
}
3 years ago
if _, err := imdb.GetUserByUserID(req.FromUserID); err != nil {
3 years ago
log.NewError(req.OperationID, "GetUserByUserID failed ", err.Error(), req.FromUserID)
3 years ago
c.ErrCode = constant.ErrDB.ErrCode
3 years ago
c.ErrMsg = "this user not exists,cant not add friend"
3 years ago
return &pbFriend.ImportFriendResp{CommonResp: &c, FailedFriendUserIDList: req.FriendUserIDList}, nil
3 years ago
}
for _, v := range req.FriendUserIDList {
3 years ago
if _, fErr := imdb.GetUserByUserID(v); fErr != nil {
3 years ago
c.ErrMsg = "some uid establish failed"
c.ErrCode = 408
3 years ago
resp.FailedFriendUserIDList = append(resp.FailedFriendUserIDList, v)
3 years ago
} else {
3 years ago
if _, err := imdb.GetFriendRelationshipFromFriend(req.FromUserID, v); err != nil {
3 years ago
//Establish two single friendship
3 years ago
toInsertFollow := imdb.Friend{OwnerUserID: req.FromUserID, FriendUserID: v}
err1 := imdb.InsertToFriend(&toInsertFollow)
3 years ago
if err1 != nil {
3 years ago
resp.FailedFriendUserIDList = append(resp.FailedFriendUserIDList, v)
3 years ago
log.NewError(req.OperationID, "InsertToFriend failed", req.FromUserID, v, err1.Error())
c.ErrMsg = "some uid establish failed"
c.ErrCode = 408
continue
}
3 years ago
toInsertFollow = imdb.Friend{OwnerUserID: v, FriendUserID: req.FromUserID}
err2 := imdb.InsertToFriend(&toInsertFollow)
3 years ago
if err2 != nil {
3 years ago
resp.FailedFriendUserIDList = append(resp.FailedFriendUserIDList, v)
3 years ago
log.NewError(req.OperationID, "InsertToFriend failed", v, req.FromUserID, err2.Error())
c.ErrMsg = "some uid establish failed"
c.ErrCode = 408
continue
}
3 years ago
for _, v := range req.FriendUserIDList {
chat.FriendAddedNotification(req.OperationID, req.OpUserID, req.FromUserID, v)
}
3 years ago
}
}
}
resp.CommonResp = &c
log.NewInfo(req.OperationID, "ImportFriend rpc ok ", resp)
return &resp, nil
}
3 years ago
//process Friend application
3 years ago
func (s *friendServer) AddFriendResponse(ctx context.Context, req *pbFriend.AddFriendResponseReq) (*pbFriend.AddFriendResponseResp, error) {
3 years ago
log.NewInfo(req.CommID.OperationID, "AddFriendResponse args ", req.String())
3 years ago
if !token_verify.CheckAccess(req.CommID.OpUserID, req.CommID.FromUserID) {
log.NewError(req.CommID.OperationID, "CheckAccess false ", req.CommID.OpUserID, req.CommID.FromUserID)
3 years ago
return &pbFriend.AddFriendResponseResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil
3 years ago
}
//Check there application before agreeing or refuse to a friend's application
//req.CommID.FromUserID process req.CommID.ToUserID
3 years ago
friendRequest, err := imdb.GetFriendApplicationByBothUserID(req.CommID.ToUserID, req.CommID.FromUserID)
if err != nil {
log.NewError(req.CommID.OperationID, "GetFriendApplicationByBothUserID failed ", err.Error(), req.CommID.ToUserID, req.CommID.FromUserID)
3 years ago
return &pbFriend.AddFriendResponseResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
3 years ago
}
3 years ago
friendRequest.HandleResult = req.Flag
3 years ago
friendRequest.HandleTime = time.Now()
3 years ago
//friendRequest.HandleTime.Unix()
3 years ago
friendRequest.HandleMsg = req.HandleMsg
friendRequest.HandlerUserID = req.CommID.OpUserID
3 years ago
err = imdb.UpdateFriendApplication(friendRequest)
3 years ago
if err != nil {
3 years ago
log.NewError(req.CommID.OperationID, "UpdateFriendApplication failed ", err.Error(), friendRequest)
3 years ago
return &pbFriend.AddFriendResponseResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
3 years ago
}
3 years ago
3 years ago
//Change the status of the friend request form
if req.Flag == constant.FriendFlag {
//Establish friendship after find friend relationship not exists
3 years ago
_, err := imdb.GetFriendRelationshipFromFriend(req.CommID.FromUserID, req.CommID.ToUserID)
3 years ago
if err == nil {
3 years ago
log.NewWarn(req.CommID.OperationID, "GetFriendRelationshipFromFriend exist", req.CommID.FromUserID, req.CommID.ToUserID)
3 years ago
} else {
//Establish two single friendship
3 years ago
toInsertFollow := imdb.Friend{OwnerUserID: req.CommID.FromUserID, FriendUserID: req.CommID.ToUserID, OperatorUserID: req.CommID.OpUserID}
err = imdb.InsertToFriend(&toInsertFollow)
3 years ago
if err != nil {
3 years ago
log.NewError(req.CommID.OperationID, "InsertToFriend failed ", err.Error(), toInsertFollow)
3 years ago
return &pbFriend.AddFriendResponseResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
3 years ago
}
}
3 years ago
_, err = imdb.GetFriendRelationshipFromFriend(req.CommID.ToUserID, req.CommID.FromUserID)
3 years ago
if err == nil {
3 years ago
log.NewWarn(req.CommID.OperationID, "GetFriendRelationshipFromFriend exist", req.CommID.ToUserID, req.CommID.FromUserID)
3 years ago
} else {
toInsertFollow := imdb.Friend{OwnerUserID: req.CommID.ToUserID, FriendUserID: req.CommID.FromUserID, OperatorUserID: req.CommID.OpUserID}
err = imdb.InsertToFriend(&toInsertFollow)
if err != nil {
log.NewError(req.CommID.OperationID, "InsertToFriend failed ", err.Error(), toInsertFollow)
return &pbFriend.AddFriendResponseResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
}
chat.FriendAddedNotification(req.CommID.OperationID, req.CommID.OpUserID, req.CommID.FromUserID, req.CommID.ToUserID)
3 years ago
}
}
3 years ago
chat.FriendApplicationProcessedNotification(req)
3 years ago
log.NewInfo(req.CommID.OperationID, "rpc AddFriendResponse ok")
3 years ago
return &pbFriend.AddFriendResponseResp{CommonResp: &pbFriend.CommonResp{}}, nil
3 years ago
}
3 years ago
func (s *friendServer) DeleteFriend(ctx context.Context, req *pbFriend.DeleteFriendReq) (*pbFriend.DeleteFriendResp, error) {
3 years ago
log.NewInfo(req.CommID.OperationID, "DeleteFriend args ", req.String())
//Parse token, to find current user information
if !token_verify.CheckAccess(req.CommID.OpUserID, req.CommID.FromUserID) {
log.NewError(req.CommID.OperationID, "CheckAccess false ", req.CommID.OpUserID, req.CommID.FromUserID)
3 years ago
return &pbFriend.DeleteFriendResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil
3 years ago
}
err := imdb.DeleteSingleFriendInfo(req.CommID.FromUserID, req.CommID.ToUserID)
if err != nil {
log.NewError(req.CommID.OperationID, "DeleteSingleFriendInfo failed", err.Error(), req.CommID.FromUserID, req.CommID.ToUserID)
3 years ago
return &pbFriend.DeleteFriendResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil
3 years ago
}
log.NewInfo(req.CommID.OperationID, "DeleteFriend rpc ok")
3 years ago
chat.FriendDeletedNotification(req)
3 years ago
return &pbFriend.DeleteFriendResp{CommonResp: &pbFriend.CommonResp{}}, nil
3 years ago
}
func (s *friendServer) GetBlacklist(ctx context.Context, req *pbFriend.GetBlacklistReq) (*pbFriend.GetBlacklistResp, error) {
log.NewInfo(req.CommID.OperationID, "GetBlacklist args ", req.String())
//Parse token, to find current user information
if !token_verify.CheckAccess(req.CommID.OpUserID, req.CommID.FromUserID) {
3 years ago
log.NewError(req.CommID.OperationID, "CheckAccess false ", req.CommID.OpUserID, req.CommID.FromUserID)
3 years ago
return &pbFriend.GetBlacklistResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}, nil
3 years ago
}
3 years ago
blackListInfo, err := imdb.GetBlackListByUserID(req.CommID.FromUserID)
3 years ago
if err != nil {
log.NewError(req.CommID.OperationID, "GetBlackListByUID failed ", err.Error(), req.CommID.FromUserID)
3 years ago
return &pbFriend.GetBlacklistResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}, nil
3 years ago
}
var (
3 years ago
userInfoList []*sdkws.PublicUserInfo
3 years ago
)
for _, blackUser := range blackListInfo {
3 years ago
var blackUserInfo sdkws.PublicUserInfo
3 years ago
//Find black user information
3 years ago
us, err := imdb.GetUserByUserID(blackUser.BlockUserID)
3 years ago
if err != nil {
3 years ago
log.NewError(req.CommID.OperationID, "GetUserByUserID failed ", err.Error(), blackUser.BlockUserID)
3 years ago
continue
}
utils.CopyStructFields(&blackUserInfo, us)
userInfoList = append(userInfoList, &blackUserInfo)
}
3 years ago
log.NewInfo(req.CommID.OperationID, "rpc GetBlacklist ok ", pbFriend.GetBlacklistResp{BlackUserInfoList: userInfoList})
3 years ago
return &pbFriend.GetBlacklistResp{BlackUserInfoList: userInfoList}, nil
3 years ago
}
3 years ago
func (s *friendServer) SetFriendRemark(ctx context.Context, req *pbFriend.SetFriendRemarkReq) (*pbFriend.SetFriendRemarkResp, error) {
3 years ago
log.NewInfo(req.CommID.OperationID, "SetFriendComment args ", req.String())
//Parse token, to find current user information
if !token_verify.CheckAccess(req.CommID.OpUserID, req.CommID.FromUserID) {
3 years ago
log.NewError(req.CommID.OperationID, "CheckAccess false ", req.CommID.OpUserID, req.CommID.FromUserID)
3 years ago
return &pbFriend.SetFriendRemarkResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil
3 years ago
}
3 years ago
err := imdb.UpdateFriendComment(req.CommID.FromUserID, req.CommID.ToUserID, req.Remark)
3 years ago
if err != nil {
3 years ago
log.NewError(req.CommID.OperationID, "UpdateFriendComment failed ", req.CommID.FromUserID, req.CommID.ToUserID, req.Remark)
3 years ago
return &pbFriend.SetFriendRemarkResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
3 years ago
}
log.NewInfo(req.CommID.OperationID, "rpc SetFriendComment ok")
3 years ago
chat.FriendInfoChangedNotification(req.CommID.OperationID, req.CommID.OpUserID, req.CommID.FromUserID, req.CommID.ToUserID)
3 years ago
return &pbFriend.SetFriendRemarkResp{CommonResp: &pbFriend.CommonResp{}}, nil
3 years ago
}
3 years ago
func (s *friendServer) RemoveBlacklist(ctx context.Context, req *pbFriend.RemoveBlacklistReq) (*pbFriend.RemoveBlacklistResp, error) {
3 years ago
log.NewInfo(req.CommID.OperationID, "RemoveBlacklist args ", req.String())
//Parse token, to find current user information
if !token_verify.CheckAccess(req.CommID.OpUserID, req.CommID.FromUserID) {
3 years ago
log.NewError(req.CommID.OperationID, "CheckAccess false ", req.CommID.OpUserID, req.CommID.FromUserID)
3 years ago
return &pbFriend.RemoveBlacklistResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil
3 years ago
}
err := imdb.RemoveBlackList(req.CommID.FromUserID, req.CommID.ToUserID)
if err != nil {
log.NewError(req.CommID.OperationID, "RemoveBlackList failed", err.Error(), req.CommID.FromUserID, req.CommID.ToUserID)
3 years ago
return &pbFriend.RemoveBlacklistResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil
3 years ago
}
3 years ago
log.NewInfo(req.CommID.OperationID, "rpc RemoveBlacklist ok ")
3 years ago
chat.BlackDeletedNotification(req)
3 years ago
return &pbFriend.RemoveBlacklistResp{CommonResp: &pbFriend.CommonResp{}}, nil
3 years ago
}
func (s *friendServer) IsInBlackList(ctx context.Context, req *pbFriend.IsInBlackListReq) (*pbFriend.IsInBlackListResp, error) {
log.NewInfo("IsInBlackList args ", req.String())
if !token_verify.CheckAccess(req.CommID.OpUserID, req.CommID.FromUserID) {
3 years ago
log.NewError(req.CommID.OperationID, "CheckAccess false ", req.CommID.OpUserID, req.CommID.FromUserID)
3 years ago
return &pbFriend.IsInBlackListResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}, nil
3 years ago
}
var isInBlacklist = false
3 years ago
err := imdb.CheckBlack(req.CommID.FromUserID, req.CommID.ToUserID)
3 years ago
if err == nil {
isInBlacklist = true
}
3 years ago
log.NewInfo(req.CommID.OperationID, "IsInBlackList rpc ok ", pbFriend.IsInBlackListResp{Response: isInBlacklist})
3 years ago
return &pbFriend.IsInBlackListResp{Response: isInBlacklist}, nil
}
func (s *friendServer) IsFriend(ctx context.Context, req *pbFriend.IsFriendReq) (*pbFriend.IsFriendResp, error) {
log.NewInfo("IsFriend args ", req.String())
3 years ago
var isFriend bool
3 years ago
if !token_verify.CheckAccess(req.CommID.OpUserID, req.CommID.FromUserID) {
log.NewError(req.CommID.OperationID, "CheckAccess false ", req.CommID.OpUserID, req.CommID.FromUserID)
3 years ago
return &pbFriend.IsFriendResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}, nil
3 years ago
}
3 years ago
_, err := imdb.GetFriendRelationshipFromFriend(req.CommID.FromUserID, req.CommID.ToUserID)
3 years ago
if err == nil {
3 years ago
isFriend = true
3 years ago
} else {
3 years ago
isFriend = false
3 years ago
}
3 years ago
log.NewInfo("IsFriend rpc ok ", pbFriend.IsFriendResp{Response: isFriend})
3 years ago
return &pbFriend.IsFriendResp{Response: isFriend}, nil
3 years ago
}
func (s *friendServer) GetFriendList(ctx context.Context, req *pbFriend.GetFriendListReq) (*pbFriend.GetFriendListResp, error) {
log.NewInfo("GetFriendList args ", req.String())
if !token_verify.CheckAccess(req.CommID.OpUserID, req.CommID.FromUserID) {
log.NewError(req.CommID.OperationID, "CheckAccess false ", req.CommID.OpUserID, req.CommID.FromUserID)
3 years ago
return &pbFriend.GetFriendListResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}, nil
3 years ago
}
3 years ago
friends, err := imdb.GetFriendListByUserID(req.CommID.FromUserID)
3 years ago
if err != nil {
3 years ago
log.NewError(req.CommID.OperationID, "FindUserInfoFromFriend failed ", err.Error(), req.CommID.FromUserID)
3 years ago
return &pbFriend.GetFriendListResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}, nil
3 years ago
}
3 years ago
var userInfoList []*sdkws.FriendInfo
3 years ago
for _, friendUser := range friends {
3 years ago
friendUserInfo := sdkws.FriendInfo{FriendUser: &sdkws.UserInfo{}}
3 years ago
cp.FriendDBCopyOpenIM(&friendUserInfo, &friendUser)
log.NewDebug(req.CommID.OperationID, "friends : ", friendUser, "openim friends: ", friendUserInfo)
3 years ago
userInfoList = append(userInfoList, &friendUserInfo)
}
3 years ago
log.NewInfo(req.CommID.OperationID, "rpc GetFriendList ok", pbFriend.GetFriendListResp{FriendInfoList: userInfoList})
return &pbFriend.GetFriendListResp{FriendInfoList: userInfoList}, nil
3 years ago
}
3 years ago
//received
3 years ago
func (s *friendServer) GetFriendApplyList(ctx context.Context, req *pbFriend.GetFriendApplyListReq) (*pbFriend.GetFriendApplyListResp, error) {
3 years ago
log.NewInfo(req.CommID.OperationID, "GetFriendApplyList args ", req.String())
//Parse token, to find current user information
if !token_verify.CheckAccess(req.CommID.OpUserID, req.CommID.FromUserID) {
log.NewError(req.CommID.OperationID, "CheckAccess false ", req.CommID.OpUserID, req.CommID.FromUserID)
3 years ago
return &pbFriend.GetFriendApplyListResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}, nil
3 years ago
}
// Find the current user friend applications received
3 years ago
ApplyUsersInfo, err := imdb.GetReceivedFriendsApplicationListByUserID(req.CommID.FromUserID)
3 years ago
if err != nil {
3 years ago
log.NewError(req.CommID.OperationID, "GetReceivedFriendsApplicationListByUserID ", err.Error(), req.CommID.FromUserID)
3 years ago
return &pbFriend.GetFriendApplyListResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}, nil
3 years ago
}
3 years ago
var appleUserList []*sdkws.FriendRequest
3 years ago
for _, applyUserInfo := range ApplyUsersInfo {
3 years ago
var userInfo sdkws.FriendRequest
utils.CopyStructFields(&userInfo, applyUserInfo)
3 years ago
appleUserList = append(appleUserList, &userInfo)
}
3 years ago
log.NewInfo(req.CommID.OperationID, "rpc GetFriendApplyList ok", pbFriend.GetFriendApplyListResp{FriendRequestList: appleUserList})
return &pbFriend.GetFriendApplyListResp{FriendRequestList: appleUserList}, nil
3 years ago
}
3 years ago
func (s *friendServer) GetSelfApplyList(ctx context.Context, req *pbFriend.GetSelfApplyListReq) (*pbFriend.GetSelfApplyListResp, error) {
3 years ago
log.NewInfo(req.CommID.OperationID, "GetSelfApplyList args ", req.String())
3 years ago
3 years ago
//Parse token, to find current user information
if !token_verify.CheckAccess(req.CommID.OpUserID, req.CommID.FromUserID) {
log.NewError(req.CommID.OperationID, "CheckAccess false ", req.CommID.OpUserID, req.CommID.FromUserID)
3 years ago
return &pbFriend.GetSelfApplyListResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}, nil
3 years ago
}
// Find the self add other userinfo
3 years ago
usersInfo, err := imdb.GetSendFriendApplicationListByUserID(req.CommID.FromUserID)
3 years ago
if err != nil {
3 years ago
log.NewError(req.CommID.OperationID, "GetSendFriendApplicationListByUserID failed ", err.Error(), req.CommID.FromUserID)
3 years ago
return &pbFriend.GetSelfApplyListResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}, nil
3 years ago
}
3 years ago
var selfApplyOtherUserList []*sdkws.FriendRequest
3 years ago
for _, selfApplyOtherUserInfo := range usersInfo {
3 years ago
var userInfo sdkws.FriendRequest // pbFriend.ApplyUserInfo
3 years ago
cp.FriendRequestDBCopyOpenIM(&userInfo, &selfApplyOtherUserInfo)
3 years ago
selfApplyOtherUserList = append(selfApplyOtherUserList, &userInfo)
}
3 years ago
log.NewInfo(req.CommID.OperationID, "rpc GetSelfApplyList ok", pbFriend.GetSelfApplyListResp{FriendRequestList: selfApplyOtherUserList})
return &pbFriend.GetSelfApplyListResp{FriendRequestList: selfApplyOtherUserList}, nil
3 years ago
}