From 8083269709ad2b955ff0969c3d4d1ade36671fd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=E2=80=99Aquino?= Date: Mon, 18 Aug 2025 17:46:40 -0700 Subject: [PATCH] Switch to local relay model MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Changelog-Changed: Switched to the local relay model Changelog-Added: Notes now load offline Signed-off-by: Daniel D’Aquino --- .../SubscriptionManager.swift | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/damus/Core/Networking/NostrNetworkManager/SubscriptionManager.swift b/damus/Core/Networking/NostrNetworkManager/SubscriptionManager.swift index 9de44e8b..d9c6a236 100644 --- a/damus/Core/Networking/NostrNetworkManager/SubscriptionManager.swift +++ b/damus/Core/Networking/NostrNetworkManager/SubscriptionManager.swift @@ -32,17 +32,14 @@ extension NostrNetworkManager { /// - Returns: An async stream of nostr data func subscribe(filters: [NostrFilter], to desiredRelays: [RelayURL]? = nil) -> AsyncStream { return AsyncStream { continuation in - let streamTask = Task { - for await item in self.pool.subscribe(filters: filters, to: desiredRelays) { + let ndbStreamTask = Task { + for await item in try self.ndb.subscribe(filters: try filters.map({ try NdbFilter(from: $0) })) { switch item { - case .eose: continuation.yield(.eose) - case .event(let nostrEvent): - // At this point of the pipeline, if the note is valid it should have been processed and verified by NostrDB, - // in which case we should pull the note from NostrDB to ensure validity. - // However, NdbNotes are unowned, so we return a function where our callers can temporarily borrow the NostrDB note - let noteId = nostrEvent.id + case .eose: + continuation.yield(.eose) + case .event(let noteKey): let lender: NdbNoteLender = { lend in - guard let ndbNoteTxn = self.ndb.lookup_note(noteId) else { + guard let ndbNoteTxn = self.ndb.lookup_note_by_key(noteKey) else { throw NdbNoteLenderError.errorLoadingNote } guard let unownedNote = UnownedNdbNote(ndbNoteTxn) else { @@ -54,8 +51,15 @@ extension NostrNetworkManager { } } } + let streamTask = Task { + for await _ in self.pool.subscribe(filters: filters, to: desiredRelays) { + // NO-OP. Notes will be automatically ingested by NostrDB + // TODO: Improve efficiency of subscriptions? + } + } continuation.onTermination = { @Sendable _ in streamTask.cancel() // Close the RelayPool stream when caller stops streaming + ndbStreamTask.cancel() } } }