substr/models/profile.ts

97 lines
2.0 KiB
TypeScript

import { nip19, NostrEvent as NEvent } from "@nostr/tools";
import { verifyNip05Address } from "../nostr.ts";
import { getImageMagickCommand } from "../utils.ts";
import config from "../config.ts";
const magick = await getImageMagickCommand();
export interface ProfileData {
name?: string;
display_name?: string;
displayName?: string;
about?: string;
picture?: string;
nip05?: string;
lud16?: string;
}
export default class Profile {
private data: ProfileData;
event: NEvent;
username?: string;
constructor(event: NEvent, username?: string) {
this.event = event;
this.data = JSON.parse(event.content);
this.username = username;
}
get updatedAt(): number {
return this.event.created_at;
}
get name(): string {
return this.data.display_name || this.data.displayName ||
this.data.name || "Anonymous";
}
get about(): string {
return this.data.about || "";
}
get picture(): string | undefined {
return this.data.picture;
}
get nip05(): string | undefined {
return this.data.nip05?.replace("_@", "");
}
get lud16(): string | undefined {
return this.data.lud16;
}
get pubkey(): string {
return this.event.pubkey;
}
get npub(): string {
return nip19.npubEncode(this.pubkey);
}
get nprofile(): string {
return nip19.nprofileEncode({
pubkey: this.pubkey,
relays: [config.relay_urls[0]],
});
}
get profileUrl(): string {
return `${config.base_url}/@${this.username}`;
}
get avatarImageUrl(): string {
if (magick) {
return `${config.base_url}/assets/g/img/p-${this.event.id}.png`;
} else {
return this.picture || "";
}
}
get ogImageUrl(): string {
if (magick) {
return `${config.base_url}/assets/g/img/og-p-${this.event.id}.png`;
} else {
return this.picture || "";
}
}
verifyNip05(): Promise<boolean> {
if (typeof this.data.nip05 !== "undefined") {
return verifyNip05Address(this.data.nip05, this.pubkey);
} else {
return Promise.resolve(false);
}
}
}