feat(backend): fetch the first page of outbox when resolving Person

This commit is contained in:
Kagami Sascha Rosylight
2023-07-05 03:38:59 +02:00
parent 92d9946f59
commit 2e1de4fca9
5 changed files with 217 additions and 25 deletions

View File

@@ -11,7 +11,7 @@ import { GlobalModule } from '@/GlobalModule.js';
import { CoreModule } from '@/core/CoreModule.js';
import { FederatedInstanceService } from '@/core/FederatedInstanceService.js';
import { LoggerService } from '@/core/LoggerService.js';
import type { IActor } from '@/core/activitypub/type.js';
import type { IActor, ICreate, IObject, IOrderedCollection, IOrderedCollectionPage, IPost } from '@/core/activitypub/type.js';
import { Note } from '@/models/index.js';
import { secureRndstr } from '@/misc/secure-rndstr.js';
import { MockResolver } from '../misc/mock-resolver.js';
@@ -32,6 +32,59 @@ function createRandomActor(): IActor & { id: string } {
};
}
function createRandomCreateActivity(actor: IActor, length: number): ICreate[] {
return new Array(length).fill(null).map((): ICreate => {
const id = secureRndstr(8);
const noteId = `${host}/notes/${id}`;
return {
type: 'Create',
id: `${noteId}/activity`,
actor,
object: {
id: noteId,
type: 'Note',
attributedTo: actor.id,
content: 'test test foo',
} satisfies IPost,
};
});
}
function createRandomNonPagedOutbox(actor: IActor, length: number): IOrderedCollection {
const orderedItems = createRandomCreateActivity(actor, length);
return {
'@context': 'https://www.w3.org/ns/activitystreams',
type: 'OrderedCollection',
id: actor.outbox as string,
totalItems: orderedItems.length,
orderedItems,
};
}
function createRandomOutboxPage(actor: IActor, id: string, length: number): IOrderedCollectionPage {
const orderedItems = createRandomCreateActivity(actor, length);
return {
'@context': 'https://www.w3.org/ns/activitystreams',
type: 'OrderedCollectionPage',
id,
totalItems: orderedItems.length,
orderedItems,
};
}
function createRandomPagedOutbox(actor: IActor): IOrderedCollection {
return {
'@context': 'https://www.w3.org/ns/activitystreams',
type: 'OrderedCollection',
id: actor.outbox as string,
totalItems: 10,
first: `${actor.outbox}?first`,
};
}
describe('ActivityPub', () => {
let noteService: ApNoteService;
let personService: ApPersonService;
@@ -53,7 +106,7 @@ describe('ActivityPub', () => {
// Prevent ApPersonService from fetching instance, as it causes Jest import-after-test error
const federatedInstanceService = app.get<FederatedInstanceService>(FederatedInstanceService);
jest.spyOn(federatedInstanceService, 'fetch').mockImplementation(() => new Promise(() => {}));
jest.spyOn(federatedInstanceService, 'fetch').mockImplementation(() => new Promise(() => { }));
});
describe('Parse minimum object', () => {
@@ -126,4 +179,74 @@ describe('ActivityPub', () => {
} as Note);
});
});
describe('Outbox', () => {
test('Fetch non-paged outbox from IActor', async () => {
const actor = createRandomActor();
const outbox = createRandomNonPagedOutbox(actor, 10);
resolver._register(actor.id, actor);
resolver._register(actor.outbox as string, outbox);
// XXX: This shouldn't be needed as the collection already has the full information
// But somehow the resolver currently doesn't use it at all and always fetches again
for (const item of outbox.orderedItems as IObject[]) {
resolver._register(item.id!, item);
}
await personService.createPerson(actor.id, resolver);
for (const item of outbox.orderedItems as ICreate[]) {
const note = await noteService.fetchNote(item.object);
assert.ok(note);
assert.strictEqual(note.text, 'test test foo');
assert.strictEqual(note.uri, (item.object as IObject).id);
}
});
test('Fetch paged outbox from IActor', async () => {
const actor = createRandomActor();
const outbox = createRandomPagedOutbox(actor);
const page = createRandomOutboxPage(actor, outbox.id!, 10);
resolver._register(actor.id, actor);
resolver._register(actor.outbox as string, outbox);
resolver._register(outbox.first as string, page);
// XXX: This shouldn't be needed as the collection already has the full information
// But somehow the resolver currently doesn't use it at all and always fetches again
for (const item of page.orderedItems as IObject[]) {
resolver._register(item.id!, item);
}
await personService.createPerson(actor.id, resolver);
for (const item of page.orderedItems as ICreate[]) {
const note = await noteService.fetchNote(item.object);
assert.ok(note);
assert.strictEqual(note.text, 'test test foo');
assert.strictEqual(note.uri, (item.object as IObject).id);
}
});
test('Fetch only the first 100 items', async () => {
const actor = createRandomActor();
const outbox = createRandomNonPagedOutbox(actor, 200);
resolver._register(actor.id, actor);
resolver._register(actor.outbox as string, outbox);
// XXX: This shouldn't be needed as the collection already has the full information
// But somehow the resolver currently doesn't use it at all and always fetches again
for (const item of outbox.orderedItems as IObject[]) {
resolver._register(item.id!, item);
}
await personService.createPerson(actor.id, resolver);
const items = outbox.orderedItems as ICreate[];
assert.ok(await noteService.fetchNote(items[99].object));
assert.ok(!await noteService.fetchNote(items[100].object));
});
});
});