mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Use notification v0.2 features in discovery
This commit is contained in:
@@ -2,52 +2,48 @@ import type { NamedNode, Quad } from '@rdfjs/types';
|
||||
import { DataFactory } from 'n3';
|
||||
import type { ResourceIdentifier } from '../../http/representation/ResourceIdentifier';
|
||||
import type { InteractionRoute } from '../../identity/interaction/routing/InteractionRoute';
|
||||
import { NOTIFY, RDF } from '../../util/Vocabularies';
|
||||
import { NOTIFY } from '../../util/Vocabularies';
|
||||
import { StorageDescriber } from '../description/StorageDescriber';
|
||||
const { namedNode, quad } = DataFactory;
|
||||
|
||||
export const DEFAULT_NOTIFICATION_FEATURES = [
|
||||
NOTIFY.accept,
|
||||
NOTIFY.expiration,
|
||||
NOTIFY.endAt,
|
||||
NOTIFY.rate,
|
||||
NOTIFY.startAt,
|
||||
NOTIFY.state,
|
||||
];
|
||||
|
||||
/**
|
||||
* Outputs quads describing how to access a specific Notification Subscription type and its features,
|
||||
* as described in https://solidproject.org/TR/notifications-protocol#discovery.
|
||||
* Outputs quads describing a Notification Subscription Service,
|
||||
* as described in https://solidproject.org/TR/2022/notifications-protocol-20221231#discovery and
|
||||
* https://solidproject.org/TR/2022/notifications-protocol-20221231#description-resource-data-model.
|
||||
*/
|
||||
export class NotificationDescriber extends StorageDescriber {
|
||||
private readonly path: NamedNode;
|
||||
private readonly relative: string;
|
||||
private readonly type: NamedNode;
|
||||
private readonly features: NamedNode[];
|
||||
|
||||
/**
|
||||
* @param route - The route describing where the subscription target is.
|
||||
* @param relative - Will be appended to the input path to generate a named node corresponding to the description.
|
||||
* E.g., "#websocketNotification".
|
||||
* @param type - The rdf:type of the subscription type.
|
||||
* @param features - Which features are enabled for this subscription type. Defaults to accept/expiration/rate/state.
|
||||
*/
|
||||
public constructor(route: InteractionRoute, relative: string, type: string,
|
||||
public constructor(route: InteractionRoute, type: string,
|
||||
features: string[] = DEFAULT_NOTIFICATION_FEATURES) {
|
||||
super();
|
||||
this.path = namedNode(route.getPath());
|
||||
this.relative = relative;
|
||||
this.type = namedNode(type);
|
||||
this.features = features.map(namedNode);
|
||||
}
|
||||
|
||||
public async handle(input: ResourceIdentifier): Promise<Quad[]> {
|
||||
const subject = namedNode(input.path);
|
||||
const subscription = namedNode(`${input.path}${this.relative}`);
|
||||
|
||||
return [
|
||||
quad(subject, NOTIFY.terms.notificationChannel, subscription),
|
||||
quad(subscription, RDF.terms.type, this.type),
|
||||
quad(subscription, NOTIFY.terms.subscription, this.path),
|
||||
...this.features.map((feature): Quad => quad(subscription, NOTIFY.terms.feature, feature)),
|
||||
quad(subject, NOTIFY.terms.subscription, this.path),
|
||||
quad(this.path, NOTIFY.terms.channelType, this.type),
|
||||
...this.features.map((feature): Quad => quad(this.path, NOTIFY.terms.feature, feature)),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,19 +3,18 @@ import { DataFactory } from 'n3';
|
||||
import type { Quad } from 'rdf-js';
|
||||
import type { ResourceIdentifier } from '../../../http/representation/ResourceIdentifier';
|
||||
import type { InteractionRoute } from '../../../identity/interaction/routing/InteractionRoute';
|
||||
import { NOTIFY, RDF } from '../../../util/Vocabularies';
|
||||
import { NOTIFY } from '../../../util/Vocabularies';
|
||||
import { DEFAULT_NOTIFICATION_FEATURES, NotificationDescriber } from '../NotificationDescriber';
|
||||
const { namedNode, quad } = DataFactory;
|
||||
|
||||
export interface WebHookStorageDescriberArgs {
|
||||
route: InteractionRoute;
|
||||
relative: string;
|
||||
webIdRoute: InteractionRoute;
|
||||
features?: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the necessary triples for describing a WebHookSubcription2021 notification channel.
|
||||
* Handles the necessary triples for describing a WebHookSubscription2021 subscription service.
|
||||
*
|
||||
* Extends {@link NotificationDescriber} by adding the necessary `notify:webid` and `notify:webhookAuth` triples.
|
||||
*/
|
||||
@@ -25,7 +24,7 @@ export class WebHookDescriber extends NotificationDescriber {
|
||||
public constructor(args: WebHookStorageDescriberArgs) {
|
||||
const features = args.features ?? [ ...DEFAULT_NOTIFICATION_FEATURES ];
|
||||
features.push(NOTIFY.webhookAuth);
|
||||
super(args.route, args.relative, NOTIFY.WebHookSubscription2021, features);
|
||||
super(args.route, NOTIFY.WebHookSubscription2021, features);
|
||||
|
||||
this.webId = namedNode(args.webIdRoute.getPath());
|
||||
}
|
||||
@@ -34,7 +33,7 @@ export class WebHookDescriber extends NotificationDescriber {
|
||||
const quads = await super.handle(input);
|
||||
|
||||
// Find the notification channel subject
|
||||
const typeQuad = quads.find((entry): boolean => entry.predicate.equals(RDF.terms.type) &&
|
||||
const typeQuad = quads.find((entry): boolean => entry.predicate.equals(NOTIFY.terms.channelType) &&
|
||||
entry.object.equals(NOTIFY.terms.WebHookSubscription2021));
|
||||
quads.push(quad(typeQuad!.subject, NOTIFY.terms.webid, this.webId));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user