89 lines
2.3 KiB
Go
89 lines
2.3 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// GetAllConversations returns all conversations (map of accountID -> messages).
|
|
func (h *AdminHandler) GetAllConversations(c *gin.Context) {
|
|
if !h.requireAdmin(c) {
|
|
return
|
|
}
|
|
convs, err := h.chatStore.ListConversations()
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"data": gin.H{"conversations": convs}})
|
|
}
|
|
|
|
// GetConversation returns all messages for a specific account.
|
|
func (h *AdminHandler) GetConversation(c *gin.Context) {
|
|
if !h.requireAdmin(c) {
|
|
return
|
|
}
|
|
accountID := c.Param("account")
|
|
if accountID == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "missing account"})
|
|
return
|
|
}
|
|
msgs, err := h.chatStore.GetMessages(accountID)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"data": gin.H{"messages": msgs}})
|
|
}
|
|
|
|
type adminChatPayload struct {
|
|
Content string `json:"content"`
|
|
}
|
|
|
|
// AdminReply sends a reply from admin to a specific user.
|
|
func (h *AdminHandler) AdminReply(c *gin.Context) {
|
|
if !h.requireAdmin(c) {
|
|
return
|
|
}
|
|
accountID := c.Param("account")
|
|
if accountID == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "missing account"})
|
|
return
|
|
}
|
|
var payload adminChatPayload
|
|
if err := c.ShouldBindJSON(&payload); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid payload"})
|
|
return
|
|
}
|
|
content := strings.TrimSpace(payload.Content)
|
|
if content == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "消息不能为空"})
|
|
return
|
|
}
|
|
msg, err := h.chatStore.SendAdminMessage(accountID, content)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"data": gin.H{"message": msg}})
|
|
}
|
|
|
|
// ClearConversation deletes all messages with a specific user.
|
|
func (h *AdminHandler) ClearConversation(c *gin.Context) {
|
|
if !h.requireAdmin(c) {
|
|
return
|
|
}
|
|
accountID := c.Param("account")
|
|
if accountID == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "missing account"})
|
|
return
|
|
}
|
|
if err := h.chatStore.ClearConversation(accountID); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"data": gin.H{"ok": true}})
|
|
}
|