aboutsummaryrefslogtreecommitdiff
path: root/src/lib/utils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/utils.ts')
-rw-r--r--src/lib/utils.ts25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/lib/utils.ts b/src/lib/utils.ts
index c709bbd..67d6e08 100644
--- a/src/lib/utils.ts
+++ b/src/lib/utils.ts
@@ -1,5 +1,6 @@
import type { NDKEvent } from "@nostr-dev-kit/ndk";
import { nip19 } from "nostr-tools";
+
export function neventEncode(event: NDKEvent, relays: string[]) {
return nip19.neventEncode({
id: event.id,
@@ -81,3 +82,27 @@ export function isElementInViewport(el: string | HTMLElement) {
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
}
+
+/**
+ * Removes `kind: 30040` index events that don't comply with the NKBIP-01 specification.
+ * @param events A set of events.
+ * @returns The filtered set of events.
+ */
+export function filterValidIndexEvents(events: Set<NDKEvent>): Set<NDKEvent> {
+ // The filter object supports only limited parameters, so we need to filter out events that
+ // don't respect NKBIP-01.
+ events.forEach(event => {
+ // Index events have no content, and they must have `title`, `d`, and `e` tags.
+ if (
+ event.content != null
+ || event.getMatchingTags('title').length === 0
+ || event.getMatchingTags('d').length === 0
+ || event.getMatchingTags('e').length === 0
+ ) {
+ events.delete(event);
+ }
+ });
+
+ console.debug(`Filtered index events: ${events.size} events remaining.`);
+ return events;
+}