took me a bit to figure out how to do this properly, but i believe it's improved my understanding of how go works
104 lines
3.2 KiB
Go
104 lines
3.2 KiB
Go
package reports
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"errors"
|
|
"log"
|
|
"net/http"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
"uuid"
|
|
|
|
"git.whoswhip.dev/whoswhip/minibin/internal/config"
|
|
"git.whoswhip.dev/whoswhip/minibin/internal/database"
|
|
"git.whoswhip.dev/whoswhip/minibin/internal/pages"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func ReportUploadHandler(db *gorm.DB, cfg config.Config, p *pages.Pages, cStore *ChallengeStore) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
if err := r.ParseForm(); err != nil {
|
|
p.HTTPError(w, r, "Invalid form data", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
powResult := strings.TrimSpace(r.PostForm.Get("pow_result"))
|
|
powNonceForm := strings.TrimSpace(r.PostForm.Get("pow_nonce"))
|
|
powChallengeId := strings.TrimSpace(r.PostForm.Get("pow_challenge_id"))
|
|
uploadId := strings.TrimSpace(r.PostForm.Get("upload_id"))
|
|
reportReason := strings.TrimSpace(r.PostForm.Get("report_reason"))
|
|
|
|
if powResult == "" || powNonceForm == "" || powChallengeId == "" {
|
|
pages.ResultPage(p, "400, bad request", "proof of work is required", 400)(w, r)
|
|
return
|
|
}
|
|
if uploadId == "" || (len(uploadId) != 10 && len(uploadId) != 16) {
|
|
pages.ResultPage(p, "400, bad request", "invalid upload id provided", 400)(w, r)
|
|
return
|
|
}
|
|
if reportReason == "" || !slices.Contains(cfg.ReportOptions, reportReason) {
|
|
pages.ResultPage(p, "400, bad request", "invalid reason provided", 400)(w, r)
|
|
return
|
|
}
|
|
|
|
powNonce, err := strconv.Atoi(powNonceForm)
|
|
if err != nil {
|
|
log.Println("failed to parse pow nonce:", err)
|
|
pages.ResultPage(p, "400, bad request", "the nonce provided is not a valid integer", 400)(w, r)
|
|
return
|
|
}
|
|
|
|
clientError, err := cStore.Verify(powChallengeId, powResult, powNonce)
|
|
if err != nil && !clientError {
|
|
log.Println("failed to verify pow:", err)
|
|
pages.ResultPage(p, "500, internal server error", "server went kaput", 500)(w, r)
|
|
return
|
|
}
|
|
if err != nil && clientError {
|
|
p.HTTPError(w, r, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
report := &database.Report{
|
|
ID: uuid.NewV7().String(),
|
|
UploadID: uploadId,
|
|
Reason: reportReason,
|
|
}
|
|
|
|
err = database.InsertReport(db.WithContext(r.Context()), report)
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrForeignKeyViolated) {
|
|
pages.ResultPage(p, "404, upload not found", "the upload you are reporting was not found", 404)(w, r)
|
|
return
|
|
}
|
|
|
|
log.Println("failed to insert report:", err)
|
|
p.HTTPError(w, r, "internal server error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
pages.ResultPage(p, "success!", "your report has been successfully submitted", 200)(w, r)
|
|
}
|
|
}
|
|
|
|
func ReportPageHandler(p *pages.Pages, store *ChallengeStore, cfg config.Config) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
id, challenge, err := store.Issue(cfg.PoWDifficulty)
|
|
if err != nil {
|
|
http.Error(w, "failed to generate PoW challenge", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
p.Render(w, "report.html", map[string]any{
|
|
"Qt": pages.GetRandomQt(p.Qts),
|
|
"Challenge": map[string]any{
|
|
"Data": hex.EncodeToString(challenge.Data),
|
|
"Difficulty": challenge.Difficulty,
|
|
"ExpiresAt": challenge.ExpiresAt,
|
|
"Id": id,
|
|
},
|
|
"ReportOptions": cfg.ReportOptions,
|
|
})
|
|
}
|
|
}
|