aboutsummaryrefslogtreecommitdiff
path: root/models
diff options
context:
space:
mode:
authorBrecht Van Lommel2023-02-24 22:15:10 +0100
committerGitHub2023-02-24 16:15:10 -0500
commitf4920c9c7f5947d3b6476610f39bc3492ab4ef3b (patch)
tree36a4063c1ec25766808a73a2190a50cf13a7b18d /models
parent740a5ecdd925aec8dcea3e73da07868f70cdacac (diff)
Add pagination for dashboard and user activity feeds (#22937)
Previously only the last few activities where available. This works for all activity and for activity on a date chosen on the heatmap.
Diffstat (limited to 'models')
-rw-r--r--models/activities/action.go16
-rw-r--r--models/activities/action_test.go27
-rw-r--r--models/activities/user_heatmap_test.go3
3 files changed, 28 insertions, 18 deletions
diff --git a/models/activities/action.go b/models/activities/action.go
index 2e845bf89..1412d2c05 100644
--- a/models/activities/action.go
+++ b/models/activities/action.go
@@ -380,14 +380,14 @@ type GetFeedsOptions struct {
}
// GetFeeds returns actions according to the provided options
-func GetFeeds(ctx context.Context, opts GetFeedsOptions) (ActionList, error) {
+func GetFeeds(ctx context.Context, opts GetFeedsOptions) (ActionList, int64, error) {
if opts.RequestedUser == nil && opts.RequestedTeam == nil && opts.RequestedRepo == nil {
- return nil, fmt.Errorf("need at least one of these filters: RequestedUser, RequestedTeam, RequestedRepo")
+ return nil, 0, fmt.Errorf("need at least one of these filters: RequestedUser, RequestedTeam, RequestedRepo")
}
cond, err := activityQueryCondition(opts)
if err != nil {
- return nil, err
+ return nil, 0, err
}
sess := db.GetEngine(ctx).Where(cond).
@@ -398,16 +398,16 @@ func GetFeeds(ctx context.Context, opts GetFeedsOptions) (ActionList, error) {
sess = db.SetSessionPagination(sess, &opts)
actions := make([]*Action, 0, opts.PageSize)
-
- if err := sess.Desc("`action`.created_unix").Find(&actions); err != nil {
- return nil, fmt.Errorf("Find: %w", err)
+ count, err := sess.Desc("`action`.created_unix").FindAndCount(&actions)
+ if err != nil {
+ return nil, 0, fmt.Errorf("FindAndCount: %w", err)
}
if err := ActionList(actions).loadAttributes(ctx); err != nil {
- return nil, fmt.Errorf("LoadAttributes: %w", err)
+ return nil, 0, fmt.Errorf("LoadAttributes: %w", err)
}
- return actions, nil
+ return actions, count, nil
}
// ActivityReadable return whether doer can read activities of user
diff --git a/models/activities/action_test.go b/models/activities/action_test.go
index f37e58f68..2fd86bb8f 100644
--- a/models/activities/action_test.go
+++ b/models/activities/action_test.go
@@ -44,7 +44,7 @@ func TestGetFeeds(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
- actions, err := activities_model.GetFeeds(db.DefaultContext, activities_model.GetFeedsOptions{
+ actions, count, err := activities_model.GetFeeds(db.DefaultContext, activities_model.GetFeedsOptions{
RequestedUser: user,
Actor: user,
IncludePrivate: true,
@@ -56,8 +56,9 @@ func TestGetFeeds(t *testing.T) {
assert.EqualValues(t, 1, actions[0].ID)
assert.EqualValues(t, user.ID, actions[0].UserID)
}
+ assert.Equal(t, int64(1), count)
- actions, err = activities_model.GetFeeds(db.DefaultContext, activities_model.GetFeedsOptions{
+ actions, count, err = activities_model.GetFeeds(db.DefaultContext, activities_model.GetFeedsOptions{
RequestedUser: user,
Actor: user,
IncludePrivate: false,
@@ -65,6 +66,7 @@ func TestGetFeeds(t *testing.T) {
})
assert.NoError(t, err)
assert.Len(t, actions, 0)
+ assert.Equal(t, int64(0), count)
}
func TestGetFeedsForRepos(t *testing.T) {
@@ -74,38 +76,42 @@ func TestGetFeedsForRepos(t *testing.T) {
pubRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 8})
// private repo & no login
- actions, err := activities_model.GetFeeds(db.DefaultContext, activities_model.GetFeedsOptions{
+ actions, count, err := activities_model.GetFeeds(db.DefaultContext, activities_model.GetFeedsOptions{
RequestedRepo: privRepo,
IncludePrivate: true,
})
assert.NoError(t, err)
assert.Len(t, actions, 0)
+ assert.Equal(t, int64(0), count)
// public repo & no login
- actions, err = activities_model.GetFeeds(db.DefaultContext, activities_model.GetFeedsOptions{
+ actions, count, err = activities_model.GetFeeds(db.DefaultContext, activities_model.GetFeedsOptions{
RequestedRepo: pubRepo,
IncludePrivate: true,
})
assert.NoError(t, err)
assert.Len(t, actions, 1)
+ assert.Equal(t, int64(1), count)
// private repo and login
- actions, err = activities_model.GetFeeds(db.DefaultContext, activities_model.GetFeedsOptions{
+ actions, count, err = activities_model.GetFeeds(db.DefaultContext, activities_model.GetFeedsOptions{
RequestedRepo: privRepo,
IncludePrivate: true,
Actor: user,
})
assert.NoError(t, err)
assert.Len(t, actions, 1)
+ assert.Equal(t, int64(1), count)
// public repo & login
- actions, err = activities_model.GetFeeds(db.DefaultContext, activities_model.GetFeedsOptions{
+ actions, count, err = activities_model.GetFeeds(db.DefaultContext, activities_model.GetFeedsOptions{
RequestedRepo: pubRepo,
IncludePrivate: true,
Actor: user,
})
assert.NoError(t, err)
assert.Len(t, actions, 1)
+ assert.Equal(t, int64(1), count)
}
func TestGetFeeds2(t *testing.T) {
@@ -114,7 +120,7 @@ func TestGetFeeds2(t *testing.T) {
org := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3})
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
- actions, err := activities_model.GetFeeds(db.DefaultContext, activities_model.GetFeedsOptions{
+ actions, count, err := activities_model.GetFeeds(db.DefaultContext, activities_model.GetFeedsOptions{
RequestedUser: org,
Actor: user,
IncludePrivate: true,
@@ -127,8 +133,9 @@ func TestGetFeeds2(t *testing.T) {
assert.EqualValues(t, 2, actions[0].ID)
assert.EqualValues(t, org.ID, actions[0].UserID)
}
+ assert.Equal(t, int64(1), count)
- actions, err = activities_model.GetFeeds(db.DefaultContext, activities_model.GetFeedsOptions{
+ actions, count, err = activities_model.GetFeeds(db.DefaultContext, activities_model.GetFeedsOptions{
RequestedUser: org,
Actor: user,
IncludePrivate: false,
@@ -137,6 +144,7 @@ func TestGetFeeds2(t *testing.T) {
})
assert.NoError(t, err)
assert.Len(t, actions, 0)
+ assert.Equal(t, int64(0), count)
}
func TestActivityReadable(t *testing.T) {
@@ -224,13 +232,14 @@ func TestGetFeedsCorrupted(t *testing.T) {
RepoID: 1700,
})
- actions, err := activities_model.GetFeeds(db.DefaultContext, activities_model.GetFeedsOptions{
+ actions, count, err := activities_model.GetFeeds(db.DefaultContext, activities_model.GetFeedsOptions{
RequestedUser: user,
Actor: user,
IncludePrivate: true,
})
assert.NoError(t, err)
assert.Len(t, actions, 0)
+ assert.Equal(t, int64(0), count)
}
func TestConsistencyUpdateAction(t *testing.T) {
diff --git a/models/activities/user_heatmap_test.go b/models/activities/user_heatmap_test.go
index 34715ab6d..98df7b38a 100644
--- a/models/activities/user_heatmap_test.go
+++ b/models/activities/user_heatmap_test.go
@@ -73,7 +73,7 @@ func TestGetUserHeatmapDataByUser(t *testing.T) {
}
// get the action for comparison
- actions, err := activities_model.GetFeeds(db.DefaultContext, activities_model.GetFeedsOptions{
+ actions, count, err := activities_model.GetFeeds(db.DefaultContext, activities_model.GetFeedsOptions{
RequestedUser: user,
Actor: doer,
IncludePrivate: true,
@@ -90,6 +90,7 @@ func TestGetUserHeatmapDataByUser(t *testing.T) {
}
assert.NoError(t, err)
assert.Len(t, actions, contributions, "invalid action count: did the test data became too old?")
+ assert.Equal(t, count, int64(contributions))
assert.Equal(t, tc.CountResult, contributions, fmt.Sprintf("testcase '%s'", tc.desc))
// Test JSON rendering