calculate ancestor reply path

This works really well going back in time because no branching, assuming
the last referenced event id is the only note they are replying to...

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2022-04-18 10:31:54 -07:00
parent cca8738519
commit 6ce5484d74
3 changed files with 88 additions and 22 deletions

View File

@@ -166,15 +166,74 @@ struct EventDetailView_Previews: PreviewProvider {
} }
*/ */
/// Find the entire reply path for the active event
func make_reply_map(active: NostrEvent, events: [NostrEvent]) -> [String: ()]
{
let event_map: [String: Int] = zip(events,0...events.count).reduce(into: [:]) { (acc, arg1) in
let (ev, i) = arg1
acc[ev.id] = i
}
var is_reply: [String: ()] = [:]
var i: Int = 0
var start: Int = 0
var iterations: Int = 0
if events.count == 0 {
return is_reply
}
for ev in events {
if ev.references(id: active.id, key: "e") {
is_reply[ev.id] = ()
start = i
} else if active.references(id: ev.id, key: "e") {
is_reply[ev.id] = ()
start = i
}
i += 1
}
i = start
while true {
if iterations > 1024 {
// infinite loop? or super large thread
print("breaking from large reply_map... big thread??")
break
}
let ev = events[i]
let ref_ids = ev.referenced_ids
if ref_ids.count == 0 {
break
}
let ref_id = ref_ids[ref_ids.count-1]
let pubkey = ref_id.ref_id
is_reply[pubkey] = ()
if let mi = event_map[pubkey] {
i = mi
} else {
break
}
iterations += 1
}
return is_reply
}
func determine_highlight(current: NostrEvent, active: NostrEvent) -> Highlight func determine_highlight(current: NostrEvent, active: NostrEvent) -> Highlight
{ {
if current.id == active.id { if current.id == active.id {
return .main return .main
} }
if active.references(id: current.id, key: "e") { if active.references(id: current.id, key: "e") {
return .replied_to(active.id) return .reply
} else if current.references(id: active.id, key: "e") { } else if current.references(id: active.id, key: "e") {
return .replied_to(current.id) return .reply
} }
return .none return .none
} }
@@ -189,10 +248,17 @@ func calculated_collapsed_events(collapsed: Bool, active: NostrEvent, events: [N
} }
} }
let reply_map = make_reply_map(active: active, events: events)
let nevents = events.count let nevents = events.count
var i: Int = 0 var i: Int = 0
return events.reduce(into: []) { (acc, ev) in return events.reduce(into: []) { (acc, ev) in
let highlight = determine_highlight(current: ev, active: active) var highlight: Highlight = .none
if ev.id == active.id {
highlight = .main
} else if reply_map[ev.id] != nil {
highlight = .reply
}
switch highlight { switch highlight {
case .none: case .none:
@@ -203,7 +269,7 @@ func calculated_collapsed_events(collapsed: Bool, active: NostrEvent, events: [N
count = 0 count = 0
} }
acc.append(.event(ev, .main)) acc.append(.event(ev, .main))
case .replied_to: case .reply:
if count != 0 { if count != 0 {
acc.append(.collapsed(count, UUID().description)) acc.append(.collapsed(count, UUID().description))
count = 0 count = 0

View File

@@ -12,7 +12,7 @@ import CachedAsyncImage
enum Highlight { enum Highlight {
case none case none
case main case main
case replied_to(String) case reply
var is_none: Bool { var is_none: Bool {
switch self { switch self {
@@ -23,7 +23,7 @@ enum Highlight {
var is_replied_to: Bool { var is_replied_to: Bool {
switch self { switch self {
case .replied_to: return true case .reply: return true
default: return false default: return false
} }
} }

View File

@@ -19,7 +19,7 @@ func highlight_color(_ h: Highlight) -> Color {
switch h { switch h {
case .none: return Color.black case .none: return Color.black
case .main: return Color.red case .main: return Color.red
case .replied_to: return Color.blue case .reply: return Color.blue
} }
} }