mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-08-07 19:56:45 +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>
92 lines
3.3 KiB
Go
92 lines
3.3 KiB
Go
// Copyright 2025 The Forgejo Authors. All rights reserved.
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package moderation
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
|
|
"forgejo.org/models/db"
|
|
"forgejo.org/modules/log"
|
|
"forgejo.org/modules/timeutil"
|
|
|
|
"xorm.io/builder"
|
|
)
|
|
|
|
type AbuseReportShadowCopy struct {
|
|
ID int64 `xorm:"pk autoincr"`
|
|
RawValue string `xorm:"LONGTEXT NOT NULL"` // A JSON with relevant fields from user, repository, issue or comment table.
|
|
CreatedUnix timeutil.TimeStamp `xorm:"created NOT NULL"`
|
|
}
|
|
|
|
// Returns the ID encapsulated in a sql.NullInt64 struct.
|
|
func (sc AbuseReportShadowCopy) NullableID() sql.NullInt64 {
|
|
return sql.NullInt64{Int64: sc.ID, Valid: sc.ID > 0}
|
|
}
|
|
|
|
// ShadowCopyField defines a pair of a value stored within the shadow copy
|
|
// (of some content reported as abusive) and a corresponding key (caption).
|
|
// A list of such pairs is used when rendering shadow copies for admins reviewing abuse reports.
|
|
type ShadowCopyField struct {
|
|
Key string
|
|
Value string
|
|
}
|
|
|
|
// ShadowCopyData interface should be implemented by the type structs used for marshaling/unmarshaling the fields
|
|
// preserved as shadow copies for abusive content reports (i.e. UserData, RepositoryData, IssueData, CommentData).
|
|
type ShadowCopyData interface {
|
|
// GetFieldsMap returns a list of <key, value> pairs with the fields stored within shadow copies
|
|
// of content reported as abusive, to be used when rendering a shadow copy in the admin UI.
|
|
GetFieldsMap() []ShadowCopyField
|
|
}
|
|
|
|
func init() {
|
|
// RegisterModel will create the table if does not already exist
|
|
// or any missing columns if the table was previously created.
|
|
// It will not drop or rename existing columns (when struct has changed).
|
|
db.RegisterModel(new(AbuseReportShadowCopy))
|
|
}
|
|
|
|
func CreateShadowCopyForUser(ctx context.Context, userID int64, content string) error {
|
|
return createShadowCopy(ctx, ReportedContentTypeUser, userID, content)
|
|
}
|
|
|
|
func CreateShadowCopyForRepository(ctx context.Context, repoID int64, content string) error {
|
|
return createShadowCopy(ctx, ReportedContentTypeRepository, repoID, content)
|
|
}
|
|
|
|
func CreateShadowCopyForIssue(ctx context.Context, issueID int64, content string) error {
|
|
return createShadowCopy(ctx, ReportedContentTypeIssue, issueID, content)
|
|
}
|
|
|
|
func CreateShadowCopyForComment(ctx context.Context, commentID int64, content string) error {
|
|
return createShadowCopy(ctx, ReportedContentTypeComment, commentID, content)
|
|
}
|
|
|
|
func createShadowCopy(ctx context.Context, contentType ReportedContentType, contentID int64, content string) error {
|
|
err := db.WithTx(ctx, func(ctx context.Context) error {
|
|
sess := db.GetEngine(ctx)
|
|
|
|
shadowCopy := &AbuseReportShadowCopy{RawValue: content}
|
|
affected, err := sess.Insert(shadowCopy)
|
|
if err != nil {
|
|
return err
|
|
} else if affected == 0 {
|
|
log.Warn("Something went wrong while trying to create the shadow copy for reported content with type %d and ID %d.", contentType, contentID)
|
|
}
|
|
|
|
_, err = sess.Where(builder.Eq{
|
|
"content_type": contentType,
|
|
"content_id": contentID,
|
|
}).And(builder.IsNull{"shadow_copy_id"}).Update(&AbuseReport{ShadowCopyID: shadowCopy.NullableID()})
|
|
if err != nil {
|
|
return fmt.Errorf("could not link the shadow copy (%d) to reported content with type %d and ID %d - %w", shadowCopy.ID, contentType, contentID, err)
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
return err
|
|
}
|