Skip to content

Commit

Permalink
fix(message): rpc and gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
Maple-pro committed Aug 24, 2023
1 parent b8ab746 commit f5dea01
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/services/message/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 {
Expand Down
42 changes: 29 additions & 13 deletions src/web/message/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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})
Expand All @@ -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,
Expand All @@ -87,22 +101,24 @@ 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
}

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})
Expand Down
22 changes: 11 additions & 11 deletions src/web/models/Message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 3 additions & 4 deletions test/rpc/messagerpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
})
Expand All @@ -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))
}
12 changes: 5 additions & 7 deletions test/web/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f5dea01

Please sign in to comment.