substr/models/article.ts
2024-10-23 00:27:20 +02:00

58 lines
1.3 KiB
TypeScript

import { render as renderMarkdown } from "@deno/gfm";
import { nip19 } from "@nostr/tools";
import { NostrEvent as NEvent } from "@nostrify/nostrify";
import config from "../config.ts";
export default class Article {
event: NEvent;
constructor(event: NEvent) {
this.event = event;
}
get identifier(): string {
const tag = this.event.tags.find((t) => t[0] === "d");
return tag ? tag[1] : "";
}
get isDraft(): boolean {
return this.event.kind === 30024;
}
get url(): string {
return `${config.base_url}/${this.naddr}`;
}
get title(): string {
const tag = this.event.tags.find((t) => t[0] === "title");
return tag ? tag[1] : "Untitled";
}
get summary(): string {
const tag = this.event.tags.find((t) => t[0] === "summary");
return tag ? tag[1] : "";
}
get publishedAt(): number {
const tag = this.event.tags.find((t) => t[0] === "published_at");
return tag ? parseInt(tag[1]) : this.event.created_at;
}
get updatedAt(): number {
return this.event.created_at;
}
get html(): string {
return renderMarkdown(this.event.content);
}
get naddr(): string {
return nip19.naddrEncode({
identifier: this.identifier,
pubkey: this.event.pubkey,
kind: this.event.kind,
relays: [config.relay_urls[0]],
});
}
}