Some fixes and improvements based on recent review.

This commit is contained in:
floss4good 2025-03-05 11:16:57 +02:00
parent 75578c64ea
commit 7046bd5e39
No known key found for this signature in database
GPG key ID: 5B948B4F4DAF819D
5 changed files with 25 additions and 13 deletions

View file

@ -14,8 +14,6 @@ import (
// CommentData represents a trimmed down comment that is used for preserving
// only the fields needed for abusive content reports (mainly string fields).
type CommentData struct {
OriginalAuthor string // TODO: decide if this is useful
TreePath string // TODO: decide if this is useful
Content string
ContentVersion int
CreatedUnix timeutil.TimeStamp
@ -26,8 +24,6 @@ type CommentData struct {
// (keeping only the fields relevant for moderation purposes)
func newCommentData(comment *Comment) CommentData {
return CommentData{
OriginalAuthor: comment.OriginalAuthor,
TreePath: comment.TreePath,
Content: comment.Content,
ContentVersion: comment.ContentVersion,
CreatedUnix: comment.CreatedUnix,

View file

@ -105,6 +105,8 @@ type AbuseReport struct {
CreatedUnix timeutil.TimeStamp `xorm:"created NOT NULL"`
}
var ErrSelfReporting = errors.New("reporting yourself is not allowed")
func init() {
// RegisterModel will create the table if does not already exist
// or any missing columns if the table was previously created.
@ -133,7 +135,7 @@ func AlreadyReportedByAndOpen(ctx context.Context, doerID int64, contentType Rep
func ReportAbuse(ctx context.Context, report *AbuseReport) error {
if report.ContentType == ReportedContentTypeUser && report.ReporterID == report.ContentID {
return errors.New("reporting yourself is not allowed")
return ErrSelfReporting
}
if AlreadyReportedByAndOpen(ctx, report.ReporterID, report.ContentType, report.ContentID) {

View file

@ -1,6 +1,6 @@
// Copyright 2014 The Gogs Authors. All rights reserved.
// Copyright 2019 The Gitea Authors. All rights reserved.
// Copyright 2024-2025 The Forgejo Authors. All rights reserved.
// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package user

View file

@ -4,6 +4,7 @@
package moderation
import (
"errors"
"net/http"
"code.gitea.io/gitea/models/moderation"
@ -22,8 +23,9 @@ const (
func NewReport(ctx *context.Context) {
contentID := ctx.FormInt64("id")
if contentID <= 0 {
setMinimalContextData(ctx)
ctx.RenderWithErr(ctx.Tr("moderation.report_abuse_form.invalid"), tplSubmitAbuseReport, nil)
log.Warn("The content ID is expected to be an integer greater that 0; the provided value is %d.", contentID)
log.Warn("The content ID is expected to be an integer greater that 0; the provided value is %s.", ctx.FormString("id"))
return
}
@ -39,12 +41,14 @@ func NewReport(ctx *context.Context) {
case "comment":
contentType = moderation.ReportedContentTypeComment
default:
setMinimalContextData(ctx)
ctx.RenderWithErr(ctx.Tr("moderation.report_abuse_form.invalid"), tplSubmitAbuseReport, nil)
log.Warn("The provided content type `%s` is not among the expected values.", contentTypeString)
return
}
if moderation.AlreadyReportedByAndOpen(ctx, ctx.Doer.ID, contentType, contentID) {
setMinimalContextData(ctx)
ctx.RenderWithErr(ctx.Tr("moderation.report_abuse_form.already_reported"), tplSubmitAbuseReport, nil)
return
}
@ -52,13 +56,18 @@ func NewReport(ctx *context.Context) {
setContextDataAndRender(ctx, contentType, contentID)
}
// setMinimalContextData adds minimal values (Title and CancelLink) into context data.
func setMinimalContextData(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("moderation.report_abuse")
ctx.Data["CancelLink"] = ctx.Doer.DashboardLink()
}
// setContextDataAndRender adds some values into context data and renders the new abuse report page.
func setContextDataAndRender(ctx *context.Context, contentType moderation.ReportedContentType, contentID int64) {
ctx.Data["Title"] = ctx.Tr("moderation.report_abuse")
setMinimalContextData(ctx)
ctx.Data["ContentID"] = contentID
ctx.Data["ContentType"] = contentType
ctx.Data["AbuseCategories"] = moderation.GetAbuseCategoriesList()
ctx.Data["CancelLink"] = ctx.Doer.DashboardLink()
ctx.HTML(http.StatusOK, tplSubmitAbuseReport)
}
@ -67,6 +76,7 @@ func CreatePost(ctx *context.Context) {
form := *web.GetForm(ctx).(*forms.ReportAbuseForm)
if form.ContentID <= 0 || !form.ContentType.IsValid() {
setMinimalContextData(ctx)
ctx.RenderWithErr(ctx.Tr("moderation.report_abuse_form.invalid"), tplSubmitAbuseReport, nil)
return
}
@ -85,8 +95,12 @@ func CreatePost(ctx *context.Context) {
}
if err := moderation.ReportAbuse(ctx, &report); err != nil {
ctx.Flash.Error(ctx.Tr("moderation.reporting_failed", err))
ctx.Redirect(ctx.Doer.DashboardLink())
if errors.Is(err, moderation.ErrSelfReporting) {
ctx.Flash.Error(ctx.Tr("moderation.reporting_failed", err))
ctx.Redirect(ctx.Doer.DashboardLink())
} else {
ctx.ServerError("Failed to save new abuse report", err)
}
return
}

View file

@ -481,8 +481,8 @@ func registerRoutes(m *web.Route) {
m.Get("/search", repo.SearchIssues)
}, reqSignIn)
m.Get("/-/abuse_reports/new", moderation.NewReport, reqSignIn)
m.Post("/-/abuse_reports/new", web.Bind(forms.ReportAbuseForm{}), moderation.CreatePost, reqSignIn)
m.Get("/-/abuse_reports/new", reqSignIn, moderation.NewReport)
m.Post("/-/abuse_reports/new", reqSignIn, web.Bind(forms.ReportAbuseForm{}), moderation.CreatePost)
m.Get("/pulls", reqSignIn, user.Pulls)
m.Get("/milestones", reqSignIn, reqMilestonesDashboardPageEnabled, user.Milestones)