mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
30 lines
1.1 KiB
TypeScript
30 lines
1.1 KiB
TypeScript
import type { ResourceSet } from '../../../storage/ResourceSet';
|
|
import { AS } from '../../../util/Vocabularies';
|
|
import type { Notification } from '../Notification';
|
|
import type { NotificationHandlerInput } from '../NotificationHandler';
|
|
import { NotificationGenerator } from './NotificationGenerator';
|
|
|
|
/**
|
|
* Determines the most relevant activity for a {@link Notification} in case none was provided.
|
|
* This is relevant for the `state` feature where a subscription needs to know the current state of a resource.
|
|
*/
|
|
export class StateNotificationGenerator extends NotificationGenerator {
|
|
private readonly source: NotificationGenerator;
|
|
private readonly resourceSet: ResourceSet;
|
|
|
|
public constructor(source: NotificationGenerator, resourceSet: ResourceSet) {
|
|
super();
|
|
this.source = source;
|
|
this.resourceSet = resourceSet;
|
|
}
|
|
|
|
public async handle(input: NotificationHandlerInput): Promise<Notification> {
|
|
if (input.activity) {
|
|
return this.source.handleSafe(input);
|
|
}
|
|
|
|
const activity = await this.resourceSet.hasResource(input.topic) ? AS.terms.Update : AS.terms.Delete;
|
|
return this.source.handleSafe({ ...input, activity });
|
|
}
|
|
}
|