mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-14 05:56:14 +01:00
9ba48419ba
Before explaining the fix itself, lets look at the `action` table, and
how it is populated. Data is only ever inserted into it via
`activities_model.NotifyWatchers`, which will:
- Insert a row for each activity with `UserID` set to the acting user's
ID - this is the original activity, and is always inserted if anything
is to be inserted at all.
- It will insert a copy of each activity with the `UserID` set to the
repo's owner, if the owner is an Organization, and isn't the acting
user.
- It will insert a copy of each activity for every watcher of the repo,
as long as the watcher in question has read permission to the repo
unit the activity is about.
This means that if a repository belongs to an organizations, for most
activities, it will have at least two rows in the table. For
repositories watched by people other than their owner, an additional row
for each watcher.
These are useful duplicates, because they record which activities are
relevant for a particular user. However, for cases where we wish to see
the activities that happen around a repository, without limiting the
results to a particular user, we're *not* interested in the duplicates
stored for the watchers and the org. We only need the originals.
And this is what this change does: it introduces an additional option to
`GetFeedsOptions`: `OnlyPerformedByActor`. When this option is set,
`activities.GetFeeds()` will only return the original activities, where
the user id and the acting user id are the same. As these are *always*
inserted, we're not missing out on any activities. We're just getting
rid of the duplicates. As this is an additional `AND` condition, it can
never introduce items that would not have been included in the result
set before, it can only reduce, not extend.
These duplicates were only affecting call sites where `RequestedRepo`
was set, but `RequestedUser` and `RequestedTeam` were not. Both of those
call sites were updated to set `OnlyPerformedByActor`. As a result,
repository RSS feeds, and the `/repos/{owner}/{repo}/activities/feeds`
API end points no longer return dupes, only the original activities.
Rather than hardcoding this behaviour into `GetFeeds()` itself, I chose
to implement it as an explicit option, for the sake of clarity.
Fixes Codeberg/Community#684, and addresses gitea#20986.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit 9cb2aa989a
)
88 lines
2.6 KiB
Go
88 lines
2.6 KiB
Go
// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package integration
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"testing"
|
|
|
|
auth_model "code.gitea.io/gitea/models/auth"
|
|
"code.gitea.io/gitea/models/unittest"
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
"code.gitea.io/gitea/tests"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestAPIRepoActivitiyFeeds(t *testing.T) {
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
|
repo, _, f := CreateDeclarativeRepoWithOptions(t, owner, DeclarativeRepoOptions{})
|
|
defer f()
|
|
|
|
feedURL := fmt.Sprintf("/api/v1/repos/%s/activities/feeds", repo.FullName())
|
|
assertAndReturnActivities := func(t *testing.T, length int) []api.Activity {
|
|
t.Helper()
|
|
|
|
req := NewRequest(t, "GET", feedURL)
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
var activities []api.Activity
|
|
DecodeJSON(t, resp, &activities)
|
|
|
|
assert.Len(t, activities, length)
|
|
|
|
return activities
|
|
}
|
|
createIssue := func(t *testing.T) {
|
|
session := loginUser(t, owner.Name)
|
|
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue)
|
|
urlStr := fmt.Sprintf("/api/v1/repos/%s/issues?state=all", repo.FullName())
|
|
req := NewRequestWithJSON(t, "POST", urlStr, &api.CreateIssueOption{
|
|
Title: "ActivityFeed test",
|
|
Body: "Nothing to see here!",
|
|
}).AddTokenAuth(token)
|
|
MakeRequest(t, req, http.StatusCreated)
|
|
}
|
|
|
|
t.Run("repo creation", func(t *testing.T) {
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
// Upon repo creation, there's a single activity.
|
|
assertAndReturnActivities(t, 1)
|
|
})
|
|
|
|
t.Run("single watcher, single issue", func(t *testing.T) {
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
// After creating an issue, we'll have two activities.
|
|
createIssue(t)
|
|
assertAndReturnActivities(t, 2)
|
|
})
|
|
|
|
t.Run("a new watcher, no new activities", func(t *testing.T) {
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
watcher := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
watcherSession := loginUser(t, watcher.Name)
|
|
watcherToken := getTokenForLoggedInUser(t, watcherSession, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeReadUser)
|
|
|
|
req := NewRequest(t, "PUT", fmt.Sprintf("/api/v1/repos/%s/subscription", repo.FullName())).
|
|
AddTokenAuth(watcherToken)
|
|
MakeRequest(t, req, http.StatusOK)
|
|
|
|
assertAndReturnActivities(t, 2)
|
|
})
|
|
|
|
t.Run("multiple watchers, new issue", func(t *testing.T) {
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
// After creating a second issue, we'll have three activities, even
|
|
// though we have multiple watchers.
|
|
createIssue(t)
|
|
assertAndReturnActivities(t, 3)
|
|
})
|
|
}
|