Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Replace WebHookSubscription2021 with WebHookChannel2023
- Loading branch information
Showing
19 changed files
with
188 additions
and
270 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/server/notifications/WebHookChannel2023/WebhookChannel2023Type.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import type { Store } from 'n3'; | ||
import type { InteractionRoute } from '../../../identity/interaction/routing/InteractionRoute'; | ||
import { getLoggerFor } from '../../../logging/LogUtil'; | ||
import { createErrorMessage } from '../../../util/errors/ErrorUtil'; | ||
import { NOTIFY } from '../../../util/Vocabularies'; | ||
import { BaseChannelType } from '../BaseChannelType'; | ||
import type { NotificationChannel } from '../NotificationChannel'; | ||
import type { StateHandler } from '../StateHandler'; | ||
|
||
/** | ||
* A {@link NotificationChannel} containing the necessary fields for a WebHookChannel2023 channel. | ||
*/ | ||
export interface WebhookChannel2023 extends NotificationChannel { | ||
/** | ||
* The "WebHookChannel2023" type. | ||
*/ | ||
type: typeof NOTIFY.WebHookChannel2023; | ||
/** | ||
* Where the notifications have to be sent. | ||
*/ | ||
sendTo: string; | ||
} | ||
|
||
export function isWebHook2023Channel(channel: NotificationChannel): channel is WebhookChannel2023 { | ||
return channel.type === NOTIFY.WebHookChannel2023; | ||
} | ||
|
||
/** | ||
* The notification channel type WebHookChannel2023 as described in | ||
* https://solid.github.io/notifications/webhook-channel-2023 | ||
* | ||
* Requires read permissions on a resource to be able to receive notifications. | ||
* | ||
* Also handles the `state` feature if present. | ||
*/ | ||
export class WebhookChannel2023Type extends BaseChannelType { | ||
protected readonly logger = getLoggerFor(this); | ||
|
||
private readonly stateHandler: StateHandler; | ||
private readonly webId: string; | ||
|
||
/** | ||
* @param route - The route corresponding to the URL of the subscription service of this channel type. | ||
* @param webIdRoute - The route to the WebID that needs to be used when generating DPoP tokens for notifications. | ||
* @param stateHandler - The {@link StateHandler} that will be called after a successful subscription. | ||
* @param features - The features that need to be enabled for this channel type. | ||
*/ | ||
public constructor(route: InteractionRoute, webIdRoute: InteractionRoute, stateHandler: StateHandler, | ||
features?: string[]) { | ||
super(NOTIFY.terms.WebHookChannel2023, | ||
route, | ||
features, | ||
[{ path: NOTIFY.sendTo, minCount: 1, maxCount: 1 }]); | ||
this.stateHandler = stateHandler; | ||
this.webId = webIdRoute.getPath(); | ||
} | ||
|
||
public async initChannel(data: Store): Promise<WebhookChannel2023> { | ||
const subject = await this.validateSubscription(data); | ||
const channel = await this.quadsToChannel(data, subject); | ||
const sendTo = data.getObjects(subject, NOTIFY.terms.sendTo, null)[0]; | ||
|
||
return { | ||
...channel, | ||
type: NOTIFY.WebHookChannel2023, | ||
sendTo: sendTo.value, | ||
}; | ||
} | ||
|
||
public async toJsonLd(channel: NotificationChannel): Promise<Record<string, unknown>> { | ||
const json = await super.toJsonLd(channel); | ||
|
||
// Add the stored WebID as sender. | ||
// We don't store it in the channel object itself as we always know what it is anyway. | ||
json.sender = this.webId; | ||
|
||
return json; | ||
} | ||
|
||
public async completeChannel(channel: NotificationChannel): Promise<void> { | ||
try { | ||
// Send the state notification, if there is one | ||
await this.stateHandler.handleSafe({ channel }); | ||
} catch (error: unknown) { | ||
this.logger.error(`Error emitting state notification: ${createErrorMessage(error)}`); | ||
} | ||
} | ||
} |
Oops, something went wrong.