diff --git a/damus/Nostr/ProfileDatabase.swift b/damus/Nostr/ProfileDatabase.swift index c16491a1..5ee1b316 100644 --- a/damus/Nostr/ProfileDatabase.swift +++ b/damus/Nostr/ProfileDatabase.swift @@ -66,7 +66,7 @@ final class ProfileDatabase { background_context?.mergePolicy = NSMergePolicy(merge: .mergeByPropertyObjectTrumpMergePolicyType) } - private func get_persisted(id: String) -> PersistedProfile? { + func get_persisted(id: String) -> PersistedProfile? { let request = NSFetchRequest(entityName: entity_name) request.predicate = NSPredicate(format: "id == %@", id) request.fetchLimit = 1 diff --git a/damus/Nostr/Profiles.swift b/damus/Nostr/Profiles.swift index 6cfa6272..d13f1117 100644 --- a/damus/Nostr/Profiles.swift +++ b/damus/Nostr/Profiles.swift @@ -9,6 +9,8 @@ import Foundation class Profiles { + static let db_freshness_threshold: TimeInterval = 24 * 60 * 60 + /// This queue is used to synchronize access to the profiles dictionary, which /// prevents data races from crashing the app. private var queue = DispatchQueue(label: "io.damus.profiles", @@ -55,4 +57,22 @@ class Profiles { return profiles[id] } } + + func has_fresh_profile(id: String) -> Bool { + // check memory first + var profile: Profile? + queue.sync { + profile = profiles[id]?.profile + } + if profile != nil { + return true + } + + // then disk + guard let persisted_profile = database.get_persisted(id: id), + let pull_date = persisted_profile.network_pull_date else { + return false + } + return Date.now.timeIntervalSince(pull_date) < Profiles.db_freshness_threshold + } }