summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorLibravatar Blackle Morisanchetto <isabelle@blackle-mori.com>2022-08-31 13:20:52 -0400
committerLibravatar GitHub <noreply@github.com>2022-08-31 19:20:52 +0200
commitecb97f4e0bae0735464880cd850e964f292f2e92 (patch)
treec0c6c6576beaf1e4a0bbd740d8db9341d77b2ae6 /internal
parent[bugfix] Use custom blackfriday renderer to only add mention/hashtag links in... (diff)
downloadgotosocial-ecb97f4e0bae0735464880cd850e964f292f2e92.tar.xz
[feature] Add support for the exclude_types[] parameter on the notifications endpoint (#784)
* Add support for the exclude_types[] parameter on the notifications endpoint * Add swagger docs to notifications
Diffstat (limited to 'internal')
-rw-r--r--internal/api/client/notification/notification.go2
-rw-r--r--internal/api/client/notification/notificationsget.go69
-rw-r--r--internal/api/model/notification.go2
-rw-r--r--internal/db/bundb/notification.go6
-rw-r--r--internal/db/bundb/notification_test.go8
-rw-r--r--internal/db/notification.go2
-rw-r--r--internal/processing/notification.go4
-rw-r--r--internal/processing/notification_test.go2
-rw-r--r--internal/processing/processor.go2
9 files changed, 85 insertions, 12 deletions
diff --git a/internal/api/client/notification/notification.go b/internal/api/client/notification/notification.go
index 61a84f60a..6ade0b02f 100644
--- a/internal/api/client/notification/notification.go
+++ b/internal/api/client/notification/notification.go
@@ -36,6 +36,8 @@ const (
BasePathWithID = BasePath + "/:" + IDKey
BasePathWithClear = BasePath + "/clear"
+ // ExcludeTypes is an array specifying notification types to exclude
+ ExcludeTypesKey = "exclude_types[]"
// MaxIDKey is the url query for setting a max notification ID to return
MaxIDKey = "max_id"
// LimitKey is for specifying maximum number of notifications to return.
diff --git a/internal/api/client/notification/notificationsget.go b/internal/api/client/notification/notificationsget.go
index 88220107e..88c2f663b 100644
--- a/internal/api/client/notification/notificationsget.go
+++ b/internal/api/client/notification/notificationsget.go
@@ -29,7 +29,70 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/oauth"
)
-// NotificationsGETHandler serves a list of notifications to the caller, with the desired query parameters
+// NotificationsGETHandler swagger:operation GET /api/v1/notifications notifications
+//
+// Get notifications for currently authorized user.
+//
+// The notifications will be returned in descending chronological order (newest first), with sequential IDs (bigger = newer).
+//
+// ---
+// tags:
+// - notifications
+//
+// produces:
+// - application/json
+//
+// parameters:
+// - name: limit
+// type: integer
+// description: Number of notifications to return.
+// default: 20
+// in: query
+// required: false
+// - name: exclude_types
+// type: array
+// items:
+// type: string
+// description: Array of types of notifications to exclude (follow, favourite, reblog, mention, poll, follow_request)
+// in: query
+// required: false
+// - name: max_id
+// type: string
+// description: |-
+// Return only notifications *OLDER* than the given max status ID.
+// The status with the specified ID will not be included in the response.
+// in: query
+// required: false
+// - name: since_id
+// type: string
+// description: |-
+// Return only notifications *NEWER* than the given since status ID.
+// The status with the specified ID will not be included in the response.
+// in: query
+// required: false
+//
+// security:
+// - OAuth2 Bearer:
+// - read:notifications
+//
+// responses:
+// '200':
+// name: notifications
+// description: Array of notifications.
+// schema:
+// type: array
+// items:
+// "$ref": "#/definitions/notification"
+// '400':
+// description: bad request
+// '401':
+// description: unauthorized
+// '404':
+// description: not found
+// '406':
+// description: not acceptable
+// '500':
+// description: internal server error
func (m *Module) NotificationsGETHandler(c *gin.Context) {
authed, err := oauth.Authed(c, true, true, true, true)
if err != nil {
@@ -66,7 +129,9 @@ func (m *Module) NotificationsGETHandler(c *gin.Context) {
sinceID = sinceIDString
}
- resp, errWithCode := m.processor.NotificationsGet(c.Request.Context(), authed, limit, maxID, sinceID)
+ excludeTypes := c.QueryArray(ExcludeTypesKey)
+
+ resp, errWithCode := m.processor.NotificationsGet(c.Request.Context(), authed, excludeTypes, limit, maxID, sinceID)
if errWithCode != nil {
api.ErrorHandler(c, errWithCode, m.processor.InstanceGet)
return
diff --git a/internal/api/model/notification.go b/internal/api/model/notification.go
index efcd2431b..c43e80b3b 100644
--- a/internal/api/model/notification.go
+++ b/internal/api/model/notification.go
@@ -19,6 +19,8 @@
package model
// Notification represents a notification of an event relevant to the user.
+//
+// swagger:model notification
type Notification struct {
// REQUIRED
diff --git a/internal/db/bundb/notification.go b/internal/db/bundb/notification.go
index 034b3b8ec..32523ca24 100644
--- a/internal/db/bundb/notification.go
+++ b/internal/db/bundb/notification.go
@@ -56,7 +56,7 @@ func (n *notificationDB) GetNotification(ctx context.Context, id string) (*gtsmo
return &dst, nil
}
-func (n *notificationDB) GetNotifications(ctx context.Context, accountID string, limit int, maxID string, sinceID string) ([]*gtsmodel.Notification, db.Error) {
+func (n *notificationDB) GetNotifications(ctx context.Context, accountID string, excludeTypes []string, limit int, maxID string, sinceID string) ([]*gtsmodel.Notification, db.Error) {
// Ensure reasonable
if limit < 0 {
limit = 0
@@ -78,6 +78,10 @@ func (n *notificationDB) GetNotifications(ctx context.Context, accountID string,
q = q.Where("id > ?", sinceID)
}
+ for _, excludeType := range excludeTypes {
+ q = q.Where("notification_type != ?", excludeType)
+ }
+
q = q.
Where("target_account_id = ?", accountID).
Order("id DESC")
diff --git a/internal/db/bundb/notification_test.go b/internal/db/bundb/notification_test.go
index d79c73ad2..704d3373b 100644
--- a/internal/db/bundb/notification_test.go
+++ b/internal/db/bundb/notification_test.go
@@ -91,7 +91,7 @@ func (suite *NotificationTestSuite) TestGetNotificationsWithSpam() {
suite.spamNotifs()
testAccount := suite.testAccounts["local_account_1"]
before := time.Now()
- notifications, err := suite.db.GetNotifications(context.Background(), testAccount.ID, 20, "ZZZZZZZZZZZZZZZZZZZZZZZZZZ", "00000000000000000000000000")
+ notifications, err := suite.db.GetNotifications(context.Background(), testAccount.ID, []string{}, 20, "ZZZZZZZZZZZZZZZZZZZZZZZZZZ", "00000000000000000000000000")
suite.NoError(err)
timeTaken := time.Since(before)
fmt.Printf("\n\n\n withSpam: got %d notifications in %s\n\n\n", len(notifications), timeTaken)
@@ -105,7 +105,7 @@ func (suite *NotificationTestSuite) TestGetNotificationsWithSpam() {
func (suite *NotificationTestSuite) TestGetNotificationsWithoutSpam() {
testAccount := suite.testAccounts["local_account_1"]
before := time.Now()
- notifications, err := suite.db.GetNotifications(context.Background(), testAccount.ID, 20, "ZZZZZZZZZZZZZZZZZZZZZZZZZZ", "00000000000000000000000000")
+ notifications, err := suite.db.GetNotifications(context.Background(), testAccount.ID, []string{}, 20, "ZZZZZZZZZZZZZZZZZZZZZZZZZZ", "00000000000000000000000000")
suite.NoError(err)
timeTaken := time.Since(before)
fmt.Printf("\n\n\n withoutSpam: got %d notifications in %s\n\n\n", len(notifications), timeTaken)
@@ -125,7 +125,7 @@ func (suite *NotificationTestSuite) TestClearNotificationsWithSpam() {
err := suite.db.ClearNotifications(context.Background(), testAccount.ID)
suite.NoError(err)
- notifications, err := suite.db.GetNotifications(context.Background(), testAccount.ID, 20, "ZZZZZZZZZZZZZZZZZZZZZZZZZZ", "00000000000000000000000000")
+ notifications, err := suite.db.GetNotifications(context.Background(), testAccount.ID, []string{}, 20, "ZZZZZZZZZZZZZZZZZZZZZZZZZZ", "00000000000000000000000000")
suite.NoError(err)
suite.NotNil(notifications)
suite.Empty(notifications)
@@ -137,7 +137,7 @@ func (suite *NotificationTestSuite) TestClearNotificationsWithTwoAccounts() {
err := suite.db.ClearNotifications(context.Background(), testAccount.ID)
suite.NoError(err)
- notifications, err := suite.db.GetNotifications(context.Background(), testAccount.ID, 20, "ZZZZZZZZZZZZZZZZZZZZZZZZZZ", "00000000000000000000000000")
+ notifications, err := suite.db.GetNotifications(context.Background(), testAccount.ID, []string{}, 20, "ZZZZZZZZZZZZZZZZZZZZZZZZZZ", "00000000000000000000000000")
suite.NoError(err)
suite.NotNil(notifications)
suite.Empty(notifications)
diff --git a/internal/db/notification.go b/internal/db/notification.go
index 7d8258d93..14d9b2c47 100644
--- a/internal/db/notification.go
+++ b/internal/db/notification.go
@@ -29,7 +29,7 @@ type Notification interface {
// GetNotifications returns a slice of notifications that pertain to the given accountID.
//
// Returned notifications will be ordered ID descending (ie., highest/newest to lowest/oldest).
- GetNotifications(ctx context.Context, accountID string, limit int, maxID string, sinceID string) ([]*gtsmodel.Notification, Error)
+ GetNotifications(ctx context.Context, accountID string, excludeTypes []string, limit int, maxID string, sinceID string) ([]*gtsmodel.Notification, Error)
// GetNotification returns one notification according to its id.
GetNotification(ctx context.Context, id string) (*gtsmodel.Notification, Error)
// ClearNotifications deletes every notification that pertain to the given accountID.
diff --git a/internal/processing/notification.go b/internal/processing/notification.go
index 66b967afa..fd2651d4a 100644
--- a/internal/processing/notification.go
+++ b/internal/processing/notification.go
@@ -29,8 +29,8 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/util"
)
-func (p *processor) NotificationsGet(ctx context.Context, authed *oauth.Auth, limit int, maxID string, sinceID string) (*apimodel.TimelineResponse, gtserror.WithCode) {
- notifs, err := p.db.GetNotifications(ctx, authed.Account.ID, limit, maxID, sinceID)
+func (p *processor) NotificationsGet(ctx context.Context, authed *oauth.Auth, excludeTypes []string, limit int, maxID string, sinceID string) (*apimodel.TimelineResponse, gtserror.WithCode) {
+ notifs, err := p.db.GetNotifications(ctx, authed.Account.ID, excludeTypes, limit, maxID, sinceID)
if err != nil {
return nil, gtserror.NewErrorInternalError(err)
}
diff --git a/internal/processing/notification_test.go b/internal/processing/notification_test.go
index 6ecca92a9..9758ea1a0 100644
--- a/internal/processing/notification_test.go
+++ b/internal/processing/notification_test.go
@@ -33,7 +33,7 @@ type NotificationTestSuite struct {
// get a notification where someone has liked our status
func (suite *NotificationTestSuite) TestGetNotifications() {
receivingAccount := suite.testAccounts["local_account_1"]
- notifsResponse, err := suite.processor.NotificationsGet(context.Background(), suite.testAutheds["local_account_1"], 10, "", "")
+ notifsResponse, err := suite.processor.NotificationsGet(context.Background(), suite.testAutheds["local_account_1"], []string{}, 10, "", "")
suite.NoError(err)
suite.Len(notifsResponse.Items, 1)
notif, ok := notifsResponse.Items[0].(*apimodel.Notification)
diff --git a/internal/processing/processor.go b/internal/processing/processor.go
index a6e47bed8..463ff72b5 100644
--- a/internal/processing/processor.go
+++ b/internal/processing/processor.go
@@ -154,7 +154,7 @@ type Processor interface {
MediaUpdate(ctx context.Context, authed *oauth.Auth, attachmentID string, form *apimodel.AttachmentUpdateRequest) (*apimodel.Attachment, gtserror.WithCode)
// NotificationsGet
- NotificationsGet(ctx context.Context, authed *oauth.Auth, limit int, maxID string, sinceID string) (*apimodel.TimelineResponse, gtserror.WithCode)
+ NotificationsGet(ctx context.Context, authed *oauth.Auth, excludeTypes []string, limit int, maxID string, sinceID string) (*apimodel.TimelineResponse, gtserror.WithCode)
// NotificationsClear
NotificationsClear(ctx context.Context, authed *oauth.Auth) gtserror.WithCode