fix: moderation updates

This commit is contained in:
2024-04-22 21:15:43 +01:00
parent bfdcbca08b
commit ee9f941b11
7 changed files with 59 additions and 41 deletions

View File

@ -12,12 +12,15 @@ import {
} from ".";
import { findTag } from "./utils";
/**
* An object which can be stored in a nostr event as a tag
*/
export interface ToNostrEventTag {
toEventTag(): Array<string> | undefined;
}
export class NostrHashtagLink implements ToNostrEventTag {
constructor(readonly tag: string) {}
constructor(readonly tag: string) { }
toEventTag() {
return ["t", this.tag];
@ -25,7 +28,7 @@ export class NostrHashtagLink implements ToNostrEventTag {
}
export class UnknownTag implements ToNostrEventTag {
constructor(readonly value: Array<string>) {}
constructor(readonly value: Array<string>) { }
toEventTag(): string[] | undefined {
return this.value;
}
@ -191,11 +194,10 @@ export class NostrLink implements ToNostrEventTag {
}
}
static fromTag<T = NostrLink>(
static fromTag(
tag: Array<string>,
author?: string,
kind?: number,
fnOther?: (tag: Array<string>) => T | undefined,
) {
const relays = tag.length > 2 ? [tag[2]] : undefined;
switch (tag[0]) {
@ -209,17 +211,15 @@ export class NostrLink implements ToNostrEventTag {
const [kind, author, dTag] = tag[1].split(":");
return new NostrLink(NostrPrefix.Address, dTag, Number(kind), author, relays, tag[3]);
}
default: {
return fnOther?.(tag) ?? new UnknownTag(tag);
}
}
throw new Error("Unknown tag!");
}
static fromTags<T = NostrLink>(tags: ReadonlyArray<Array<string>>, fnOther?: (tag: Array<string>) => T | undefined) {
static fromTags(tags: ReadonlyArray<Array<string>>) {
return removeUndefined(
tags.map(a => {
try {
return NostrLink.fromTag<T>(a, undefined, undefined, fnOther);
return NostrLink.fromTag(a);
} catch {
// ignored, cant be mapped
}
@ -227,6 +227,21 @@ export class NostrLink implements ToNostrEventTag {
);
}
/**
* Parse all tags even if they are unknown
*/
static fromAllTags(tags: ReadonlyArray<Array<string>>): Array<ToNostrEventTag> {
return removeUndefined(
tags.map(a => {
try {
return NostrLink.fromTag(a);
} catch {
return new UnknownTag(a);
}
}),
);
}
static fromEvent(ev: TaggedNostrEvent | NostrEvent) {
const relays = "relays" in ev ? ev.relays : undefined;