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>
This commit is contained in:
William Casarin
2024-05-09 14:33:32 -07:00
parent a190a5e8fb
commit bb1f912f78
7 changed files with 14 additions and 57 deletions

View File

@@ -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
}
}

View File

@@ -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)
}
}

View File

@@ -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
}

View File

@@ -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()
}

View File

@@ -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)
}

View File

@@ -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)

View File

@@ -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? {