forked from forgejo/forgejo
[MODERATION] user blocking
- Add the ability to block a user via their profile page. - This will unstar their repositories and visa versa. - Blocked users cannot create issues or pull requests on your the doer's repositories (mind that this is not the case for organizations). - Blocked users cannot comment on the doer's opened issues or pull requests. - Blocked users cannot add reactions to doer's comments. - Blocked users cannot cause a notification trough mentioning the doer. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/540 (cherry picked from commit687d852480
) (cherry picked from commit0c32a4fde5
) (cherry picked from commit1791130e3c
) (cherry picked from commit37858b7e8f
) (cherry picked from commita3e2bfd7e9
) (cherry picked from commit7009b9fe87
) Conflicts: https://codeberg.org/forgejo/forgejo/pulls/1014 routers/web/user/profile.go templates/user/profile.tmpl (cherry picked from commitb2aec34791
) (cherry picked from commite2f1b73752
) [MODERATION] organization blocking a user (#802) - Resolves #476 - Follow up for: #540 - Ensure that the doer and blocked person cannot follow each other. - Ensure that the block person cannot watch doer's repositories. - Add unblock button to the blocked user list. - Add blocked since information to the blocked user list. - Add extra testing to moderation code. - Blocked user will unwatch doer's owned repository upon blocking. - Add flash messages to let the user know the block/unblock action was successful. - Add "You haven't blocked any users" message. - Add organization blocking a user. Co-authored-by: Gusted <postmaster@gusted.xyz> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/802 (cherry picked from commit0505a10421
) (cherry picked from commit37b4e6ef9b
) (cherry picked from commitc17c121f2c
) [MODERATION] organization blocking a user (#802) (squash) Changes to adapt to:6bbccdd177
Improve AJAX link and modal confirm dialog (#25210) Refs: https://codeberg.org/forgejo/forgejo/pulls/882/files#issuecomment-945962 Refs: https://codeberg.org/forgejo/forgejo/pulls/882#issue-330561 (cherry picked from commit523635f83c
) (cherry picked from commit4743eaa6a0
) (cherry picked from commiteff5b43d2e
) Conflicts: https://codeberg.org/forgejo/forgejo/pulls/1014 routers/web/user/profile.go (cherry picked from commit9d359be5ed
) (cherry picked from commitb1f3069a22
) [MODERATION] add user blocking API - Follow up for: #540, #802 - Add API routes for user blocking from user and organization perspective. - The new routes have integration testing. - The new model functions have unit tests. - Actually quite boring to write and to read this pull request. (cherry picked from commitf3afaf15c7
) (cherry picked from commit6d754db3e5
) (cherry picked from commit2a89ddc0ac
) (cherry picked from commit4a147bff7e
) Conflicts: routers/api/v1/api.go templates/swagger/v1_json.tmpl (cherry picked from commitbb8c339185
) (cherry picked from commit5a11569a01
) (cherry picked from commit2373c801ee
) [MODERATION] restore redirect on unblock ctx.RedirectToFirst(ctx.FormString("redirect_to"), ctx.ContextUser.HomeLink()) was replaced by ctx.JSONOK() in128d77a3a
Following up fixes for "Fix inconsistent user profile layout across tabs" (#25739) thus changing the behavior (nicely spotted by the tests). This restores it. (cherry picked from commit597c243707
) (cherry picked from commitcfa539e590
) [MODERATION] Add test case (squash) - Add an test case, to test an property of the function. (cherry picked from commit70dadb1916
) [MODERATION] Block adding collaborators - Ensure that the doer and blocked user cannot add each other as collaborators to repositories. - The Web UI gets an detailed message of the specific situation, the API gets an generic Forbidden code. - Unit tests has been added. - Integration testing for Web and API has been added. - This commit doesn't introduce removing each other as collaborators on the block action, due to the complexity of database calls that needs to be figured out. That deserves its own commit and test code. (cherry picked from commit747be949a1
) [MODERATION] move locale_en-US.ini strings to avoid conflicts Conflicts: web_src/css/org.css web_src/css/user.css https://codeberg.org/forgejo/forgejo/pulls/1180 (cherry picked from commite53f955c88
) Conflicts: services/issue/comments.go https://codeberg.org/forgejo/forgejo/pulls/1212
This commit is contained in:
parent
ce02ef9078
commit
b4a454b576
63 changed files with 1808 additions and 66 deletions
|
@ -580,7 +580,7 @@ func NotifyWatchers(ctx context.Context, actions ...*Action) error {
|
|||
|
||||
if repoChanged {
|
||||
// Add feeds for user self and all watchers.
|
||||
watchers, err = repo_model.GetWatchers(ctx, act.RepoID)
|
||||
watchers, err = repo_model.GetWatchersExcludeBlocked(ctx, act.RepoID, act.ActUserID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("get watchers: %w", err)
|
||||
}
|
||||
|
|
|
@ -235,6 +235,15 @@ func createOrUpdateIssueNotifications(ctx context.Context, issueID, commentID, n
|
|||
for _, id := range issueUnWatches {
|
||||
toNotify.Remove(id)
|
||||
}
|
||||
|
||||
// Remove users who have the notification author blocked.
|
||||
blockedAuthorIDs, err := user_model.ListBlockedByUsersID(ctx, notificationAuthorID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, id := range blockedAuthorIDs {
|
||||
toNotify.Remove(id)
|
||||
}
|
||||
}
|
||||
|
||||
err = issue.LoadRepo(ctx)
|
||||
|
|
5
models/fixtures/forgejo_blocked_user.yml
Normal file
5
models/fixtures/forgejo_blocked_user.yml
Normal file
|
@ -0,0 +1,5 @@
|
|||
-
|
||||
id: 1
|
||||
user_id: 4
|
||||
block_id: 1
|
||||
created_unix: 1671607299
|
|
@ -37,7 +37,7 @@
|
|||
lower_name: repo2
|
||||
name: repo2
|
||||
default_branch: master
|
||||
num_watches: 0
|
||||
num_watches: 1
|
||||
num_stars: 1
|
||||
num_forks: 0
|
||||
num_issues: 2
|
||||
|
|
|
@ -26,4 +26,10 @@
|
|||
id: 5
|
||||
user_id: 11
|
||||
repo_id: 1
|
||||
mode: 3 # auto
|
||||
mode: 3 # auto
|
||||
|
||||
-
|
||||
id: 6
|
||||
user_id: 4
|
||||
repo_id: 2
|
||||
mode: 1 # normal
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
|
||||
forgejo_v1_20 "code.gitea.io/gitea/models/forgejo_migrations/v1_20"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
|
@ -34,7 +35,9 @@ func NewMigration(desc string, fn func(*xorm.Engine) error) *Migration {
|
|||
|
||||
// This is a sequence of additional Forgejo migrations.
|
||||
// Add new migrations to the bottom of the list.
|
||||
var migrations = []*Migration{}
|
||||
var migrations = []*Migration{
|
||||
NewMigration("Add Forgejo Blocked Users table", forgejo_v1_20.AddForgejoBlockedUser),
|
||||
}
|
||||
|
||||
// GetCurrentDBVersion returns the current Forgejo database version.
|
||||
func GetCurrentDBVersion(x *xorm.Engine) (int64, error) {
|
||||
|
|
21
models/forgejo_migrations/v1_20/v1.go
Normal file
21
models/forgejo_migrations/v1_20/v1.go
Normal file
|
@ -0,0 +1,21 @@
|
|||
// Copyright 2023 The Forgejo Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package forgejo_v1_20 //nolint:revive
|
||||
|
||||
import (
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
func AddForgejoBlockedUser(x *xorm.Engine) error {
|
||||
type ForgejoBlockedUser struct {
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
BlockID int64 `xorm:"index"`
|
||||
UserID int64 `xorm:"index"`
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"created"`
|
||||
}
|
||||
|
||||
return x.Sync(new(ForgejoBlockedUser))
|
||||
}
|
|
@ -429,6 +429,8 @@ func TestIssue_ResolveMentions(t *testing.T) {
|
|||
testSuccess("user2", "repo1", "user1", []string{"nonexisting"}, []int64{})
|
||||
// Public repo, doer
|
||||
testSuccess("user2", "repo1", "user1", []string{"user1"}, []int64{})
|
||||
// Public repo, blocked user
|
||||
testSuccess("user2", "repo1", "user1", []string{"user4"}, []int64{})
|
||||
// Private repo, team member
|
||||
testSuccess("user17", "big_test_private_4", "user20", []string{"user2"}, []int64{2})
|
||||
// Private repo, not a team member
|
||||
|
|
|
@ -608,9 +608,11 @@ func ResolveIssueMentionsByVisibility(ctx context.Context, issue *Issue, doer *u
|
|||
teamusers := make([]*user_model.User, 0, 20)
|
||||
if err := db.GetEngine(ctx).
|
||||
Join("INNER", "team_user", "team_user.uid = `user`.id").
|
||||
Join("LEFT", "forgejo_blocked_user", "forgejo_blocked_user.user_id = `user`.id").
|
||||
In("`team_user`.team_id", checked).
|
||||
And("`user`.is_active = ?", true).
|
||||
And("`user`.prohibit_login = ?", false).
|
||||
And(builder.Or(builder.IsNull{"`forgejo_blocked_user`.block_id"}, builder.Neq{"`forgejo_blocked_user`.block_id": doer.ID})).
|
||||
Find(&teamusers); err != nil {
|
||||
return nil, fmt.Errorf("get teams users: %w", err)
|
||||
}
|
||||
|
@ -644,8 +646,10 @@ func ResolveIssueMentionsByVisibility(ctx context.Context, issue *Issue, doer *u
|
|||
|
||||
unchecked := make([]*user_model.User, 0, len(mentionUsers))
|
||||
if err := db.GetEngine(ctx).
|
||||
Join("LEFT", "forgejo_blocked_user", "forgejo_blocked_user.user_id = `user`.id").
|
||||
Where("`user`.is_active = ?", true).
|
||||
And("`user`.prohibit_login = ?", false).
|
||||
And(builder.Or(builder.IsNull{"`forgejo_blocked_user`.block_id"}, builder.Neq{"`forgejo_blocked_user`.block_id": doer.ID})).
|
||||
In("`user`.lower_name", mentionUsers).
|
||||
Find(&unchecked); err != nil {
|
||||
return nil, fmt.Errorf("find mentioned users: %w", err)
|
||||
|
|
|
@ -218,12 +218,12 @@ type ReactionOptions struct {
|
|||
}
|
||||
|
||||
// CreateReaction creates reaction for issue or comment.
|
||||
func CreateReaction(opts *ReactionOptions) (*Reaction, error) {
|
||||
func CreateReaction(ctx context.Context, opts *ReactionOptions) (*Reaction, error) {
|
||||
if !setting.UI.ReactionsLookup.Contains(opts.Type) {
|
||||
return nil, ErrForbiddenIssueReaction{opts.Type}
|
||||
}
|
||||
|
||||
ctx, committer, err := db.TxContext(db.DefaultContext)
|
||||
ctx, committer, err := db.TxContext(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -240,25 +240,6 @@ func CreateReaction(opts *ReactionOptions) (*Reaction, error) {
|
|||
return reaction, nil
|
||||
}
|
||||
|
||||
// CreateIssueReaction creates a reaction on issue.
|
||||
func CreateIssueReaction(doerID, issueID int64, content string) (*Reaction, error) {
|
||||
return CreateReaction(&ReactionOptions{
|
||||
Type: content,
|
||||
DoerID: doerID,
|
||||
IssueID: issueID,
|
||||
})
|
||||
}
|
||||
|
||||
// CreateCommentReaction creates a reaction on comment.
|
||||
func CreateCommentReaction(doerID, issueID, commentID int64, content string) (*Reaction, error) {
|
||||
return CreateReaction(&ReactionOptions{
|
||||
Type: content,
|
||||
DoerID: doerID,
|
||||
IssueID: issueID,
|
||||
CommentID: commentID,
|
||||
})
|
||||
}
|
||||
|
||||
// DeleteReaction deletes reaction for issue or comment.
|
||||
func DeleteReaction(ctx context.Context, opts *ReactionOptions) error {
|
||||
reaction := &Reaction{
|
||||
|
|
|
@ -19,11 +19,14 @@ import (
|
|||
func addReaction(t *testing.T, doerID, issueID, commentID int64, content string) {
|
||||
var reaction *issues_model.Reaction
|
||||
var err error
|
||||
if commentID == 0 {
|
||||
reaction, err = issues_model.CreateIssueReaction(doerID, issueID, content)
|
||||
} else {
|
||||
reaction, err = issues_model.CreateCommentReaction(doerID, issueID, commentID, content)
|
||||
}
|
||||
// NOTE: This doesn't do user blocking checking.
|
||||
reaction, err = issues_model.CreateReaction(db.DefaultContext, &issues_model.ReactionOptions{
|
||||
DoerID: doerID,
|
||||
IssueID: issueID,
|
||||
CommentID: commentID,
|
||||
Type: content,
|
||||
})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, reaction)
|
||||
}
|
||||
|
@ -49,7 +52,7 @@ func TestIssueAddDuplicateReaction(t *testing.T) {
|
|||
|
||||
addReaction(t, user1.ID, issue1ID, 0, "heart")
|
||||
|
||||
reaction, err := issues_model.CreateReaction(&issues_model.ReactionOptions{
|
||||
reaction, err := issues_model.CreateReaction(db.DefaultContext, &issues_model.ReactionOptions{
|
||||
DoerID: user1.ID,
|
||||
IssueID: issue1ID,
|
||||
Type: "heart",
|
||||
|
|
|
@ -177,3 +177,16 @@ func GetIssuePostersWithSearch(ctx context.Context, repo *Repository, isPull boo
|
|||
Limit(30).
|
||||
Find(&users)
|
||||
}
|
||||
|
||||
// GetWatchedRepoIDsOwnedBy returns the repos owned by a particular user watched by a particular user
|
||||
func GetWatchedRepoIDsOwnedBy(ctx context.Context, userID, ownedByUserID int64) ([]int64, error) {
|
||||
repoIDs := make([]int64, 0, 10)
|
||||
err := db.GetEngine(ctx).
|
||||
Table("repository").
|
||||
Select("`repository`.id").
|
||||
Join("LEFT", "watch", "`repository`.id=`watch`.repo_id").
|
||||
Where("`watch`.user_id=?", userID).
|
||||
And("`watch`.mode<>?", WatchModeDont).
|
||||
And("`repository`.owner_id=?", ownedByUserID).Find(&repoIDs)
|
||||
return repoIDs, err
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"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"
|
||||
)
|
||||
|
@ -71,3 +72,15 @@ func TestRepoGetReviewers(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
assert.Len(t, reviewers, 1)
|
||||
}
|
||||
|
||||
func GetWatchedRepoIDsOwnedBy(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 9})
|
||||
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
||||
|
||||
repoIDs, err := repo_model.GetWatchedRepoIDsOwnedBy(db.DefaultContext, user1.ID, user2.ID)
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, repoIDs, 1)
|
||||
assert.EqualValues(t, 1, repoIDs[0])
|
||||
}
|
||||
|
|
|
@ -10,6 +10,8 @@ import (
|
|||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"xorm.io/builder"
|
||||
)
|
||||
|
||||
// WatchMode specifies what kind of watch the user has on a repository
|
||||
|
@ -142,6 +144,21 @@ func GetWatchers(ctx context.Context, repoID int64) ([]*Watch, error) {
|
|||
Find(&watches)
|
||||
}
|
||||
|
||||
// GetWatchersExcludeBlocked returns all watchers of given repository, whereby
|
||||
// the doer isn't blocked by one of the watchers.
|
||||
func GetWatchersExcludeBlocked(ctx context.Context, repoID, doerID int64) ([]*Watch, error) {
|
||||
watches := make([]*Watch, 0, 10)
|
||||
return watches, db.GetEngine(ctx).
|
||||
Join("INNER", "`user`", "`user`.id = `watch`.user_id").
|
||||
Join("LEFT", "forgejo_blocked_user", "forgejo_blocked_user.user_id = `watch`.user_id").
|
||||
Where("`watch`.repo_id=?", repoID).
|
||||
And("`watch`.mode<>?", WatchModeDont).
|
||||
And("`user`.is_active=?", true).
|
||||
And("`user`.prohibit_login=?", false).
|
||||
And(builder.Or(builder.IsNull{"`forgejo_blocked_user`.block_id"}, builder.Neq{"`forgejo_blocked_user`.block_id": doerID})).
|
||||
Find(&watches)
|
||||
}
|
||||
|
||||
// GetRepoWatchersIDs returns IDs of watchers for a given repo ID
|
||||
// but avoids joining with `user` for performance reasons
|
||||
// User permissions must be verified elsewhere if required
|
||||
|
@ -184,3 +201,9 @@ func WatchIfAuto(ctx context.Context, userID, repoID int64, isWrite bool) error
|
|||
}
|
||||
return watchRepoMode(ctx, watch, WatchModeAuto)
|
||||
}
|
||||
|
||||
// UnwatchRepos will unwatch the user from all given repositories.
|
||||
func UnwatchRepos(ctx context.Context, userID int64, repoIDs []int64) error {
|
||||
_, err := db.GetEngine(ctx).Where("user_id=?", userID).In("repo_id", repoIDs).Delete(&Watch{})
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -43,6 +43,24 @@ func TestGetWatchers(t *testing.T) {
|
|||
assert.Len(t, watches, 0)
|
||||
}
|
||||
|
||||
func TestGetWatchersExcludeBlocked(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
||||
watches, err := repo_model.GetWatchersExcludeBlocked(db.DefaultContext, repo.ID, 1)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// One watchers are inactive and one watcher is blocked, thus minus 2
|
||||
assert.Len(t, watches, repo.NumWatches-2)
|
||||
for _, watch := range watches {
|
||||
assert.EqualValues(t, repo.ID, watch.RepoID)
|
||||
}
|
||||
|
||||
watches, err = repo_model.GetWatchersExcludeBlocked(db.DefaultContext, unittest.NonexistentID, 1)
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, watches, 0)
|
||||
}
|
||||
|
||||
func TestRepository_GetWatchers(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
|
@ -137,3 +155,16 @@ func TestWatchRepoMode(t *testing.T) {
|
|||
assert.NoError(t, repo_model.WatchRepoMode(12, 1, repo_model.WatchModeNone))
|
||||
unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1}, 0)
|
||||
}
|
||||
|
||||
func TestUnwatchRepos(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
unittest.AssertExistsAndLoadBean(t, &repo_model.Watch{UserID: 4, RepoID: 1})
|
||||
unittest.AssertExistsAndLoadBean(t, &repo_model.Watch{UserID: 4, RepoID: 2})
|
||||
|
||||
err := repo_model.UnwatchRepos(db.DefaultContext, 4, []int64{1, 2})
|
||||
assert.NoError(t, err)
|
||||
|
||||
unittest.AssertNotExistsBean(t, &repo_model.Watch{UserID: 4, RepoID: 1})
|
||||
unittest.AssertNotExistsBean(t, &repo_model.Watch{UserID: 4, RepoID: 2})
|
||||
}
|
||||
|
|
91
models/user/block.go
Normal file
91
models/user/block.go
Normal file
|
@ -0,0 +1,91 @@
|
|||
// Copyright 2023 The Forgejo Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
)
|
||||
|
||||
// ErrBlockedByUser defines an error stating that the user is not allowed to perform the action because they are blocked.
|
||||
var ErrBlockedByUser = errors.New("user is blocked by the poster or repository owner")
|
||||
|
||||
// BlockedUser represents a blocked user entry.
|
||||
type BlockedUser struct {
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
// UID of the one who got blocked.
|
||||
BlockID int64 `xorm:"index"`
|
||||
// UID of the one who did the block action.
|
||||
UserID int64 `xorm:"index"`
|
||||
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"created"`
|
||||
}
|
||||
|
||||
// TableName provides the real table name
|
||||
func (*BlockedUser) TableName() string {
|
||||
return "forgejo_blocked_user"
|
||||
}
|
||||
|
||||
func init() {
|
||||
db.RegisterModel(new(BlockedUser))
|
||||
}
|
||||
|
||||
// IsBlocked returns if userID has blocked blockID.
|
||||
func IsBlocked(ctx context.Context, userID, blockID int64) bool {
|
||||
has, _ := db.GetEngine(ctx).Exist(&BlockedUser{UserID: userID, BlockID: blockID})
|
||||
return has
|
||||
}
|
||||
|
||||
// IsBlockedMultiple returns if one of the userIDs has blocked blockID.
|
||||
func IsBlockedMultiple(ctx context.Context, userIDs []int64, blockID int64) bool {
|
||||
has, _ := db.GetEngine(ctx).In("user_id", userIDs).Exist(&BlockedUser{BlockID: blockID})
|
||||
return has
|
||||
}
|
||||
|
||||
// UnblockUser removes the blocked user entry.
|
||||
func UnblockUser(ctx context.Context, userID, blockID int64) error {
|
||||
_, err := db.GetEngine(ctx).Delete(&BlockedUser{UserID: userID, BlockID: blockID})
|
||||
return err
|
||||
}
|
||||
|
||||
// CountBlockedUsers returns the number of users the user has blocked.
|
||||
func CountBlockedUsers(ctx context.Context, userID int64) (int64, error) {
|
||||
return db.GetEngine(ctx).Where("user_id=?", userID).Count(&BlockedUser{})
|
||||
}
|
||||
|
||||
// ListBlockedUsers returns the users that the user has blocked.
|
||||
// The created_unix field of the user struct is overridden by the creation_unix
|
||||
// field of blockeduser.
|
||||
func ListBlockedUsers(ctx context.Context, userID int64, opts db.ListOptions) ([]*User, error) {
|
||||
sess := db.GetEngine(ctx).
|
||||
Select("`forgejo_blocked_user`.created_unix, `user`.*").
|
||||
Join("INNER", "forgejo_blocked_user", "`user`.id=`forgejo_blocked_user`.block_id").
|
||||
Where("`forgejo_blocked_user`.user_id=?", userID)
|
||||
|
||||
if opts.Page > 0 {
|
||||
sess = db.SetSessionPagination(sess, &opts)
|
||||
users := make([]*User, 0, opts.PageSize)
|
||||
|
||||
return users, sess.Find(&users)
|
||||
}
|
||||
|
||||
users := make([]*User, 0, 8)
|
||||
return users, sess.Find(&users)
|
||||
}
|
||||
|
||||
// ListBlockedByUsersID returns the ids of the users that blocked the user.
|
||||
func ListBlockedByUsersID(ctx context.Context, userID int64) ([]int64, error) {
|
||||
users := make([]int64, 0, 8)
|
||||
err := db.GetEngine(ctx).
|
||||
Table("user").
|
||||
Select("`user`.id").
|
||||
Join("INNER", "forgejo_blocked_user", "`user`.id=`forgejo_blocked_user`.user_id").
|
||||
Where("`forgejo_blocked_user`.block_id=?", userID).
|
||||
Find(&users)
|
||||
|
||||
return users, err
|
||||
}
|
77
models/user/block_test.go
Normal file
77
models/user/block_test.go
Normal file
|
@ -0,0 +1,77 @@
|
|||
// Copyright 2023 The Forgejo Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package user_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/models/unittest"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestIsBlocked(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
assert.True(t, user_model.IsBlocked(db.DefaultContext, 4, 1))
|
||||
|
||||
// Simple test cases to ensure the function can also respond with false.
|
||||
assert.False(t, user_model.IsBlocked(db.DefaultContext, 1, 1))
|
||||
assert.False(t, user_model.IsBlocked(db.DefaultContext, 3, 2))
|
||||
}
|
||||
|
||||
func TestIsBlockedMultiple(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
assert.True(t, user_model.IsBlockedMultiple(db.DefaultContext, []int64{4}, 1))
|
||||
assert.True(t, user_model.IsBlockedMultiple(db.DefaultContext, []int64{4, 3, 4, 5}, 1))
|
||||
|
||||
// Simple test cases to ensure the function can also respond with false.
|
||||
assert.False(t, user_model.IsBlockedMultiple(db.DefaultContext, []int64{1}, 1))
|
||||
assert.False(t, user_model.IsBlockedMultiple(db.DefaultContext, []int64{3, 4, 1}, 2))
|
||||
}
|
||||
|
||||
func TestUnblockUser(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
assert.True(t, user_model.IsBlocked(db.DefaultContext, 4, 1))
|
||||
|
||||
assert.NoError(t, user_model.UnblockUser(db.DefaultContext, 4, 1))
|
||||
|
||||
// Simple test cases to ensure the function can also respond with false.
|
||||
assert.False(t, user_model.IsBlocked(db.DefaultContext, 4, 1))
|
||||
}
|
||||
|
||||
func TestListBlockedUsers(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
blockedUsers, err := user_model.ListBlockedUsers(db.DefaultContext, 4, db.ListOptions{})
|
||||
assert.NoError(t, err)
|
||||
if assert.Len(t, blockedUsers, 1) {
|
||||
assert.EqualValues(t, 1, blockedUsers[0].ID)
|
||||
// The function returns the created Unix of the block, not that of the user.
|
||||
assert.EqualValues(t, 1671607299, blockedUsers[0].CreatedUnix)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListBlockedByUsersID(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
blockedByUserIDs, err := user_model.ListBlockedByUsersID(db.DefaultContext, 1)
|
||||
assert.NoError(t, err)
|
||||
if assert.Len(t, blockedByUserIDs, 1) {
|
||||
assert.EqualValues(t, 4, blockedByUserIDs[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestCountBlockedUsers(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
count, err := user_model.CountBlockedUsers(db.DefaultContext, 4)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 1, count)
|
||||
|
||||
count, err = user_model.CountBlockedUsers(db.DefaultContext, 1)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 0, count)
|
||||
}
|
|
@ -4,6 +4,8 @@
|
|||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
)
|
||||
|
@ -22,17 +24,26 @@ func init() {
|
|||
|
||||
// IsFollowing returns true if user is following followID.
|
||||
func IsFollowing(userID, followID int64) bool {
|
||||
has, _ := db.GetEngine(db.DefaultContext).Get(&Follow{UserID: userID, FollowID: followID})
|
||||
return IsFollowingCtx(db.DefaultContext, userID, followID)
|
||||
}
|
||||
|
||||
// IsFollowingCtx returns true if user is following followID.
|
||||
func IsFollowingCtx(ctx context.Context, userID, followID int64) bool {
|
||||
has, _ := db.GetEngine(ctx).Get(&Follow{UserID: userID, FollowID: followID})
|
||||
return has
|
||||
}
|
||||
|
||||
// FollowUser marks someone be another's follower.
|
||||
func FollowUser(userID, followID int64) (err error) {
|
||||
if userID == followID || IsFollowing(userID, followID) {
|
||||
func FollowUser(ctx context.Context, userID, followID int64) (err error) {
|
||||
if userID == followID || IsFollowingCtx(ctx, userID, followID) {
|
||||
return nil
|
||||
}
|
||||
|
||||
ctx, committer, err := db.TxContext(db.DefaultContext)
|
||||
if IsBlocked(ctx, userID, followID) || IsBlocked(ctx, followID, userID) {
|
||||
return ErrBlockedByUser
|
||||
}
|
||||
|
||||
ctx, committer, err := db.TxContext(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -53,12 +64,12 @@ func FollowUser(userID, followID int64) (err error) {
|
|||
}
|
||||
|
||||
// UnfollowUser unmarks someone as another's follower.
|
||||
func UnfollowUser(userID, followID int64) (err error) {
|
||||
if userID == followID || !IsFollowing(userID, followID) {
|
||||
func UnfollowUser(ctx context.Context, userID, followID int64) (err error) {
|
||||
if userID == followID || !IsFollowingCtx(ctx, userID, followID) {
|
||||
return nil
|
||||
}
|
||||
|
||||
ctx, committer, err := db.TxContext(db.DefaultContext)
|
||||
ctx, committer, err := db.TxContext(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -449,13 +449,19 @@ func TestFollowUser(t *testing.T) {
|
|||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
testSuccess := func(followerID, followedID int64) {
|
||||
assert.NoError(t, user_model.FollowUser(followerID, followedID))
|
||||
assert.NoError(t, user_model.FollowUser(db.DefaultContext, followerID, followedID))
|
||||
unittest.AssertExistsAndLoadBean(t, &user_model.Follow{UserID: followerID, FollowID: followedID})
|
||||
}
|
||||
testSuccess(4, 2)
|
||||
testSuccess(5, 2)
|
||||
|
||||
assert.NoError(t, user_model.FollowUser(2, 2))
|
||||
assert.NoError(t, user_model.FollowUser(db.DefaultContext, 2, 2))
|
||||
|
||||
// Blocked user.
|
||||
assert.ErrorIs(t, user_model.ErrBlockedByUser, user_model.FollowUser(db.DefaultContext, 1, 4))
|
||||
assert.ErrorIs(t, user_model.ErrBlockedByUser, user_model.FollowUser(db.DefaultContext, 4, 1))
|
||||
unittest.AssertNotExistsBean(t, &user_model.Follow{UserID: 1, FollowID: 4})
|
||||
unittest.AssertNotExistsBean(t, &user_model.Follow{UserID: 4, FollowID: 1})
|
||||
|
||||
unittest.CheckConsistencyFor(t, &user_model.User{})
|
||||
}
|
||||
|
@ -464,7 +470,7 @@ func TestUnfollowUser(t *testing.T) {
|
|||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
testSuccess := func(followerID, followedID int64) {
|
||||
assert.NoError(t, user_model.UnfollowUser(followerID, followedID))
|
||||
assert.NoError(t, user_model.UnfollowUser(db.DefaultContext, followerID, followedID))
|
||||
unittest.AssertNotExistsBean(t, &user_model.Follow{UserID: followerID, FollowID: followedID})
|
||||
}
|
||||
testSuccess(4, 2)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue