From 160b293359f9b4c9177aa92e9ada77b5d46988ae Mon Sep 17 00:00:00 2001 From: William Casarin Date: Mon, 17 Jul 2023 13:19:05 -0700 Subject: [PATCH] performance: don't spam nip05 validation on startup Since we don't show these on events anymore, we don't need to spam nip05 validation. We can just check when we go to the profile page Changelog-Fixed: Eliminate nostr address validation bandwidth on startup --- damus/Models/HomeModel.swift | 23 ++++++----------------- damus/Nostr/Profiles.swift | 6 ++++++ damus/Util/NIP05.swift | 3 ++- damus/Views/Profile/ProfileView.swift | 23 +++++++++++++++++++++++ 4 files changed, 37 insertions(+), 18 deletions(-) diff --git a/damus/Models/HomeModel.swift b/damus/Models/HomeModel.swift index 868c500e..5efdbbb9 100644 --- a/damus/Models/HomeModel.swift +++ b/damus/Models/HomeModel.swift @@ -794,25 +794,14 @@ func process_metadata_profile(our_pubkey: String, profiles: Profiles, profile: P } } + if old_nip05 != profile.nip05 { + // if it's been validated before, invalidate it now + profiles.invalidate_nip05(ev.pubkey) + } + let tprof = TimestampedProfile(profile: profile, timestamp: ev.created_at, event: ev) profiles.add(id: ev.pubkey, profile: tprof) - - if let nip05 = profile.nip05, old_nip05 != profile.nip05 { - - Task.detached(priority: .background) { - let validated = await validate_nip05(pubkey: ev.pubkey, nip05_str: nip05) - if validated != nil { - print("validated nip05 for '\(nip05)'") - } - - Task { @MainActor in - profiles.set_validated(ev.pubkey, nip05: validated) - profiles.nip05_pubkey[nip05] = ev.pubkey - notify(.profile_updated, ProfileUpdate(pubkey: ev.pubkey, profile: profile)) - } - } - } - + // load pfps asap var changed = false diff --git a/damus/Nostr/Profiles.swift b/damus/Nostr/Profiles.swift index 2b1e6cdb..a75d1a5e 100644 --- a/damus/Nostr/Profiles.swift +++ b/damus/Nostr/Profiles.swift @@ -40,6 +40,12 @@ class Profiles { } } + func invalidate_nip05(_ pk: String) { + validated_queue.async(flags: .barrier) { + self.validated.removeValue(forKey: pk) + } + } + func set_validated(_ pk: String, nip05: NIP05?) { validated_queue.async(flags: .barrier) { self.validated[pk] = nip05 diff --git a/damus/Util/NIP05.swift b/damus/Util/NIP05.swift index 469d88c2..455732c8 100644 --- a/damus/Util/NIP05.swift +++ b/damus/Util/NIP05.swift @@ -37,7 +37,8 @@ func fetch_nip05(nip05: NIP05) async -> NIP05Response? { guard let url = nip05.url else { return nil } - + + print("fetching nip05 \(url.absoluteString)") guard let ret = try? await URLSession.shared.data(from: url) else { return nil } diff --git a/damus/Views/Profile/ProfileView.swift b/damus/Views/Profile/ProfileView.swift index 8d6c0ddb..77b8d0ba 100644 --- a/damus/Views/Profile/ProfileView.swift +++ b/damus/Views/Profile/ProfileView.swift @@ -457,6 +457,7 @@ struct ProfileView: View { dismiss() } .onAppear() { + check_nip05_validity(pubkey: self.profile.pubkey, profiles: self.damus_state.profiles) profile.subscribe() //followers.subscribe() } @@ -565,3 +566,25 @@ extension View { .foregroundStyle(scheme == .dark ? .white : .black, scheme == .dark ? .white : .black) } } + +func check_nip05_validity(pubkey: String, profiles: Profiles) { + guard let profile = profiles.lookup(id: pubkey), + let nip05 = profile.nip05, + profiles.is_validated(pubkey) == nil + else { + return + } + + Task.detached(priority: .background) { + let validated = await validate_nip05(pubkey: pubkey, nip05_str: nip05) + if validated != nil { + print("validated nip05 for '\(nip05)'") + } + + Task { @MainActor in + profiles.set_validated(pubkey, nip05: validated) + profiles.nip05_pubkey[nip05] = pubkey + notify(.profile_updated, ProfileUpdate(pubkey: pubkey, profile: profile)) + } + } +}