mirror of
https://github.com/Eggbertx/gochan.git
synced 2025-08-19 20:56:23 -07:00
An empty slice can be represented by `nil` or an empty slice literal. They are functionally equivalent — their `len` and `cap` are both zero — but the `nil` slice is the preferred style. For more information about empty slices, see [Declaring Empty Slices](https://github.com/golang/go/wiki/CodeReviewComments#declaring-empty-slices).
65 lines
1.6 KiB
Go
65 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
|
|
}
|
|
var 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
|
|
}
|