Live Music & Generic Statuses

Changelog-Added: Added live music statuses
Changelog-Added: Added generic user statuses
This commit is contained in:
William Casarin
2023-08-21 22:12:01 -07:00
parent 59cf8056bd
commit 0338297bfe
18 changed files with 537 additions and 55 deletions

View File

@@ -7,6 +7,36 @@
import Foundation
class ValidationModel: ObservableObject {
@Published var validated: NIP05?
init() {
self.validated = nil
}
}
class ProfileDataModel: ObservableObject {
@Published var profile: TimestampedProfile?
init() {
self.profile = nil
}
}
class ProfileData {
var status: UserStatusModel
var profile_model: ProfileDataModel
var validation_model: ValidationModel
var zapper: Pubkey?
init() {
status = .init()
profile_model = .init()
validation_model = .init()
zapper = nil
}
}
class Profiles {
static let db_freshness_threshold: TimeInterval = 24 * 60 * 60
@@ -21,10 +51,9 @@ class Profiles {
qos: .userInteractive,
attributes: .concurrent)
private var profiles: [Pubkey: TimestampedProfile] = [:]
private var validated: [Pubkey: NIP05] = [:]
private var profiles: [Pubkey: ProfileData] = [:]
var nip05_pubkey: [String: Pubkey] = [:]
var zappers: [Pubkey: Pubkey] = [:]
private let database = ProfileDatabase()
@@ -36,36 +65,40 @@ class Profiles {
func is_validated(_ pk: Pubkey) -> NIP05? {
validated_queue.sync {
validated[pk]
self.profile_data(pk).validation_model.validated
}
}
func invalidate_nip05(_ pk: Pubkey) {
validated_queue.async(flags: .barrier) {
self.validated.removeValue(forKey: pk)
self.profile_data(pk).validation_model.validated = nil
}
}
func set_validated(_ pk: Pubkey, nip05: NIP05?) {
validated_queue.async(flags: .barrier) {
self.validated[pk] = nip05
self.profile_data(pk).validation_model.validated = nip05
}
}
func enumerated() -> EnumeratedSequence<[Pubkey: TimestampedProfile]> {
return profiles_queue.sync {
return profiles.enumerated()
func profile_data(_ pubkey: Pubkey) -> ProfileData {
guard let data = profiles[pubkey] else {
let data = ProfileData()
profiles[pubkey] = data
return data
}
return data
}
func lookup_zapper(pubkey: Pubkey) -> Pubkey? {
zappers[pubkey]
profile_data(pubkey).zapper
}
func add(id: Pubkey, profile: TimestampedProfile) {
profiles_queue.async(flags: .barrier) {
let old_timestamped_profile = self.profiles[id]
self.profiles[id] = profile
let old_timestamped_profile = self.profile_data(id).profile_model.profile
self.profile_data(id).profile_model.profile = profile
self.user_search_cache.updateProfile(id: id, profiles: self, oldProfile: old_timestamped_profile?.profile, newProfile: profile.profile)
}
@@ -81,21 +114,21 @@ class Profiles {
func lookup(id: Pubkey) -> Profile? {
var profile: Profile?
profiles_queue.sync {
profile = profiles[id]?.profile
profile = self.profile_data(id).profile_model.profile?.profile
}
return profile ?? database.get(id: id)
}
func lookup_with_timestamp(id: Pubkey) -> TimestampedProfile? {
profiles_queue.sync {
return profiles[id]
return self.profile_data(id).profile_model.profile
}
}
func has_fresh_profile(id: Pubkey) -> Bool {
var profile: Profile?
profiles_queue.sync {
profile = profiles[id]?.profile
profile = self.profile_data(id).profile_model.profile?.profile
}
if profile != nil {
return true
@@ -113,6 +146,6 @@ class Profiles {
func invalidate_zapper_cache(pubkey: Pubkey, profiles: Profiles, lnurl: LNUrls) {
profiles.zappers.removeValue(forKey: pubkey)
profiles.profile_data(pubkey).zapper = nil
lnurl.endpoints.removeValue(forKey: pubkey)
}