mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-06 04:15:46 +08:00
friend rpc service initial commit
This commit is contained in:
parent
f1e3912edd
commit
62d5714093
27
src/rpc/friend/Makefile
Normal file
27
src/rpc/friend/Makefile
Normal file
@ -0,0 +1,27 @@
|
||||
.PHONY: all build run gotool install clean help
|
||||
|
||||
BINARY_NAME=open_im_friend
|
||||
BIN_DIR=../../../bin/
|
||||
LAN_FILE=.go
|
||||
GO_FILE:=${BINARY_NAME}${LAN_FILE}
|
||||
|
||||
all: gotool build
|
||||
|
||||
build:
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ${BINARY_NAME} ${GO_FILE}
|
||||
|
||||
run:
|
||||
@go run ./
|
||||
|
||||
gotool:
|
||||
go fmt ./
|
||||
go vet ./
|
||||
|
||||
install:
|
||||
make build
|
||||
mv ${BINARY_NAME} ${BIN_DIR}
|
||||
|
||||
clean:
|
||||
@if [ -f ${BINARY_NAME} ] ; then rm ${BINARY_NAME} ; fi
|
||||
|
||||
|
28
src/rpc/friend/friend/add_blacklist.go
Normal file
28
src/rpc/friend/friend/add_blacklist.go
Normal file
@ -0,0 +1,28 @@
|
||||
package friend
|
||||
|
||||
import (
|
||||
"Open_IM/src/common/config"
|
||||
"Open_IM/src/common/db/mysql_model/im_mysql_model"
|
||||
"Open_IM/src/common/log"
|
||||
pbFriend "Open_IM/src/proto/friend"
|
||||
"Open_IM/src/utils"
|
||||
"context"
|
||||
)
|
||||
|
||||
func (s *friendServer) AddBlacklist(ctx context.Context, req *pbFriend.AddBlacklistReq) (*pbFriend.CommonResp, error) {
|
||||
log.Info(req.Token, req.OperationID, "rpc add blacklist is server,args=%s", req.String())
|
||||
//Parse token, to find current user information
|
||||
claims, err := utils.ParseToken(req.Token)
|
||||
if err != nil {
|
||||
log.Error(req.Token, req.OperationID, "err=%s,parse token failed", err.Error())
|
||||
return &pbFriend.CommonResp{ErrorCode: config.ErrParseToken.ErrCode, ErrorMsg: config.ErrParseToken.ErrMsg}, nil
|
||||
} else {
|
||||
err := im_mysql_model.InsertInToUserBlackList(claims.UID, req.Uid)
|
||||
if err != nil {
|
||||
log.Error(req.Token, req.OperationID, "err=%s,Failed to add blacklist", err.Error())
|
||||
return &pbFriend.CommonResp{ErrorCode: config.ErrMysql.ErrCode, ErrorMsg: config.ErrMysql.ErrMsg}, nil
|
||||
}
|
||||
}
|
||||
log.Info(req.Token, req.OperationID, "rpc add blacklist success return,uid=%s", req.Uid)
|
||||
return &pbFriend.CommonResp{}, nil
|
||||
}
|
47
src/rpc/friend/friend/add_friend.go
Normal file
47
src/rpc/friend/friend/add_friend.go
Normal file
@ -0,0 +1,47 @@
|
||||
package friend
|
||||
|
||||
import (
|
||||
"Open_IM/src/common/config"
|
||||
"Open_IM/src/common/constant"
|
||||
"Open_IM/src/common/db/mysql_model/im_mysql_model"
|
||||
"Open_IM/src/common/log"
|
||||
pbChat "Open_IM/src/proto/chat"
|
||||
pbFriend "Open_IM/src/proto/friend"
|
||||
"Open_IM/src/push/logic"
|
||||
"Open_IM/src/utils"
|
||||
"context"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func (s *friendServer) AddFriend(ctx context.Context, req *pbFriend.AddFriendReq) (*pbFriend.CommonResp, error) {
|
||||
log.Info(req.Token, req.OperationID, "rpc add friend is server,userid=%s", req.Uid)
|
||||
//Parse token, to find current user information
|
||||
claims, err := utils.ParseToken(req.Token)
|
||||
if err != nil {
|
||||
log.Error(req.Token, req.OperationID, "err=%s,parse token failed", err.Error())
|
||||
return &pbFriend.CommonResp{ErrorCode: config.ErrParseToken.ErrCode, ErrorMsg: config.ErrParseToken.ErrMsg}, nil
|
||||
}
|
||||
//Establish a relationship in the friend request table
|
||||
err = im_mysql_model.InsertIntoFriendReq(claims.UID, req.Uid, constant.NotFriendFlag, req.ReqMessage)
|
||||
if err != nil {
|
||||
log.Error(req.Token, req.OperationID, fmt.Sprintf("err=%s,create friend request ship failed", err.Error()))
|
||||
return &pbFriend.CommonResp{ErrorCode: config.ErrAddFriend.ErrCode, ErrorMsg: config.ErrAddFriend.ErrMsg}, nil
|
||||
}
|
||||
log.Info(req.Token, req.OperationID, "rpc add friend is success return,uid=%s", req.Uid)
|
||||
//Push message when add friend successfully
|
||||
senderInfo, errSend := im_mysql_model.FindUserByUID(claims.UID)
|
||||
receiverInfo, errReceive := im_mysql_model.FindUserByUID(req.Uid)
|
||||
if errSend == nil && errReceive == nil {
|
||||
logic.SendMsgByWS(&pbChat.WSToMsgSvrChatMsg{
|
||||
SendID: senderInfo.UID,
|
||||
RecvID: receiverInfo.UID,
|
||||
Content: senderInfo.Name + " asked to add you as a friend",
|
||||
SendTime: utils.GetCurrentTimestampBySecond(),
|
||||
MsgFrom: constant.SysMsgType,
|
||||
ContentType: constant.AddFriendTip,
|
||||
SessionType: constant.SingleChatType,
|
||||
OperationID: req.OperationID,
|
||||
})
|
||||
}
|
||||
return &pbFriend.CommonResp{}, nil
|
||||
}
|
58
src/rpc/friend/friend/add_friend_response.go
Normal file
58
src/rpc/friend/friend/add_friend_response.go
Normal file
@ -0,0 +1,58 @@
|
||||
package friend
|
||||
|
||||
import (
|
||||
"Open_IM/src/common/config"
|
||||
"Open_IM/src/common/constant"
|
||||
"Open_IM/src/common/db/mysql_model/im_mysql_model"
|
||||
"Open_IM/src/common/log"
|
||||
pbChat "Open_IM/src/proto/chat"
|
||||
pbFriend "Open_IM/src/proto/friend"
|
||||
"Open_IM/src/push/logic"
|
||||
"Open_IM/src/utils"
|
||||
"context"
|
||||
)
|
||||
|
||||
func (s *friendServer) AddedFriend(ctx context.Context, req *pbFriend.AddedFriendReq) (*pbFriend.CommonResp, error) {
|
||||
log.Info(req.Token, req.OperationID, "rpc add friend response is server,args=%s", req.String())
|
||||
//Parse token, to find current user information
|
||||
claims, err := utils.ParseToken(req.Token)
|
||||
if err != nil {
|
||||
log.Error(req.Token, req.OperationID, "err=%s,parse token failed", err.Error())
|
||||
return &pbFriend.CommonResp{ErrorCode: config.ErrParseToken.ErrCode, ErrorMsg: config.ErrParseToken.ErrMsg}, nil
|
||||
}
|
||||
err = im_mysql_model.UpdateFriendRelationshipToFriendReq(req.Uid, claims.UID, req.Flag)
|
||||
if err != nil {
|
||||
log.Error(req.Token, req.OperationID, "err=%s,update friend request table failed", err.Error())
|
||||
return &pbFriend.CommonResp{ErrorCode: config.ErrMysql.ErrCode, ErrorMsg: config.ErrMysql.ErrMsg}, nil
|
||||
}
|
||||
log.Info(req.Token, req.OperationID, "rpc add friend response success return,userid=%s,flag=%d", req.Uid, req.Flag)
|
||||
//Change the status of the friend request form
|
||||
if req.Flag == 1 {
|
||||
//Establish two single friendship
|
||||
err = im_mysql_model.InsertToFriend(claims.UID, req.Uid, req.Flag)
|
||||
if err != nil {
|
||||
log.Error(req.Token, req.OperationID, "err=%s,create friendship failed", err.Error())
|
||||
return &pbFriend.CommonResp{ErrorCode: config.ErrAddFriend.ErrCode, ErrorMsg: config.ErrAddFriend.ErrMsg}, nil
|
||||
}
|
||||
err = im_mysql_model.InsertToFriend(req.Uid, claims.UID, req.Flag)
|
||||
if err != nil {
|
||||
log.Error(req.Token, req.OperationID, "err=%s,create friendship failed", err.Error())
|
||||
return &pbFriend.CommonResp{ErrorCode: config.ErrAddFriend.ErrCode, ErrorMsg: config.ErrAddFriend.ErrMsg}, nil
|
||||
}
|
||||
//Push message when establish friends successfully
|
||||
senderInfo, errSend := im_mysql_model.FindUserByUID(claims.UID)
|
||||
if errSend == nil {
|
||||
logic.SendMsgByWS(&pbChat.WSToMsgSvrChatMsg{
|
||||
SendID: claims.UID,
|
||||
RecvID: req.Uid,
|
||||
Content: senderInfo.Name + " agreed to add you as a friend.",
|
||||
SendTime: utils.GetCurrentTimestampBySecond(),
|
||||
MsgFrom: constant.SysMsgType, //Notification message identification
|
||||
ContentType: constant.AgreeAddFriendTip, //Add friend flag
|
||||
SessionType: constant.SingleChatType,
|
||||
OperationID: req.OperationID,
|
||||
})
|
||||
}
|
||||
}
|
||||
return &pbFriend.CommonResp{}, nil
|
||||
}
|
27
src/rpc/friend/friend/delete_friend.go
Normal file
27
src/rpc/friend/friend/delete_friend.go
Normal file
@ -0,0 +1,27 @@
|
||||
package friend
|
||||
|
||||
import (
|
||||
"Open_IM/src/common/config"
|
||||
"Open_IM/src/common/db/mysql_model/im_mysql_model"
|
||||
"Open_IM/src/common/log"
|
||||
pbFriend "Open_IM/src/proto/friend"
|
||||
"Open_IM/src/utils"
|
||||
"context"
|
||||
)
|
||||
|
||||
func (s *friendServer) DeleteFriend(ctx context.Context, req *pbFriend.DeleteFriendReq) (*pbFriend.CommonResp, error) {
|
||||
log.Info(req.Token, req.OperationID, "rpc delete friend is server,args=%s", req.String())
|
||||
//Parse token, to find current user information
|
||||
claims, err := utils.ParseToken(req.Token)
|
||||
if err != nil {
|
||||
log.Error(req.Token, req.OperationID, "err=%s,parse token failed", err.Error())
|
||||
return &pbFriend.CommonResp{ErrorCode: config.ErrParseToken.ErrCode, ErrorMsg: config.ErrParseToken.ErrMsg}, nil
|
||||
}
|
||||
err = im_mysql_model.DeleteSingleFriendInfo(claims.UID, req.Uid)
|
||||
if err != nil {
|
||||
log.Error(req.Token, req.OperationID, "err=%s,delete friend failed", err.Error())
|
||||
return &pbFriend.CommonResp{ErrorCode: config.ErrMysql.ErrCode, ErrorMsg: config.ErrMysql.ErrMsg}, nil
|
||||
}
|
||||
log.Info(req.Token, req.OperationID, "rpc delete friend success return")
|
||||
return &pbFriend.CommonResp{}, nil
|
||||
}
|
55
src/rpc/friend/friend/get_blacklist.go
Normal file
55
src/rpc/friend/friend/get_blacklist.go
Normal file
@ -0,0 +1,55 @@
|
||||
package friend
|
||||
|
||||
import (
|
||||
"Open_IM/src/common/config"
|
||||
"Open_IM/src/common/db/mysql_model/im_mysql_model"
|
||||
"Open_IM/src/common/log"
|
||||
pbFriend "Open_IM/src/proto/friend"
|
||||
"Open_IM/src/utils"
|
||||
"context"
|
||||
)
|
||||
|
||||
func (s *friendServer) GetBlacklist(ctx context.Context, req *pbFriend.GetBlacklistReq) (*pbFriend.GetBlacklistResp, error) {
|
||||
log.Info(req.Token, req.OperationID, "rpc get blacklist is server,args=%s", req.String())
|
||||
var (
|
||||
userInfoList []*pbFriend.UserInfo
|
||||
comment string
|
||||
)
|
||||
//Parse token, to find current user information
|
||||
claims, err := utils.ParseToken(req.Token)
|
||||
if err != nil {
|
||||
log.Error(req.Token, req.OperationID, "err=%s,parse token failed", err.Error())
|
||||
return &pbFriend.GetBlacklistResp{ErrorCode: config.ErrParseToken.ErrCode, ErrorMsg: config.ErrParseToken.ErrMsg}, nil
|
||||
}
|
||||
blackListInfo, err := im_mysql_model.GetBlackListByUID(claims.UID)
|
||||
if err != nil {
|
||||
log.Error(req.Token, req.OperationID, "err=%s get blacklist failed", err.Error())
|
||||
return &pbFriend.GetBlacklistResp{ErrorCode: config.ErrGetBlackList.ErrCode, ErrorMsg: config.ErrGetBlackList.ErrMsg}, nil
|
||||
}
|
||||
for _, blackUser := range blackListInfo {
|
||||
var blackUserInfo pbFriend.UserInfo
|
||||
//Find black user information
|
||||
us, err := im_mysql_model.FindUserByUID(blackUser.BlockId)
|
||||
if err != nil {
|
||||
log.Error(req.Token, req.OperationID, "err=%s search black list userInfo failed", err.Error())
|
||||
continue
|
||||
}
|
||||
friendShip, err := im_mysql_model.FindFriendRelationshipFromFriend(claims.UID, blackUser.BlockId)
|
||||
if err == nil {
|
||||
comment = friendShip.Comment
|
||||
}
|
||||
blackUserInfo.Uid = us.UID
|
||||
blackUserInfo.Icon = us.Icon
|
||||
blackUserInfo.Name = us.Name
|
||||
blackUserInfo.Gender = us.Gender
|
||||
blackUserInfo.Mobile = us.Mobile
|
||||
blackUserInfo.Birth = us.Birth
|
||||
blackUserInfo.Email = us.Email
|
||||
blackUserInfo.Ex = us.Ex
|
||||
blackUserInfo.Comment = comment
|
||||
|
||||
userInfoList = append(userInfoList, &blackUserInfo)
|
||||
}
|
||||
log.Info(req.Token, req.OperationID, "rpc get blacklist success return")
|
||||
return &pbFriend.GetBlacklistResp{Data: userInfoList}, nil
|
||||
}
|
58
src/rpc/friend/friend/get_friend_apply_list.go
Normal file
58
src/rpc/friend/friend/get_friend_apply_list.go
Normal file
@ -0,0 +1,58 @@
|
||||
package friend
|
||||
|
||||
import (
|
||||
"Open_IM/src/common/config"
|
||||
"Open_IM/src/common/db/mysql_model/im_mysql_model"
|
||||
"Open_IM/src/common/log"
|
||||
pbFriend "Open_IM/src/proto/friend"
|
||||
"Open_IM/src/utils"
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (s *friendServer) GetFriendApplyList(ctx context.Context, req *pbFriend.GetFriendApplyReq) (*pbFriend.GetFriendApplyResp, error) {
|
||||
log.Info(req.Token, req.OperationID, "rpc get friend apply list is server,args=%s", req.String())
|
||||
var appleUserList []*pbFriend.ApplyUserInfo
|
||||
//Parse token, to find current user information
|
||||
claims, err := utils.ParseToken(req.Token)
|
||||
if err != nil {
|
||||
log.Error(req.Token, req.OperationID, "err=%s,parse token failed", err.Error())
|
||||
return &pbFriend.GetFriendApplyResp{ErrorCode: config.ErrParseToken.ErrCode, ErrorMsg: config.ErrParseToken.ErrMsg}, nil
|
||||
}
|
||||
// Find the current user friend applications received
|
||||
ApplyUsersInfo, err := im_mysql_model.FindFriendsApplyFromFriendReq(claims.UID)
|
||||
if err != nil {
|
||||
log.Error(req.Token, req.OperationID, "err=%s,search applyInfo failed", err.Error())
|
||||
return &pbFriend.GetFriendApplyResp{ErrorCode: config.ErrMysql.ErrCode, ErrorMsg: config.ErrMysql.ErrMsg}, nil
|
||||
}
|
||||
for _, applyUserInfo := range ApplyUsersInfo {
|
||||
var userInfo pbFriend.ApplyUserInfo
|
||||
//Find friend application status
|
||||
friendReqStatus, err := im_mysql_model.FindFriendRelationshipFromFriendReq(applyUserInfo.ReqId, applyUserInfo.UserId)
|
||||
if err != nil {
|
||||
log.Error(req.Token, req.OperationID, "err=%s,search friendRelationshipStatus failed", err.Error())
|
||||
} else {
|
||||
userInfo.Flag = friendReqStatus.Flag
|
||||
userInfo.ReqMessage = friendReqStatus.ReqMessage
|
||||
userInfo.ApplyTime = strconv.FormatInt(friendReqStatus.CreateTime.Unix(), 10)
|
||||
}
|
||||
//Find user information
|
||||
us, err := im_mysql_model.FindUserByUID(applyUserInfo.ReqId)
|
||||
if err != nil {
|
||||
log.Error(req.Token, req.OperationID, fmt.Sprintf("err=%s,search userInfo failed", err.Error()))
|
||||
} else {
|
||||
userInfo.Uid = us.UID
|
||||
userInfo.Icon = us.Icon
|
||||
userInfo.Name = us.Name
|
||||
userInfo.Gender = us.Gender
|
||||
userInfo.Mobile = us.Mobile
|
||||
userInfo.Birth = us.Birth
|
||||
userInfo.Email = us.Email
|
||||
userInfo.Ex = us.Ex
|
||||
appleUserList = append(appleUserList, &userInfo)
|
||||
}
|
||||
}
|
||||
log.Info(req.Token, req.OperationID, fmt.Sprintf("rpc get friendapplylist success return"))
|
||||
return &pbFriend.GetFriendApplyResp{Data: appleUserList}, nil
|
||||
}
|
59
src/rpc/friend/friend/get_friend_list.go
Normal file
59
src/rpc/friend/friend/get_friend_list.go
Normal file
@ -0,0 +1,59 @@
|
||||
package friend
|
||||
|
||||
import (
|
||||
"Open_IM/src/common/config"
|
||||
"Open_IM/src/common/constant"
|
||||
"Open_IM/src/common/db/mysql_model/im_mysql_model"
|
||||
"Open_IM/src/common/log"
|
||||
pbFriend "Open_IM/src/proto/friend"
|
||||
"Open_IM/src/utils"
|
||||
"context"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func (s *friendServer) GetFriendList(ctx context.Context, req *pbFriend.GetFriendListReq) (*pbFriend.GetFriendListResp, error) {
|
||||
log.Info(req.Token, req.OperationID, "rpc get friend list is server,args=%s", req.String())
|
||||
var userInfoList []*pbFriend.UserInfo
|
||||
//Parse token, to find current user information
|
||||
claims, err := utils.ParseToken(req.Token)
|
||||
if err != nil {
|
||||
log.Error(req.Token, req.OperationID, "err=%s,parse token failed", err.Error())
|
||||
return &pbFriend.GetFriendListResp{ErrorCode: config.ErrParseToken.ErrCode, ErrorMsg: config.ErrParseToken.ErrMsg}, nil
|
||||
}
|
||||
friends, err := im_mysql_model.FindUserInfoFromFriend(claims.UID)
|
||||
if err != nil {
|
||||
log.Error(req.Token, req.OperationID, "err=%s search friendInfo failed", err.Error())
|
||||
return &pbFriend.GetFriendListResp{ErrorCode: config.ErrSearchUserInfo.ErrCode, ErrorMsg: config.ErrSearchUserInfo.ErrMsg}, nil
|
||||
}
|
||||
for _, friendUser := range friends {
|
||||
var friendUserInfo pbFriend.UserInfo
|
||||
|
||||
//find user is in blackList
|
||||
err = im_mysql_model.FindRelationshipFromBlackList(claims.UID, friendUser.FriendId)
|
||||
if err == nil {
|
||||
friendUserInfo.IsInBlackList = constant.BlackListFlag
|
||||
} else {
|
||||
friendUserInfo.IsInBlackList = 0
|
||||
}
|
||||
//Find user information
|
||||
us, err := im_mysql_model.FindUserByUID(friendUser.FriendId)
|
||||
if err != nil {
|
||||
log.Error(req.Token, req.OperationID, "err=%s search userInfo failed", err.Error())
|
||||
} else {
|
||||
friendUserInfo.Uid = friendUser.FriendId
|
||||
friendUserInfo.Comment = friendUser.Comment
|
||||
friendUserInfo.Icon = us.Icon
|
||||
friendUserInfo.Name = us.Name
|
||||
friendUserInfo.Gender = us.Gender
|
||||
friendUserInfo.Mobile = us.Mobile
|
||||
friendUserInfo.Birth = us.Birth
|
||||
friendUserInfo.Email = us.Email
|
||||
friendUserInfo.Ex = us.Ex
|
||||
|
||||
userInfoList = append(userInfoList, &friendUserInfo)
|
||||
}
|
||||
|
||||
}
|
||||
log.Info(req.Token, req.OperationID, fmt.Sprintf("rpc get friend list success return"))
|
||||
return &pbFriend.GetFriendListResp{Data: userInfoList}, nil
|
||||
}
|
22
src/rpc/friend/friend/is_in_blacklist.go
Normal file
22
src/rpc/friend/friend/is_in_blacklist.go
Normal file
@ -0,0 +1,22 @@
|
||||
package friend
|
||||
|
||||
import (
|
||||
"Open_IM/src/common/db/mysql_model/im_mysql_model"
|
||||
"Open_IM/src/common/log"
|
||||
pbFriend "Open_IM/src/proto/friend"
|
||||
"context"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func (s *friendServer) IsInBlackList(ctx context.Context, req *pbFriend.IsInBlackListReq) (*pbFriend.IsInBlackListResp, error) {
|
||||
log.InfoByArgs("rpc is in blacklist is server,args=%s", req.String())
|
||||
var isInBlacklist = false
|
||||
err := im_mysql_model.FindRelationshipFromBlackList(req.ReceiveUid, req.SendUid)
|
||||
if err != nil {
|
||||
log.Error("", req.OperationID, "err=%s,", err.Error())
|
||||
} else {
|
||||
isInBlacklist = true
|
||||
}
|
||||
log.InfoByArgs(fmt.Sprintf("rpc is in blackList success return"))
|
||||
return &pbFriend.IsInBlackListResp{Response: isInBlacklist}, nil
|
||||
}
|
27
src/rpc/friend/friend/remove_blacklist.go
Normal file
27
src/rpc/friend/friend/remove_blacklist.go
Normal file
@ -0,0 +1,27 @@
|
||||
package friend
|
||||
|
||||
import (
|
||||
"Open_IM/src/common/config"
|
||||
"Open_IM/src/common/db/mysql_model/im_mysql_model"
|
||||
"Open_IM/src/common/log"
|
||||
pbFriend "Open_IM/src/proto/friend"
|
||||
"Open_IM/src/utils"
|
||||
"context"
|
||||
)
|
||||
|
||||
func (s *friendServer) RemoveBlacklist(ctx context.Context, req *pbFriend.RemoveBlacklistReq) (*pbFriend.CommonResp, error) {
|
||||
log.Info(req.Token, req.OperationID, "rpc remove blacklist is server,userid=%s", req.Uid)
|
||||
//Parse token, to find current user information
|
||||
claims, err := utils.ParseToken(req.Token)
|
||||
if err != nil {
|
||||
log.Error(req.Token, req.OperationID, "err=%s,parse token failed", err.Error())
|
||||
return &pbFriend.CommonResp{ErrorCode: config.ErrParseToken.ErrCode, ErrorMsg: config.ErrParseToken.ErrMsg}, nil
|
||||
}
|
||||
err = im_mysql_model.RemoveBlackList(claims.UID, req.Uid)
|
||||
if err != nil {
|
||||
log.Error(req.Token, req.OperationID, "err=%s,remove blacklist failed", err.Error())
|
||||
return &pbFriend.CommonResp{ErrorCode: config.ErrMysql.ErrCode, ErrorMsg: config.ErrMysql.ErrMsg}, nil
|
||||
}
|
||||
log.Info(req.Token, req.OperationID, "rpc remove blacklist success return,userid=%s", req.Uid)
|
||||
return &pbFriend.CommonResp{}, nil
|
||||
}
|
111
src/rpc/friend/friend/search_friend.go
Normal file
111
src/rpc/friend/friend/search_friend.go
Normal file
@ -0,0 +1,111 @@
|
||||
package friend
|
||||
|
||||
import (
|
||||
"Open_IM/src/common/config"
|
||||
"Open_IM/src/common/constant"
|
||||
"Open_IM/src/common/db/mysql_model/im_mysql_model"
|
||||
"Open_IM/src/common/log"
|
||||
pbFriend "Open_IM/src/proto/friend"
|
||||
"Open_IM/src/utils"
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/skiffer-git/grpc-etcdv3/getcdv3"
|
||||
"google.golang.org/grpc"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type friendServer struct {
|
||||
rpcPort int
|
||||
rpcRegisterName string
|
||||
etcdSchema string
|
||||
etcdAddr []string
|
||||
}
|
||||
|
||||
func NewFriendServer(port int) *friendServer {
|
||||
return &friendServer{
|
||||
rpcPort: port,
|
||||
rpcRegisterName: config.Config.RpcRegisterName.OpenImFriendName,
|
||||
etcdSchema: config.Config.Etcd.EtcdSchema,
|
||||
etcdAddr: config.Config.Etcd.EtcdAddr,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *friendServer) Run() {
|
||||
log.Info("", "", fmt.Sprintf("rpc friend init...."))
|
||||
|
||||
ip := utils.ServerIP
|
||||
registerAddress := ip + ":" + strconv.Itoa(s.rpcPort)
|
||||
//listener network
|
||||
listener, err := net.Listen("tcp", registerAddress)
|
||||
if err != nil {
|
||||
log.InfoByArgs(fmt.Sprintf("Failed to listen rpc friend network,err=%s", err.Error()))
|
||||
return
|
||||
}
|
||||
log.Info("", "", "listen network success, address = %s", registerAddress)
|
||||
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 {
|
||||
log.ErrorByArgs("register rpc fiend service to etcd failed,err=%s", err.Error())
|
||||
return
|
||||
}
|
||||
err = srv.Serve(listener)
|
||||
if err != nil {
|
||||
log.ErrorByArgs("listen rpc friend error,err=%s", err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (s *friendServer) SearchFriend(ctx context.Context, req *pbFriend.SearchFriendReq) (*pbFriend.SearchFriendResp, error) {
|
||||
log.Info(req.Token, req.OperationID, fmt.Sprintf("rpc search user is server,args=%s", req.String()))
|
||||
var (
|
||||
isInBlackList int32
|
||||
isFriend int32
|
||||
comment string
|
||||
)
|
||||
//Parse token, to find current user information
|
||||
claims, err := utils.ParseToken(req.Token)
|
||||
if err != nil {
|
||||
log.Error(req.Token, req.OperationID, "err=%s,parse token failed", err.Error())
|
||||
return &pbFriend.SearchFriendResp{ErrorCode: config.ErrParseToken.ErrCode, ErrorMsg: config.ErrParseToken.ErrMsg}, nil
|
||||
}
|
||||
friendShip, err := im_mysql_model.FindFriendRelationshipFromFriend(claims.UID, req.Uid)
|
||||
if err == nil {
|
||||
isFriend = constant.FriendFlag
|
||||
comment = friendShip.Comment
|
||||
}
|
||||
friendUserInfo, err := im_mysql_model.FindUserByUID(req.Uid)
|
||||
if err != nil {
|
||||
log.Error(req.Token, req.OperationID, "err=%s,no this user", err.Error())
|
||||
return &pbFriend.SearchFriendResp{ErrorCode: config.ErrSearchUserInfo.ErrCode, ErrorMsg: config.ErrSearchUserInfo.ErrMsg}, nil
|
||||
}
|
||||
err = im_mysql_model.FindRelationshipFromBlackList(claims.UID, req.Uid)
|
||||
if err == nil {
|
||||
isInBlackList = constant.BlackListFlag
|
||||
}
|
||||
log.Info(req.Token, req.OperationID, "rpc search friend success return")
|
||||
return &pbFriend.SearchFriendResp{
|
||||
ErrorCode: 0,
|
||||
ErrorMsg: "",
|
||||
Data: &pbFriend.SearchFriendData{
|
||||
Uid: friendUserInfo.UID,
|
||||
Icon: friendUserInfo.Icon,
|
||||
Name: friendUserInfo.Name,
|
||||
Gender: friendUserInfo.Gender,
|
||||
Mobile: friendUserInfo.Mobile,
|
||||
Birth: friendUserInfo.Birth,
|
||||
Email: friendUserInfo.Email,
|
||||
Ex: friendUserInfo.Ex,
|
||||
Comment: comment,
|
||||
IsFriend: isFriend,
|
||||
IsInBlackList: isInBlackList,
|
||||
},
|
||||
}, nil
|
||||
|
||||
}
|
27
src/rpc/friend/friend/set_friend_comment.go
Normal file
27
src/rpc/friend/friend/set_friend_comment.go
Normal file
@ -0,0 +1,27 @@
|
||||
package friend
|
||||
|
||||
import (
|
||||
"Open_IM/src/common/config"
|
||||
"Open_IM/src/common/db/mysql_model/im_mysql_model"
|
||||
"Open_IM/src/common/log"
|
||||
pbFriend "Open_IM/src/proto/friend"
|
||||
"Open_IM/src/utils"
|
||||
"context"
|
||||
)
|
||||
|
||||
func (s *friendServer) SetFriendComment(ctx context.Context, req *pbFriend.SetFriendCommentReq) (*pbFriend.CommonResp, error) {
|
||||
log.Info(req.Token, req.OperationID, "rpc set friend comment is server,params=%s", req.String())
|
||||
//Parse token, to find current user information
|
||||
claims, err := utils.ParseToken(req.Token)
|
||||
if err != nil {
|
||||
log.Error(req.Token, req.OperationID, "err=%s,parse token failed", err.Error())
|
||||
return &pbFriend.CommonResp{ErrorCode: config.ErrParseToken.ErrCode, ErrorMsg: config.ErrParseToken.ErrMsg}, nil
|
||||
}
|
||||
err = im_mysql_model.UpdateFriendComment(claims.UID, req.Uid, req.Comment)
|
||||
if err != nil {
|
||||
log.Error(req.Token, req.OperationID, "set friend comment failed,err=%s", err.Error())
|
||||
return &pbFriend.CommonResp{ErrorCode: config.ErrSetFriendComment.ErrCode, ErrorMsg: config.ErrSetFriendComment.ErrMsg}, nil
|
||||
}
|
||||
log.Info(req.Token, req.OperationID, "rpc set friend comment is success return")
|
||||
return &pbFriend.CommonResp{}, nil
|
||||
}
|
14
src/rpc/friend/open_im_friend.go
Normal file
14
src/rpc/friend/open_im_friend.go
Normal file
@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"Open_IM/src/rpc/friend/friend"
|
||||
"flag"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
rpcPort := flag.Int("port", 10200, "get RpcFriendPort from cmd,default 12000 as port")
|
||||
flag.Parse()
|
||||
rpcServer := friend.NewFriendServer(*rpcPort)
|
||||
rpcServer.Run()
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user