commit bb1f912f782f54e7cba8c6796fe2a859feb76b93
parent a190a5e8fb2f762d3ed70b371f0f40c8e5c7b0ae
Author: William Casarin <jb55@jb55.com>
Date: Thu, 9 May 2024 14:33:32 -0700
nip10: consolidate event_ref logic into ThreadReply
These are overlapping concepts, lets slowly get rid of EventRef
Signed-off-by: William Casarin <jb55@jb55.com>
Diffstat:
7 files changed, 14 insertions(+), 57 deletions(-)
diff --git a/damus/NIP10/EventRef.swift b/damus/NIP10/EventRef.swift
@@ -149,8 +149,3 @@ func interpret_event_refs(blocks: [Block], tags: Tags) -> [EventRef] {
}
-func event_is_reply(_ refs: [EventRef]) -> Bool {
- return refs.contains { evref in
- return evref.is_reply != nil
- }
-}
diff --git a/damus/Util/EventCache.swift b/damus/Util/EventCache.swift
@@ -169,7 +169,7 @@ class EventCache {
var ev = event
while true {
- guard let direct_reply = ev.direct_replies(keypair).last,
+ guard let direct_reply = ev.direct_replies(keypair),
let next_ev = lookup(direct_reply), next_ev != ev
else {
break
@@ -183,7 +183,7 @@ class EventCache {
}
func add_replies(ev: NostrEvent, keypair: Keypair) {
- for reply in ev.direct_replies(keypair) {
+ if let reply = ev.direct_replies(keypair) {
replies.add(id: reply, reply_id: ev.id)
}
}
diff --git a/damus/Util/ReplyCounter.swift b/damus/Util/ReplyCounter.swift
@@ -39,7 +39,7 @@ class ReplyCounter {
counted.insert(event.id)
- for reply in event.direct_replies(keypair) {
+ if let reply = event.direct_replies(keypair) {
if event.pubkey == our_pubkey {
self.our_replies[reply] = event
}
diff --git a/damus/Views/Events/Components/ReplyPart.swift b/damus/Views/Events/Components/ReplyPart.swift
@@ -13,18 +13,10 @@ struct ReplyPart: View {
let keypair: Keypair
let ndb: Ndb
- var replying_to: NostrEvent? {
- guard let note_ref = event.event_refs(keypair).first(where: { evref in evref.is_direct_reply != nil })?.is_direct_reply else {
- return nil
- }
-
- return events.lookup(note_ref.note_id)
- }
-
var body: some View {
Group {
- if event_is_reply(event.event_refs(keypair)) {
- ReplyDescription(event: event, replying_to: replying_to, ndb: ndb)
+ if let reply_ref = event.thread_reply(keypair)?.reply {
+ ReplyDescription(event: event, replying_to: events.lookup(reply_ref.note_id), ndb: ndb)
} else {
EmptyView()
}
diff --git a/damus/Views/Events/SelectedEventView.swift b/damus/Views/Events/SelectedEventView.swift
@@ -18,14 +18,6 @@ struct SelectedEventView: View {
@StateObject var bar: ActionBarModel
- var replying_to: NostrEvent? {
- guard let note_ref = event.event_refs(damus.keypair).first(where: { evref in evref.is_direct_reply != nil })?.is_direct_reply else {
- return nil
- }
-
- return damus.events.lookup(note_ref.note_id)
- }
-
init(damus: DamusState, event: NostrEvent, size: EventViewKind) {
self.damus = damus
self.event = event
@@ -48,8 +40,8 @@ struct SelectedEventView: View {
.minimumScaleFactor(0.75)
.lineLimit(1)
- if event_is_reply(event.event_refs(damus.keypair)) {
- ReplyDescription(event: event, replying_to: replying_to, ndb: damus.ndb)
+ if let reply_ref = event.thread_reply(damus.keypair)?.reply {
+ ReplyDescription(event: event, replying_to: damus.events.lookup(reply_ref.note_id), ndb: damus.ndb)
.padding(.horizontal)
}
diff --git a/damusTests/NIP10Tests.swift b/damusTests/NIP10Tests.swift
@@ -146,18 +146,6 @@ final class NIP10Tests: XCTestCase {
let refs = interp_event_refs_without_mentions_ndb(note.referenced_noterefs)
let thread_reply = ThreadReply(event_refs: refs)!
- XCTAssertEqual(refs.reduce(into: Array<NoteId>(), { xs, r in
- if let note_id = r.is_thread_id?.note_id { xs.append(note_id) }
- }), [root_note_id])
-
- XCTAssertEqual(refs.reduce(into: Array<NoteId>(), { xs, r in
- if let note_id = r.is_direct_reply?.note_id { xs.append(note_id) }
- }), [root_note_id])
-
- XCTAssertEqual(refs.reduce(into: Array<NoteId>(), { xs, r in
- if let note_id = r.is_reply?.note_id { xs.append(note_id) }
- }), [root_note_id])
-
XCTAssertEqual(thread_reply.mention, nil)
XCTAssertEqual(thread_reply.root.note_id, root_note_id)
XCTAssertEqual(thread_reply.reply!.note_id, root_note_id)
diff --git a/nostrdb/NdbNote.swift b/nostrdb/NdbNote.swift
@@ -340,12 +340,8 @@ extension NdbNote {
References<RefId>(tags: self.tags)
}
- func event_refs(_ keypair: Keypair) -> [EventRef] {
- return interpret_event_refs_ndb(blocks: self.blocks(keypair).blocks, tags: self.tags)
- }
-
func thread_reply(_ keypair: Keypair) -> ThreadReply? {
- ThreadReply(event_refs: event_refs(keypair))
+ ThreadReply(event_refs: interpret_event_refs_ndb(blocks: self.blocks(keypair).blocks, tags: self.tags))
}
func get_content(_ keypair: Keypair) -> String {
@@ -391,23 +387,17 @@ extension NdbNote {
return dec
}
- public func direct_replies(_ keypair: Keypair) -> [NoteId] {
- return event_refs(keypair).reduce(into: []) { acc, evref in
- if let direct_reply = evref.is_direct_reply {
- acc.append(direct_reply.note_id)
- }
- }
+ public func direct_replies(_ keypair: Keypair) -> NoteId? {
+ return thread_reply(keypair)?.reply?.note_id
}
// NDBTODO: just use Id
public func thread_id(keypair: Keypair) -> NoteId {
- for ref in event_refs(keypair) {
- if let thread_id = ref.is_thread_id {
- return thread_id.note_id
- }
+ guard let root = self.thread_reply(keypair)?.root else {
+ return self.id
}
- return self.id
+ return root.note_id
}
public func last_refid() -> NoteId? {
@@ -432,7 +422,7 @@ extension NdbNote {
*/
func is_reply(_ keypair: Keypair) -> Bool {
- return event_is_reply(self.event_refs(keypair))
+ return thread_reply(keypair)?.reply != nil
}
func note_language(_ keypair: Keypair) -> String? {