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