From 492ef3e931a695ffb5a4056bad465b5182f77fc5 Mon Sep 17 00:00:00 2001 From: Tom Casavant Date: Fri, 8 Nov 2024 15:16:18 -0500 Subject: [PATCH 1/3] Fixed express.json configuration, fixed content type issues --- server.js | 8 +++++--- src/routes/activitypub/message.js | 1 + src/routes/activitypub/user.js | 7 ++++++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/server.js b/server.js index cd31faa..15ca3f9 100644 --- a/server.js +++ b/server.js @@ -17,9 +17,10 @@ const PORT = process.env.PORT || 3000; const app = express(); app.use(express.static('public')); + app.use(express.urlencoded({ extended: true })); -app.use(express.json()); -app.use(express.json({ type: 'application/activity+json' })); +app.use(express.json({ type: ['application/json', 'application/ld+json', 'application/activity+json'] })); + app.use(session()); app.use((req, res, next) => { @@ -117,6 +118,7 @@ app.engine('.hbs', hbs.engine); app.use(simpleLogger); + app.use('/admin', isAuthenticated, routes.admin); app.use('/', routes.auth); app.use('/bookmark', routes.bookmark); @@ -131,4 +133,4 @@ app.use('/nodeinfo/2.0', routes.nodeinfo); app.use('/nodeinfo/2.1', routes.nodeinfo); app.use('/opensearch.xml', routes.opensearch); -app.listen(PORT, () => console.log(`App listening on port ${PORT}`)); +app.listen(PORT, () => console.log(`App listening on port ${PORT}`)); \ No newline at end of file diff --git a/src/routes/activitypub/message.js b/src/routes/activitypub/message.js index 6ab053e..7ccc664 100644 --- a/src/routes/activitypub/message.js +++ b/src/routes/activitypub/message.js @@ -34,6 +34,7 @@ router.get('/:guid', async (req, res) => { object = synthesizeActivity(object); } + res.setHeader('Content-Type', 'application/activity+json'); return res.json(object); }); diff --git a/src/routes/activitypub/user.js b/src/routes/activitypub/user.js index 21f96fc..8fb0e7a 100644 --- a/src/routes/activitypub/user.js +++ b/src/routes/activitypub/user.js @@ -16,7 +16,7 @@ router.get('/:name', async (req, res) => { const domain = req.app.get('domain'); const username = name; name = `${name}@${domain}`; - + const actor = await db.getActor(); if (actor === undefined) { @@ -31,6 +31,7 @@ router.get('/:name', async (req, res) => { if (tempActor.outbox === undefined) { tempActor.outbox = `https://${domain}/u/${username}/outbox`; } + res.setHeader('Content-Type', 'application/activity+json'); return res.json(tempActor); }); @@ -63,6 +64,7 @@ router.get('/:name/followers', async (req, res) => { }, '@context': ['https://www.w3.org/ns/activitystreams'], }; + res.setHeader('Content-Type', 'application/activity+json'); return res.json(followersCollection); }); @@ -90,6 +92,7 @@ router.get('/:name/following', async (req, res) => { }, '@context': ['https://www.w3.org/ns/activitystreams'], }; + res.setHeader('Content-Type', 'application/activity+json'); return res.json(followingCollection); }); @@ -116,6 +119,7 @@ router.get('/:name/outbox', async (req, res) => { last: pageLink(lastPage), '@context': ['https://www.w3.org/ns/activitystreams'], }; + res.setHeader('Content-Type', 'application/activity+json'); return res.json(outboxCollection); } @@ -147,6 +151,7 @@ router.get('/:name/outbox', async (req, res) => { if (page > 1) { collectionPage.prev = pageLink(page - 1); } + res.setHeader('Content-Type', 'application/activity+json'); return res.json(collectionPage); }); From 3210abbda42b3519aa7002a7999627dd4a7e7ca5 Mon Sep 17 00:00:00 2001 From: Tom Casavant Date: Fri, 8 Nov 2024 15:25:46 -0500 Subject: [PATCH 2/3] Added in fixes from glitch that I missed for fetching user inboxes --- src/activitypub.js | 11 +++++++---- src/signature.js | 4 ++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/activitypub.js b/src/activitypub.js index 09fd880..1de8a3b 100644 --- a/src/activitypub.js +++ b/src/activitypub.js @@ -19,9 +19,11 @@ export async function signAndSend(message, name, domain, db, targetDomain, inbox console.log(`Sent message to an inbox at ${targetDomain}!`); console.log('Response Status Code:', response.status); console.log('Response body:', data); + return response } catch (error) { console.log('Error:', error.message); console.log('Stacktrace: ', error.stack); + return error } } @@ -144,7 +146,7 @@ export async function createFollowMessage(account, domain, target, db) { const guid = crypto.randomBytes(16).toString('hex'); const followMessage = { '@context': 'https://www.w3.org/ns/activitystreams', - id: guid, + id: `https://${domain}/m/${guid}`, type: 'Follow', actor: `https://${domain}/u/${account}`, object: target, @@ -182,8 +184,9 @@ export async function createUnfollowMessage(account, domain, target, db) { } export async function getInboxFromActorProfile(profileUrl) { - const response = await signedGetJSON(`${profileUrl}.json`); + const response = await signedGetJSON(`${profileUrl}`); const data = await response.json(); + console.log(data); if (data?.inbox) { return data.inbox; @@ -196,7 +199,7 @@ export async function lookupActorInfo(actorUsername) { const parsedDomain = actorUsername.split('@').slice(-1); const parsedUsername = actorUsername.split('@').slice(-2, -1); try { - const response = await fetch(`https://${parsedDomain}/.well-known/webfinger/?resource=acct:${parsedUsername}@${parsedDomain}`); + const response = await fetch(`https://${parsedDomain}/.well-known/webfinger?resource=acct:${parsedUsername}@${parsedDomain}`); const data = await response.json(); const selfLink = data.links.find((o) => o.rel === 'self'); if (!selfLink || !selfLink.href) { @@ -276,4 +279,4 @@ export function synthesizeActivity(note) { actor: note.attributedTo, object: note, }; -} +} \ No newline at end of file diff --git a/src/signature.js b/src/signature.js index 473d10d..7c79f4b 100644 --- a/src/signature.js +++ b/src/signature.js @@ -132,13 +132,13 @@ export async function signedFetch(url, init = {}) { */ function _signedFetchJSON(url, method = 'GET', init = {}) { const { body, headers = {}, ...rest } = init; - const contentTypeHeader = body ? { 'Content-Type': 'application/json' } : {}; + const contentTypeHeader = body ? { 'Content-Type': 'application/activity+json' } : {}; return signedFetch(url, { body, headers: { ...headers, - Accept: 'application/json', + Accept: 'application/activity+json', ...contentTypeHeader, }, ...rest, From f1b731b2dac780afd9f4d49ad4971f34b97d135b Mon Sep 17 00:00:00 2001 From: Tom Casavant Date: Fri, 8 Nov 2024 15:34:13 -0500 Subject: [PATCH 3/3] Linting fixes --- server.js | 3 +-- src/activitypub.js | 6 +++--- src/routes/activitypub/user.js | 1 - 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/server.js b/server.js index 15ca3f9..d9a617a 100644 --- a/server.js +++ b/server.js @@ -118,7 +118,6 @@ app.engine('.hbs', hbs.engine); app.use(simpleLogger); - app.use('/admin', isAuthenticated, routes.admin); app.use('/', routes.auth); app.use('/bookmark', routes.bookmark); @@ -133,4 +132,4 @@ app.use('/nodeinfo/2.0', routes.nodeinfo); app.use('/nodeinfo/2.1', routes.nodeinfo); app.use('/opensearch.xml', routes.opensearch); -app.listen(PORT, () => console.log(`App listening on port ${PORT}`)); \ No newline at end of file +app.listen(PORT, () => console.log(`App listening on port ${PORT}`)); diff --git a/src/activitypub.js b/src/activitypub.js index 1de8a3b..86eaff1 100644 --- a/src/activitypub.js +++ b/src/activitypub.js @@ -19,11 +19,11 @@ export async function signAndSend(message, name, domain, db, targetDomain, inbox console.log(`Sent message to an inbox at ${targetDomain}!`); console.log('Response Status Code:', response.status); console.log('Response body:', data); - return response + return response; } catch (error) { console.log('Error:', error.message); console.log('Stacktrace: ', error.stack); - return error + return error; } } @@ -279,4 +279,4 @@ export function synthesizeActivity(note) { actor: note.attributedTo, object: note, }; -} \ No newline at end of file +} diff --git a/src/routes/activitypub/user.js b/src/routes/activitypub/user.js index 8fb0e7a..59cb735 100644 --- a/src/routes/activitypub/user.js +++ b/src/routes/activitypub/user.js @@ -16,7 +16,6 @@ router.get('/:name', async (req, res) => { const domain = req.app.get('domain'); const username = name; name = `${name}@${domain}`; - const actor = await db.getActor(); if (actor === undefined) {