This commit takes a step back from the full local relay model by treating NostrDB as one of the many relays streamed from, instead of the one exclusive relay that other classes rely on. This was done to reduce regression risk from the local relay model migration, without discarding the migration work already done. The full "local relay model" behavior (exclusive NDB streaming) was hidden behind a feature flag for easy migration later on. Closes: https://github.com/damus-io/damus/issues/3225 Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
40 lines
1.5 KiB
Swift
40 lines
1.5 KiB
Swift
//
|
||
// Ndb+.swift
|
||
// damus
|
||
//
|
||
// Created by Daniel D’Aquino on 2025-04-04.
|
||
//
|
||
|
||
/// ## Implementation notes
|
||
///
|
||
/// 1. This was created as a separate file because it contains dependencies to damus-specific structures such as `NostrFilter`, which is not yet available inside the NostrDB codebase.
|
||
|
||
import Foundation
|
||
|
||
extension Ndb {
|
||
/// Subscribe to events matching the provided NostrFilters
|
||
/// - Parameters:
|
||
/// - filters: Array of NostrFilter objects
|
||
/// - maxSimultaneousResults: Maximum number of initial results to return
|
||
/// - Returns: AsyncStream of StreamItem events
|
||
/// - Throws: NdbStreamError if subscription fails
|
||
func subscribe(filters: [NostrFilter], maxSimultaneousResults: Int = 1000) throws(NdbStreamError) -> AsyncStream<StreamItem> {
|
||
let ndbFilters: [NdbFilter]
|
||
do {
|
||
ndbFilters = try filters.toNdbFilters()
|
||
} catch {
|
||
throw .cannotConvertFilter(error)
|
||
}
|
||
return try self.subscribe(filters: ndbFilters, maxSimultaneousResults: maxSimultaneousResults)
|
||
}
|
||
|
||
/// Determines if a given note was seen on any of the listed relay URLs
|
||
func was(noteKey: NoteKey, seenOnAnyOf relayUrls: [RelayURL], txn: SafeNdbTxn<()>? = nil) throws -> Bool {
|
||
return try self.was(noteKey: noteKey, seenOnAnyOf: relayUrls.map({ $0.absoluteString }), txn: txn)
|
||
}
|
||
|
||
func processEvent(_ str: String, originRelayURL: RelayURL? = nil) -> Bool {
|
||
self.process_event(str, originRelayURL: originRelayURL?.absoluteString)
|
||
}
|
||
}
|