1
0
Fork 0
forked from forgejo/forgejo

[MODERATION] Refactor excluding watchers mechanism (squash)

This solves two bugs. One bug is that due to the JOIN with the
`forgejo_blocked_users` table, duplicated users were generated if a user
had more than one user blocked, this lead to receiving more than one
entry in the actions table. The other bug is that if a user blocked more
than one user, it would still receive a action entry by a
blocked user, because the SQL query would not exclude the other
duplicated users that was generated by the JOIN.

The new solution is somewhat non-optimal in my eyes, but it's better
than rewriting the query to become a potential perfomance blocker (usage
of WHERE IN, which cannot be rewritten to a JOIN). It simply removes the
watchers after it was retrieved by the SQL query.
This commit is contained in:
Gusted 2024-01-15 00:22:06 +01:00
parent cd1eadd923
commit c63c00b39b
No known key found for this signature in database
GPG key ID: FD821B732837125F
5 changed files with 87 additions and 17 deletions

View file

@ -11,6 +11,8 @@ import (
"strconv"
"testing"
"code.gitea.io/gitea/models/activities"
"code.gitea.io/gitea/models/db"
issue_model "code.gitea.io/gitea/models/issues"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
@ -390,3 +392,45 @@ func TestBlockActions(t *testing.T) {
)
})
}
func TestBlockedNotification(t *testing.T) {
defer tests.AddFixtures("tests/integration/fixtures/TestBlockedNotifications")()
defer tests.PrepareTestEnv(t)()
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
normalUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
blockedUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 10})
issue := unittest.AssertExistsAndLoadBean(t, &issue_model.Issue{ID: 1000})
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue.RepoID})
issueURL := fmt.Sprintf("%s/issues/%d", repo.FullName(), issue.Index)
notificationBean := &activities.Notification{UserID: doer.ID, RepoID: repo.ID, IssueID: issue.ID}
assert.False(t, user_model.IsBlocked(db.DefaultContext, doer.ID, normalUser.ID))
BlockUser(t, doer, blockedUser)
mentionDoer := func(t *testing.T, session *TestSession) {
t.Helper()
req := NewRequestWithValues(t, "POST", issueURL+"/comments", map[string]string{
"_csrf": GetCSRF(t, session, issueURL),
"content": "I'm annoying. Pinging @" + doer.Name,
})
session.MakeRequest(t, req, http.StatusOK)
}
t.Run("Blocks notification of blocked user", func(t *testing.T) {
session := loginUser(t, blockedUser.Name)
unittest.AssertNotExistsBean(t, notificationBean)
mentionDoer(t, session)
unittest.AssertNotExistsBean(t, notificationBean)
})
t.Run("Do not block notifications of normal user", func(t *testing.T) {
session := loginUser(t, normalUser.Name)
unittest.AssertNotExistsBean(t, notificationBean)
mentionDoer(t, session)
unittest.AssertExistsAndLoadBean(t, notificationBean)
})
}