From 4fdbef04b4ae41e6193d4f4416c83edf91e1bfb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Art=C3=A9mis?= Date: Mon, 1 Aug 2022 11:13:49 +0200 Subject: [PATCH] [feature] Implemented notification clear (#720) * Implemented notification clear * Added the cache clear mechanism * added multi user check test --- .../api/client/notification/notification.go | 4 +- .../client/notification/notificationsclear.go | 50 +++++++++++++++++++ internal/db/bundb/notification.go | 14 ++++++ internal/db/bundb/notification_test.go | 29 +++++++++++ internal/db/notification.go | 2 + internal/processing/notification.go | 9 ++++ internal/processing/processor.go | 2 + 7 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 internal/api/client/notification/notificationsclear.go diff --git a/internal/api/client/notification/notification.go b/internal/api/client/notification/notification.go index e5c527696..61a84f60a 100644 --- a/internal/api/client/notification/notification.go +++ b/internal/api/client/notification/notification.go @@ -33,7 +33,8 @@ BasePath = "/api/v1/notifications" // BasePathWithID is just the base path with the ID key in it. // Use this anywhere you need to know the ID of the notification being queried. - BasePathWithID = BasePath + "/:" + IDKey + BasePathWithID = BasePath + "/:" + IDKey + BasePathWithClear = BasePath + "/clear" // MaxIDKey is the url query for setting a max notification ID to return MaxIDKey = "max_id" @@ -58,5 +59,6 @@ func New(processor processing.Processor) api.ClientModule { // Route attaches all routes from this module to the given router func (m *Module) Route(r router.Router) error { r.AttachHandler(http.MethodGet, BasePath, m.NotificationsGETHandler) + r.AttachHandler(http.MethodPost, BasePathWithClear, m.NotificationsClearPOSTHandler) return nil } diff --git a/internal/api/client/notification/notificationsclear.go b/internal/api/client/notification/notificationsclear.go new file mode 100644 index 000000000..bcf7175d7 --- /dev/null +++ b/internal/api/client/notification/notificationsclear.go @@ -0,0 +1,50 @@ +/* + GoToSocial + Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +package notification + +import ( + "net/http" + + "github.com/gin-gonic/gin" + "github.com/superseriousbusiness/gotosocial/internal/api" + "github.com/superseriousbusiness/gotosocial/internal/gtserror" + "github.com/superseriousbusiness/gotosocial/internal/oauth" +) + +// NotificationsClearPOSTHandler clears all the notifications +func (m *Module) NotificationsClearPOSTHandler(c *gin.Context) { + authed, err := oauth.Authed(c, true, true, true, true) + if err != nil { + api.ErrorHandler(c, gtserror.NewErrorUnauthorized(err, err.Error()), m.processor.InstanceGet) + return + } + + if _, err := api.NegotiateAccept(c, api.JSONAcceptHeaders...); err != nil { + api.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGet) + return + } + + errWithCode := m.processor.NotificationsClear(c.Request.Context(), authed) + if errWithCode != nil { + api.ErrorHandler(c, errWithCode, m.processor.InstanceGet) + return + } + + c.JSON(http.StatusOK, struct{}{}) +} diff --git a/internal/db/bundb/notification.go b/internal/db/bundb/notification.go index 5e825d096..034b3b8ec 100644 --- a/internal/db/bundb/notification.go +++ b/internal/db/bundb/notification.go @@ -108,3 +108,17 @@ func (n *notificationDB) GetNotifications(ctx context.Context, accountID string, return notifs, nil } + +func (n *notificationDB) ClearNotifications(ctx context.Context, accountID string) db.Error { + if _, err := n.conn. + NewDelete(). + Table("notifications"). + Where("target_account_id = ?", accountID). + Exec(ctx); err != nil { + return n.conn.ProcessError(err) + } + + n.cache.Clear() + + return nil +} diff --git a/internal/db/bundb/notification_test.go b/internal/db/bundb/notification_test.go index b14704dcc..d822c9a28 100644 --- a/internal/db/bundb/notification_test.go +++ b/internal/db/bundb/notification_test.go @@ -118,6 +118,35 @@ func (suite *NotificationTestSuite) TestGetNotificationsWithoutSpam() { } } +func (suite *NotificationTestSuite) TestClearNotificationsWithSpam() { + suite.spamNotifs() + testAccount := suite.testAccounts["local_account_1"] + err := suite.db.ClearNotifications(context.Background(), testAccount.ID) + suite.NoError(err) + + notifications, err := suite.db.GetNotifications(context.Background(), testAccount.ID, 20, "ZZZZZZZZZZZZZZZZZZZZZZZZZZ", "00000000000000000000000000") + suite.NoError(err) + suite.NotNil(notifications) + suite.Empty(notifications) +} + +func (suite *NotificationTestSuite) TestClearNotificationsWithTwoAccounts() { + suite.spamNotifs() + testAccount := suite.testAccounts["local_account_1"] + err := suite.db.ClearNotifications(context.Background(), testAccount.ID) + suite.NoError(err) + + notifications, err := suite.db.GetNotifications(context.Background(), testAccount.ID, 20, "ZZZZZZZZZZZZZZZZZZZZZZZZZZ", "00000000000000000000000000") + suite.NoError(err) + suite.NotNil(notifications) + suite.Empty(notifications) + + notif := []*gtsmodel.Notification{} + err = suite.db.GetAll(context.Background(), ¬if) + suite.NoError(err) + suite.NotEmpty(notif) +} + func TestNotificationTestSuite(t *testing.T) { suite.Run(t, new(NotificationTestSuite)) } diff --git a/internal/db/notification.go b/internal/db/notification.go index d8291ae51..7d8258d93 100644 --- a/internal/db/notification.go +++ b/internal/db/notification.go @@ -32,4 +32,6 @@ type Notification interface { GetNotifications(ctx context.Context, accountID 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. + ClearNotifications(ctx context.Context, accountID string) Error } diff --git a/internal/processing/notification.go b/internal/processing/notification.go index 9b99141a6..66b967afa 100644 --- a/internal/processing/notification.go +++ b/internal/processing/notification.go @@ -58,3 +58,12 @@ func (p *processor) NotificationsGet(ctx context.Context, authed *oauth.Auth, li Limit: limit, }) } + +func (p *processor) NotificationsClear(ctx context.Context, authed *oauth.Auth) gtserror.WithCode { + err := p.db.ClearNotifications(ctx, authed.Account.ID) + if err != nil { + return gtserror.NewErrorInternalError(err) + } + + return nil +} diff --git a/internal/processing/processor.go b/internal/processing/processor.go index 5a4abb55b..a6e47bed8 100644 --- a/internal/processing/processor.go +++ b/internal/processing/processor.go @@ -155,6 +155,8 @@ type Processor interface { // NotificationsGet NotificationsGet(ctx context.Context, authed *oauth.Auth, limit int, maxID string, sinceID string) (*apimodel.TimelineResponse, gtserror.WithCode) + // NotificationsClear + NotificationsClear(ctx context.Context, authed *oauth.Auth) gtserror.WithCode OAuthHandleTokenRequest(r *http.Request) (map[string]interface{}, gtserror.WithCode) OAuthHandleAuthorizeRequest(w http.ResponseWriter, r *http.Request) error