mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-04-08 02:31:36 +02:00
add support for saving shadow copies of repositories
This commit is contained in:
parent
78ff27984c
commit
ca0636a6fc
3 changed files with 76 additions and 0 deletions
66
models/repo/moderation.go
Normal file
66
models/repo/moderation.go
Normal file
|
@ -0,0 +1,66 @@
|
|||
// Copyright 2025 The Forgejo Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package repo
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"forgejo.org/models/moderation"
|
||||
"forgejo.org/modules/json"
|
||||
"forgejo.org/modules/timeutil"
|
||||
)
|
||||
|
||||
// RepositoryData represents a trimmed down repository that is used for preserving
|
||||
// only the fields needed for abusive content reports (mainly string fields).
|
||||
type RepositoryData struct {
|
||||
OwnerID int64
|
||||
OwnerName string
|
||||
Name string
|
||||
Description string
|
||||
Website string
|
||||
Topics []string
|
||||
Avatar string
|
||||
CreatedUnix timeutil.TimeStamp
|
||||
UpdatedUnix timeutil.TimeStamp
|
||||
}
|
||||
|
||||
// newRepositoryData creates a trimmed down repository to be used just to create a JSON structure
|
||||
// (keeping only the fields relevant for moderation purposes)
|
||||
func newRepositoryData(repo *Repository) RepositoryData {
|
||||
return RepositoryData{
|
||||
OwnerID: repo.OwnerID,
|
||||
OwnerName: repo.OwnerName,
|
||||
Name: repo.Name,
|
||||
Description: repo.Description,
|
||||
Website: repo.Website,
|
||||
Topics: repo.Topics,
|
||||
Avatar: repo.Avatar,
|
||||
CreatedUnix: repo.CreatedUnix,
|
||||
UpdatedUnix: repo.UpdatedUnix,
|
||||
}
|
||||
}
|
||||
|
||||
// IfNeededCreateShadowCopyForRepository checks if for the given repository there are any reports of abusive content submitted
|
||||
// and if found a shadow copy of relevant repository fields will be stored into DB and linked to the above report(s).
|
||||
// This function should be called when a repository is deleted or updated.
|
||||
func IfNeededCreateShadowCopyForRepository(ctx context.Context, repo *Repository, forUpdates bool) error {
|
||||
if moderation.IsReported(ctx, moderation.ReportedContentTypeRepository, repo.ID) {
|
||||
if forUpdates {
|
||||
var err error
|
||||
// get the unmodified repository fields
|
||||
repo, err = GetRepositoryByID(ctx, repo.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
repoData := newRepositoryData(repo)
|
||||
content, err := json.Marshal(repoData)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return moderation.CreateShadowCopyForRepository(ctx, repo.ID, string(content))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -241,6 +241,11 @@ func UpdateRepository(ctx context.Context, repo *repo_model.Repository, visibili
|
|||
|
||||
e := db.GetEngine(ctx)
|
||||
|
||||
// If the repository was reported as abusive, a shadow copy should be created before first update.
|
||||
if err := repo_model.IfNeededCreateShadowCopyForRepository(ctx, repo, true); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err = e.ID(repo.ID).AllCols().Update(repo); err != nil {
|
||||
return fmt.Errorf("update: %w", err)
|
||||
}
|
||||
|
|
|
@ -89,6 +89,11 @@ func DeleteRepositoryDirectly(ctx context.Context, doer *user_model.User, repoID
|
|||
}
|
||||
}
|
||||
|
||||
// If the repository was reported as abusive, a shadow copy should be created before deletion.
|
||||
if err := repo_model.IfNeededCreateShadowCopyForRepository(ctx, repo, false); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if cnt, err := sess.ID(repoID).Delete(&repo_model.Repository{}); err != nil {
|
||||
return err
|
||||
} else if cnt != 1 {
|
||||
|
|
Loading…
Add table
Reference in a new issue