Skip to content

Commit e56f0a5

Browse files
committed
fixes
Signed-off-by: William Casarin <[email protected]>
1 parent 1961498 commit e56f0a5

File tree

7 files changed

+116
-154
lines changed

7 files changed

+116
-154
lines changed

damus.xcodeproj/project.pbxproj

+69-138
Large diffs are not rendered by default.

damus/Models/DraftsModel.swift

+12-9
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,10 @@ class DraftArtifacts: Equatable {
9393
/// - draft_id: The unique ID of this draft, used for keeping draft identities stable. UUIDs are recommended but not required.
9494
/// - damus_state: The user's Damus state, used for fetching profiles in NostrDB
9595
/// - Returns: The draft that can be loaded into `PostView`.
96-
static func from(event: NostrEvent, draft_id: String, damus_state: DamusState) -> DraftArtifacts {
97-
let parsed_blocks = parse_note_content(content: .init(note: event, keypair: damus_state.keypair))
96+
static func from(event: NostrEvent, draft_id: String, damus_state: DamusState) -> DraftArtifacts? {
97+
guard let parsed_blocks = parse_note_content(content: .init(note: event, keypair: damus_state.keypair)) else {
98+
return nil
99+
}
98100
return Self.from(parsed_blocks: parsed_blocks, references: Array(event.references), draft_id: draft_id, damus_state: damus_state)
99101
}
100102

@@ -112,7 +114,7 @@ class DraftArtifacts: Equatable {
112114
for block in parsed_blocks.blocks {
113115
switch block {
114116
case .mention(let mention):
115-
if case .pubkey(let pubkey) = mention.ref {
117+
if let pubkey = mention.ref.nip19.pubkey() {
116118
// A profile reference, format things properly.
117119
let profile = damus_state.ndb.lookup_profile(pubkey)?.unsafeUnownedValue?.profile
118120
let profile_name = parse_display_name(profile: profile, pubkey: pubkey).username
@@ -128,12 +130,10 @@ class DraftArtifacts: Equatable {
128130
]
129131
)
130132
rich_text_content.append(attributed_string)
131-
}
132-
else if case .note(_) = mention.ref {
133+
} else if case .note(_) = mention.ref.nip19 {
133134
// These note references occur when we quote a note, and since that is tracked via `PostAction` in `PostView`, ignore it here to avoid attaching the same event twice in a note
134135
continue
135-
}
136-
else {
136+
} else {
137137
// Other references
138138
rich_text_content.append(.init(string: block.asString))
139139
}
@@ -195,7 +195,10 @@ class Drafts: ObservableObject {
195195
) else { return }
196196

197197
// Find out where to place these drafts
198-
let blocks = parse_note_content(content: .note(nip37_draft.unwrapped_note))
198+
guard let blocks = parse_note_content(content: .note(nip37_draft.unwrapped_note)) else {
199+
return
200+
}
201+
199202
switch known_kind {
200203
case .text:
201204
if let replied_to_note_id = nip37_draft.unwrapped_note.direct_replies() {
@@ -204,7 +207,7 @@ class Drafts: ObservableObject {
204207
else {
205208
for block in blocks.blocks {
206209
if case .mention(let mention) = block {
207-
if case .note(let note_id) = mention.ref {
210+
if case .note(let note_id) = mention.ref.nip19 {
208211
self.quotes[note_id] = draft_artifacts
209212
return
210213
}

damus/Types/Block.swift

+16-1
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,26 @@ fileprivate extension Block {
115115
fileprivate extension Block {
116116
/// Failable initializer for the C-backed type `invoice_block_t`.
117117
init?(invoice: ndb_invoice_block) {
118-
guard let invoice = invoice.as_invoice() else { return nil }
118+
119+
guard let invoice = invoice_block_as_invoice(invoice) else { return nil }
119120
self = .invoice(invoice)
120121
}
121122
}
122123

124+
func invoice_block_as_invoice(_ invoice: ndb_invoice_block) -> Invoice? {
125+
let invstr = invoice.invstr.as_str()
126+
let b11 = invoice.invoice
127+
128+
guard let description = convert_invoice_description(b11: b11) else {
129+
return nil
130+
}
131+
132+
let amount: Amount = b11.amount == 0 ? .any : .specific(Int64(b11.amount))
133+
134+
return Invoice(description: description, amount: amount, string: invstr, expiry: b11.expiry, created_at: b11.timestamp)
135+
136+
}
137+
123138
fileprivate extension Block {
124139
/// Failable initializer for the C-backed type `mention_bech32_block_t`. This initializer will inspect the
125140
/// bech32 type code and build the appropriate enum type.

damus/Util/Bech32Object.swift

+15-2
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,19 @@ enum Bech32Object : Equatable, Hashable {
9898
case nrelay(String)
9999
case naddr(NAddr)
100100

101+
func pubkey() -> Pubkey? {
102+
switch self {
103+
case .nprofile(let nprofile): return nprofile.author
104+
case .npub(let pubkey): return pubkey
105+
case .nevent(let ev): return ev.author
106+
case .naddr(let naddr): return naddr.author
107+
case .nscript: return nil
108+
case .nsec: return nil // TODO privkey_to_pubkey ?
109+
case .note: return nil
110+
case .nrelay: return nil
111+
}
112+
}
113+
101114
init?(block: ndb_mention_bech32_block) {
102115
let b32 = block.bech32
103116
switch block.bech32_type {
@@ -120,7 +133,7 @@ enum Bech32Object : Equatable, Hashable {
120133
author = Pubkey(nevent.pubkey.as_data(size: 32))
121134
}
122135
var kind: UInt32? = nil
123-
if nevent.has_kind != 0 {
136+
if nevent.has_kind {
124137
kind = nevent.kind
125138
}
126139

@@ -195,7 +208,7 @@ func decodeCBech32(_ b: nostr_bech32_t) -> Bech32Object? {
195208
case NOSTR_BECH32_NEVENT:
196209
let note_id = NoteId(Data(bytes: b.nevent.event_id, count: 32))
197210
let pubkey = b.nevent.pubkey != nil ? Pubkey(Data(bytes: b.nevent.pubkey, count: 32)) : nil
198-
let kind: UInt32? = b.nevent.has_kind == 0 ? nil : b.nevent.kind
211+
let kind: UInt32? = !b.nevent.has_kind ? nil : b.nevent.kind
199212
let relays = b.nevent.relays.as_urls()
200213
return .nevent(NEvent(noteid: note_id, relays: relays, author: pubkey, kind: kind))
201214
case NOSTR_BECH32_NPUB:

damus/Views/Chat/ChatEventView.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ struct ChatEventView: View {
144144
let blur_images = should_blur_images(settings: damus_state.settings, contacts: damus_state.contacts, ev: event, our_pubkey: damus_state.pubkey)
145145
NoteContentView(damus_state: damus_state, event: event, blur_images: blur_images, size: .normal, options: [.truncate_content])
146146
.padding(2)
147-
if let mention = first_eref_mention(ev: event, keypair: damus_state.keypair) {
147+
if let mention = first_eref_mention(ndb: damus_state.ndb, ev: event, keypair: damus_state.keypair) {
148148
MentionView(damus_state: damus_state, mention: mention)
149149
.background(DamusColors.adaptableWhite)
150150
.clipShape(RoundedRectangle(cornerSize: CGSize(width: 10, height: 10)))

nostrdb/Ndb.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class Ndb {
112112
while !ok && mapsize > 1024 * 1024 * 700 {
113113
var cfg = ndb_config(flags: 0, ingester_threads: ingest_threads, mapsize: mapsize, filter_context: nil, ingest_filter: nil, sub_cb_ctx: nil, sub_cb: nil)
114114
let res = ndb_init(&ndb_p, testdir, &cfg);
115-
let ok = res != 0;
115+
ok = res != 0;
116116
if !ok {
117117
Log.error("ndb_init failed: %d, reducing mapsize from %d to %d", for: .storage, res, mapsize, mapsize / 2)
118118
mapsize /= 2

nostrdb/src/nostrdb.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ struct bech32_nrelay {
390390
struct ndb_str_block relay;
391391
};
392392

393-
struct nostr_bech32 {
393+
typedef struct nostr_bech32 {
394394
enum nostr_bech32_type type;
395395

396396
union {
@@ -402,7 +402,7 @@ struct nostr_bech32 {
402402
struct bech32_naddr naddr;
403403
struct bech32_nrelay nrelay;
404404
};
405-
};
405+
} nostr_bech32_t;
406406

407407

408408
struct ndb_mention_bech32_block {

0 commit comments

Comments
 (0)