Skip to content

Commit

Permalink
network: Broadcast quoted notes when posting a note with quotes
Browse files Browse the repository at this point in the history
This change addresses an issue where notes with quotes sometimes are not loaded correctly because the quoted note was not available in the same relay. Now whenever a user posts a note with a quoted note, the quoted note is also broadcast to the user's selected relays.

Issue repro
-----------

ISSUE REPRODUCED

Device: iPhone 14 Pro Simulator
iOS: 17.0
Damus: `1fabd4c0fe98d1f47b1fa0f76984ad78095bd49c`
Setup:
- Make sure you have a debugger connected
- Have a test note that you can quote

Steps:

1. Start Damus and let logs settle
2. Observe where the last log is
3. Quote the test note
4. Copy newly generated logs and paste on a text editor.
5. Analyze those logs. Pay attention to the new note id, as well as the note id of the quoted event (`["q", <QUOTED_NOTE_ID>]`)

Results: Logs show that the newly posted event is being flushed to the relays, but not the note that is being quoted.

Testing of the fix
------------------

PASS

Device: iPhone 14 Pro Simulator
iOS: 17.0
Damus: This commit
Setup:
- Make sure you have a debugger connected
- Have a test note that you can quote

Steps:

1. Start Damus and let logs settle
2. Observe where the last log is
3. Quote the test note
4. Copy newly generated logs and paste on a text editor.
5. Analyze those logs. Pay attention to the new note id, as well as the note id of the quoted event (`["q", <QUOTED_NOTE_ID>]`)

Results:
- Logs show the new event being flushed to the relays. PASS
- Logs show the quoted event also being flushed to the relays. PASS

Closes: #1495
Changelog-Fixed: Broadcast quoted notes when posting a note with quotes
Signed-off-by: Daniel D’Aquino <[email protected]>
Reviewed-by: William Casarin <[email protected]>
Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
danieldaquino authored and jb55 committed Oct 7, 2023
1 parent 0f9e87c commit a06be64
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
6 changes: 6 additions & 0 deletions damus/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,12 @@ func handle_post_notification(keypair: FullKeypair, postbox: PostBox, events: Ev
postbox.send(ev)
}
}
for qref in new_ev.referenced_quote_ids.prefix(3) {
// also broadcast at most 3 referenced quoted events
if let ev = events.lookup(qref.note_id) {
postbox.send(ev)
}
}
return true
case .cancel:
print("post cancelled")
Expand Down
25 changes: 24 additions & 1 deletion damus/Nostr/Id.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,37 @@ protocol TagConvertible {
static func from_tag(tag: TagSequence) -> Self?
}

struct QuoteId: IdType, TagKey {
struct QuoteId: IdType, TagKey, TagConvertible {
let id: Data

init(_ data: Data) {
self.id = data
}

/// Refer to this QuoteId as a NoteId
var note_id: NoteId {
NoteId(self.id)
}

var keychar: AsciiCharacter { "q" }

var tag: [String] {
["q", self.hex()]
}

static func from_tag(tag: TagSequence) -> QuoteId? {
var i = tag.makeIterator()

guard tag.count >= 2,
let t0 = i.next(),
let key = t0.single_char,
key == "q",
let t1 = i.next(),
let quote_id = t1.id().map(QuoteId.init)
else { return nil }

return quote_id
}
}


Expand Down
4 changes: 4 additions & 0 deletions nostrdb/NdbNote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ extension NdbNote {
public var referenced_ids: References<NoteId> {
References<NoteId>(tags: self.tags)
}

public var referenced_quote_ids: References<QuoteId> {
References<QuoteId>(tags: self.tags)
}

public var referenced_noterefs: References<NoteRef> {
References<NoteRef>(tags: self.tags)
Expand Down

0 comments on commit a06be64

Please sign in to comment.