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:
parent
90ba350777
commit
c64d7b16e8
4 changed files with 75 additions and 15 deletions
|
@ -62,6 +62,65 @@ func SetFormattedInDatabase(messages []MessagePostContainer) error {
|
||||||
return err
|
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
|
// PermanentlyRemoveDeletedPosts removes all posts and files marked as deleted from the database
|
||||||
func PermanentlyRemoveDeletedPosts() error {
|
func PermanentlyRemoveDeletedPosts() error {
|
||||||
const sql1 = `DELETE FROM DBPREFIXposts WHERE is_deleted`
|
const sql1 = `DELETE FROM DBPREFIXposts WHERE is_deleted`
|
||||||
|
|
|
@ -335,6 +335,16 @@ type Report struct {
|
||||||
IsCleared bool
|
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 {
|
type LoginSession struct {
|
||||||
ID uint
|
ID uint
|
||||||
Name string
|
Name string
|
||||||
|
@ -373,7 +383,7 @@ func (s *Staff) RankString() string {
|
||||||
case 1:
|
case 1:
|
||||||
return "Janitor"
|
return "Janitor"
|
||||||
}
|
}
|
||||||
return ""
|
return "Unknown"
|
||||||
}
|
}
|
||||||
|
|
||||||
type BoardCooldowns struct {
|
type BoardCooldowns struct {
|
||||||
|
|
|
@ -105,7 +105,7 @@ func ServeCaptcha(writer http.ResponseWriter, request *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err = serverutil.MinifyTemplate(gctemplates.Captcha, captchaStruct, writer, "text/html"); err != nil {
|
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()))
|
gclog.Print(gclog.LErrorLog, "Error executing captcha template: ", err.Error()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,12 +10,6 @@ import (
|
||||||
"github.com/gochan-org/gochan/pkg/gcutil"
|
"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 (
|
var (
|
||||||
ErrInvalidReport = errors.New("invalid report submitted")
|
ErrInvalidReport = errors.New("invalid report submitted")
|
||||||
ErrInvalidPost = errors.New("post does not exist")
|
ErrInvalidPost = errors.New("post does not exist")
|
||||||
|
@ -52,20 +46,17 @@ func HandleReport(request *http.Request) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, postID := range reportedPosts {
|
for _, postID := range reportedPosts {
|
||||||
var count int
|
|
||||||
// check to see if the post has already been reported with this report string
|
// check to see if the post has already been reported with this report string
|
||||||
err = gcsql.QueryRowSQL(duplicateCheckSQL,
|
isDuplicate, err := gcsql.CheckDuplicateReport(postID, reason)
|
||||||
[]interface{}{&postID, reason},
|
|
||||||
[]interface{}{&count})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if count > 0 {
|
if isDuplicate {
|
||||||
// post has already been reported, and for the same reason, moving on
|
// post has already been reported, and for the same reason, moving on
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
_, err := gcsql.ExecSQL(reportInsertSQL, postID, ip, reason)
|
|
||||||
if err != nil {
|
if _, err = gcsql.CreateReport(postID, ip, reason); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue