ndb: switch profile queries to use transactions
this should ensure no crashing occurs when querying profiles
This commit is contained in:
@@ -27,7 +27,6 @@ func get_friend_type(contacts: Contacts, pubkey: Pubkey) -> FriendType? {
|
||||
struct ProfileName: View {
|
||||
let damus_state: DamusState
|
||||
let pubkey: Pubkey
|
||||
let profile: Profile?
|
||||
let prefix: String
|
||||
|
||||
let show_nip5_domain: Bool
|
||||
@@ -36,9 +35,8 @@ struct ProfileName: View {
|
||||
@State var nip05: NIP05?
|
||||
@State var donation: Int?
|
||||
|
||||
init(pubkey: Pubkey, profile: Profile?, prefix: String = "", damus: DamusState, show_nip5_domain: Bool = true) {
|
||||
init(pubkey: Pubkey, prefix: String = "", damus: DamusState, show_nip5_domain: Bool = true) {
|
||||
self.pubkey = pubkey
|
||||
self.profile = profile
|
||||
self.prefix = prefix
|
||||
self.damus_state = damus
|
||||
self.show_nip5_domain = show_nip5_domain
|
||||
@@ -53,15 +51,15 @@ struct ProfileName: View {
|
||||
nip05 ?? damus_state.profiles.is_validated(pubkey)
|
||||
}
|
||||
|
||||
var current_display_name: DisplayName {
|
||||
func current_display_name(profile: Profile?) -> DisplayName {
|
||||
return display_name ?? Profile.displayName(profile: profile, pubkey: pubkey)
|
||||
}
|
||||
|
||||
var name_choice: String {
|
||||
return prefix == "@" ? current_display_name.username.truncate(maxLength: 50) : current_display_name.displayName.truncate(maxLength: 50)
|
||||
func name_choice(profile: Profile?) -> String {
|
||||
return prefix == "@" ? current_display_name(profile: profile).username.truncate(maxLength: 50) : current_display_name(profile: profile).displayName.truncate(maxLength: 50)
|
||||
}
|
||||
|
||||
var onlyzapper: Bool {
|
||||
func onlyzapper(profile: Profile?) -> Bool {
|
||||
guard let profile else {
|
||||
return false
|
||||
}
|
||||
@@ -69,7 +67,7 @@ struct ProfileName: View {
|
||||
return profile.reactions == false
|
||||
}
|
||||
|
||||
var supporter: Int? {
|
||||
func supporter(profile: Profile?) -> Int? {
|
||||
guard let profile,
|
||||
let donation = profile.damus_donation,
|
||||
donation > 0
|
||||
@@ -81,21 +79,28 @@ struct ProfileName: View {
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
let profile_txn = damus_state.profiles.lookup(id: pubkey)
|
||||
let profile = profile_txn.unsafeUnownedValue
|
||||
|
||||
HStack(spacing: 2) {
|
||||
Text(verbatim: "\(prefix)\(name_choice)")
|
||||
Text(verbatim: "\(prefix)\(name_choice(profile: profile))")
|
||||
.font(.body)
|
||||
.fontWeight(prefix == "@" ? .none : .bold)
|
||||
|
||||
if let nip05 = current_nip05 {
|
||||
NIP05Badge(nip05: nip05, pubkey: pubkey, contacts: damus_state.contacts, show_domain: show_nip5_domain, profiles: damus_state.profiles)
|
||||
}
|
||||
|
||||
if let friend = friend_type, current_nip05 == nil {
|
||||
FriendIcon(friend: friend)
|
||||
}
|
||||
if onlyzapper {
|
||||
|
||||
if onlyzapper(profile: profile) {
|
||||
Image("zap-hashtag")
|
||||
.frame(width: 14, height: 14)
|
||||
}
|
||||
if let supporter {
|
||||
|
||||
if let supporter = supporter(profile: profile) {
|
||||
SupporterBadge(percent: supporter)
|
||||
}
|
||||
}
|
||||
@@ -103,16 +108,38 @@ struct ProfileName: View {
|
||||
if update.pubkey != pubkey {
|
||||
return
|
||||
}
|
||||
display_name = Profile.displayName(profile: update.profile, pubkey: pubkey)
|
||||
nip05 = damus_state.profiles.is_validated(pubkey)
|
||||
donation = profile?.damus_donation
|
||||
|
||||
var profile: Profile!
|
||||
var profile_txn: NdbTxn<Profile?>!
|
||||
|
||||
switch update {
|
||||
case .remote(let pubkey):
|
||||
profile_txn = damus_state.profiles.lookup(id: pubkey)
|
||||
guard let prof = profile_txn.unsafeUnownedValue else { return }
|
||||
profile = prof
|
||||
case .manual(_, let prof):
|
||||
profile = prof
|
||||
}
|
||||
|
||||
let display_name = Profile.displayName(profile: profile, pubkey: pubkey)
|
||||
if self.display_name != display_name {
|
||||
self.display_name = display_name
|
||||
}
|
||||
|
||||
let nip05 = damus_state.profiles.is_validated(pubkey)
|
||||
if nip05 != self.nip05 {
|
||||
self.nip05 = nip05
|
||||
}
|
||||
|
||||
if donation != profile.damus_donation {
|
||||
donation = profile.damus_donation
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ProfileName_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
ProfileName(pubkey:
|
||||
test_damus_state().pubkey, profile: make_test_profile(), damus: test_damus_state())
|
||||
ProfileName(pubkey: test_damus_state().pubkey, damus: test_damus_state())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user