186 lines
4.8 KiB
Go
186 lines
4.8 KiB
Go
package moderation
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// ModerationHandler manages HTTP requests for content moderation
|
|
type ModerationHandler struct {
|
|
service *ModerationService
|
|
}
|
|
|
|
// NewModerationHandler creates a new moderation handler
|
|
func NewModerationHandler(service *ModerationService) *ModerationHandler {
|
|
return &ModerationHandler{
|
|
service: service,
|
|
}
|
|
}
|
|
|
|
// HandleFlagContent handles the flag-content endpoint
|
|
func (h *ModerationHandler) HandleFlagContent(c *gin.Context) {
|
|
var request FlagContentRequest
|
|
|
|
// Parse the request body
|
|
if err := c.ShouldBindJSON(&request); err != nil {
|
|
c.JSON(http.StatusBadRequest, FlagContentResponse{
|
|
Success: false,
|
|
Message: "Invalid request format",
|
|
})
|
|
return
|
|
}
|
|
|
|
// Flag the content
|
|
flag, err := h.service.FlagContent(request.ContentID, request.Reason, request.FlaggerAddress)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, FlagContentResponse{
|
|
Success: false,
|
|
Message: "Error flagging content: " + err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
// Return success response
|
|
c.JSON(http.StatusOK, FlagContentResponse{
|
|
Success: true,
|
|
Message: "Content flagged successfully",
|
|
})
|
|
}
|
|
|
|
// HandleModerationQueue handles the moderation-queue endpoint
|
|
func (h *ModerationHandler) HandleModerationQueue(c *gin.Context) {
|
|
// Get the moderation queue
|
|
queue, err := h.service.GetModerationQueue()
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"success": false,
|
|
"error": "Error getting moderation queue: " + err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
// Return the queue
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": true,
|
|
"queue": queue,
|
|
})
|
|
}
|
|
|
|
// HandleModerateContent handles the moderate-content endpoint
|
|
func (h *ModerationHandler) HandleModerateContent(c *gin.Context) {
|
|
var request ModerateContentRequest
|
|
|
|
// Parse the request body
|
|
if err := c.ShouldBindJSON(&request); err != nil {
|
|
c.JSON(http.StatusBadRequest, ModerateContentResponse{
|
|
Success: false,
|
|
Message: "Invalid request format",
|
|
})
|
|
return
|
|
}
|
|
|
|
// Moderate the content
|
|
err := h.service.ModerateContent(request.ContentID, request.Decision)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, ModerateContentResponse{
|
|
Success: false,
|
|
Message: "Error moderating content: " + err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
// Return success response
|
|
c.JSON(http.StatusOK, ModerateContentResponse{
|
|
Success: true,
|
|
Message: "Content moderated successfully",
|
|
})
|
|
}
|
|
|
|
// HandleSubmitForAIModeration handles the submit-for-moderation endpoint
|
|
func (h *ModerationHandler) HandleSubmitForAIModeration(c *gin.Context) {
|
|
var content ContentToModerate
|
|
|
|
// Parse the request body
|
|
if err := c.ShouldBindJSON(&content); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"success": false,
|
|
"error": "Invalid request format",
|
|
})
|
|
return
|
|
}
|
|
|
|
// Check economic barrier for unverified users
|
|
barrierPassed, err := h.service.CheckEconomicBarrier(content.UserAddress, content.IsVerified)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"success": false,
|
|
"error": "Error checking economic barrier: " + err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
if !barrierPassed {
|
|
c.JSON(http.StatusPaymentRequired, gin.H{
|
|
"success": false,
|
|
"error": "Economic barrier not passed. Please submit a small POL fee (0.01 POL).",
|
|
})
|
|
return
|
|
}
|
|
|
|
// Run AI moderation
|
|
flagged, reason, err := h.service.RunAIModeration(&content)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"success": false,
|
|
"error": "Error running AI moderation: " + err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
// Return response based on moderation result
|
|
if flagged {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": true,
|
|
"flagged": true,
|
|
"reason": reason,
|
|
})
|
|
} else {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": true,
|
|
"flagged": false,
|
|
})
|
|
}
|
|
}
|
|
|
|
// RegisterRoutes registers the moderation routes with the provided router
|
|
func (h *ModerationHandler) RegisterRoutes(router *gin.Engine) {
|
|
// Add CORS OPTIONS handlers
|
|
router.OPTIONS("/flag-content", corsMiddleware)
|
|
router.OPTIONS("/moderation-queue", corsMiddleware)
|
|
router.OPTIONS("/moderate-content", corsMiddleware)
|
|
router.OPTIONS("/submit-for-moderation", corsMiddleware)
|
|
|
|
// Add the main handlers with CORS headers
|
|
router.POST("/flag-content", corsMiddleware, h.HandleFlagContent)
|
|
router.GET("/moderation-queue", corsMiddleware, h.HandleModerationQueue)
|
|
router.POST("/moderate-content", corsMiddleware, h.HandleModerateContent)
|
|
router.POST("/submit-for-moderation", corsMiddleware, h.HandleSubmitForAIModeration)
|
|
}
|
|
|
|
// corsMiddleware adds CORS headers to the response
|
|
func corsMiddleware(c *gin.Context) {
|
|
c.Header("Access-Control-Allow-Origin", "*")
|
|
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
|
|
c.Header("Access-Control-Allow-Headers", "Content-Type")
|
|
|
|
// If it's an OPTIONS request, return 200 OK
|
|
if c.Request.Method == "OPTIONS" {
|
|
c.Status(http.StatusOK)
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
c.Next()
|
|
}
|