Skip to content

Commit d2cfe21

Browse files
authored
Merge pull request #200 from TomCasavant/content_type
ActivityPub fixes for ContentType and Follow Messages
2 parents 58d1ca1 + f1b731b commit d2cfe21

5 files changed

Lines changed: 17 additions & 8 deletions

File tree

server.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ const PORT = process.env.PORT || 3000;
1717

1818
const app = express();
1919
app.use(express.static('public'));
20+
2021
app.use(express.urlencoded({ extended: true }));
21-
app.use(express.json());
22-
app.use(express.json({ type: 'application/activity+json' }));
22+
app.use(express.json({ type: ['application/json', 'application/ld+json', 'application/activity+json'] }));
23+
2324
app.use(session());
2425

2526
app.use((req, res, next) => {

src/activitypub.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ export async function signAndSend(message, name, domain, db, targetDomain, inbox
1919
console.log(`Sent message to an inbox at ${targetDomain}!`);
2020
console.log('Response Status Code:', response.status);
2121
console.log('Response body:', data);
22+
return response;
2223
} catch (error) {
2324
console.log('Error:', error.message);
2425
console.log('Stacktrace: ', error.stack);
26+
return error;
2527
}
2628
}
2729

@@ -144,7 +146,7 @@ export async function createFollowMessage(account, domain, target, db) {
144146
const guid = crypto.randomBytes(16).toString('hex');
145147
const followMessage = {
146148
'@context': 'https://www.w3.org/ns/activitystreams',
147-
id: guid,
149+
id: `https://${domain}/m/${guid}`,
148150
type: 'Follow',
149151
actor: `https://${domain}/u/${account}`,
150152
object: target,
@@ -182,8 +184,9 @@ export async function createUnfollowMessage(account, domain, target, db) {
182184
}
183185

184186
export async function getInboxFromActorProfile(profileUrl) {
185-
const response = await signedGetJSON(`${profileUrl}.json`);
187+
const response = await signedGetJSON(`${profileUrl}`);
186188
const data = await response.json();
189+
console.log(data);
187190

188191
if (data?.inbox) {
189192
return data.inbox;
@@ -196,7 +199,7 @@ export async function lookupActorInfo(actorUsername) {
196199
const parsedDomain = actorUsername.split('@').slice(-1);
197200
const parsedUsername = actorUsername.split('@').slice(-2, -1);
198201
try {
199-
const response = await fetch(`https://${parsedDomain}/.well-known/webfinger/?resource=acct:${parsedUsername}@${parsedDomain}`);
202+
const response = await fetch(`https://${parsedDomain}/.well-known/webfinger?resource=acct:${parsedUsername}@${parsedDomain}`);
200203
const data = await response.json();
201204
const selfLink = data.links.find((o) => o.rel === 'self');
202205
if (!selfLink || !selfLink.href) {

src/routes/activitypub/message.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ router.get('/:guid', async (req, res) => {
3434
object = synthesizeActivity(object);
3535
}
3636

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

src/routes/activitypub/user.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ router.get('/:name', async (req, res) => {
1616
const domain = req.app.get('domain');
1717
const username = name;
1818
name = `${name}@${domain}`;
19-
2019
const actor = await db.getActor();
2120

2221
if (actor === undefined) {
@@ -31,6 +30,7 @@ router.get('/:name', async (req, res) => {
3130
if (tempActor.outbox === undefined) {
3231
tempActor.outbox = `https://${domain}/u/${username}/outbox`;
3332
}
33+
res.setHeader('Content-Type', 'application/activity+json');
3434
return res.json(tempActor);
3535
});
3636

@@ -63,6 +63,7 @@ router.get('/:name/followers', async (req, res) => {
6363
},
6464
'@context': ['https://www.w3.org/ns/activitystreams'],
6565
};
66+
res.setHeader('Content-Type', 'application/activity+json');
6667
return res.json(followersCollection);
6768
});
6869

@@ -90,6 +91,7 @@ router.get('/:name/following', async (req, res) => {
9091
},
9192
'@context': ['https://www.w3.org/ns/activitystreams'],
9293
};
94+
res.setHeader('Content-Type', 'application/activity+json');
9395
return res.json(followingCollection);
9496
});
9597

@@ -116,6 +118,7 @@ router.get('/:name/outbox', async (req, res) => {
116118
last: pageLink(lastPage),
117119
'@context': ['https://www.w3.org/ns/activitystreams'],
118120
};
121+
res.setHeader('Content-Type', 'application/activity+json');
119122

120123
return res.json(outboxCollection);
121124
}
@@ -147,6 +150,7 @@ router.get('/:name/outbox', async (req, res) => {
147150
if (page > 1) {
148151
collectionPage.prev = pageLink(page - 1);
149152
}
153+
res.setHeader('Content-Type', 'application/activity+json');
150154

151155
return res.json(collectionPage);
152156
});

src/signature.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,13 @@ export async function signedFetch(url, init = {}) {
132132
*/
133133
function _signedFetchJSON(url, method = 'GET', init = {}) {
134134
const { body, headers = {}, ...rest } = init;
135-
const contentTypeHeader = body ? { 'Content-Type': 'application/json' } : {};
135+
const contentTypeHeader = body ? { 'Content-Type': 'application/activity+json' } : {};
136136

137137
return signedFetch(url, {
138138
body,
139139
headers: {
140140
...headers,
141-
Accept: 'application/json',
141+
Accept: 'application/activity+json',
142142
...contentTypeHeader,
143143
},
144144
...rest,

0 commit comments

Comments
 (0)