integrate ProfileDatabase with existing Profiles caching class

This commit is contained in:
Bryan Montz
2023-05-12 07:21:59 -05:00
parent 4646f0e23c
commit 3b0bb48dd4

View File

@@ -6,8 +6,6 @@
// //
import Foundation import Foundation
import UIKit
class Profiles { class Profiles {
@@ -22,28 +20,34 @@ class Profiles {
var nip05_pubkey: [String: String] = [:] var nip05_pubkey: [String: String] = [:]
var zappers: [String: String] = [:] var zappers: [String: String] = [:]
private let database = ProfileDatabase()
func is_validated(_ pk: String) -> NIP05? { func is_validated(_ pk: String) -> NIP05? {
return validated[pk] validated[pk]
} }
func lookup_zapper(pubkey: String) -> String? { func lookup_zapper(pubkey: String) -> String? {
if let zapper = zappers[pubkey] { zappers[pubkey]
return zapper
}
return nil
} }
func add(id: String, profile: TimestampedProfile) { func add(id: String, profile: TimestampedProfile) {
queue.async(flags: .barrier) { queue.async(flags: .barrier) {
self.profiles[id] = profile self.profiles[id] = profile
} }
do {
try database.upsert(id: id, profile: profile.profile, last_update: Date(timeIntervalSince1970: TimeInterval(profile.timestamp)))
} catch {
print("⚠️ Warning: Profiles failed to save a profile: \(error)")
}
} }
func lookup(id: String) -> Profile? { func lookup(id: String) -> Profile? {
var profile: Profile?
queue.sync { queue.sync {
return profiles[id]?.profile profile = profiles[id]?.profile
} }
return profile ?? database.get(id: id)
} }
func lookup_with_timestamp(id: String) -> TimestampedProfile? { func lookup_with_timestamp(id: String) -> TimestampedProfile? {