Skip to content

Commit

Permalink
fix(route/fediverse): Fixes for ActivityPub (#16360)
Browse files Browse the repository at this point in the history
  • Loading branch information
xtexx authored Aug 4, 2024
1 parent 8482502 commit 49b967c
Showing 1 changed file with 35 additions and 18 deletions.
53 changes: 35 additions & 18 deletions lib/routes/fediverse/timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const route: Route = {
};

const allowedDomain = new Set(['mastodon.social', 'pawoo.net', config.mastodon.apiHost].filter(Boolean));
const activityPubTypes = new Set(['application/activity+json', 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"']);

async function handler(ctx) {
const account = ctx.req.param('account');
Expand All @@ -41,13 +42,17 @@ async function handler(ctx) {

const requestOptions = {
headers: {
Accept: 'application/activity+json',
Accept: 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
},
};

const acc = await ofetch(`https://${domain}/.well-known/webfinger?resource=acct:${account}`, requestOptions);
const acc = await ofetch(`https://${domain}/.well-known/webfinger?resource=acct:${account}`, {
headers: {
Accept: 'application/jrd+json',
},
});

const jsonLink = acc.links.find((link) => link.rel === 'self')?.href;
const jsonLink = acc.links.find((link) => link.rel === 'self' && activityPubTypes.has(link.type))?.href;
const link = acc.links.find((link) => link.rel === 'http://webfinger.net/rel/profile-page')?.href;

const self = await ofetch(jsonLink, requestOptions);
Expand All @@ -57,25 +62,37 @@ async function handler(ctx) {

const items = firstOutbox.orderedItems;

const itemResolvers = [] as Promise<any>[];

for (const item of items) {
if (!['Announce', 'Create', 'Update'].includes(item.type)) {
continue;
}
if (typeof item.object === 'string') {
itemResolvers.push(
(async (item) => {
item.object = await ofetch(item.object, requestOptions);
return item;
})(item)
);
} else {
itemResolvers.push(Promise.resolve(item));
}
}

const resolvedItems = await Promise.all(itemResolvers);

return {
title: `${self.name || self.preferredUsername} (Fediverse@${account})`,
description: self.summary,
image: self.icon?.url || self.image?.url,
link,
item: items.map((item) => {
const object =
typeof item.object === 'string'
? {
content: item.object,
}
: item.object;
return {
title: object.content,
description: `${object.content}\n${object.attachment?.map((attachment) => `<img src="${attachment.url}" width="${attachment.width}" height="${attachment.height}" />`).join('\n') || ''}`,
link: item.url,
pubDate: parseDate(item.published),
guid: item.id,
};
}),
item: resolvedItems.map((item) => ({
title: item.object.content,
description: `${item.object.content}\n${item.object.attachment?.map((attachment) => `<img src="${attachment.url}" width="${attachment.width}" height="${attachment.height}" />`).join('\n') || ''}`,
link: item.url,
pubDate: parseDate(item.published),
guid: item.id,
})),
};
}

0 comments on commit 49b967c

Please sign in to comment.