From 2373b4177ada41cd772b647b0f239887370dee6d Mon Sep 17 00:00:00 2001 From: Gusted Date: Mon, 22 Aug 2022 19:36:14 +0200 Subject: [PATCH] Add paginition to Person's outbox --- models/action.go | 21 +++++++----- routers/api/v1/activitypub/person.go | 48 ++++++++++++++++++---------- 2 files changed, 45 insertions(+), 24 deletions(-) diff --git a/models/action.go b/models/action.go index 14e021389a..0d0c16d42c 100644 --- a/models/action.go +++ b/models/action.go @@ -331,14 +331,15 @@ func (a *Action) GetIssueContent() string { // GetFeedsOptions options for retrieving feeds type GetFeedsOptions struct { db.ListOptions - RequestedUser *user_model.User // the user we want activity for - RequestedTeam *organization.Team // the team we want activity for - RequestedRepo *repo_model.Repository // the repo we want activity for - Actor *user_model.User // the user viewing the activity - IncludePrivate bool // include private actions - OnlyPerformedBy bool // only actions performed by requested user - IncludeDeleted bool // include deleted actions - Date string // the day we want activity for: YYYY-MM-DD + RequestedUser *user_model.User // the user we want activity for + RequestedTeam *organization.Team // the team we want activity for + RequestedRepo *repo_model.Repository // the repo we want activity for + RequestedActionType ActionType // the type of activity we want + Actor *user_model.User // the user viewing the activity + IncludePrivate bool // include private actions + OnlyPerformedBy bool // only actions performed by requested user + IncludeDeleted bool // include deleted actions + Date string // the day we want activity for: YYYY-MM-DD } // GetFeeds returns actions according to the provided options @@ -449,6 +450,10 @@ func activityQueryCondition(opts GetFeedsOptions) (builder.Cond, error) { } } + if opts.RequestedActionType != 0 { + cond = cond.And(builder.Eq{"`action`.op_type": opts.RequestedActionType}) + } + return cond, nil } diff --git a/routers/api/v1/activitypub/person.go b/routers/api/v1/activitypub/person.go index 576d3ce2dd..7928a80744 100644 --- a/routers/api/v1/activitypub/person.go +++ b/routers/api/v1/activitypub/person.go @@ -5,6 +5,7 @@ package activitypub import ( + "fmt" "io" "net/http" "strings" @@ -15,6 +16,7 @@ import ( user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/activitypub" "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/forgefed" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" @@ -164,31 +166,45 @@ func PersonOutbox(ctx *context.APIContext) { link := setting.AppURL + "api/v1/activitypub/user/" + ctx.ContextUser.Name - outbox := ap.OrderedCollectionNew(ap.IRI(link + "/outbox")) + orderedCollection := ap.OrderedCollectionNew(ap.IRI(link + "/outbox")) + orderedCollection.First = ap.IRI(link + "/outbox?page=1") + + outbox := ap.OrderedCollectionPageNew(orderedCollection) + outbox.First = ap.IRI(link + "/outbox?page=1") feed, err := models.GetFeeds(ctx, models.GetFeedsOptions{ - RequestedUser: ctx.ContextUser, - Actor: ctx.ContextUser, - IncludePrivate: false, - IncludeDeleted: false, - ListOptions: db.ListOptions{Page: 1, PageSize: 1000000}, + RequestedUser: ctx.ContextUser, + RequestedActionType: models.ActionCreateRepo, + Actor: ctx.Doer, + IncludePrivate: false, + IncludeDeleted: false, + ListOptions: utils.GetListOptions(ctx), }) + + // Only specify next if this amount of feed corresponds to the calculated limit. + if len(feed) == convert.ToCorrectPageSize(ctx.FormInt("limit")) { + outbox.Next = ap.IRI(fmt.Sprintf("%s/outbox?page=%d", link, ctx.FormInt("page")+1)) + } + + // Only specify previous page when there is one. + if ctx.FormInt("page") > 1 { + outbox.Prev = ap.IRI(fmt.Sprintf("%s/outbox?page=%d", link, ctx.FormInt("page")-1)) + } + if err != nil { ctx.ServerError("Couldn't fetch feed", err) return } for _, action := range feed { - if action.OpType == models.ActionCreateRepo { - // Created a repo - object := ap.Note{Type: ap.NoteType, Content: ap.NaturalLanguageValuesNew()} - _ = object.Content.Set("en", ap.Content(action.GetRepoName())) - create := ap.Create{Type: ap.CreateType, Object: object} - err := outbox.OrderedItems.Append(create) - if err != nil { - ctx.ServerError("OrderedItems.Append", err) - return - } + // Created a repo + object := ap.Note{Type: ap.NoteType, Content: ap.NaturalLanguageValuesNew()} + _ = object.Content.Set("en", ap.Content(action.GetRepoName())) + create := ap.Create{Type: ap.CreateType, Object: object} + err := outbox.OrderedItems.Append(create) + if err != nil { + ctx.ServerError("OrderedItems.Append", err) + return } }