1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-26 18:56:24 -07:00
gochan/pkg/posting/reports.go
2022-08-01 16:08:57 -07:00

64 lines
1.6 KiB
Go

package posting
import (
"errors"
"fmt"
"net/http"
"strings"
"github.com/gochan-org/gochan/pkg/gcsql"
"github.com/gochan-org/gochan/pkg/gcutil"
)
var (
ErrInvalidReport = errors.New("invalid report submitted")
ErrInvalidPost = errors.New("post does not exist")
ErrNoReportedPosts = errors.New("no posts selected")
ErrNoReportReason = errors.New("no report reason given")
ErrDuplicateReport = errors.New("post already reported")
)
func HandleReport(request *http.Request) error {
board := request.FormValue("board")
if request.Method != "POST" {
return ErrInvalidReport
}
reportedPosts := []int{}
var id int
if !gcsql.DoesBoardExistByDir(board) {
return gcsql.ErrBoardDoesNotExist
}
var err error
for key, val := range request.Form {
if _, err = fmt.Sscanf(key, "check%d", &id); err != nil || val[0] != "on" {
err = nil
continue
}
reportedPosts = append(reportedPosts, id)
}
if len(reportedPosts) == 0 {
return ErrNoReportedPosts
}
ip := gcutil.GetRealIP(request)
reason := strings.TrimSpace(request.PostFormValue("reason"))
if reason == "" {
return ErrNoReportReason
}
for _, postID := range reportedPosts {
// check to see if the post has already been reported with this report string or if it can't be reported
isDuplicate, isBlocked, err := gcsql.CheckPostReports(postID, reason)
if err != nil {
return err
}
if isDuplicate || isBlocked {
// post has already been reported, and for the same reason, moving on
continue
}
if _, err = gcsql.CreateReport(postID, ip, reason); err != nil {
return err
}
}
return nil
}