1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-02 15:06:23 -07:00

Move internal report creation code to gcsql

Also insert the report's timestamp into the report audit table
This commit is contained in:
Eggbertx 2022-07-26 11:31:13 -07:00
parent 90ba350777
commit c64d7b16e8
4 changed files with 75 additions and 15 deletions

View file

@ -62,6 +62,65 @@ func SetFormattedInDatabase(messages []MessagePostContainer) error {
return err
}
func CreateReport(postID int, ip string, reason string) (*Report, error) {
currentTime := time.Now()
sql := `INSERT INTO DBPREFIXreports (post_id, ip, reason) VALUES(?, ?, ?)`
result, err := ExecSQL(sql, postID, ip, reason)
if err != nil {
return nil, err
}
reportID, err := result.LastInsertId()
if err != nil {
return nil, err
}
sql = `INSERT INTO gc_reports_audit (report_id,timestamp) VALUES(?, ?)`
if _, err = ExecSQL(sql, reportID, currentTime); err != nil {
return nil, err
}
return &Report{
ID: int(reportID),
HandledByStaffID: -1,
PostID: postID,
IP: ip,
Reason: reason,
IsCleared: false,
}, nil
}
func CheckDuplicateReport(postID int, reason string) (bool, error) {
sql := `SELECT COUNT(*) FROM DBPREFIXreports
WHERE post_id = ? AND reason = ?`
var num int
err := QueryRowSQL(sql, interfaceSlice(postID, reason), interfaceSlice(&num))
return num > 0, err
}
func GetReports(includeCleared bool) ([]Report, error) {
sql := `SELECT id,handled_by_staff_id,post_id,ip,reason,is_cleared FROM DBPREFIXreports`
if !includeCleared {
sql += ` WHERE is_cleared = 0`
}
rows, err := QuerySQL(sql)
if err != nil {
return nil, err
}
var reports []Report
for rows.Next() {
var report Report
var staffID interface{}
err = rows.Scan(&report.ID, &staffID, &report.PostID, &report.IP, &report.Reason, &report.IsCleared)
if err != nil {
return nil, err
}
staffID64, _ := (staffID.(int64))
report.HandledByStaffID = int(staffID64)
reports = append(reports, report)
}
return reports, nil
}
// PermanentlyRemoveDeletedPosts removes all posts and files marked as deleted from the database
func PermanentlyRemoveDeletedPosts() error {
const sql1 = `DELETE FROM DBPREFIXposts WHERE is_deleted`

View file

@ -335,6 +335,16 @@ type Report struct {
IsCleared bool
}
func (r *Report) Timestamp() (*time.Time, error) {
sql := `SELECT timestamp FROM DBPREFIXreports_audit WHERE report_id = ?`
timestamp := new(time.Time)
err := QueryRowSQL(sql, interfaceSlice(r.ID), interfaceSlice(timestamp))
if err != nil {
return nil, err
}
return timestamp, nil
}
type LoginSession struct {
ID uint
Name string
@ -373,7 +383,7 @@ func (s *Staff) RankString() string {
case 1:
return "Janitor"
}
return ""
return "Unknown"
}
type BoardCooldowns struct {

View file

@ -105,7 +105,7 @@ func ServeCaptcha(writer http.ResponseWriter, request *http.Request) {
}
}
if err = serverutil.MinifyTemplate(gctemplates.Captcha, captchaStruct, writer, "text/html"); err != nil {
fmt.Fprintf(writer,
fmt.Fprint(writer,
gclog.Print(gclog.LErrorLog, "Error executing captcha template: ", err.Error()))
}
}

View file

@ -10,12 +10,6 @@ import (
"github.com/gochan-org/gochan/pkg/gcutil"
)
const (
reportInsertSQL = `INSERT INTO DBPREFIXreports (post_id,ip,reason) VALUES(?,?,?)`
duplicateCheckSQL = `SELECT COUNT(*) FROM DBPREFIXreports
WHERE post_id = ? AND reason = ?`
)
var (
ErrInvalidReport = errors.New("invalid report submitted")
ErrInvalidPost = errors.New("post does not exist")
@ -52,20 +46,17 @@ func HandleReport(request *http.Request) error {
}
for _, postID := range reportedPosts {
var count int
// check to see if the post has already been reported with this report string
err = gcsql.QueryRowSQL(duplicateCheckSQL,
[]interface{}{&postID, reason},
[]interface{}{&count})
isDuplicate, err := gcsql.CheckDuplicateReport(postID, reason)
if err != nil {
return err
}
if count > 0 {
if isDuplicate {
// post has already been reported, and for the same reason, moving on
continue
}
_, err := gcsql.ExecSQL(reportInsertSQL, postID, ip, reason)
if err != nil {
if _, err = gcsql.CreateReport(postID, ip, reason); err != nil {
return err
}
}