Merge branch 'rebase-forgejo-moderation' into wip-forgejo
This commit is contained in:
commit
c6fe41239a
79 changed files with 2428 additions and 56 deletions
|
@ -46,6 +46,11 @@ func CreateRefComment(ctx context.Context, doer *user_model.User, repo *repo_mod
|
|||
|
||||
// CreateIssueComment creates a plain issue comment.
|
||||
func CreateIssueComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, issue *issues_model.Issue, content string, attachments []string) (*issues_model.Comment, error) {
|
||||
// Check if doer is blocked by the poster of the issue or by the owner of the repository.
|
||||
if user_model.IsBlockedMultiple(ctx, []int64{issue.PosterID, repo.OwnerID}, doer.ID) {
|
||||
return nil, user_model.ErrBlockedByUser
|
||||
}
|
||||
|
||||
comment, err := issues_model.CreateComment(ctx, &issues_model.CreateCommentOptions{
|
||||
Type: issues_model.CommentTypeComment,
|
||||
Doer: doer,
|
||||
|
|
|
@ -24,6 +24,11 @@ import (
|
|||
|
||||
// NewIssue creates new issue with labels for repository.
|
||||
func NewIssue(ctx context.Context, repo *repo_model.Repository, issue *issues_model.Issue, labelIDs []int64, uuids []string, assigneeIDs []int64) error {
|
||||
// Check if the user is not blocked by the repo's owner.
|
||||
if user_model.IsBlocked(ctx, repo.OwnerID, issue.PosterID) {
|
||||
return user_model.ErrBlockedByUser
|
||||
}
|
||||
|
||||
if err := issues_model.NewIssue(ctx, repo, issue, labelIDs, uuids); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
47
services/issue/reaction.go
Normal file
47
services/issue/reaction.go
Normal file
|
@ -0,0 +1,47 @@
|
|||
// Copyright 2023 The Forgejo Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
package issue
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
issues_model "code.gitea.io/gitea/models/issues"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
)
|
||||
|
||||
// CreateIssueReaction creates a reaction on issue.
|
||||
func CreateIssueReaction(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, content string) (*issues_model.Reaction, error) {
|
||||
if err := issue.LoadRepo(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Check if the doer is blocked by the issue's poster or repository owner.
|
||||
if user_model.IsBlockedMultiple(ctx, []int64{issue.PosterID, issue.Repo.OwnerID}, doer.ID) {
|
||||
return nil, user_model.ErrBlockedByUser
|
||||
}
|
||||
|
||||
return issues_model.CreateReaction(ctx, &issues_model.ReactionOptions{
|
||||
Type: content,
|
||||
DoerID: doer.ID,
|
||||
IssueID: issue.ID,
|
||||
})
|
||||
}
|
||||
|
||||
// CreateCommentReaction creates a reaction on comment.
|
||||
func CreateCommentReaction(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, comment *issues_model.Comment, content string) (*issues_model.Reaction, error) {
|
||||
if err := issue.LoadRepo(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Check if the doer is blocked by the issue's poster, the comment's poster or repository owner.
|
||||
if user_model.IsBlockedMultiple(ctx, []int64{comment.PosterID, issue.PosterID, issue.Repo.OwnerID}, doer.ID) {
|
||||
return nil, user_model.ErrBlockedByUser
|
||||
}
|
||||
|
||||
return issues_model.CreateReaction(ctx, &issues_model.ReactionOptions{
|
||||
Type: content,
|
||||
DoerID: doer.ID,
|
||||
IssueID: issue.ID,
|
||||
CommentID: comment.ID,
|
||||
})
|
||||
}
|
|
@ -40,6 +40,11 @@ var pullWorkingPool = sync.NewExclusivePool()
|
|||
|
||||
// NewPullRequest creates new pull request with labels for repository.
|
||||
func NewPullRequest(ctx context.Context, repo *repo_model.Repository, issue *issues_model.Issue, labelIDs []int64, uuids []string, pr *issues_model.PullRequest, assigneeIDs []int64) error {
|
||||
// Check if the doer is not blocked by the repository's owner.
|
||||
if user_model.IsBlocked(ctx, repo.OwnerID, issue.PosterID) {
|
||||
return user_model.ErrBlockedByUser
|
||||
}
|
||||
|
||||
prCtx, cancel, err := createTemporaryRepoForPR(ctx, pr)
|
||||
if err != nil {
|
||||
if !git_model.IsErrBranchNotExist(err) {
|
||||
|
|
|
@ -362,6 +362,10 @@ func ChangeRepositoryName(ctx context.Context, doer *user_model.User, repo *repo
|
|||
// StartRepositoryTransfer transfer a repo from one owner to a new one.
|
||||
// it make repository into pending transfer state, if doer can not create repo for new owner.
|
||||
func StartRepositoryTransfer(ctx context.Context, doer, newOwner *user_model.User, repo *repo_model.Repository, teams []*organization.Team) error {
|
||||
if user_model.IsBlocked(ctx, newOwner.ID, doer.ID) {
|
||||
return user_model.ErrBlockedByUser
|
||||
}
|
||||
|
||||
if err := models.TestRepositoryReadyForTransfer(repo.Status); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -64,7 +64,7 @@ func TestStartRepositoryTransferSetPermission(t *testing.T) {
|
|||
|
||||
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3})
|
||||
recipient := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5})
|
||||
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
|
||||
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 5})
|
||||
repo.Owner = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
|
||||
|
||||
hasAccess, err := access_model.HasAccess(db.DefaultContext, recipient.ID, repo)
|
||||
|
|
95
services/user/block.go
Normal file
95
services/user/block.go
Normal file
|
@ -0,0 +1,95 @@
|
|||
// Copyright 2023 The Forgejo Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
model "code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
|
||||
"xorm.io/builder"
|
||||
)
|
||||
|
||||
// BlockUser adds a blocked user entry for userID to block blockID.
|
||||
// TODO: Figure out if instance admins should be immune to blocking.
|
||||
// TODO: Add more mechanism like removing blocked user as collaborator on
|
||||
// repositories where the user is an owner.
|
||||
func BlockUser(ctx context.Context, userID, blockID int64) error {
|
||||
if userID == blockID || user_model.IsBlocked(ctx, userID, blockID) {
|
||||
return nil
|
||||
}
|
||||
|
||||
ctx, committer, err := db.TxContext(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer committer.Close()
|
||||
|
||||
// Add the blocked user entry.
|
||||
_, err = db.GetEngine(ctx).Insert(&user_model.BlockedUser{UserID: userID, BlockID: blockID})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Unfollow the user from the block's perspective.
|
||||
err = user_model.UnfollowUser(ctx, blockID, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Unfollow the user from the doer's perspective.
|
||||
err = user_model.UnfollowUser(ctx, userID, blockID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Blocked user unwatch all repository owned by the doer.
|
||||
repoIDs, err := repo_model.GetWatchedRepoIDsOwnedBy(ctx, blockID, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = repo_model.UnwatchRepos(ctx, blockID, repoIDs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Remove blocked user as collaborator from repositories the user owns as an
|
||||
// individual.
|
||||
collabsID, err := repo_model.GetCollaboratorWithUser(ctx, userID, blockID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = db.GetEngine(ctx).In("id", collabsID).Delete(&repo_model.Collaboration{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Remove pending repository transfers, and set the status on those repository
|
||||
// back to ready.
|
||||
pendingTransfersIDs, err := model.GetPendingTransferIDs(ctx, userID, blockID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Use a subquery instead of a JOIN, because not every database supports JOIN
|
||||
// on a UPDATE query.
|
||||
_, err = db.GetEngine(ctx).Table("repository").
|
||||
In("id", builder.Select("repo_id").From("repo_transfer").Where(builder.In("id", pendingTransfersIDs))).
|
||||
Cols("status").
|
||||
Update(&repo_model.Repository{Status: repo_model.RepositoryReady})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = db.GetEngine(ctx).In("id", pendingTransfersIDs).Delete(&model.RepoTransfer{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return committer.Commit()
|
||||
}
|
91
services/user/block_test.go
Normal file
91
services/user/block_test.go
Normal file
|
@ -0,0 +1,91 @@
|
|||
// Copyright 2023 The Forgejo Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
model "code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
"code.gitea.io/gitea/models/unittest"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// TestBlockUser will ensure that when you block a user, certain actions have
|
||||
// been taken, like unfollowing each other etc.
|
||||
func TestBlockUser(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5})
|
||||
blockedUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
||||
|
||||
t.Run("Follow", func(t *testing.T) {
|
||||
defer user_model.UnblockUser(db.DefaultContext, doer.ID, blockedUser.ID)
|
||||
|
||||
// Follow each other.
|
||||
assert.NoError(t, user_model.FollowUser(db.DefaultContext, doer.ID, blockedUser.ID))
|
||||
assert.NoError(t, user_model.FollowUser(db.DefaultContext, blockedUser.ID, doer.ID))
|
||||
|
||||
assert.NoError(t, BlockUser(db.DefaultContext, doer.ID, blockedUser.ID))
|
||||
|
||||
// Ensure they aren't following each other anymore.
|
||||
assert.False(t, user_model.IsFollowing(db.DefaultContext, doer.ID, blockedUser.ID))
|
||||
assert.False(t, user_model.IsFollowing(db.DefaultContext, blockedUser.ID, doer.ID))
|
||||
})
|
||||
|
||||
t.Run("Watch", func(t *testing.T) {
|
||||
defer user_model.UnblockUser(db.DefaultContext, doer.ID, blockedUser.ID)
|
||||
|
||||
// Blocked user watch repository of doer.
|
||||
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: doer.ID})
|
||||
assert.NoError(t, repo_model.WatchRepo(db.DefaultContext, blockedUser.ID, repo.ID, true))
|
||||
|
||||
assert.NoError(t, BlockUser(db.DefaultContext, doer.ID, blockedUser.ID))
|
||||
|
||||
// Ensure blocked user isn't following doer's repository.
|
||||
assert.False(t, repo_model.IsWatching(db.DefaultContext, blockedUser.ID, repo.ID))
|
||||
})
|
||||
|
||||
t.Run("Collaboration", func(t *testing.T) {
|
||||
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 16})
|
||||
blockedUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 18})
|
||||
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 22, OwnerID: doer.ID})
|
||||
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 21, OwnerID: doer.ID})
|
||||
defer user_model.UnblockUser(db.DefaultContext, doer.ID, blockedUser.ID)
|
||||
|
||||
isBlockedUserCollab := func(repo *repo_model.Repository) bool {
|
||||
isCollaborator, err := repo_model.IsCollaborator(db.DefaultContext, repo.ID, blockedUser.ID)
|
||||
assert.NoError(t, err)
|
||||
return isCollaborator
|
||||
}
|
||||
|
||||
assert.True(t, isBlockedUserCollab(repo1))
|
||||
assert.True(t, isBlockedUserCollab(repo2))
|
||||
|
||||
assert.NoError(t, BlockUser(db.DefaultContext, doer.ID, blockedUser.ID))
|
||||
|
||||
assert.False(t, isBlockedUserCollab(repo1))
|
||||
assert.False(t, isBlockedUserCollab(repo2))
|
||||
})
|
||||
|
||||
t.Run("Pending transfers", func(t *testing.T) {
|
||||
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
||||
blockedUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3})
|
||||
defer user_model.UnblockUser(db.DefaultContext, doer.ID, blockedUser.ID)
|
||||
|
||||
unittest.AssertExistsIf(t, true, &repo_model.Repository{ID: 3, OwnerID: blockedUser.ID, Status: repo_model.RepositoryPendingTransfer})
|
||||
unittest.AssertExistsIf(t, true, &model.RepoTransfer{ID: 1, RecipientID: doer.ID, DoerID: blockedUser.ID})
|
||||
|
||||
assert.NoError(t, BlockUser(db.DefaultContext, doer.ID, blockedUser.ID))
|
||||
|
||||
unittest.AssertExistsIf(t, false, &model.RepoTransfer{ID: 1, RecipientID: doer.ID, DoerID: blockedUser.ID})
|
||||
|
||||
// Don't use AssertExistsIf, as it doesn't include the zero values in the condition such as `repo_model.RepositoryReady`.
|
||||
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3, OwnerID: blockedUser.ID})
|
||||
assert.Equal(t, repo_model.RepositoryReady, repo.Status)
|
||||
})
|
||||
}
|
|
@ -23,6 +23,7 @@ import (
|
|||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
issue_service "code.gitea.io/gitea/services/issue"
|
||||
|
||||
"xorm.io/builder"
|
||||
)
|
||||
|
@ -92,6 +93,8 @@ func deleteUser(ctx context.Context, u *user_model.User, purge bool) (err error)
|
|||
&pull_model.ReviewState{UserID: u.ID},
|
||||
&user_model.Redirect{RedirectUserID: u.ID},
|
||||
&actions_model.ActionRunner{OwnerID: u.ID},
|
||||
&user_model.BlockedUser{BlockID: u.ID},
|
||||
&user_model.BlockedUser{UserID: u.ID},
|
||||
); err != nil {
|
||||
return fmt.Errorf("deleteBeans: %w", err)
|
||||
}
|
||||
|
@ -127,6 +130,31 @@ func deleteUser(ctx context.Context, u *user_model.User, purge bool) (err error)
|
|||
}
|
||||
}
|
||||
|
||||
// ***** START: Issues *****
|
||||
if purge {
|
||||
const batchSize = 50
|
||||
|
||||
for {
|
||||
issues := make([]*issues_model.Issue, 0, batchSize)
|
||||
if err = e.Where("poster_id=?", u.ID).Limit(batchSize, 0).Find(&issues); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(issues) == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
for _, issue := range issues {
|
||||
// NOTE: Don't open git repositories just to remove the reference data,
|
||||
// `git gc` is able to remove that reference which is run as a cron job
|
||||
// by default. Also use the deleted user as doer to delete the issue.
|
||||
if err = issue_service.DeleteIssue(ctx, u, nil, issue); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// ***** END: Issues *****
|
||||
|
||||
// ***** START: Branch Protections *****
|
||||
{
|
||||
const batchSize = 50
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue