Skip to content

Commit

Permalink
Updated following and followers collection to only return URLs (#262
Browse files Browse the repository at this point in the history
)

refs [AP-649](https://linear.app/ghost/issue/AP-649/update-activitypub-follows-and-following-collections-to-only-return)

Updated `following` and `followers` collection to only return URLs in preparation
for the upcoming changes around storing `accounts`. We return just the minimal
information required for the `following` and `followers` collections so we can
reduce the amount of data we need to store
  • Loading branch information
mike182uk authored Jan 16, 2025
1 parent f410f44 commit 558ac46
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
7 changes: 4 additions & 3 deletions features/step_definitions/stepdefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,8 @@ Then(
// Check that the activity was sent to all followers
const activity = this.activities[activityName];

for (const follower of followers) {
for (const followerUrl of followers) {
const follower = await (await fetchActivityPub(followerUrl)).json();
const inbox = new URL(follower.inbox);

const found = await waitForRequest(
Expand Down Expand Up @@ -1253,7 +1254,7 @@ Then('{string} is in our Followers', async function (actorName) {
const actor = this.actors[actorName];

const found = (followers.orderedItems || []).find(
(item) => item.id === actor.id,
(item) => item === actor.id,
);

assert(found);
Expand All @@ -1280,7 +1281,7 @@ Then('{string} is in our Followers once only', async function (actorName) {
const followers = await firstPageResponse.json();
const actor = this.actors[actorName];
const found = (followers.orderedItems || []).filter(
(item) => item.id === actor.id,
(item) => item === actor.id,
);

assert.equal(found.length, 1);
Expand Down
15 changes: 13 additions & 2 deletions src/dispatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,14 @@ export async function followersDispatcher(
return {
items: (
await Promise.all(items.map((item) => APObject.fromJsonLd(item)))
).filter((item): item is Actor => isActor(item)),
)
.filter((item): item is Actor => isActor(item))
.map((item) => {
return {
id: item.id,
inboxId: item.inboxId,
};
}),
nextCursor,
};
}
Expand Down Expand Up @@ -592,7 +599,11 @@ export async function followingDispatcher(
});
}
}),
).then((results) => results.filter((r): r is Actor => isActor(r)));
).then((results) =>
results
.filter((r): r is Actor => isActor(r) && r.id !== null)
.map((r) => new URL(r.id!)),
);

return {
items,
Expand Down
18 changes: 6 additions & 12 deletions src/dispatchers.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,11 @@ describe('dispatchers', () => {

// Check correct items are returned in the correct order
expect(result.items.length).toEqual(2);
expect(result.items[0] instanceof Person).toBeTruthy();
expect(result.items[1] instanceof Person).toBeTruthy();
// @ts-ignore: We know that this is the correct type because of the above assertions
expect(result.items[0].id.toString()).toEqual(
'https://example.com/person/123',
expect(result.items[0]).toEqual(
new URL('https://example.com/person/123'),
);
// @ts-ignore: We know that this is the correct type because of the above assertions
expect(result.items[1].id.toString()).toEqual(
'https://example.com/person/456',
expect(result.items[1]).toEqual(
new URL('https://example.com/person/456'),
);
});

Expand All @@ -123,10 +119,8 @@ describe('dispatchers', () => {

// Check correct items are returned
expect(result.items.length).toEqual(1);
expect(result.items[0] instanceof Person).toBeTruthy();
// @ts-ignore: We know that this is the correct type because of the above assertions
expect(result.items[0].id.toString()).toEqual(
'https://example.com/person/456',
expect(result.items[0]).toEqual(
new URL('https://example.com/person/456'),
);
});
});
Expand Down

0 comments on commit 558ac46

Please sign in to comment.