From 43017828e2a481bc5664951a55867af314d0bf6d Mon Sep 17 00:00:00 2001 From: Bryan Montz Date: Mon, 8 May 2023 06:44:58 -0500 Subject: [PATCH] fix user notifications from old events immediately shown on install and login Changelog-Fixed: Fix user notifications from old events immediately shown on install and login Closes: #1106 --- damus/Models/HomeModel.swift | 11 ++++++++++- damus/Nostr/NostrEvent.swift | 5 +++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/damus/Models/HomeModel.swift b/damus/Models/HomeModel.swift index ae08e3e7..4aee02b8 100644 --- a/damus/Models/HomeModel.swift +++ b/damus/Models/HomeModel.swift @@ -24,6 +24,9 @@ struct NewEventsBits: OptionSet { } class HomeModel: ObservableObject { + // Don't trigger a user notification for events older than a certain age + static let event_max_age_for_notification: TimeInterval = 12 * 60 * 60 + var damus_state: DamusState var has_event: [String: Set] = [:] @@ -543,7 +546,8 @@ class HomeModel: ObservableObject { func got_new_dm(notifs: NewEventsBits, ev: NostrEvent) { self.new_events = notifs - if damus_state.settings.dm_notification { + + if damus_state.settings.dm_notification && ev.age < HomeModel.event_max_age_for_notification { let convo = ev.decrypted(privkey: self.damus_state.keypair.privkey) ?? NSLocalizedString("New encrypted direct message", comment: "Notification that the user has received a new direct message") let notify = LocalNotification(type: .dm, event: ev, target: ev, content: convo) create_local_notification(profiles: damus_state.profiles, notify: notify) @@ -1111,6 +1115,11 @@ func process_local_notification(damus_state: DamusState, event ev: NostrEvent) { if damus_state.muted_threads.isMutedThread(ev, privkey: damus_state.keypair.privkey) { return } + + // Don't show notifications for old events + guard ev.age < HomeModel.event_max_age_for_notification else { + return + } if type == .text && damus_state.settings.mention_notification { let blocks = ev.blocks(damus_state.keypair.privkey) diff --git a/damus/Nostr/NostrEvent.swift b/damus/Nostr/NostrEvent.swift index 0c91da43..af60911d 100644 --- a/damus/Nostr/NostrEvent.swift +++ b/damus/Nostr/NostrEvent.swift @@ -278,6 +278,11 @@ class NostrEvent: Codable, Identifiable, CustomStringConvertible, Equatable, Has func sign(privkey: String) { self.sig = sign_event(privkey: privkey, ev: self) } + + var age: TimeInterval { + let event_date = Date(timeIntervalSince1970: TimeInterval(created_at)) + return Date.now.timeIntervalSince(event_date) + } } func sign_event(privkey: String, ev: NostrEvent) -> String {