diff options
author | 2024-11-25 15:33:21 +0000 | |
---|---|---|
committer | 2024-11-25 15:33:21 +0000 | |
commit | a444adee979375ed5d7af38346029a3d90bc77eb (patch) | |
tree | 8ff7fedc012bdf242451c4d2c4a22c0a714b9b8f /internal/db | |
parent | [chore] Bump tooling versions, bump go -> v1.23.0 (#3258) (diff) | |
download | gotosocial-a444adee979375ed5d7af38346029a3d90bc77eb.tar.xz |
[bugfix] notification types missing from link header (#3571)
* ensure notification types get included in link header query for notifications
* fix type query keys
Diffstat (limited to 'internal/db')
-rw-r--r-- | internal/db/bundb/notification.go | 53 | ||||
-rw-r--r-- | internal/db/bundb/notification_test.go | 46 | ||||
-rw-r--r-- | internal/db/notification.go | 3 |
3 files changed, 47 insertions, 55 deletions
diff --git a/internal/db/bundb/notification.go b/internal/db/bundb/notification.go index a20ab7e3f..d4f8799bd 100644 --- a/internal/db/bundb/notification.go +++ b/internal/db/bundb/notification.go @@ -26,8 +26,8 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/gtscontext" "github.com/superseriousbusiness/gotosocial/internal/gtserror" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" - "github.com/superseriousbusiness/gotosocial/internal/id" "github.com/superseriousbusiness/gotosocial/internal/log" + "github.com/superseriousbusiness/gotosocial/internal/paging" "github.com/superseriousbusiness/gotosocial/internal/state" "github.com/superseriousbusiness/gotosocial/internal/util/xslices" "github.com/uptrace/bun" @@ -192,22 +192,19 @@ func (n *notificationDB) PopulateNotification(ctx context.Context, notif *gtsmod func (n *notificationDB) GetAccountNotifications( ctx context.Context, accountID string, - maxID string, - sinceID string, - minID string, - limit int, + page *paging.Page, types []gtsmodel.NotificationType, excludeTypes []gtsmodel.NotificationType, ) ([]*gtsmodel.Notification, error) { - // Ensure reasonable - if limit < 0 { - limit = 0 - } - - // Make educated guess for slice size var ( - notifIDs = make([]string, 0, limit) - frontToBack = true + // Get paging params. + minID = page.GetMin() + maxID = page.GetMax() + limit = page.GetLimit() + order = page.GetOrder() + + // Make educated guess for slice size + notifIDs = make([]string, 0, limit) ) q := n.db. @@ -215,23 +212,14 @@ func (n *notificationDB) GetAccountNotifications( TableExpr("? AS ?", bun.Ident("notifications"), bun.Ident("notification")). Column("notification.id") - if maxID == "" { - maxID = id.Highest - } - - // Return only notifs LOWER (ie., older) than maxID. - q = q.Where("? < ?", bun.Ident("notification.id"), maxID) - - if sinceID != "" { - // Return only notifs HIGHER (ie., newer) than sinceID. - q = q.Where("? > ?", bun.Ident("notification.id"), sinceID) + if maxID != "" { + // Return only notifs LOWER (ie., older) than maxID. + q = q.Where("? < ?", bun.Ident("notification.id"), maxID) } if minID != "" { // Return only notifs HIGHER (ie., newer) than minID. q = q.Where("? > ?", bun.Ident("notification.id"), minID) - - frontToBack = false // page up } if len(types) > 0 { @@ -251,12 +239,12 @@ func (n *notificationDB) GetAccountNotifications( q = q.Limit(limit) } - if frontToBack { - // Page down. - q = q.Order("notification.id DESC") - } else { + if order == paging.OrderAscending { // Page up. q = q.Order("notification.id ASC") + } else { + // Page down. + q = q.Order("notification.id DESC") } if err := q.Scan(ctx, ¬ifIDs); err != nil { @@ -269,11 +257,8 @@ func (n *notificationDB) GetAccountNotifications( // If we're paging up, we still want notifications // to be sorted by ID desc, so reverse ids slice. - // https://zchee.github.io/golang-wiki/SliceTricks/#reversing - if !frontToBack { - for l, r := 0, len(notifIDs)-1; l < r; l, r = l+1, r-1 { - notifIDs[l], notifIDs[r] = notifIDs[r], notifIDs[l] - } + if order == paging.OrderAscending { + slices.Reverse(notifIDs) } // Fetch notification models by their IDs. diff --git a/internal/db/bundb/notification_test.go b/internal/db/bundb/notification_test.go index eb2c02066..8e2fb8031 100644 --- a/internal/db/bundb/notification_test.go +++ b/internal/db/bundb/notification_test.go @@ -28,6 +28,7 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/gtscontext" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/id" + "github.com/superseriousbusiness/gotosocial/internal/paging" "github.com/superseriousbusiness/gotosocial/internal/util" ) @@ -92,10 +93,11 @@ func (suite *NotificationTestSuite) TestGetAccountNotificationsWithSpam() { notifications, err := suite.db.GetAccountNotifications( gtscontext.SetBarebones(context.Background()), testAccount.ID, - id.Highest, - id.Lowest, - "", - 20, + &paging.Page{ + Min: paging.EitherMinID("", id.Lowest), + Max: paging.MaxID(id.Highest), + Limit: 20, + }, nil, nil, ) @@ -115,10 +117,11 @@ func (suite *NotificationTestSuite) TestGetAccountNotificationsWithoutSpam() { notifications, err := suite.db.GetAccountNotifications( gtscontext.SetBarebones(context.Background()), testAccount.ID, - id.Highest, - id.Lowest, - "", - 20, + &paging.Page{ + Min: paging.EitherMinID("", id.Lowest), + Max: paging.MaxID(id.Highest), + Limit: 20, + }, nil, nil, ) @@ -140,10 +143,11 @@ func (suite *NotificationTestSuite) TestDeleteNotificationsWithSpam() { notifications, err := suite.db.GetAccountNotifications( gtscontext.SetBarebones(context.Background()), testAccount.ID, - id.Highest, - id.Lowest, - "", - 20, + &paging.Page{ + Min: paging.EitherMinID("", id.Lowest), + Max: paging.MaxID(id.Highest), + Limit: 20, + }, nil, nil, ) @@ -161,10 +165,11 @@ func (suite *NotificationTestSuite) TestDeleteNotificationsWithSpam() { notifications, err = suite.db.GetAccountNotifications( gtscontext.SetBarebones(context.Background()), testAccount.ID, - id.Highest, - id.Lowest, - "", - 20, + &paging.Page{ + Min: paging.EitherMinID("", id.Lowest), + Max: paging.MaxID(id.Highest), + Limit: 20, + }, nil, nil, ) @@ -183,10 +188,11 @@ func (suite *NotificationTestSuite) TestDeleteNotificationsWithTwoAccounts() { notifications, err := suite.db.GetAccountNotifications( gtscontext.SetBarebones(context.Background()), testAccount.ID, - id.Highest, - id.Lowest, - "", - 20, + &paging.Page{ + Min: paging.EitherMinID("", id.Lowest), + Max: paging.MaxID(id.Highest), + Limit: 20, + }, nil, nil, ) diff --git a/internal/db/notification.go b/internal/db/notification.go index c962023be..c608261dc 100644 --- a/internal/db/notification.go +++ b/internal/db/notification.go @@ -21,6 +21,7 @@ import ( "context" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/paging" ) // Notification contains functions for creating and getting notifications. @@ -29,7 +30,7 @@ type Notification interface { // // Returned notifications will be ordered ID descending (ie., highest/newest to lowest/oldest). // If types is empty, *all* notification types will be included. - GetAccountNotifications(ctx context.Context, accountID string, maxID string, sinceID string, minID string, limit int, types []gtsmodel.NotificationType, excludeTypes []gtsmodel.NotificationType) ([]*gtsmodel.Notification, error) + GetAccountNotifications(ctx context.Context, accountID string, page *paging.Page, types []gtsmodel.NotificationType, excludeTypes []gtsmodel.NotificationType) ([]*gtsmodel.Notification, error) // GetNotificationByID returns one notification according to its id. GetNotificationByID(ctx context.Context, id string) (*gtsmodel.Notification, error) |