diff options
| author | 2022-08-01 11:13:49 +0200 | |
|---|---|---|
| committer | 2022-08-01 11:13:49 +0200 | |
| commit | 4fdbef04b4ae41e6193d4f4416c83edf91e1bfb7 (patch) | |
| tree | f28b7d40c38ee6b83ae5ff386cf25dc55ed0e157 /internal/db | |
| parent | serve HEAD requests via the fileserver (#735) (diff) | |
| download | gotosocial-4fdbef04b4ae41e6193d4f4416c83edf91e1bfb7.tar.xz | |
[feature] Implemented notification clear (#720)
* Implemented notification clear
* Added the cache clear mechanism
* added multi user check test
Diffstat (limited to 'internal/db')
| -rw-r--r-- | internal/db/bundb/notification.go | 14 | ||||
| -rw-r--r-- | internal/db/bundb/notification_test.go | 29 | ||||
| -rw-r--r-- | internal/db/notification.go | 2 | 
3 files changed, 45 insertions, 0 deletions
| 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  } | 
