"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebHookUnsubscriber = void 0;
const ResetResponseDescription_1 = require("../../../http/output/response/ResetResponseDescription");
const LogUtil_1 = require("../../../logging/LogUtil");
const ForbiddenHttpError_1 = require("../../../util/errors/ForbiddenHttpError");
const NotFoundHttpError_1 = require("../../../util/errors/NotFoundHttpError");
const OperationHttpHandler_1 = require("../../OperationHttpHandler");
const WebHookSubscription2021_1 = require("./WebHookSubscription2021");
/**
 * Allows clients to unsubscribe from a WebHookSubscription2021.
 * Should be wrapped in a route handler that only allows `DELETE` operations.
 */
class WebHookUnsubscriber extends OperationHttpHandler_1.OperationHttpHandler {
    constructor(credentialsExtractor, storage) {
        super();
        this.logger = (0, LogUtil_1.getLoggerFor)(this);
        this.credentialsExtractor = credentialsExtractor;
        this.storage = storage;
    }
    async handle({ operation, request }) {
        const id = operation.target.path;
        const channel = await this.storage.get(id);
        if (!channel || !(0, WebHookSubscription2021_1.isWebHook2021Channel)(channel)) {
            throw new NotFoundHttpError_1.NotFoundHttpError();
        }
        const credentials = await this.credentialsExtractor.handleSafe(request);
        if (channel.webId !== credentials.agent?.webId) {
            throw new ForbiddenHttpError_1.ForbiddenHttpError();
        }
        this.logger.debug(`Deleting WebHook notification channel ${id}`);
        await this.storage.delete(id);
        return new ResetResponseDescription_1.ResetResponseDescription();
    }
}
exports.WebHookUnsubscriber = WebHookUnsubscriber;
//# sourceMappingURL=WebHookUnsubscriber.js.map