add support for saving shadow copies of issues

This commit is contained in:
floss4good 2025-04-04 00:12:58 +03:00
parent 957cda9f09
commit 78ff27984c
No known key found for this signature in database
GPG key ID: 5B948B4F4DAF819D
3 changed files with 61 additions and 1 deletions

View file

@ -275,6 +275,11 @@ func ChangeIssueContent(ctx context.Context, issue *Issue, doer *user_model.User
}
}
// If the issue was reported as abusive, a shadow copy should be created before first update.
if err := IfNeededCreateShadowCopyForIssue(ctx, issue); err != nil {
return err
}
issue.Content = content
issue.ContentVersion = contentVersion + 1

View file

@ -11,6 +11,34 @@ import (
"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
}
// 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,
Content: issue.Content,
Title: issue.Title,
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 {
@ -35,6 +63,22 @@ func newCommentData(comment *Comment) CommentData {
}
}
// 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 {
if moderation.IsReported(ctx, moderation.ReportedContentTypeIssue, issue.ID) {
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.

View file

@ -59,7 +59,6 @@ func NewIssue(ctx context.Context, repo *repo_model.Repository, issue *issues_mo
// ChangeTitle changes the title of this issue, as the given user.
func ChangeTitle(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, title string) error {
oldTitle := issue.Title
issue.Title = title
if oldTitle == title {
return nil
@ -73,6 +72,12 @@ func ChangeTitle(ctx context.Context, issue *issues_model.Issue, doer *user_mode
return user_model.ErrBlockedByUser
}
// If the issue was reported as abusive, a shadow copy should be created before first update.
if err := issues_model.IfNeededCreateShadowCopyForIssue(ctx, issue); err != nil {
return err
}
issue.Title = title
if err := issues_model.ChangeIssueTitle(ctx, issue, doer, oldTitle); err != nil {
return err
}
@ -252,6 +257,12 @@ func deleteIssue(ctx context.Context, issue *issues_model.Issue) error {
defer committer.Close()
e := db.GetEngine(ctx)
// If the issue was reported as abusive, a shadow copy should be created before deletion.
if err := issues_model.IfNeededCreateShadowCopyForIssue(ctx, issue); err != nil {
return err
}
if _, err := e.ID(issue.ID).NoAutoCondition().Delete(issue); err != nil {
return err
}