"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.userSubscriptionGetValidator = exports.userSubscriptionAddValidator = exports.userSubscriptionListValidator = exports.areSubscriptionsExistValidator = void 0;
const tslib_1 = require("tslib");
const express_validator_1 = require("express-validator");
const core_utils_1 = require("../../../shared/core-utils");
const actor_1 = require("../../helpers/custom-validators/activitypub/actor");
const constants_1 = require("../../initializers/constants");
const actor_follow_1 = require("../../models/actor/actor-follow");
const shared_1 = require("./shared");
const userSubscriptionListValidator = [
    (0, express_validator_1.query)('search')
        .optional()
        .not().isEmpty(),
    (req, res, next) => {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        return next();
    }
];
exports.userSubscriptionListValidator = userSubscriptionListValidator;
const userSubscriptionAddValidator = [
    (0, express_validator_1.body)('uri')
        .custom(actor_1.isValidActorHandle).withMessage('Should have a valid URI to follow (username@domain)'),
    (req, res, next) => {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        return next();
    }
];
exports.userSubscriptionAddValidator = userSubscriptionAddValidator;
const areSubscriptionsExistValidator = [
    (0, express_validator_1.query)('uris')
        .customSanitizer(core_utils_1.arrayify)
        .custom(actor_1.areValidActorHandles).withMessage('Should have a valid array of URIs'),
    (req, res, next) => {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        return next();
    }
];
exports.areSubscriptionsExistValidator = areSubscriptionsExistValidator;
const userSubscriptionGetValidator = [
    (0, express_validator_1.param)('uri')
        .custom(actor_1.isValidActorHandle),
    (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        let [name, host] = req.params.uri.split('@');
        if (host === constants_1.WEBSERVER.HOST)
            host = null;
        const user = res.locals.oauth.token.User;
        const subscription = yield actor_follow_1.ActorFollowModel.loadByActorAndTargetNameAndHostForAPI({
            actorId: user.Account.Actor.id,
            targetName: name,
            targetHost: host,
            state: 'accepted'
        });
        if (!(subscription === null || subscription === void 0 ? void 0 : subscription.ActorFollowing.VideoChannel)) {
            return res.fail({
                status: 404,
                message: `Subscription ${req.params.uri} not found.`
            });
        }
        res.locals.subscription = subscription;
        return next();
    })
];
exports.userSubscriptionGetValidator = userSubscriptionGetValidator;
//# sourceMappingURL=user-subscriptions.js.map