Path: blob/main/assets/js/emfed/client.js
1678 views
/**1* Fetch recent toots for a user, given their Mastodon URL.2*/3export async function getToots(userURL, accountId, limit, excludeReplies) {4const url = new URL(userURL);5// Either use the account id specified or look it up based on the username6// in the link.7const userId = accountId ??8(await (async () => {9// Extract username from URL.10const parts = /@(\w+)$/.exec(url.pathname);11if (!parts) {12throw "not a Mastodon user URL";13}14const username = parts[1];15// Look up user ID from username.16const lookupURL = Object.assign(new URL(url), {17pathname: "/api/v1/accounts/lookup",18search: `?acct=${username}`,19});20return (await (await fetch(lookupURL)).json())["id"];21})());22// Fetch toots.23const tootURL = Object.assign(new URL(url), {24pathname: `/api/v1/accounts/${userId}/statuses`,25search: `?limit=${limit ?? 5}&exclude_replies=${!!excludeReplies}`,26});27return await (await fetch(tootURL)).json();28}293031