1
0
mirror of https://github.com/lus/pasty.git synced 2023-08-10 21:13:09 +03:00
pasty/internal/web/v2_end_report_paste.go

49 lines
1.1 KiB
Go
Raw Normal View History

2023-06-17 16:16:22 +03:00
package web
import (
"encoding/json"
"github.com/lus/pasty/internal/pastes"
"github.com/lus/pasty/internal/reports"
"io"
"net/http"
)
type v2EndpointReportPastePayload struct {
Reason string `json:"reason"`
}
func (server *Server) v2EndpointReportPaste(writer http.ResponseWriter, request *http.Request) {
paste, ok := request.Context().Value("paste").(*pastes.Paste)
if !ok {
writeString(writer, http.StatusInternalServerError, "missing paste object")
return
}
// Read, parse and validate the request payload
body, err := io.ReadAll(request.Body)
if err != nil {
2023-06-17 18:00:44 +03:00
writeErr(request, writer, err)
2023-06-17 16:16:22 +03:00
return
}
payload := new(v2EndpointReportPastePayload)
if err := json.Unmarshal(body, payload); err != nil {
2023-06-17 18:00:44 +03:00
writeErr(request, writer, err)
2023-06-17 16:16:22 +03:00
return
}
if payload.Reason == "" {
writeString(writer, http.StatusBadRequest, "missing report reason")
return
}
report := &reports.Report{
Paste: paste.ID,
Reason: payload.Reason,
}
response, err := server.ReportClient.Send(report)
if err != nil {
2023-06-17 18:00:44 +03:00
writeErr(request, writer, err)
2023-06-17 16:16:22 +03:00
return
}
2023-06-17 18:00:44 +03:00
writeJSONOrErr(request, writer, http.StatusOK, response)
2023-06-17 16:16:22 +03:00
}