mirror of
https://codeberg.org/davrot/forgejo.git
synced 2025-07-26 01:00:05 +02:00

- Implementation of milestone 5. from **Task F. Moderation features: Reporting** (part of [amendment of the workplan](https://codeberg.org/forgejo/sustainability/src/branch/main/2022-12-01-nlnet/2025-02-07-extended-workplan.md#task-f-moderation-features-reporting) for NLnet 2022-12-035): `5. Forgejo admins can see a list of reports` There is a lot of room for improvements, but it was decided to start with a basic version so that feedback can be collected from real-life usages (based on which the UI might change a lot). - Also covers milestone 2. from same **Task F. Moderation features: Reporting**: `2. Reports from multiple users are combined in the database and don't create additional reports.` But instead of combining the reports when stored, they are grouped when retrieved (it was concluded _that it might be preferable to take care of the deduplication while implementing the admin interface_; see https://codeberg.org/forgejo/forgejo/pulls/7939#issuecomment-4841754 for more details). --- Follow-up of !6977 ### See also: - forgejo/design#30 --- This adds a new _Moderation reports_ section (/admin/moderation/reports) within the _Site administration_ page, where administrators can see an overview with the submitted abuse reports that are still open (not yet handled in any way). When multiple reports exist for the same content (submitted by distinct users) only the first one will be shown in the list and a counter can be seen on the right side (indicating the number of open reports for the same content type and ID). Clicking on the counter or the icon from the right side will open the details page where a list with all the reports (when multiple) linked to the reported content is available, as well as any shadow copy saved for the current report(s). The new section is available only when moderation in enabled ([moderation] ENABLED config is set as true within app.ini). Discussions regarding the UI/UX started with https://codeberg.org/forgejo/design/issues/30#issuecomment-2908849 Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7905 Reviewed-by: Otto <otto@codeberg.org> Reviewed-by: jerger <jerger@noreply.codeberg.org> Co-authored-by: floss4good <floss4good@disroot.org> Co-committed-by: floss4good <floss4good@disroot.org>
141 lines
5 KiB
Go
141 lines
5 KiB
Go
// Copyright 2025 The Forgejo Authors. All rights reserved.
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package issues
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
|
|
"forgejo.org/models/moderation"
|
|
"forgejo.org/modules/json"
|
|
"forgejo.org/modules/timeutil"
|
|
)
|
|
|
|
// IssueData represents a trimmed down issue that is used for preserving
|
|
// only the fields needed for abusive content reports (mainly string fields).
|
|
type IssueData struct {
|
|
RepoID int64
|
|
Index int64
|
|
PosterID int64
|
|
Title string
|
|
Content string
|
|
ContentVersion int
|
|
CreatedUnix timeutil.TimeStamp
|
|
UpdatedUnix timeutil.TimeStamp
|
|
}
|
|
|
|
// Implements GetFieldsMap() from ShadowCopyData interface, returning a list of <key, value> pairs
|
|
// to be used when rendering the shadow copy for admins reviewing the corresponding abuse report(s).
|
|
func (cd IssueData) GetFieldsMap() []moderation.ShadowCopyField {
|
|
return []moderation.ShadowCopyField{
|
|
{Key: "RepoID", Value: strconv.FormatInt(cd.RepoID, 10)},
|
|
{Key: "Index", Value: strconv.FormatInt(cd.Index, 10)},
|
|
{Key: "PosterID", Value: strconv.FormatInt(cd.PosterID, 10)},
|
|
{Key: "Title", Value: cd.Title},
|
|
{Key: "Content", Value: cd.Content},
|
|
{Key: "ContentVersion", Value: strconv.Itoa(cd.ContentVersion)},
|
|
{Key: "CreatedUnix", Value: cd.CreatedUnix.AsLocalTime().String()},
|
|
{Key: "UpdatedUnix", Value: cd.UpdatedUnix.AsLocalTime().String()},
|
|
}
|
|
}
|
|
|
|
// newIssueData creates a trimmed down issue to be used just to create a JSON structure
|
|
// (keeping only the fields relevant for moderation purposes)
|
|
func newIssueData(issue *Issue) IssueData {
|
|
return IssueData{
|
|
RepoID: issue.RepoID,
|
|
Index: issue.Index,
|
|
PosterID: issue.PosterID,
|
|
Title: issue.Title,
|
|
Content: issue.Content,
|
|
ContentVersion: issue.ContentVersion,
|
|
CreatedUnix: issue.CreatedUnix,
|
|
UpdatedUnix: issue.UpdatedUnix,
|
|
}
|
|
}
|
|
|
|
// 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 {
|
|
PosterID int64
|
|
IssueID int64
|
|
Content string
|
|
ContentVersion int
|
|
CreatedUnix timeutil.TimeStamp
|
|
UpdatedUnix timeutil.TimeStamp
|
|
}
|
|
|
|
// Implements GetFieldsMap() from ShadowCopyData interface, returning a list of <key, value> pairs
|
|
// to be used when rendering the shadow copy for admins reviewing the corresponding abuse report(s).
|
|
func (cd CommentData) GetFieldsMap() []moderation.ShadowCopyField {
|
|
return []moderation.ShadowCopyField{
|
|
{Key: "PosterID", Value: strconv.FormatInt(cd.PosterID, 10)},
|
|
{Key: "IssueID", Value: strconv.FormatInt(cd.IssueID, 10)},
|
|
{Key: "Content", Value: cd.Content},
|
|
{Key: "ContentVersion", Value: strconv.Itoa(cd.ContentVersion)},
|
|
{Key: "CreatedUnix", Value: cd.CreatedUnix.AsLocalTime().String()},
|
|
{Key: "UpdatedUnix", Value: cd.UpdatedUnix.AsLocalTime().String()},
|
|
}
|
|
}
|
|
|
|
// newCommentData creates a trimmed down comment to be used just to create a JSON structure
|
|
// (keeping only the fields relevant for moderation purposes)
|
|
func newCommentData(comment *Comment) CommentData {
|
|
return CommentData{
|
|
PosterID: comment.PosterID,
|
|
IssueID: comment.IssueID,
|
|
Content: comment.Content,
|
|
ContentVersion: comment.ContentVersion,
|
|
CreatedUnix: comment.CreatedUnix,
|
|
UpdatedUnix: comment.UpdatedUnix,
|
|
}
|
|
}
|
|
|
|
// IfNeededCreateShadowCopyForIssue checks if for the given issue there are any reports of abusive content submitted
|
|
// and if found a shadow copy of relevant issue fields will be stored into DB and linked to the above report(s).
|
|
// This function should be called before a issue is deleted or updated.
|
|
func IfNeededCreateShadowCopyForIssue(ctx context.Context, issue *Issue) error {
|
|
shadowCopyNeeded, err := moderation.IsShadowCopyNeeded(ctx, moderation.ReportedContentTypeIssue, issue.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if shadowCopyNeeded {
|
|
issueData := newIssueData(issue)
|
|
content, err := json.Marshal(issueData)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return moderation.CreateShadowCopyForIssue(ctx, issue.ID, string(content))
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// IfNeededCreateShadowCopyForComment checks if for the given comment there are any reports of abusive content submitted
|
|
// and if found a shadow copy of relevant comment fields will be stored into DB and linked to the above report(s).
|
|
// This function should be called before a comment is deleted or updated.
|
|
func IfNeededCreateShadowCopyForComment(ctx context.Context, comment *Comment, forUpdates bool) error {
|
|
shadowCopyNeeded, err := moderation.IsShadowCopyNeeded(ctx, moderation.ReportedContentTypeComment, comment.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if shadowCopyNeeded {
|
|
if forUpdates {
|
|
// get the unaltered comment fields (for updates the provided variable is already altered but not yet saved)
|
|
if comment, err = GetCommentByID(ctx, comment.ID); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
commentData := newCommentData(comment)
|
|
content, err := json.Marshal(commentData)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return moderation.CreateShadowCopyForComment(ctx, comment.ID, string(content))
|
|
}
|
|
|
|
return nil
|
|
}
|