mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-17 05:07:23 +08:00
feat(go-api) sessions message update (#16517)
### Summary ``` /api/v1/chats/<chat_id>/sessions/<session_id>/messages/<msg_id> DELETE /api/v1/chats/<chat_id>/sessions/<session_id>/messages/<msg_id>/feedback PUT ``` Migrates the chat session message delete and feedback APIs to the Go server, matching the Python behavior for authorization, session ownership checks, message/reference updates, and feedback validation.
This commit is contained in:
@@ -398,3 +398,60 @@ func (h *ChatSessionHandler) UpdateSession(c *gin.Context) {
|
||||
}
|
||||
jsonResponse(c, common.CodeSuccess, result, "success")
|
||||
}
|
||||
|
||||
func (h *ChatSessionHandler) DeleteSessionMessage(c *gin.Context) {
|
||||
user, errorCode, errorMessage := GetUser(c)
|
||||
if errorCode != common.CodeSuccess {
|
||||
jsonError(c, errorCode, errorMessage)
|
||||
return
|
||||
}
|
||||
userID := user.ID
|
||||
chatID, sessionID, msgID := c.Param("chat_id"), c.Param("session_id"), c.Param("msg_id")
|
||||
|
||||
result, code, err := h.chatSessionService.DeleteSessionMessage(userID, chatID, sessionID, msgID)
|
||||
if err != nil {
|
||||
if code == common.CodeAuthenticationError {
|
||||
jsonResponse(c, code, false, err.Error())
|
||||
return
|
||||
}
|
||||
jsonError(c, code, err.Error())
|
||||
return
|
||||
}
|
||||
jsonResponse(c, common.CodeSuccess, result, "success")
|
||||
}
|
||||
|
||||
func (h *ChatSessionHandler) UpdateMessageFeedback(c *gin.Context) {
|
||||
user, errorCode, errorMessage := GetUser(c)
|
||||
if errorCode != common.CodeSuccess {
|
||||
jsonError(c, errorCode, errorMessage)
|
||||
return
|
||||
}
|
||||
|
||||
userID := user.ID
|
||||
chatID, sessionID, msgID := c.Param("chat_id"), c.Param("session_id"), c.Param("msg_id")
|
||||
|
||||
req := map[string]interface{}{}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
if errors.Is(err, io.EOF) {
|
||||
jsonError(c, common.CodeArgumentError, "Request body cannot be empty")
|
||||
return
|
||||
}
|
||||
jsonError(c, common.CodeArgumentError, "Invalid request: "+err.Error())
|
||||
return
|
||||
}
|
||||
if len(req) == 0 {
|
||||
jsonError(c, common.CodeArgumentError, "Request body cannot be empty")
|
||||
return
|
||||
}
|
||||
|
||||
result, code, err := h.chatSessionService.UpdateMessageFeedback(c.Request.Context(), userID, chatID, sessionID, msgID, req)
|
||||
if err != nil {
|
||||
if code == common.CodeAuthenticationError {
|
||||
jsonResponse(c, code, false, err.Error())
|
||||
return
|
||||
}
|
||||
jsonError(c, code, err.Error())
|
||||
return
|
||||
}
|
||||
jsonResponse(c, common.CodeSuccess, result, "success")
|
||||
}
|
||||
|
||||
@@ -72,3 +72,64 @@ func TestChatSessionHandlerUpdateSession_RejectsEmptyJSONObject(t *testing.T) {
|
||||
t.Fatalf("message=%v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatSessionHandlerUpdateMessageFeedback_RejectsEmptyBody(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
ctx, _ := gin.CreateTestContext(recorder)
|
||||
ctx.Request = httptest.NewRequest(http.MethodPut, "/api/v1/chats/chat-1/sessions/session-1/messages/msg-1/feedback", nil)
|
||||
ctx.Params = gin.Params{
|
||||
{Key: "chat_id", Value: "chat-1"},
|
||||
{Key: "session_id", Value: "session-1"},
|
||||
{Key: "msg_id", Value: "msg-1"},
|
||||
}
|
||||
ctx.Set("user", &entity.User{ID: "user-1"})
|
||||
|
||||
handler := NewChatSessionHandler(service.NewChatSessionService(), nil)
|
||||
handler.UpdateMessageFeedback(ctx)
|
||||
|
||||
if recorder.Code != http.StatusOK {
|
||||
t.Fatalf("status=%d", recorder.Code)
|
||||
}
|
||||
|
||||
var body map[string]interface{}
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &body); err != nil {
|
||||
t.Fatalf("decode response body: %v", err)
|
||||
}
|
||||
if got := body["code"]; got != float64(common.CodeArgumentError) {
|
||||
t.Fatalf("code=%v", got)
|
||||
}
|
||||
if got := body["message"]; got != "Request body cannot be empty" {
|
||||
t.Fatalf("message=%v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatSessionHandlerUpdateMessageFeedback_RejectsEmptyJSONObject(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
ctx, _ := gin.CreateTestContext(recorder)
|
||||
ctx.Request = httptest.NewRequest(http.MethodPut, "/api/v1/chats/chat-1/sessions/session-1/messages/msg-1/feedback", strings.NewReader(`{}`))
|
||||
ctx.Request.Header.Set("Content-Type", "application/json")
|
||||
ctx.Params = gin.Params{
|
||||
{Key: "chat_id", Value: "chat-1"},
|
||||
{Key: "session_id", Value: "session-1"},
|
||||
{Key: "msg_id", Value: "msg-1"},
|
||||
}
|
||||
ctx.Set("user", &entity.User{ID: "user-1"})
|
||||
|
||||
handler := NewChatSessionHandler(service.NewChatSessionService(), nil)
|
||||
handler.UpdateMessageFeedback(ctx)
|
||||
|
||||
var body map[string]interface{}
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &body); err != nil {
|
||||
t.Fatalf("decode response body: %v", err)
|
||||
}
|
||||
if got := body["code"]; got != float64(common.CodeArgumentError) {
|
||||
t.Fatalf("code=%v", got)
|
||||
}
|
||||
if got := body["message"]; got != "Request body cannot be empty" {
|
||||
t.Fatalf("message=%v", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user