Skip to content

Commit 06ff17f

Browse files
committed
Add getToots
1 parent a7877d4 commit 06ff17f

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

components/TootCard/TootCard.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const TootCard: FC<ITootCardProps> = ({ toots }) => {
1616
const { author, data } = toots;
1717
return (
1818
<section className={styles["toot-card"]}>
19-
<a href={`https://mastodon.social/${author.username}`}>@{author.username}</a>
19+
<a href={author.url}>@{author.username}@mastodon.social</a>
2020
{data.map(({ id, text, created_at }) => (
2121
<div key={id}>
2222
<p>

lib/mastodon/getToots.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
interface IMastodonAccountResponse {
2+
id: string;
3+
username: string;
4+
acct: string;
5+
display_name: string;
6+
created_at: string; // 'yyyy-mm-ddT00:00:00.000Z',
7+
note: string; // html
8+
url: string;
9+
avatar: string;
10+
avatar_static: string;
11+
header: string;
12+
header_static: string;
13+
followers_count: number;
14+
following_count: number;
15+
statuses_count: number;
16+
last_status_at: string; // yyyy-mm-dd
17+
}
18+
19+
interface ITootResponse {
20+
id: string;
21+
created_at: string; // 'yyyy-mm-ddT00:00:00.000Z',
22+
url: string;
23+
replies_count: number;
24+
reblogs_count: number;
25+
favourites_count: number;
26+
content: string; // html
27+
account: IMastodonAccountResponse;
28+
}
29+
30+
type AccountsStatusesResponse = ITootResponse[];
31+
32+
export interface IToots {
33+
data: { id: string; text: string; created_at: string }[];
34+
author: { username: string; url: string };
35+
}
36+
37+
const MASTODON_API_ACCOUNTS_URL = "https://mastodon.social/api/v1/accounts/";
38+
39+
const getTootsById = async (
40+
MASTODON_ACCESS_TOKEN: string,
41+
MASTODON_ID: string
42+
): Promise<AccountsStatusesResponse | undefined> => {
43+
try {
44+
const response = await fetch(
45+
`${MASTODON_API_ACCOUNTS_URL}${MASTODON_ID}/statuses`,
46+
{
47+
headers: {
48+
Authorization: `Bearer ${MASTODON_ACCESS_TOKEN}`,
49+
},
50+
}
51+
);
52+
if (response.ok) {
53+
const toots: AccountsStatusesResponse = await response.json();
54+
return toots;
55+
} else {
56+
console.log("getTootsById not ok");
57+
return;
58+
}
59+
} catch (err) {
60+
return;
61+
}
62+
};
63+
64+
export const getToots = async (): Promise<IToots | null> => {
65+
const { MASTODON_ACCESS_TOKEN, MASTODON_ID } = process.env;
66+
const count = 2;
67+
68+
if (!MASTODON_ACCESS_TOKEN || !MASTODON_ID) {
69+
console.log("getToots envars not set");
70+
return null;
71+
}
72+
73+
try {
74+
const toots = await getTootsById(MASTODON_ACCESS_TOKEN, MASTODON_ID);
75+
76+
if (!toots || toots.length <= 0) {
77+
throw Error(`no toots found for ${MASTODON_ID}`);
78+
}
79+
return {
80+
data: toots.slice(0, count).map(({ id, created_at, content }) => ({
81+
id,
82+
created_at,
83+
text: content,
84+
})),
85+
author: {
86+
username: toots[0].account.username,
87+
url: toots[0].account.url,
88+
},
89+
};
90+
} catch (err) {
91+
console.log("error: " + err);
92+
return null;
93+
}
94+
};

0 commit comments

Comments
 (0)