diff --git a/damus/ContentView.swift b/damus/ContentView.swift index 46e180a19..eadf2b76a 100644 --- a/damus/ContentView.swift +++ b/damus/ContentView.swift @@ -689,7 +689,7 @@ extension UINavigationController: UIGestureRecognizerDelegate { } struct LastNotification { - let id: String + let id: NoteId let created_at: Int64 } @@ -698,17 +698,20 @@ func get_last_event(_ timeline: Timeline) -> LastNotification? { let last = UserDefaults.standard.string(forKey: "last_\(str)") let last_created = UserDefaults.standard.string(forKey: "last_\(str)_time") .flatMap { Int64($0) } - - return last.flatMap { id in - last_created.map { created in - return LastNotification(id: id, created_at: created) - } + + guard let last, + let note_id = NoteId(hex: last), + let last_created + else { + return nil } + + return LastNotification(id: note_id, created_at: last_created) } func save_last_event(_ ev: NostrEvent, timeline: Timeline) { let str = timeline.rawValue - UserDefaults.standard.set(ev.id, forKey: "last_\(str)") + UserDefaults.standard.set(ev.id.hex(), forKey: "last_\(str)") UserDefaults.standard.set(String(ev.created_at), forKey: "last_\(str)_time") } diff --git a/damus/Models/HomeModel.swift b/damus/Models/HomeModel.swift index e5272ccae..3ab1b0f4e 100644 --- a/damus/Models/HomeModel.swift +++ b/damus/Models/HomeModel.swift @@ -70,6 +70,7 @@ class HomeModel { var incoming_dms: [NostrEvent] = [] let dm_debouncer = Debouncer(interval: 0.5) let resub_debouncer = Debouncer(interval: 3.0) + let save_last_event_debouncer = Debouncer(interval: 3.0) var should_debounce_dms = true let home_subid = UUID().description @@ -231,7 +232,7 @@ class HomeModel { return } - guard let new_bits = handle_last_events(new_events: self.notification_status.new_events, ev: ev, timeline: .notifications, shouldNotify: true) else { + guard let new_bits = handle_last_events(debouncer: self.save_last_event_debouncer, new_events: self.notification_status.new_events, ev: ev, timeline: .notifications, shouldNotify: true) else { return } @@ -592,7 +593,7 @@ class HomeModel { @discardableResult func handle_last_event(ev: NostrEvent, timeline: Timeline, shouldNotify: Bool = true) -> Bool { - if let new_bits = handle_last_events(new_events: self.notification_status.new_events, ev: ev, timeline: timeline, shouldNotify: shouldNotify) { + if let new_bits = handle_last_events(debouncer: save_last_event_debouncer, new_events: self.notification_status.new_events, ev: ev, timeline: timeline, shouldNotify: shouldNotify) { self.notification_status.new_events = new_bits return true } else { @@ -643,7 +644,7 @@ class HomeModel { if !should_debounce_dms { self.incoming_dms.append(ev) - if let notifs = handle_incoming_dms(prev_events: notification_status.new_events, dms: self.dms, our_pubkey: self.damus_state.pubkey, evs: self.incoming_dms) { + if let notifs = handle_incoming_dms(debouncer: save_last_event_debouncer, prev_events: notification_status.new_events, dms: self.dms, our_pubkey: self.damus_state.pubkey, evs: self.incoming_dms) { got_new_dm(notifs: notifs, ev: ev) } self.incoming_dms = [] @@ -653,7 +654,7 @@ class HomeModel { incoming_dms.append(ev) dm_debouncer.debounce { [self] in - if let notifs = handle_incoming_dms(prev_events: notification_status.new_events, dms: self.dms, our_pubkey: self.damus_state.pubkey, evs: self.incoming_dms) { + if let notifs = handle_incoming_dms(debouncer: save_last_event_debouncer, prev_events: notification_status.new_events, dms: self.dms, our_pubkey: self.damus_state.pubkey, evs: self.incoming_dms) { got_new_dm(notifs: notifs, ev: ev) } self.incoming_dms = [] @@ -977,7 +978,7 @@ func fetch_relay_metadata(relay_id: String) async throws -> RelayMetadata? { } @discardableResult -func handle_incoming_dm(ev: NostrEvent, our_pubkey: Pubkey, dms: DirectMessagesModel, prev_events: NewEventsBits) -> (Bool, NewEventsBits?) { +func handle_incoming_dm(debouncer: Debouncer?, ev: NostrEvent, our_pubkey: Pubkey, dms: DirectMessagesModel, prev_events: NewEventsBits) -> (Bool, NewEventsBits?) { var inserted = false var found = false @@ -1014,20 +1015,20 @@ func handle_incoming_dm(ev: NostrEvent, our_pubkey: Pubkey, dms: DirectMessagesM var new_bits: NewEventsBits? = nil if inserted { - new_bits = handle_last_events(new_events: prev_events, ev: ev, timeline: .dms, shouldNotify: !ours) + new_bits = handle_last_events(debouncer: debouncer, new_events: prev_events, ev: ev, timeline: .dms, shouldNotify: !ours) } return (inserted, new_bits) } @discardableResult -func handle_incoming_dms(prev_events: NewEventsBits, dms: DirectMessagesModel, our_pubkey: Pubkey, evs: [NostrEvent]) -> NewEventsBits? { +func handle_incoming_dms(debouncer: Debouncer, prev_events: NewEventsBits, dms: DirectMessagesModel, our_pubkey: Pubkey, evs: [NostrEvent]) -> NewEventsBits? { var inserted = false var new_events: NewEventsBits? = nil for ev in evs { - let res = handle_incoming_dm(ev: ev, our_pubkey: our_pubkey, dms: dms, prev_events: prev_events) + let res = handle_incoming_dm(debouncer: debouncer, ev: ev, our_pubkey: our_pubkey, dms: dms, prev_events: prev_events) inserted = res.0 || inserted if let new = res.1 { new_events = new @@ -1086,11 +1087,17 @@ func timeline_to_notification_bits(_ timeline: Timeline, ev: NostrEvent?) -> New } /// A helper to determine if we need to notify the user of new events -func handle_last_events(new_events: NewEventsBits, ev: NostrEvent, timeline: Timeline, shouldNotify: Bool = true) -> NewEventsBits? { +func handle_last_events(debouncer: Debouncer?, new_events: NewEventsBits, ev: NostrEvent, timeline: Timeline, shouldNotify: Bool = true) -> NewEventsBits? { let last_ev = get_last_event(timeline) if last_ev == nil || last_ev!.created_at < ev.created_at { - save_last_event(ev, timeline: timeline) + if let debouncer { + debouncer.debounce { + save_last_event(ev, timeline: timeline) + } + } else { + save_last_event(ev, timeline: timeline) + } if shouldNotify { return new_events.union(timeline_to_notification_bits(timeline, ev: ev)) } diff --git a/damus/Nostr/Profiles.swift b/damus/Nostr/Profiles.swift index 6523e7ef2..75db29245 100644 --- a/damus/Nostr/Profiles.swift +++ b/damus/Nostr/Profiles.swift @@ -93,7 +93,6 @@ class Profiles { } func has_fresh_profile(id: Pubkey) -> Bool { - // check memory first var profile: Profile? profiles_queue.sync { profile = profiles[id]?.profile @@ -101,7 +100,9 @@ class Profiles { if profile != nil { return true } - + // check memory first + return false + // then disk guard let pull_date = database.get_network_pull_date(id: id) else { return false diff --git a/damus/Views/DMChatView.swift b/damus/Views/DMChatView.swift index a905c68a3..eac5c1338 100644 --- a/damus/Views/DMChatView.swift +++ b/damus/Views/DMChatView.swift @@ -141,7 +141,7 @@ struct DMChatView: View, KeyboardReadable { damus_state.postbox.send(dm) - handle_incoming_dm(ev: dm, our_pubkey: damus_state.pubkey, dms: damus_state.dms, prev_events: NewEventsBits()) + handle_incoming_dm(debouncer: nil, ev: dm, our_pubkey: damus_state.pubkey, dms: damus_state.dms, prev_events: NewEventsBits()) end_editing() }