Parse mentions

This commit is contained in:
Alex Gleason 2023-03-18 17:03:32 -05:00
parent c28c644265
commit 46cdda7f7f
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 23 additions and 11 deletions

View File

@ -16,31 +16,43 @@ function parseContent(event: Event<0>): MetaContent {
function toAccount(event: Event<0>) { function toAccount(event: Event<0>) {
const { pubkey } = event; const { pubkey } = event;
const content: MetaContent = parseContent(event); const { name, nip05, picture, banner, about }: MetaContent = parseContent(event);
const { host, origin } = new URL(LOCAL_DOMAIN); const { host, origin } = new URL(LOCAL_DOMAIN);
const npub = nip19.npubEncode(pubkey); const npub = nip19.npubEncode(pubkey);
return { return {
id: pubkey, id: pubkey,
acct: content.nip05 || npub, acct: nip05 || npub,
avatar: content.picture || DEFAULT_AVATAR, avatar: picture || DEFAULT_AVATAR,
avatar_static: content.picture || DEFAULT_AVATAR, avatar_static: picture || DEFAULT_AVATAR,
bot: false, bot: false,
created_at: event ? new Date(event.created_at * 1000).toISOString() : new Date().toISOString(), created_at: event ? new Date(event.created_at * 1000).toISOString() : new Date().toISOString(),
display_name: content.name, display_name: name,
emojis: [], emojis: [],
fields: [], fields: [],
follow_requests_count: 0, follow_requests_count: 0,
followers_count: 0, followers_count: 0,
following_count: 0, following_count: 0,
statuses_count: 0, statuses_count: 0,
header: content.banner, header: banner,
header_static: content.banner, header_static: banner,
locked: false, locked: false,
note: content.about, note: about,
fqn: content.nip05 || `${npub}@${host}`, fqn: nip05 || `${npub}@${host}`,
url: `${origin}/users/${pubkey}`, url: `${origin}/users/${pubkey}`,
username: content.nip05 || npub, username: nip05 ? nip05.split('@')[0] : npub,
};
}
async function toMention(tag: string[]) {
const profile = await fetchUser(tag[1]);
const account = profile ? toAccount(profile) : undefined;
return {
id: account?.id || tag[1],
acct: account?.acct || tag[1],
username: account?.username || tag[1],
url: account?.url,
}; };
} }
@ -72,7 +84,7 @@ async function toStatus(event: Event<1>) {
reblog: null, reblog: null,
application: null, application: null,
media_attachments: [], media_attachments: [],
mentions: [], mentions: await Promise.all(event.tags.filter((tag) => tag[0] === 'p').map(toMention)),
tags: [], tags: [],
emojis: [], emojis: [],
card: null, card: null,