Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActivityPub fixes for ContentType and Follow Messages #200

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
9 changes: 6 additions & 3 deletions src/activitypub.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/routes/activitypub/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ router.get('/:guid', async (req, res) => {
object = synthesizeActivity(object);
}

res.setHeader('Content-Type', 'application/activity+json');
return res.json(object);
});

Expand Down
6 changes: 5 additions & 1 deletion src/routes/activitypub/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
});

Expand Down Expand Up @@ -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);
});

Expand Down Expand Up @@ -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);
});

Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
});
Expand Down
4 changes: 2 additions & 2 deletions src/signature.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down