feat: Use ETagHandler for ETag generation and comparison

This commit is contained in:
Joachim Van Herwegen
2023-07-27 09:38:46 +02:00
parent 5ec6eddbfa
commit afcbfdaacf
31 changed files with 367 additions and 234 deletions

View File

@@ -1,4 +1,4 @@
import { sameResourceState } from '../../storage/conditions/Conditions';
import type { ETagHandler } from '../../storage/conditions/ETagHandler';
import type { NotificationGenerator } from './generate/NotificationGenerator';
import type { NotificationEmitter } from './NotificationEmitter';
import type { NotificationHandlerInput } from './NotificationHandler';
@@ -9,6 +9,7 @@ export interface ComposedNotificationHandlerArgs {
generator: NotificationGenerator;
serializer: NotificationSerializer;
emitter: NotificationEmitter;
eTagHandler: ETagHandler;
}
/**
@@ -21,12 +22,14 @@ export class ComposedNotificationHandler extends NotificationHandler {
private readonly generator: NotificationGenerator;
private readonly serializer: NotificationSerializer;
private readonly emitter: NotificationEmitter;
private readonly eTagHandler: ETagHandler;
public constructor(args: ComposedNotificationHandlerArgs) {
super();
this.generator = args.generator;
this.serializer = args.serializer;
this.emitter = args.emitter;
this.eTagHandler = args.eTagHandler;
}
public async canHandle(input: NotificationHandlerInput): Promise<void> {
@@ -38,7 +41,8 @@ export class ComposedNotificationHandler extends NotificationHandler {
const { state } = input.channel;
// In case the state matches there is no need to send the notification
if (typeof state === 'string' && notification.state && sameResourceState(state, notification.state)) {
if (typeof state === 'string' && notification.state &&
this.eTagHandler.sameResourceState(state, notification.state)) {
return;
}

View File

@@ -1,4 +1,4 @@
import { getETag } from '../../../storage/conditions/Conditions';
import type { ETagHandler } from '../../../storage/conditions/ETagHandler';
import type { ResourceStore } from '../../../storage/ResourceStore';
import { NotImplementedHttpError } from '../../../util/errors/NotImplementedHttpError';
import { AS } from '../../../util/Vocabularies';
@@ -13,10 +13,12 @@ import { NotificationGenerator } from './NotificationGenerator';
*/
export class ActivityNotificationGenerator extends NotificationGenerator {
private readonly store: ResourceStore;
private readonly eTagHandler: ETagHandler;
public constructor(store: ResourceStore) {
public constructor(store: ResourceStore, eTagHandler: ETagHandler) {
super();
this.store = store;
this.eTagHandler = eTagHandler;
}
public async canHandle({ activity }: NotificationHandlerInput): Promise<void> {
@@ -28,8 +30,7 @@ export class ActivityNotificationGenerator extends NotificationGenerator {
public async handle({ topic, activity }: NotificationHandlerInput): Promise<Notification> {
const representation = await this.store.getRepresentation(topic, {});
representation.data.destroy();
const state = getETag(representation.metadata);
const state = this.eTagHandler.getETag(representation.metadata);
return {
'@context': [

View File

@@ -1,4 +1,4 @@
import { getETag } from '../../../storage/conditions/Conditions';
import type { ETagHandler } from '../../../storage/conditions/ETagHandler';
import type { ResourceStore } from '../../../storage/ResourceStore';
import { InternalServerError } from '../../../util/errors/InternalServerError';
import { NotImplementedHttpError } from '../../../util/errors/NotImplementedHttpError';
@@ -15,10 +15,12 @@ import { NotificationGenerator } from './NotificationGenerator';
*/
export class AddRemoveNotificationGenerator extends NotificationGenerator {
private readonly store: ResourceStore;
private readonly eTagHandler: ETagHandler;
public constructor(store: ResourceStore) {
public constructor(store: ResourceStore, eTagHandler: ETagHandler) {
super();
this.store = store;
this.eTagHandler = eTagHandler;
}
public async canHandle({ activity }: NotificationHandlerInput): Promise<void> {
@@ -30,8 +32,8 @@ export class AddRemoveNotificationGenerator extends NotificationGenerator {
public async handle({ activity, topic, metadata }: NotificationHandlerInput): Promise<Notification> {
const representation = await this.store.getRepresentation(topic, {});
representation.data.destroy();
const state = this.eTagHandler.getETag(representation.metadata);
const state = getETag(representation.metadata);
const objects = metadata?.getAll(AS.terms.object);
if (!objects || objects.length === 0) {
throw new InternalServerError(`Missing as:object metadata for ${activity?.value} activity on ${topic.path}`);