From b699fd899a0f8da9800df8715602e638496fd944 Mon Sep 17 00:00:00 2001 From: away <1819625428@qq.com> Date: Mon, 28 Jun 2021 15:27:20 +0800 Subject: [PATCH] friend api --- src/api/friend/add_friend_response.go | 4 +- src/api/friend/get_friend_apply_list.go | 54 ++++++++++++++++++- src/api/friend/get_friends_info.go | 69 +++++++++++++++++++++++++ src/api/friend/is_friend.go | 47 +++++++++++++++++ 4 files changed, 170 insertions(+), 4 deletions(-) create mode 100644 src/api/friend/get_friends_info.go create mode 100644 src/api/friend/is_friend.go diff --git a/src/api/friend/add_friend_response.go b/src/api/friend/add_friend_response.go index 92c77f51f..9c4992561 100644 --- a/src/api/friend/add_friend_response.go +++ b/src/api/friend/add_friend_response.go @@ -30,14 +30,14 @@ func AddFriendResponse(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) return } - req := &pbFriend.AddedFriendReq{ + req := &pbFriend.AddFriendResponseReq{ Uid: params.UID, Flag: params.Flag, OperationID: params.OperationID, Token: c.Request.Header.Get("token"), } log.Info(req.Token, req.OperationID, "api add friend response is server:userID=%s", req.Uid) - RpcResp, err := client.AddedFriend(context.Background(), req) + RpcResp, err := client.AddFriendResponse(context.Background(), req) if err != nil { log.Error(req.Token, req.OperationID, "err=%s,call add_friend_response rpc server failed", err) c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "call add_friend_response rpc server failed"}) diff --git a/src/api/friend/get_friend_apply_list.go b/src/api/friend/get_friend_apply_list.go index d51d20c62..4a9911cd6 100644 --- a/src/api/friend/get_friend_apply_list.go +++ b/src/api/friend/get_friend_apply_list.go @@ -11,7 +11,7 @@ import ( "strings" ) -type paramsGetFriendApplyList struct { +type paramsGetApplyList struct { OperationID string `json:"operationID" binding:"required"` } type UserInfo struct { @@ -35,7 +35,7 @@ func GetFriendApplyList(c *gin.Context) { client := pbFriend.NewFriendClient(etcdConn) defer etcdConn.Close() - params := paramsGetFriendApplyList{} + params := paramsGetApplyList{} if err := c.BindJSON(¶ms); err != nil { c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) return @@ -77,3 +77,53 @@ func GetFriendApplyList(c *gin.Context) { } log.InfoByArgs("api get friend apply list success return,get args=%s,return args=%s", req.String(), RpcResp.String()) } + +func GetSelfApplyList(c *gin.Context) { + log.Info("", "", "api get self friend apply list init ....") + + etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImFriendName) + client := pbFriend.NewFriendClient(etcdConn) + defer etcdConn.Close() + + params := paramsGetApplyList{} + if err := c.BindJSON(¶ms); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) + return + } + req := &pbFriend.GetFriendApplyReq{ + OperationID: params.OperationID, + Token: c.Request.Header.Get("token"), + } + log.Info(req.Token, req.OperationID, "api get self apply list is server") + RpcResp, err := client.GetSelfApplyList(context.Background(), req) + if err != nil { + log.Error(req.Token, req.OperationID, "err=%s,call get self apply list rpc server failed", err) + c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "call get self apply list rpc server failed"}) + return + } + log.InfoByArgs("call get self apply list rpc server success,args=%s", RpcResp.String()) + if RpcResp.ErrorCode == 0 { + userInfoList := make([]UserInfo, 0) + for _, selfApplyOtherUserinfo := range RpcResp.Data { + var un UserInfo + un.UID = selfApplyOtherUserinfo.Uid + un.Name = selfApplyOtherUserinfo.Name + un.Icon = selfApplyOtherUserinfo.Icon + un.Gender = selfApplyOtherUserinfo.Gender + un.Mobile = selfApplyOtherUserinfo.Mobile + un.Birth = selfApplyOtherUserinfo.Birth + un.Email = selfApplyOtherUserinfo.Email + un.Ex = selfApplyOtherUserinfo.Ex + un.Flag = selfApplyOtherUserinfo.Flag + un.ApplyTime = selfApplyOtherUserinfo.ApplyTime + un.ReqMessage = selfApplyOtherUserinfo.ReqMessage + userInfoList = append(userInfoList, un) + } + resp := gin.H{"errCode": RpcResp.ErrorCode, "errMsg": RpcResp.ErrorMsg, "data": userInfoList} + c.JSON(http.StatusOK, resp) + } else { + resp := gin.H{"errCode": RpcResp.ErrorCode, "errMsg": RpcResp.ErrorMsg} + c.JSON(http.StatusOK, resp) + } + log.InfoByArgs("api get self apply list success return,get args=%s,return args=%s", req.String(), RpcResp.String()) +} diff --git a/src/api/friend/get_friends_info.go b/src/api/friend/get_friends_info.go new file mode 100644 index 000000000..f41513de0 --- /dev/null +++ b/src/api/friend/get_friends_info.go @@ -0,0 +1,69 @@ +package friend + +import ( + "Open_IM/src/common/config" + "Open_IM/src/common/log" + pbFriend "Open_IM/src/proto/friend" + "context" + "fmt" + "github.com/gin-gonic/gin" + "github.com/skiffer-git/grpc-etcdv3/getcdv3" + "net/http" + "strings" +) + +type paramsSearchFriend struct { + OperationID string `json:"operationID" binding:"required"` + UID string `json:"uid" binding:"required"` +} + +func GetFriendsInfo(c *gin.Context) { + log.Info("", "", fmt.Sprintf("api search friend init ....")) + etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImFriendName) + client := pbFriend.NewFriendClient(etcdConn) + defer etcdConn.Close() + + params := paramsSearchFriend{} + if err := c.BindJSON(¶ms); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) + return + } + req := &pbFriend.GetFriendsInfoReq{ + Uid: params.UID, + OperationID: params.OperationID, + Token: c.Request.Header.Get("token"), + } + log.Info(req.Token, req.OperationID, "api search_friend is server") + RpcResp, err := client.GetFriendsInfo(context.Background(), req) + if err != nil { + log.Error(req.Token, req.OperationID, "err=%s,call search friend rpc server failed", err) + c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "call search friend rpc server failed"}) + return + } + log.InfoByArgs("call search friend rpc server success,args=%s", RpcResp.String()) + if RpcResp.ErrorCode == 0 { + resp := gin.H{ + "errCode": RpcResp.ErrorCode, + "errMsg": RpcResp.ErrorMsg, + "data": gin.H{ + "uid": RpcResp.Data.Uid, + "icon": RpcResp.Data.Icon, + "name": RpcResp.Data.Name, + "gender": RpcResp.Data.Gender, + "mobile": RpcResp.Data.Mobile, + "birth": RpcResp.Data.Birth, + "email": RpcResp.Data.Email, + "ex": RpcResp.Data.Ex, + "comment": RpcResp.Data.Comment, + }, + } + c.JSON(http.StatusOK, resp) + } else { + resp := gin.H{ + "errCode": RpcResp.ErrorCode, + "errMsg": RpcResp.ErrorMsg, + } + c.JSON(http.StatusOK, resp) + } + log.InfoByArgs("api search_friend success return,get args=%s,return=%s", req.String(), RpcResp.String()) +} diff --git a/src/api/friend/is_friend.go b/src/api/friend/is_friend.go new file mode 100644 index 000000000..611a9cb24 --- /dev/null +++ b/src/api/friend/is_friend.go @@ -0,0 +1,47 @@ +package friend + +import ( + "Open_IM/src/common/config" + "Open_IM/src/common/log" + pbFriend "Open_IM/src/proto/friend" + "context" + "github.com/gin-gonic/gin" + "github.com/skiffer-git/grpc-etcdv3/getcdv3" + "net/http" + "strings" +) + +type paramsIsFriend struct { + OperationID string `json:"operationID" binding:"required"` + ReceiveUid string `json:"receive_uid"` +} + +func IsFriend(c *gin.Context) { + log.Info("", "", "api is friend init....") + + etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImFriendName) + client := pbFriend.NewFriendClient(etcdConn) + defer etcdConn.Close() + + params := paramsIsFriend{} + if err := c.BindJSON(¶ms); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) + return + } + req := &pbFriend.IsFriendReq{ + OperationID: params.OperationID, + ReceiveUid: params.OperationID, + Token: c.Request.Header.Get("token"), + } + log.Info(req.Token, req.OperationID, "api is friend is server") + RpcResp, err := client.IsFriend(context.Background(), req) + if err != nil { + log.Error(req.Token, req.OperationID, "err=%s,call add friend rpc server failed", err) + c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "call add friend rpc server failed"}) + return + } + log.InfoByArgs("call is friend rpc server success,args=%s", RpcResp.String()) + resp := gin.H{"errCode": RpcResp.ErrorCode, "errMsg": RpcResp.ErrorMsg, "isFriend": RpcResp.ShipType} + c.JSON(http.StatusOK, resp) + log.InfoByArgs("api is friend success return,get args=%s,return args=%s", req.String(), RpcResp.String()) +}