From 837b371551aa7678b9ecfa6911e6d03439b4efea Mon Sep 17 00:00:00 2001 From: buttercat1791 Date: Fri, 14 Mar 2025 08:03:12 -0500 Subject: [PATCH] Add a method to return the hierarchy in which an event lives --- src/lib/data_structures/publication_tree.ts | 22 +++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/lib/data_structures/publication_tree.ts b/src/lib/data_structures/publication_tree.ts index 4306b1e..bac54a0 100644 --- a/src/lib/data_structures/publication_tree.ts +++ b/src/lib/data_structures/publication_tree.ts @@ -130,6 +130,28 @@ export class PublicationTree implements AsyncIterable { return event; } + /** + * Retrieves the events in the hierarchy of the event with the given address. + * @param address The address of the event for which to retrieve the hierarchy. + * @returns Returns an array of events in the addressed event's hierarchy, beginning with the + * root and ending with the addressed event. + */ + async getHierarchy(address: string): Promise { + let node = this.#nodes.get(address); + if (!node) { + return null; + } + + const hierarchy: NDKEvent[] = [this.#events.get(address)!]; + + while (node.parent) { + hierarchy.push(this.#events.get(node.parent.address)!); + node = node.parent; + } + + return hierarchy.reverse(); + } + /** * Sets a start point for iteration over the leaves of the tree. * @param address The address of the event to bookmark.