From df30c061635036b14ab2cd9a6cf3c85b04f2a0eb Mon Sep 17 00:00:00 2001 From: Pascal Precht Date: Fri, 24 Sep 2021 12:57:15 +0200 Subject: [PATCH] feat: add API to mark activity center messages as unread --- protocol/activity_center_persistence.go | 14 ++++++++++++++ protocol/messenger_activity_center.go | 4 ++++ protocol/persistence_test.go | 6 ++++++ services/ext/api.go | 4 ++++ 4 files changed, 28 insertions(+) diff --git a/protocol/activity_center_persistence.go b/protocol/activity_center_persistence.go index 8e328979c..85e3d2327 100644 --- a/protocol/activity_center_persistence.go +++ b/protocol/activity_center_persistence.go @@ -382,6 +382,20 @@ func (db sqlitePersistence) MarkActivityCenterNotificationsRead(ids []types.HexB } +func (db sqlitePersistence) MarkActivityCenterNotificationsUnread(ids []types.HexBytes) error { + + idsArgs := make([]interface{}, 0, len(ids)) + for _, id := range ids { + idsArgs = append(idsArgs, id) + } + + inVector := strings.Repeat("?, ", len(ids)-1) + "?" + query := "UPDATE activity_center_notifications SET read = 0 WHERE id IN (" + inVector + ")" // nolint: gosec + _, err := db.db.Exec(query, idsArgs...) + return err + +} + func (db sqlitePersistence) UnreadActivityCenterNotificationsCount() (uint64, error) { var count uint64 err := db.db.QueryRow(`SELECT COUNT(1) FROM activity_center_notifications WHERE NOT read AND NOT dismissed AND NOT accepted`).Scan(&count) diff --git a/protocol/messenger_activity_center.go b/protocol/messenger_activity_center.go index fe4791bd8..4144d65a2 100644 --- a/protocol/messenger_activity_center.go +++ b/protocol/messenger_activity_center.go @@ -16,6 +16,10 @@ func (m *Messenger) MarkActivityCenterNotificationsRead(ids []types.HexBytes) er return m.persistence.MarkActivityCenterNotificationsRead(ids) } +func (m *Messenger) MarkActivityCenterNotificationsUnread(ids []types.HexBytes) error { + return m.persistence.MarkActivityCenterNotificationsUnread(ids) +} + func (m *Messenger) processAcceptedActivityCenterNotifications(notifications []*ActivityCenterNotification) (*MessengerResponse, error) { response := &MessengerResponse{} var chats []*Chat diff --git a/protocol/persistence_test.go b/protocol/persistence_test.go index 9361575d6..c77754698 100644 --- a/protocol/persistence_test.go +++ b/protocol/persistence_test.go @@ -1214,6 +1214,12 @@ func TestActivityCenterPersistence(t *testing.T) { require.NoError(t, err) require.Equal(t, uint64(1), count) + // Mark first one as unread + require.NoError(t, p.MarkActivityCenterNotificationsUnread([]types.HexBytes{nID1})) + count, err = p.UnreadActivityCenterNotificationsCount() + require.NoError(t, err) + require.Equal(t, uint64(2), count) + // Mark all read require.NoError(t, p.MarkAllActivityCenterNotificationsRead()) _, notifications, err = p.ActivityCenterNotifications(cursor, 2) diff --git a/services/ext/api.go b/services/ext/api.go index 17985f042..f4a339e0c 100644 --- a/services/ext/api.go +++ b/services/ext/api.go @@ -875,6 +875,10 @@ func (api *PublicAPI) MarkActivityCenterNotificationsRead(ids []types.HexBytes) return api.service.messenger.MarkActivityCenterNotificationsRead(ids) } +func (api *PublicAPI) MarkActivityCenterNotificationsUnread(ids []types.HexBytes) error { + return api.service.messenger.MarkActivityCenterNotificationsUnread(ids) +} + func (api *PublicAPI) AcceptAllActivityCenterNotifications() (*protocol.MessengerResponse, error) { return api.service.messenger.AcceptAllActivityCenterNotifications() }