feat: Support Add/Remove notifications on containers

This commit is contained in:
Joachim Van Herwegen
2023-02-07 13:06:19 +01:00
parent 9e1e65cdb9
commit 134237a80f
17 changed files with 326 additions and 83 deletions

View File

@@ -1,4 +1,5 @@
import { EventEmitter } from 'events';
import { RepresentationMetadata } from '../../../../src/http/representation/RepresentationMetadata';
import type { ResourceIdentifier } from '../../../../src/http/representation/ResourceIdentifier';
import type { Logger } from '../../../../src/logging/Logger';
import { getLoggerFor } from '../../../../src/logging/LogUtil';
@@ -21,6 +22,7 @@ describe('A ListeningActivityHandler', (): void => {
const logger: jest.Mocked<Logger> = getLoggerFor('mock') as any;
const topic: ResourceIdentifier = { path: 'http://example.com/foo' };
const activity = AS.terms.Update;
const metadata = new RepresentationMetadata();
let channel: NotificationChannel;
let storage: jest.Mocked<NotificationChannelStorage>;
let emitter: ActivityEmitter;
@@ -52,12 +54,12 @@ describe('A ListeningActivityHandler', (): void => {
});
it('calls the NotificationHandler if there is an event.', async(): Promise<void> => {
emitter.emit('changed', topic, activity);
emitter.emit('changed', topic, activity, metadata);
await flushPromises();
expect(notificationHandler.handleSafe).toHaveBeenCalledTimes(1);
expect(notificationHandler.handleSafe).toHaveBeenLastCalledWith({ channel, activity, topic });
expect(notificationHandler.handleSafe).toHaveBeenLastCalledWith({ channel, activity, topic, metadata });
expect(logger.error).toHaveBeenCalledTimes(0);
expect(storage.update).toHaveBeenCalledTimes(0);
});
@@ -65,12 +67,12 @@ describe('A ListeningActivityHandler', (): void => {
it('updates the lastEmit value of the channel if it has a rate limit.', async(): Promise<void> => {
jest.useFakeTimers();
channel.rate = 10 * 1000;
emitter.emit('changed', topic, activity);
emitter.emit('changed', topic, activity, metadata);
await flushPromises();
expect(notificationHandler.handleSafe).toHaveBeenCalledTimes(1);
expect(notificationHandler.handleSafe).toHaveBeenLastCalledWith({ channel, activity, topic });
expect(notificationHandler.handleSafe).toHaveBeenLastCalledWith({ channel, activity, topic, metadata });
expect(logger.error).toHaveBeenCalledTimes(0);
expect(storage.update).toHaveBeenCalledTimes(1);
expect(storage.update).toHaveBeenLastCalledWith({
@@ -84,7 +86,7 @@ describe('A ListeningActivityHandler', (): void => {
channel.rate = 100000;
channel.lastEmit = Date.now();
emitter.emit('changed', topic, activity);
emitter.emit('changed', topic, activity, metadata);
await flushPromises();
@@ -95,7 +97,7 @@ describe('A ListeningActivityHandler', (): void => {
it('does not emit an event on channels if their start time has not been reached.', async(): Promise<void> => {
channel.startAt = Date.now() + 100000;
emitter.emit('changed', topic, activity);
emitter.emit('changed', topic, activity, metadata);
await flushPromises();
@@ -107,7 +109,7 @@ describe('A ListeningActivityHandler', (): void => {
storage.getAll.mockResolvedValue([ channel.id, channel.id ]);
notificationHandler.handleSafe.mockRejectedValueOnce(new Error('bad input'));
emitter.emit('changed', topic, activity);
emitter.emit('changed', topic, activity, metadata);
await flushPromises();
@@ -119,7 +121,7 @@ describe('A ListeningActivityHandler', (): void => {
it('logs an error if something goes wrong handling the event.', async(): Promise<void> => {
storage.getAll.mockRejectedValue(new Error('bad event'));
emitter.emit('changed', topic, activity);
emitter.emit('changed', topic, activity, metadata);
await flushPromises();
expect(notificationHandler.handleSafe).toHaveBeenCalledTimes(0);
@@ -130,7 +132,7 @@ describe('A ListeningActivityHandler', (): void => {
it('ignores undefined channels.', async(): Promise<void> => {
storage.get.mockResolvedValue(undefined);
emitter.emit('changed', topic, activity);
emitter.emit('changed', topic, activity, metadata);
await flushPromises();

View File

@@ -0,0 +1,81 @@
import { BasicRepresentation } from '../../../../../src/http/representation/BasicRepresentation';
import { RepresentationMetadata } from '../../../../../src/http/representation/RepresentationMetadata';
import type { ResourceIdentifier } from '../../../../../src/http/representation/ResourceIdentifier';
import {
AddRemoveNotificationGenerator,
} from '../../../../../src/server/notifications/generate/AddRemoveNotificationGenerator';
import type { NotificationChannel } from '../../../../../src/server/notifications/NotificationChannel';
import type { ResourceStore } from '../../../../../src/storage/ResourceStore';
import { AS, DC, LDP, RDF } from '../../../../../src/util/Vocabularies';
describe('An AddRemoveNotificationGenerator', (): void => {
const topic: ResourceIdentifier = { path: 'http://example.com/' };
const object: ResourceIdentifier = { path: 'http://example.com/foo' };
const channel: NotificationChannel = {
id: 'id',
topic: topic.path,
type: 'type',
};
let metadata: RepresentationMetadata;
let store: jest.Mocked<ResourceStore>;
let generator: AddRemoveNotificationGenerator;
beforeEach(async(): Promise<void> => {
metadata = new RepresentationMetadata(topic, { [AS.object]: object.path });
const responseMetadata = new RepresentationMetadata({
[RDF.type]: LDP.terms.Resource,
// Needed for ETag
[DC.modified]: new Date().toISOString(),
});
store = {
getRepresentation: jest.fn().mockResolvedValue(new BasicRepresentation('', responseMetadata)),
} as any;
generator = new AddRemoveNotificationGenerator(store);
});
it('only handles Add/Remove activities.', async(): Promise<void> => {
await expect(generator.canHandle({ topic, channel, metadata }))
.rejects.toThrow('Only Add/Remove activity updates are supported.');
await expect(generator.canHandle({ topic, channel, metadata, activity: AS.terms.Add })).resolves.toBeUndefined();
await expect(generator.canHandle({ topic, channel, metadata, activity: AS.terms.Remove })).resolves.toBeUndefined();
});
it('requires one object metadata to be present.', async(): Promise<void> => {
metadata = new RepresentationMetadata();
await expect(generator.handle({ topic, channel, activity: AS.terms.Add })).rejects.toThrow(
'Missing as:object metadata for https://www.w3.org/ns/activitystreams#Add activity on http://example.com/',
);
await expect(generator.handle({ topic, channel, metadata, activity: AS.terms.Add })).rejects.toThrow(
'Missing as:object metadata for https://www.w3.org/ns/activitystreams#Add activity on http://example.com/',
);
metadata = new RepresentationMetadata(topic, { [AS.object]: [ object.path, 'http://example.com/otherObject' ]});
await expect(generator.handle({ topic, channel, metadata, activity: AS.terms.Add })).rejects.toThrow(
'Found more than one as:object for https://www.w3.org/ns/activitystreams#Add activity on http://example.com/',
);
});
it('generates a notification.', async(): Promise<void> => {
const date = '1988-03-09T14:48:00.000Z';
const ms = Date.parse(date);
jest.useFakeTimers();
jest.setSystemTime(ms);
await expect(generator.handle({ topic, channel, metadata, activity: AS.terms.Add })).resolves.toEqual({
'@context': [
'https://www.w3.org/ns/activitystreams',
'https://www.w3.org/ns/solid/notification/v1',
],
id: `urn:${ms}:http://example.com/`,
type: 'Add',
object: 'http://example.com/foo',
target: 'http://example.com/',
state: expect.stringMatching(/"\d+"/u),
published: date,
});
jest.useRealTimers();
});
});