From 8cc3edf195a27dc35b0b350207c5ae977b466587 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Mon, 18 Apr 2022 12:32:55 -0700 Subject: [PATCH] use direct references on root events references to root should be treated slightly differently, otherwise the entire thread will be shown when you select the root event. Signed-off-by: William Casarin --- damus/Nostr/NostrEvent.swift | 34 ++++++++++++++++++++++++++++++- damus/Views/EventDetailView.swift | 26 +++++++++++++++++------ 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/damus/Nostr/NostrEvent.swift b/damus/Nostr/NostrEvent.swift index 59880948..39c59d7a 100644 --- a/damus/Nostr/NostrEvent.swift +++ b/damus/Nostr/NostrEvent.swift @@ -64,7 +64,39 @@ class NostrEvent: Codable, Identifiable { } } } - + + public func is_root_event() -> Bool { + for tag in tags { + if tag.count >= 1 && tag[0] == "e" { + return false + } + } + return true + } + + public func directly_references(_ id: String) -> Bool { + // conditions: if it only has 1 e ref + // OR it has more than 1 e ref, ignoring the first + + var nrefs = 0 + var i = 0 + var first_matched = false + var matched = true + + for tag in tags { + if tag.count >= 2 && tag[0] == "e" { + nrefs += 1 + if tag[1] == id { + matched = true + first_matched = nrefs == 1 + } + } + i += 1 + } + + return (nrefs == 1 && matched) || (nrefs > 1 && matched && !first_matched) + } + public func references(id: String, key: String) -> Bool { for tag in tags { if tag.count >= 2 && tag[0] == key { diff --git a/damus/Views/EventDetailView.swift b/damus/Views/EventDetailView.swift index 2f2a6a77..c67e17a5 100644 --- a/damus/Views/EventDetailView.swift +++ b/damus/Views/EventDetailView.swift @@ -208,9 +208,14 @@ func make_reply_map(active: NostrEvent, events: [NostrEvent]) -> [String: ()] if events.count == 0 { return is_reply } - + + let is_root = active.is_root_event() + for ev in events { - if ev.references(id: active.id, key: "e") { + if is_root && ev.directly_references(active.id) { + is_reply[ev.id] = () + start = i + } else if !is_root && ev.references(id: active.id, key: "e") { is_reply[ev.id] = () start = i } else if active.references(id: ev.id, key: "e") { @@ -257,11 +262,20 @@ func determine_highlight(current: NostrEvent, active: NostrEvent) -> Highlight if current.id == active.id { return .main } - if active.references(id: current.id, key: "e") { - return .reply - } else if current.references(id: active.id, key: "e") { - return .reply + if active.is_root_event() { + if active.directly_references(current.id) { + return .reply + } else if current.directly_references(active.id) { + return .reply + } + } else { + if active.references(id: current.id, key: "e") { + return .reply + } else if current.references(id: active.id, key: "e") { + return .reply + } } + return .none }