diff --git a/server.js b/server.js index cd31faa..d9a617a 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) => { diff --git a/src/activitypub.js b/src/activitypub.js index 09fd880..86eaff1 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) { 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..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) { @@ -31,6 +30,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 +63,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 +91,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 +118,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 +150,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); }); 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,