From 832befd727a257581d774bb6bbdeb0f81cd658e2 Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Mon, 10 Oct 2022 15:52:49 +0200 Subject: [chore] Make paging logic more generic (#901) * make paging logic more generic not just for timelines! * linty linterson --- internal/processing/notification.go | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) (limited to 'internal/processing/notification.go') diff --git a/internal/processing/notification.go b/internal/processing/notification.go index fd2651d4a..7e748bdc3 100644 --- a/internal/processing/notification.go +++ b/internal/processing/notification.go @@ -25,36 +25,48 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/gtserror" "github.com/superseriousbusiness/gotosocial/internal/log" "github.com/superseriousbusiness/gotosocial/internal/oauth" - "github.com/superseriousbusiness/gotosocial/internal/timeline" "github.com/superseriousbusiness/gotosocial/internal/util" ) -func (p *processor) NotificationsGet(ctx context.Context, authed *oauth.Auth, excludeTypes []string, limit int, maxID string, sinceID string) (*apimodel.TimelineResponse, gtserror.WithCode) { +func (p *processor) NotificationsGet(ctx context.Context, authed *oauth.Auth, excludeTypes []string, limit int, maxID string, sinceID string) (*apimodel.PageableResponse, gtserror.WithCode) { notifs, err := p.db.GetNotifications(ctx, authed.Account.ID, excludeTypes, limit, maxID, sinceID) if err != nil { return nil, gtserror.NewErrorInternalError(err) } - if len(notifs) == 0 { - return util.EmptyTimelineResponse(), nil + count := len(notifs) + + if count == 0 { + return util.EmptyPageableResponse(), nil } - timelineables := []timeline.Timelineable{} - for _, n := range notifs { - apiNotif, err := p.tc.NotificationToAPINotification(ctx, n) + items := []interface{}{} + nextMaxIDValue := "" + prevMinIDValue := "" + for i, n := range notifs { + item, err := p.tc.NotificationToAPINotification(ctx, n) if err != nil { log.Debugf("got an error converting a notification to api, will skip it: %s", err) continue } - timelineables = append(timelineables, apiNotif) + + if i == count-1 { + nextMaxIDValue = item.GetID() + } + + if i == 0 { + prevMinIDValue = item.GetID() + } + + items = append(items, item) } - return util.PackageTimelineableResponse(util.TimelineableResponseParams{ - Items: timelineables, + return util.PackagePageableResponse(util.PageableResponseParams{ + Items: items, Path: "api/v1/notifications", - NextMaxIDValue: timelineables[len(timelineables)-1].GetID(), + NextMaxIDValue: nextMaxIDValue, PrevMinIDKey: "since_id", - PrevMinIDValue: timelineables[0].GetID(), + PrevMinIDValue: prevMinIDValue, Limit: limit, }) } -- cgit v1.2.3