Skip to content

Commit

Permalink
Add user blocking (#29028)
Browse files Browse the repository at this point in the history
Fixes #17453

This PR adds the abbility to block a user from a personal account or
organization to restrict how the blocked user can interact with the
blocker. The docs explain what's the consequence of blocking a user.

Screenshots:


![grafik](https://github.com/go-gitea/gitea/assets/1666336/4ed884f3-e06a-4862-afd3-3b8aa2488dc6)


![grafik](https://github.com/go-gitea/gitea/assets/1666336/ae6d4981-f252-4f50-a429-04f0f9f1cdf1)


![grafik](https://github.com/go-gitea/gitea/assets/1666336/ca153599-5b0f-4b4a-90fe-18bdfd6f0b6b)

---------

Co-authored-by: Lauris BH <lauris@nix.lv>
  • Loading branch information
KN4CK3R and lafriks authored Mar 4, 2024
1 parent 8e12ba3 commit c337ff0
Show file tree
Hide file tree
Showing 109 changed files with 2,873 additions and 543 deletions.
56 changes: 56 additions & 0 deletions docs/content/usage/blocking-users.en-us.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
date: "2024-01-31T00:00:00+00:00"
title: "Blocking a user"
slug: "blocking-user"
sidebar_position: 25
toc: false
draft: false
aliases:
- /en-us/webhooks
menu:
sidebar:
parent: "usage"
name: "Blocking a user"
sidebar_position: 30
identifier: "blocking-user"
---

# Blocking a user

Gitea supports blocking of users to restrict how they can interact with you and your content.

You can block a user in your account settings, from the user's profile or from comments created by the user.
The user is not directly notified about the block, but they can notice they are blocked when they attempt to interact with you.
Organization owners can block anyone who is not a member of the organization too.
If a blocked user has admin permissions, they can still perform all actions even if blocked.

### When you block a user

- the user stops following you
- you stop following the user
- the user's stars are removed from your repositories
- your stars are removed from their repositories
- the user stops watching your repositories
- you stop watching their repositories
- the user's issue assignments are removed from your repositories
- your issue assignments are removed from their repositories
- the user is removed as a collaborator on your repositories
- you are removed as a collaborator on their repositories
- any pending repository transfers to or from the blocked user are canceled

### When you block a user, the user cannot

- follow you
- watch your repositories
- star your repositories
- fork your repositories
- transfer repositories to you
- open issues or pull requests on your repositories
- comment on issues or pull requests you've created
- comment on issues or pull requests on your repositories
- react to your comments on issues or pull requests
- react to comments on issues or pull requests on your repositories
- assign you to issues or pull requests
- add you as a collaborator on their repositories
- send you notifications by @mentioning your username
- be added as team member (if blocked by an organization)
50 changes: 31 additions & 19 deletions models/fixtures/access.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,120 +42,132 @@

-
id: 8
user_id: 15
user_id: 10
repo_id: 21
mode: 2

-
id: 9
user_id: 10
repo_id: 32
mode: 2

-
id: 10
user_id: 15
repo_id: 21
mode: 2

-
id: 11
user_id: 15
repo_id: 22
mode: 2

-
id: 10
id: 12
user_id: 15
repo_id: 23
mode: 4

-
id: 11
id: 13
user_id: 15
repo_id: 24
mode: 4

-
id: 12
id: 14
user_id: 15
repo_id: 32
mode: 2

-
id: 13
id: 15
user_id: 18
repo_id: 21
mode: 2

-
id: 14
id: 16
user_id: 18
repo_id: 22
mode: 2

-
id: 15
id: 17
user_id: 18
repo_id: 23
mode: 4

-
id: 16
id: 18
user_id: 18
repo_id: 24
mode: 4

-
id: 17
id: 19
user_id: 20
repo_id: 24
mode: 1

-
id: 18
id: 20
user_id: 20
repo_id: 27
mode: 4

-
id: 19
id: 21
user_id: 20
repo_id: 28
mode: 4

-
id: 20
id: 22
user_id: 29
repo_id: 4
mode: 2

-
id: 21
id: 23
user_id: 29
repo_id: 24
mode: 1

-
id: 22
id: 24
user_id: 31
repo_id: 27
mode: 4

-
id: 23
id: 25
user_id: 31
repo_id: 28
mode: 4

-
id: 24
id: 26
user_id: 38
repo_id: 60
mode: 2

-
id: 25
id: 27
user_id: 38
repo_id: 61
mode: 1

-
id: 26
id: 28
user_id: 39
repo_id: 61
mode: 1

-
id: 27
id: 29
user_id: 40
repo_id: 61
mode: 4
12 changes: 12 additions & 0 deletions models/fixtures/collaboration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,15 @@
repo_id: 60
user_id: 38
mode: 2 # write

-
id: 10
repo_id: 21
user_id: 10
mode: 2 # write

-
id: 11
repo_id: 32
user_id: 10
mode: 2 # write
4 changes: 4 additions & 0 deletions models/fixtures/issue_assignees.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@
id: 4
assignee_id: 2
issue_id: 17
-
id: 5
assignee_id: 10
issue_id: 6
16 changes: 16 additions & 0 deletions models/fixtures/repo_transfer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,19 @@
repo_id: 3
created_unix: 1553610671
updated_unix: 1553610671

-
id: 2
doer_id: 16
recipient_id: 10
repo_id: 21
created_unix: 1553610671
updated_unix: 1553610671

-
id: 3
doer_id: 3
recipient_id: 10
repo_id: 32
created_unix: 1553610671
updated_unix: 1553610671
8 changes: 4 additions & 4 deletions models/fixtures/repository.yml
Original file line number Diff line number Diff line change
Expand Up @@ -614,8 +614,8 @@
owner_name: user16
lower_name: big_test_public_3
name: big_test_public_3
num_watches: 0
num_stars: 0
num_watches: 1
num_stars: 1
num_forks: 0
num_issues: 0
num_closed_issues: 0
Expand Down Expand Up @@ -945,8 +945,8 @@
owner_name: org3
lower_name: repo21
name: repo21
num_watches: 0
num_stars: 0
num_watches: 1
num_stars: 1
num_forks: 0
num_issues: 2
num_closed_issues: 0
Expand Down
10 changes: 10 additions & 0 deletions models/fixtures/star.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@
id: 2
uid: 2
repo_id: 4

-
id: 3
uid: 10
repo_id: 21

-
id: 4
uid: 10
repo_id: 32
2 changes: 1 addition & 1 deletion models/fixtures/user.yml
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@
use_custom_avatar: false
num_followers: 0
num_following: 0
num_stars: 0
num_stars: 2
num_repos: 3
num_teams: 0
num_members: 0
Expand Down
19 changes: 19 additions & 0 deletions models/fixtures/user_blocking.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-
id: 1
blocker_id: 2
blockee_id: 29

-
id: 2
blocker_id: 17
blockee_id: 28

-
id: 3
blocker_id: 2
blockee_id: 34

-
id: 4
blocker_id: 50
blockee_id: 34
12 changes: 12 additions & 0 deletions models/fixtures/watch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,15 @@
user_id: 11
repo_id: 1
mode: 3 # auto

-
id: 6
user_id: 10
repo_id: 21
mode: 1 # normal

-
id: 7
user_id: 10
repo_id: 32
mode: 1 # normal
21 changes: 21 additions & 0 deletions models/issues/assignees.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,27 @@ func IsUserAssignedToIssue(ctx context.Context, issue *Issue, user *user_model.U
return db.Exist[IssueAssignees](ctx, builder.Eq{"assignee_id": user.ID, "issue_id": issue.ID})
}

type AssignedIssuesOptions struct {
db.ListOptions
AssigneeID int64
RepoOwnerID int64
}

func (opts *AssignedIssuesOptions) ToConds() builder.Cond {
cond := builder.NewCond()
if opts.AssigneeID != 0 {
cond = cond.And(builder.In("issue.id", builder.Select("issue_id").From("issue_assignees").Where(builder.Eq{"assignee_id": opts.AssigneeID})))
}
if opts.RepoOwnerID != 0 {
cond = cond.And(builder.In("issue.repo_id", builder.Select("id").From("repository").Where(builder.Eq{"owner_id": opts.RepoOwnerID})))
}
return cond
}

func GetAssignedIssues(ctx context.Context, opts *AssignedIssuesOptions) ([]*Issue, int64, error) {
return db.FindAndCount[Issue](ctx, opts)
}

// ToggleIssueAssignee changes a user between assigned and not assigned for this issue, and make issue comment for it.
func ToggleIssueAssignee(ctx context.Context, issue *Issue, doer *user_model.User, assigneeID int64) (removed bool, comment *Comment, err error) {
ctx, committer, err := db.TxContext(ctx)
Expand Down
9 changes: 9 additions & 0 deletions models/issues/issue_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,15 @@ func FindAndUpdateIssueMentions(ctx context.Context, issue *Issue, doer *user_mo
if err != nil {
return nil, fmt.Errorf("UpdateIssueMentions [%d]: %w", issue.ID, err)
}

notBlocked := make([]*user_model.User, 0, len(mentions))
for _, user := range mentions {
if !user_model.IsUserBlockedBy(ctx, doer, user.ID) {
notBlocked = append(notBlocked, user)
}
}
mentions = notBlocked

if err = UpdateIssueMentions(ctx, issue.ID, mentions); err != nil {
return nil, fmt.Errorf("UpdateIssueMentions [%d]: %w", issue.ID, err)
}
Expand Down
4 changes: 4 additions & 0 deletions models/issues/issue_xref.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ func (issue *Issue) verifyReferencedIssue(stdCtx context.Context, ctx *crossRefe
if !perm.CanReadIssuesOrPulls(refIssue.IsPull) {
return nil, references.XRefActionNone, nil
}
if user_model.IsUserBlockedBy(stdCtx, ctx.Doer, refIssue.PosterID, refIssue.Repo.OwnerID) {
return nil, references.XRefActionNone, nil
}

// Accept close/reopening actions only if the poster is able to close the
// referenced issue manually at this moment. The only exception is
// the poster of a new PR referencing an issue on the same repo: then the merger
Expand Down
Loading

0 comments on commit c337ff0

Please sign in to comment.