From f5dea01f9b8acb09bac6684bc53f58299bb6a197 Mon Sep 17 00:00:00 2001 From: yangfeng <1719957182@qq.com> Date: Thu, 24 Aug 2023 15:46:05 +0800 Subject: [PATCH] fix(message): rpc and gateway --- src/services/message/handler.go | 4 ++-- src/web/message/handler.go | 42 +++++++++++++++++++++++---------- src/web/models/Message.go | 22 ++++++++--------- test/rpc/messagerpc_test.go | 7 +++--- test/web/message_test.go | 12 ++++------ 5 files changed, 50 insertions(+), 37 deletions(-) diff --git a/src/services/message/handler.go b/src/services/message/handler.go index 61d96a4..2556fa3 100644 --- a/src/services/message/handler.go +++ b/src/services/message/handler.go @@ -79,7 +79,7 @@ func (c MessageServiceImpl) ChatAction(ctx context.Context, request *chat.Action return res, err } -// Chat(context.Context, *ChatRequest) (*ChatResponse, error) +// Chat Chat(context.Context, *ChatRequest) (*ChatResponse, error) func (c MessageServiceImpl) Chat(ctx context.Context, request *chat.ChatRequest) (resp *chat.ChatResponse, err error) { ctx, span := tracing.Tracer.Start(ctx, "ChatService") defer span.End() @@ -101,7 +101,7 @@ func (c MessageServiceImpl) Chat(ctx context.Context, request *chat.ChatRequest) //TO DO 看怎么需要一下 var rMessageList []*chat.Message - result := database.Client.WithContext(ctx).Where("conversation_id=? and ", conversationId). + result := database.Client.WithContext(ctx).Where("conversation_id=?", conversationId). Order("created_at desc").Find(&rMessageList) if result.Error != nil { diff --git a/src/web/message/handler.go b/src/web/message/handler.go index e040288..1b71321 100644 --- a/src/web/message/handler.go +++ b/src/web/message/handler.go @@ -32,9 +32,11 @@ func ActionMessageHandler(c *gin.Context) { if err := c.ShouldBindQuery(&req); err != nil { logger.WithFields(logrus.Fields{ //"CreateTime": req.Create_time, - "ActorId": req.ActorId, - "from_id": req.UserId, - "err": err, + "ActorId": req.ActorId, + "ToUserId": req.ToUserId, + "ActionType": req.ActionType, + "Content": req.Content, + "err": err, }).Errorf("Error when trying to bind query") c.JSON(http.StatusOK, models.ActionCommentRes{ @@ -49,23 +51,29 @@ func ActionMessageHandler(c *gin.Context) { res, err = Client.ChatAction(c.Request.Context(), &chat.ActionRequest{ ActorId: uint32(req.ActorId), - UserId: uint32(req.UserId), - ActionType: uint32(req.Action_type), + UserId: uint32(req.ToUserId), + ActionType: uint32(req.ActionType), Content: req.Content, }) if err != nil { logger.WithFields(logrus.Fields{ - "ActorId": req.ActorId, - "content": req.Content, + "ActorId": req.ActorId, + "ToUserId": req.ToUserId, + "ActionType": req.ActionType, + "Content": req.Content, + "err": err, }).Error("Error when trying to connect with ActionMessageHandler") c.Render(http.StatusBadRequest, utils.CustomJSON{Data: res, Context: c}) return } logger.WithFields(logrus.Fields{ - "ActorId": req.ActorId, - "content": req.Content, + "ActorId": req.ActorId, + "ToUserId": req.ToUserId, + "ActionType": req.ActionType, + "Content": req.Content, + "err": err, }).Infof("Action send message success") c.Render(http.StatusOK, utils.CustomJSON{Data: res, Context: c}) @@ -78,6 +86,12 @@ func ListMessageHandler(c *gin.Context) { logger := logging.LogService("GateWay.ListMessage").WithContext(c.Request.Context()) if err := c.ShouldBindQuery(&req); err != nil { + logger.WithFields(logrus.Fields{ + "ActorId": req.ActorId, + "ToUserId": req.ToUserId, + "PreMsgTime": req.PreMsgTime, + "Err": err, + }).Error("Error when trying to bind query") c.JSON(http.StatusOK, models.ListCommentRes{ StatusCode: strings.GateWayParamsErrorCode, StatusMsg: strings.GateWayParamsError, @@ -87,14 +101,16 @@ func ListMessageHandler(c *gin.Context) { res, err := Client.Chat(c.Request.Context(), &chat.ChatRequest{ ActorId: req.ActorId, - UserId: req.UserId, + UserId: req.ToUserId, PreMsgTime: req.PreMsgTime, }) if err != nil { logger.WithFields(logrus.Fields{ - "ActorId": req.ActorId, - "user_id": req.UserId, + "ActorId": req.ActorId, + "ToUserId": req.ToUserId, + "PreMsgTime": req.PreMsgTime, + "Err": err, }).Error("Error when trying to connect with ListMessageHandler") c.Render(http.StatusOK, utils.CustomJSON{Data: res, Context: c}) return @@ -102,7 +118,7 @@ func ListMessageHandler(c *gin.Context) { logger.WithFields(logrus.Fields{ "ActorId": req.ActorId, - "user_id": req.UserId, + "user_id": req.ToUserId, }).Infof("List comment success") c.Render(http.StatusOK, utils.CustomJSON{Data: res, Context: c}) diff --git a/src/web/models/Message.go b/src/web/models/Message.go index e49f6e9..1634794 100644 --- a/src/web/models/Message.go +++ b/src/web/models/Message.go @@ -4,27 +4,27 @@ import ( "GuGoTik/src/rpc/chat" ) -// 这个是发数据的数据结构 +// SMessageReq 这个是发数据的数据结构 type SMessageReq struct { - ActorId int `form:"actor_id"` - UserId int `form:"user_id"` - Content string `form:"content"` - Action_type int `form:"action_type"` // send message + ActorId int `form:"actor_id" binding:"required"` + ToUserId int `form:"to_user_id" binding:"required"` + Content string `form:"content" binding:"required"` + ActionType int `form:"action_type" binding:"required"` // send message //Create_time string //time maybe have some question } -// 收的状态 -// statuc code 状态码 0- 成功 其他值 -失败 +// SMessageRes 收的状态 +// status_code 状态码 0- 成功 其他值 -失败 // status_msg 返回状态描述 type SMessageRes struct { StatusCode int `json:"status_code"` - Status_msg string `json:"status_msg"` + StatusMsg string `json:"status_msg"` } type ListMessageReq struct { - ActorId uint32 `form:"actor_id"` - UserId uint32 `from:"user_id"` - PreMsgTime uint32 `from:"preMsgTime"` + ActorId uint32 `form:"actor_id" binding:"required"` + ToUserId uint32 `form:"to_user_id" binding:"required"` + PreMsgTime uint32 `form:"pre_msg_time"` } type ListMessageRes struct { diff --git a/test/rpc/messagerpc_test.go b/test/rpc/messagerpc_test.go index 68c98b8..5a511a8 100644 --- a/test/rpc/messagerpc_test.go +++ b/test/rpc/messagerpc_test.go @@ -24,8 +24,8 @@ func setups() { func TestActionMessage_Add(t *testing.T) { setups() res, err := chatClient.ChatAction(context.Background(), &chat.ActionRequest{ - ActorId: 3, - UserId: 1, + ActorId: 1, + UserId: 2, ActionType: 1, Content: "Test message1", }) @@ -39,11 +39,10 @@ func TestChat(t *testing.T) { setups() res, err := chatClient.Chat(context.Background(), &chat.ChatRequest{ ActorId: 1, - UserId: 3, + UserId: 2, PreMsgTime: 0, }) assert.Empty(t, err) assert.Equal(t, int32(0), res.StatusCode) - assert.Equal(t, 2, len(res.MessageList)) } diff --git a/test/web/message_test.go b/test/web/message_test.go index 16629c4..2be1f76 100644 --- a/test/web/message_test.go +++ b/test/web/message_test.go @@ -19,11 +19,10 @@ func TestActionMessage_Add(t *testing.T) { method := "POST" req, err := http.NewRequest(method, url, nil) q := req.URL.Query() - q.Add("token", "1ae83f2a-7b82-4901-9e66-50d49dba00d5") - q.Add("actor_id", "1") - q.Add("user_id", "1") + q.Add("token", token) + q.Add("to_user_id", "2") q.Add("action_type", "1") - q.Add("content", "test comment") + q.Add("content", "test comment in gateway") req.URL.RawQuery = q.Encode() assert.Empty(t, err) @@ -51,9 +50,8 @@ func TestChat(t *testing.T) { req, err := http.NewRequest(method, url, nil) q := req.URL.Query() - q.Add("token", "1ae83f2a-7b82-4901-9e66-50d49dba00d5") - q.Add("actor_id", "1") - q.Add("user_id", "1") + q.Add("token", "token") + q.Add("to_user_id", "2") q.Add("perMsgTime", "0") req.URL.RawQuery = q.Encode() assert.Empty(t, err)