damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit 9666180db5da4292eaf722ec2dc48ebda18714e5
parent 3b81592ca28a45005b6273221e7b864489d88d2c
Author: William Casarin <jb55@jb55.com>
Date:   Sun,  1 Jan 2023 10:24:43 -0800

threadv2: refactor handle_event logic

Diffstat:
Mdamus/Views/ThreadV2View.swift | 154++++++++++++++++++++++++++++++++++++++++++-------------------------------------
1 file changed, 82 insertions(+), 72 deletions(-)

diff --git a/damus/Views/ThreadV2View.swift b/damus/Views/ThreadV2View.swift @@ -84,6 +84,86 @@ struct BuildThreadV2View: View { return sub_id } + func handle_current_events(ev: NostrEvent) { + if current_event != nil { + return + } + + current_event = ev + + thread = ThreadV2( + parentEvents: [], + current: current_event!, + childEvents: [] + ) + + // Get parents + parents_ids = current_event!.tags.enumerated().filter { (index, tag) in + return tag.count >= 2 && tag[0] == "e" && !current_event!.content.contains("#[\(index)]") + }.map { tag in + return tag.1[1] + } + + print("ThreadV2View: Parents list: (\(parents_ids)") + + if parents_ids.count > 0 { + // Ask for parents + let parents_events = NostrFilter( + ids: parents_ids, + limit: UInt32(parents_ids.count) + ) + + let uuid = subscribe(filters: [parents_events]) + parents_events_uuids.append(uuid) + print("ThreadV2View: Ask for parents (\(uuid)) (\(parents_events))") + } + + // Ask for children + let childs_events = NostrFilter( + kinds: [1], + referenced_ids: [self.event_id], + limit: 50 + ) + childs_events_uuid = subscribe(filters: [childs_events]) + print("ThreadV2View: Ask for children (\(childs_events) (\(childs_events_uuid))") + } + + func handle_parent_events(sub_id: String, nostr_event: NostrEvent) { + + // We are filtering this later + thread!.parentEvents.append(nostr_event) + + // Get parents of parents + let local_parents_ids = nostr_event.tags.enumerated().filter { (index, tag) in + return tag.count >= 2 && tag[0] == "e" && !nostr_event.content.contains("#[\(index)]") + }.map { tag in + return tag.1[1] + }.filter { tag_id in + return !parents_ids.contains(tag_id) + } + + print("ThreadV2View: Sub Parents list: (\(local_parents_ids))") + + // Expand new parents id + parents_ids.append(contentsOf: local_parents_ids) + + if local_parents_ids.count > 0 { + // Ask for parents + let parents_events = NostrFilter( + ids: local_parents_ids, + limit: UInt32(local_parents_ids.count) + ) + let uuid = subscribe(filters: [parents_events]) + parents_events_uuids.append(uuid) + print("ThreadV2View: Ask for sub_parents (\(local_parents_ids)) \(uuid)") + } + + thread!.clean() + unsubscribe(sub_id) + return + + } + func handle_event(relay_id: String, ev: NostrConnectionEvent) { guard case .nostr_event(let nostr_response) = ev else { return @@ -95,82 +175,12 @@ struct BuildThreadV2View: View { // Is current event if id == current_events_uuid { - if current_event != nil { - return - } - - current_event = nostr_event - - thread = ThreadV2( - parentEvents: [], - current: current_event!, - childEvents: [] - ) - - // Get parents - parents_ids = current_event!.tags.enumerated().filter { (index, tag) in - return tag.count >= 2 && tag[0] == "e" && !current_event!.content.contains("#[\(index)]") - }.map { tag in - return tag.1[1] - } - - print("ThreadV2View: Parents list: (\(parents_ids)") - - if parents_ids.count > 0 { - // Ask for parents - let parents_events = NostrFilter( - ids: parents_ids, - limit: UInt32(parents_ids.count) - ) - - let uuid = subscribe(filters: [parents_events]) - parents_events_uuids.append(uuid) - print("ThreadV2View: Ask for parents (\(uuid)) (\(parents_events))") - } - - // Ask for children - let childs_events = NostrFilter( - kinds: [1], - referenced_ids: [self.event_id], - limit: 50 - ) - childs_events_uuid = subscribe(filters: [childs_events]) - print("ThreadV2View: Ask for children (\(childs_events) (\(childs_events_uuid))") - + handle_current_events(ev: nostr_event) return } if parents_events_uuids.contains(id) { - // We are filtering this later - thread!.parentEvents.append(nostr_event) - - // Get parents of parents - let local_parents_ids = nostr_event.tags.enumerated().filter { (index, tag) in - return tag.count >= 2 && tag[0] == "e" && !nostr_event.content.contains("#[\(index)]") - }.map { tag in - return tag.1[1] - }.filter { tag_id in - return !parents_ids.contains(tag_id) - } - - print("ThreadV2View: Sub Parents list: (\(local_parents_ids))") - - // Expand new parents id - parents_ids.append(contentsOf: local_parents_ids) - - if local_parents_ids.count > 0 { - // Ask for parents - let parents_events = NostrFilter( - ids: local_parents_ids, - limit: UInt32(local_parents_ids.count) - ) - let uuid = subscribe(filters: [parents_events]) - parents_events_uuids.append(uuid) - print("ThreadV2View: Ask for sub_parents (\(local_parents_ids)) \(uuid)") - } - - thread!.clean() - unsubscribe(id) + handle_parent_events(sub_id: id, nostr_event: nostr_event) return }