From fe8b3df1635cfb248c2c3eadfe2456ef32aa0eb4 Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 17 Dec 2024 10:36:03 +0800 Subject: [PATCH] Add chat deletion endpoints and handlers in Neo API - Introduced new API endpoints for deleting individual chats and clearing all chats for a user, enhancing chat management capabilities. - Implemented handleChatDelete and handleChatsDeleteAll methods to process deletion requests, including error handling for missing session and chat IDs. - Updated API router to register new endpoints under the 'dangerous' path, ensuring clear separation of critical operations. - Enhanced overall API structure and maintainability by adding robust feedback mechanisms for deletion operations. --- neo/api.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/neo/api.go b/neo/api.go index 6a214fbe..58320cc5 100644 --- a/neo/api.go +++ b/neo/api.go @@ -35,6 +35,7 @@ func (neo *DSL) API(router *gin.Engine, path string) error { router.OPTIONS(path+"/upload", neo.optionsHandler) router.OPTIONS(path+"/download", neo.optionsHandler) router.OPTIONS(path+"/mentions", neo.optionsHandler) + router.OPTIONS(path+"/dangerous/clear_chats", neo.optionsHandler) // Register endpoints with middlewares router.GET(path, append(middlewares, neo.handleChat)...) @@ -47,6 +48,7 @@ func (neo *DSL) API(router *gin.Engine, path string) error { router.GET(path+"/chats", append(middlewares, neo.handleChatList)...) router.GET(path+"/chats/:id", append(middlewares, neo.handleChatDetail)...) router.POST(path+"/chats/:id", append(middlewares, neo.handleChatUpdate)...) + router.DELETE(path+"/chats/:id", append(middlewares, neo.handleChatDelete)...) // History api router.GET(path+"/history", append(middlewares, neo.handleChatHistory)...) @@ -58,6 +60,9 @@ func (neo *DSL) API(router *gin.Engine, path string) error { // Mention api router.GET(path+"/mentions", append(middlewares, neo.handleMentions)...) + // Dangerous operations + router.DELETE(path+"/dangerous/clear_chats", append(middlewares, neo.handleChatsDeleteAll)...) + return nil } @@ -439,3 +444,50 @@ func (neo *DSL) handleChatUpdate(c *gin.Context) { c.JSON(200, gin.H{"message": "ok", "title": body.Title, "chat_id": chatID}) c.Done() } + +// handleChatDelete handles deleting a single chat +func (neo *DSL) handleChatDelete(c *gin.Context) { + sid := c.GetString("__sid") + if sid == "" { + c.JSON(400, gin.H{"message": "sid is required", "code": 400}) + c.Done() + return + } + + chatID := c.Param("id") + if chatID == "" { + c.JSON(400, gin.H{"message": "chat id is required", "code": 400}) + c.Done() + return + } + + err := neo.Conversation.DeleteChat(sid, chatID) + if err != nil { + c.JSON(500, gin.H{"message": err.Error(), "code": 500}) + c.Done() + return + } + + c.JSON(200, gin.H{"message": "ok"}) + c.Done() +} + +// handleChatsDeleteAll handles deleting all chats for a user +func (neo *DSL) handleChatsDeleteAll(c *gin.Context) { + sid := c.GetString("__sid") + if sid == "" { + c.JSON(400, gin.H{"message": "sid is required", "code": 400}) + c.Done() + return + } + + err := neo.Conversation.DeleteAllChats(sid) + if err != nil { + c.JSON(500, gin.H{"message": err.Error(), "code": 500}) + c.Done() + return + } + + c.JSON(200, gin.H{"message": "ok"}) + c.Done() +}