This commit tries to replace all usage of `String` to represent relay URLs and use `RelayURL` which automatically converts strings to a canonical relay URL format that is more reliable and avoids issues related to trailing slashes. Test 1: Main issue fix ----------------------- PASS Device: iPhone 15 Simulator iOS: 17.4 Damus: This commit Steps: 1. Delete all connected relays 2. Add `wss://relay.damus.io/` (with the trailing slash) to the relay list 3. Try to post. Post should succeed. PASS 4. Try removing this newly added relay. Relay should be removed successfully. PASS Test 2: Persistent relay list after upgrade -------------------------------------------- PASS Device: iPhone 15 Simulator iOS: 17.4 Damus: 1.8 (1) `247f313b` + This commit Steps: 1. Downgrade to old version 2. Add some relays to the list, some without a trailing slash, some with 3. Upgrade to this commit 4. All relays added in step 2 should still be there, and ones with a trailing slash should have been corrected to remove the trailing slash Test 3: Miscellaneous regression tests -------------------------------------- Device: iPhone 15 Simulator iOS: 17.4 Damus: This commit Coverage: 1. Posting works 2. Search works 3. Relay connection status works 4. Adding relays work 5. Removing relays work 6. Adding relay with trailing slashes works (it fixes itself to remove the trailing slash) 7. Adding relays with different paths works (e.g. wss://yabu.me/v1 and wss://yabu.me/v2) 8. Adding duplicate relay (but with trailing slash) gets rejected as expected 9. Relay details page works. All items on that view loads correctly 10. Relay logs work 11. Getting follower counts and seeing follow lists on profiles still work 12. Relay list changes persist after app restart 13. Notifications view still work 14. Copying the user's pubkey and profile link works 15. Share note + copy link button still works 16. Connecting NWC wallet works 17. One-tap zaps work 18. Onboarding works 19. Unit tests all passing Closes: https://github.com/damus-io/damus/issues/2072 Changelog-Fixed: Fix bug that would cause connection issues with relays defined with a trailing slash URL, and an inability to delete them. Signed-off-by: Daniel D’Aquino <daniel@daquino.me> Signed-off-by: William Casarin <jb55@jb55.com>
181 lines
5.1 KiB
Swift
181 lines
5.1 KiB
Swift
//
|
|
// PostBox.swift
|
|
// damus
|
|
//
|
|
// Created by William Casarin on 2023-03-20.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
|
|
class Relayer {
|
|
let relay: RelayURL
|
|
var attempts: Int
|
|
var retry_after: Double
|
|
var last_attempt: Int64?
|
|
|
|
init(relay: RelayURL, attempts: Int, retry_after: Double) {
|
|
self.relay = relay
|
|
self.attempts = attempts
|
|
self.retry_after = retry_after
|
|
self.last_attempt = nil
|
|
}
|
|
}
|
|
|
|
enum OnFlush {
|
|
case once((PostedEvent) -> Void)
|
|
case all((PostedEvent) -> Void)
|
|
}
|
|
|
|
class PostedEvent {
|
|
let event: NostrEvent
|
|
let skip_ephemeral: Bool
|
|
var remaining: [Relayer]
|
|
let flush_after: Date?
|
|
var flushed_once: Bool
|
|
let on_flush: OnFlush?
|
|
|
|
init(event: NostrEvent, remaining: [RelayURL], skip_ephemeral: Bool, flush_after: Date?, on_flush: OnFlush?) {
|
|
self.event = event
|
|
self.skip_ephemeral = skip_ephemeral
|
|
self.flush_after = flush_after
|
|
self.on_flush = on_flush
|
|
self.flushed_once = false
|
|
self.remaining = remaining.map {
|
|
Relayer(relay: $0, attempts: 0, retry_after: 10.0)
|
|
}
|
|
}
|
|
}
|
|
|
|
enum CancelSendErr {
|
|
case nothing_to_cancel
|
|
case not_delayed
|
|
case too_late
|
|
}
|
|
|
|
class PostBox {
|
|
let pool: RelayPool
|
|
var events: [NoteId: PostedEvent]
|
|
|
|
init(pool: RelayPool) {
|
|
self.pool = pool
|
|
self.events = [:]
|
|
pool.register_handler(sub_id: "postbox", handler: handle_event)
|
|
}
|
|
|
|
// only works reliably on delay-sent events
|
|
func cancel_send(evid: NoteId) -> CancelSendErr? {
|
|
guard let ev = events[evid] else {
|
|
return .nothing_to_cancel
|
|
}
|
|
|
|
guard let after = ev.flush_after else {
|
|
return .not_delayed
|
|
}
|
|
|
|
guard Date.now < after else {
|
|
return .too_late
|
|
}
|
|
|
|
events.removeValue(forKey: evid)
|
|
return nil
|
|
}
|
|
|
|
func try_flushing_events() {
|
|
let now = Int64(Date().timeIntervalSince1970)
|
|
for kv in events {
|
|
let event = kv.value
|
|
|
|
// some are delayed
|
|
if let after = event.flush_after, Date.now.timeIntervalSince1970 < after.timeIntervalSince1970 {
|
|
continue
|
|
}
|
|
|
|
for relayer in event.remaining {
|
|
if relayer.last_attempt == nil ||
|
|
(now >= (relayer.last_attempt! + Int64(relayer.retry_after))) {
|
|
print("attempt #\(relayer.attempts) to flush event '\(event.event.content)' to \(relayer.relay) after \(relayer.retry_after) seconds")
|
|
flush_event(event, to_relay: relayer)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func handle_event(relay_id: RelayURL, _ ev: NostrConnectionEvent) {
|
|
guard case .nostr_event(let resp) = ev else {
|
|
return
|
|
}
|
|
|
|
guard case .ok(let cr) = resp else {
|
|
return
|
|
}
|
|
|
|
remove_relayer(relay_id: relay_id, event_id: cr.event_id)
|
|
}
|
|
|
|
@discardableResult
|
|
func remove_relayer(relay_id: RelayURL, event_id: NoteId) -> Bool {
|
|
guard let ev = self.events[event_id] else {
|
|
return false
|
|
}
|
|
|
|
if let on_flush = ev.on_flush {
|
|
switch on_flush {
|
|
case .once(let cb):
|
|
if !ev.flushed_once {
|
|
ev.flushed_once = true
|
|
cb(ev)
|
|
}
|
|
case .all(let cb):
|
|
cb(ev)
|
|
}
|
|
}
|
|
|
|
let prev_count = ev.remaining.count
|
|
ev.remaining = ev.remaining.filter { $0.relay != relay_id }
|
|
let after_count = ev.remaining.count
|
|
if ev.remaining.count == 0 {
|
|
self.events.removeValue(forKey: event_id)
|
|
}
|
|
return prev_count != after_count
|
|
}
|
|
|
|
private func flush_event(_ event: PostedEvent, to_relay: Relayer? = nil) {
|
|
var relayers = event.remaining
|
|
if let to_relay {
|
|
relayers = [to_relay]
|
|
}
|
|
|
|
for relayer in relayers {
|
|
relayer.attempts += 1
|
|
relayer.last_attempt = Int64(Date().timeIntervalSince1970)
|
|
relayer.retry_after *= 1.5
|
|
if pool.get_relay(relayer.relay) != nil {
|
|
print("flushing event \(event.event.id) to \(relayer.relay)")
|
|
} else {
|
|
print("could not find relay when flushing: \(relayer.relay)")
|
|
}
|
|
pool.send(.event(event.event), to: [relayer.relay], skip_ephemeral: event.skip_ephemeral)
|
|
}
|
|
}
|
|
|
|
func send(_ event: NostrEvent, to: [RelayURL]? = nil, skip_ephemeral: Bool = true, delay: TimeInterval? = nil, on_flush: OnFlush? = nil) {
|
|
// Don't add event if we already have it
|
|
if events[event.id] != nil {
|
|
return
|
|
}
|
|
|
|
let remaining = to ?? pool.our_descriptors.map { $0.url }
|
|
let after = delay.map { d in Date.now.addingTimeInterval(d) }
|
|
let posted_ev = PostedEvent(event: event, remaining: remaining, skip_ephemeral: skip_ephemeral, flush_after: after, on_flush: on_flush)
|
|
|
|
events[event.id] = posted_ev
|
|
|
|
if after == nil {
|
|
flush_event(posted_ev)
|
|
}
|
|
}
|
|
}
|
|
|
|
|