|
| 1 | +import { convert } from "html-to-text"; |
| 2 | + |
| 3 | +interface IMastodonAccountResponse { |
| 4 | + id: string; |
| 5 | + username: string; |
| 6 | + acct: string; |
| 7 | + display_name: string; |
| 8 | + created_at: string; // 'yyyy-mm-ddT00:00:00.000Z', |
| 9 | + note: string; // html |
| 10 | + url: string; |
| 11 | + avatar: string; |
| 12 | + avatar_static: string; |
| 13 | + header: string; |
| 14 | + header_static: string; |
| 15 | + followers_count: number; |
| 16 | + following_count: number; |
| 17 | + statuses_count: number; |
| 18 | + last_status_at: string; // yyyy-mm-dd |
| 19 | +} |
| 20 | + |
| 21 | +interface ITootResponse { |
| 22 | + id: string; |
| 23 | + created_at: string; // 'yyyy-mm-ddT00:00:00.000Z', |
| 24 | + url: string; |
| 25 | + replies_count: number; |
| 26 | + reblogs_count: number; |
| 27 | + favourites_count: number; |
| 28 | + content: string; // html |
| 29 | + account: IMastodonAccountResponse; |
| 30 | +} |
| 31 | + |
| 32 | +type AccountsStatusesResponse = ITootResponse[]; |
| 33 | + |
| 34 | +interface IToot { |
| 35 | + id: string; |
| 36 | + text: string; |
| 37 | + created_at: string; |
| 38 | + url: string; |
| 39 | + reblogs_count: number; |
| 40 | + replies_count: number; |
| 41 | + favourites_count: number; |
| 42 | +} |
| 43 | + |
| 44 | +export interface IToots { |
| 45 | + data: IToot[]; |
| 46 | + author: { username: string; url: string }; |
| 47 | +} |
| 48 | + |
| 49 | +const MASTODON_API_ACCOUNTS_URL = "https://mastodon.social/api/v1/accounts/"; |
| 50 | + |
| 51 | +const getTootsById = async ( |
| 52 | + MASTODON_ACCESS_TOKEN: string, |
| 53 | + MASTODON_ID: string |
| 54 | +): Promise<AccountsStatusesResponse | undefined> => { |
| 55 | + try { |
| 56 | + const response = await fetch( |
| 57 | + `${MASTODON_API_ACCOUNTS_URL}${MASTODON_ID}/statuses`, |
| 58 | + { |
| 59 | + headers: { |
| 60 | + Authorization: `Bearer ${MASTODON_ACCESS_TOKEN}`, |
| 61 | + }, |
| 62 | + } |
| 63 | + ); |
| 64 | + if (response.ok) { |
| 65 | + const toots: AccountsStatusesResponse = await response.json(); |
| 66 | + return toots; |
| 67 | + } else { |
| 68 | + console.log("getTootsById not ok"); |
| 69 | + return; |
| 70 | + } |
| 71 | + } catch (err) { |
| 72 | + return; |
| 73 | + } |
| 74 | +}; |
| 75 | + |
| 76 | +const mapTootResponseToToot = ({ |
| 77 | + id, |
| 78 | + created_at, |
| 79 | + content, |
| 80 | + url, |
| 81 | + reblogs_count, |
| 82 | + replies_count, |
| 83 | + favourites_count, |
| 84 | +}: ITootResponse): IToot => ({ |
| 85 | + id, |
| 86 | + created_at, |
| 87 | + url, |
| 88 | + text: convert(content, { |
| 89 | + wordwrap: 130, |
| 90 | + ignoreHref: true, |
| 91 | + }), |
| 92 | + replies_count, |
| 93 | + reblogs_count, |
| 94 | + favourites_count, |
| 95 | +}); |
| 96 | + |
| 97 | +export const getToots = async (): Promise<IToots | null> => { |
| 98 | + const { MASTODON_ACCESS_TOKEN, MASTODON_ID } = process.env; |
| 99 | + const count = 2; |
| 100 | + |
| 101 | + if (!MASTODON_ACCESS_TOKEN || !MASTODON_ID) { |
| 102 | + console.log("getToots envars not set"); |
| 103 | + return null; |
| 104 | + } |
| 105 | + |
| 106 | + try { |
| 107 | + const toots = await getTootsById(MASTODON_ACCESS_TOKEN, MASTODON_ID); |
| 108 | + |
| 109 | + if (!toots || toots.length <= 0) { |
| 110 | + throw Error(`no toots found for ${MASTODON_ID}`); |
| 111 | + } |
| 112 | + return { |
| 113 | + data: toots.slice(0, count).map(mapTootResponseToToot), |
| 114 | + author: { |
| 115 | + username: toots[0].account.username, |
| 116 | + url: toots[0].account.url, |
| 117 | + }, |
| 118 | + }; |
| 119 | + } catch (err) { |
| 120 | + console.log("error: " + err); |
| 121 | + return null; |
| 122 | + } |
| 123 | +}; |
0 commit comments