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

implement paste reports

This commit is contained in:
Lukas Schulte Pelkum
2023-06-17 15:16:22 +02:00
parent b9a6a81821
commit bdac813e59
4 changed files with 128 additions and 7 deletions

View File

@@ -5,6 +5,7 @@ import (
"github.com/go-chi/chi/v5"
"github.com/lus/pasty/internal/meta"
"github.com/lus/pasty/internal/pastes"
"github.com/lus/pasty/internal/reports"
"github.com/lus/pasty/internal/storage"
"net/http"
)
@@ -16,6 +17,10 @@ type Server struct {
// The storage driver to use.
Storage storage.Driver
// The report client to use to send reports.
// If this is set to nil, the report system will be considered deactivated.
ReportClient *reports.Client
// Whether the Hastebin support should be enabled.
// If this is set to 'false', the Hastebin specific endpoints will not be registered.
HastebinSupport bool
@@ -63,12 +68,15 @@ func (server *Server) Start() error {
router.Post("/api/v2/pastes", server.v2EndpointCreatePaste)
router.With(server.v2MiddlewareInjectPaste, server.v2MiddlewareAuthorize).Patch("/api/v2/pastes/{paste_id}", server.v2EndpointModifyPaste)
router.With(server.v2MiddlewareInjectPaste, server.v2MiddlewareAuthorize).Delete("/api/v2/pastes/{paste_id}", server.v2EndpointDeletePaste)
if server.ReportClient != nil {
router.With(server.v2MiddlewareInjectPaste).Post("/api/v2/pastes/{paste_id}/report", server.v2EndpointReportPaste)
}
router.Get("/api/v2/info", func(writer http.ResponseWriter, request *http.Request) {
writeJSONOrErr(writer, http.StatusOK, map[string]any{
"version": meta.Version,
"modificationTokens": server.ModificationTokensEnabled,
"reports": false, // TODO: Return report state
"pasteLifetime": -1, // TODO: Return paste lifetime
"reports": server.ReportClient != nil,
"pasteLifetime": -1, // TODO: Return paste lifetime
})
})

View File

@@ -0,0 +1,48 @@
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 {
writeErr(writer, err)
return
}
payload := new(v2EndpointReportPastePayload)
if err := json.Unmarshal(body, payload); err != nil {
writeErr(writer, err)
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 {
writeErr(writer, err)
return
}
writeJSONOrErr(writer, http.StatusOK, response)
}