From 63e14542ff1d62ef22e2e53ddc4adc36ab3c2ee6 Mon Sep 17 00:00:00 2001 From: Jon Staab Date: Fri, 10 Nov 2023 22:46:11 -0800 Subject: [PATCH] Fix tag normalization --- package.json | 2 +- src/util/nostr.ts | 33 ++++++++++++++++++++------------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index d8e9f62..bd43a0e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "paravel", - "version": "0.4.1", + "version": "0.4.2", "description": "Yet another toolkit for nostr", "author": "hodlbod", "license": "MIT", diff --git a/src/util/nostr.ts b/src/util/nostr.ts index f47ebb4..00aefc0 100644 --- a/src/util/nostr.ts +++ b/src/util/nostr.ts @@ -180,26 +180,33 @@ export class Tags extends Fluent { // Support the deprecated version where tags are not marked as replies normalize() { - const tags = this.type(["a", "e"]).filter(t => last(t) !== "mention") - const legacy = tags.any(t => !["reply", "root"].includes(last(t))) + const tags = this.type(["a", "e"]) - if (!legacy) { + // If we have a mark, we're not using the legacy format + if (tags.any(t => ["reply", "root"].includes(last(t)))) { return this } - const reply = tags.last() - const root = tags.count() > 1 ? tags.first() : null - const newTags = tags.reject(t => [reply?.[1], root?.[1]].includes(t[1])).all() + const reply = tags.values().last() + const root = tags.count() > 1 ? tags.values().first() : null - if (reply) { - newTags.push(reply.slice(0, 3).concat("reply")) - } + return new Tags(tags.all().map(t => { + t = t.slice(0, 3) - if (root) { - newTags.push(root.slice(0, 3).concat("root")) - } + if (t.length === 2) { + t.push("") + } - return new Tags(newTags) + let mark = 'mention' + + if (t[1] === reply) { + mark = 'reply' + } else if (t[1] === root) { + mark = 'root' + } + + return [...t, mark] + })) } getReply = () => {