"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.blocklistStatusValidator = exports.unblockServerByServerValidator = exports.unblockAccountByServerValidator = exports.unblockServerByAccountValidator = exports.unblockAccountByAccountValidator = exports.blockAccountValidator = exports.blockServerValidator = void 0;
const tslib_1 = require("tslib");
const express_validator_1 = require("express-validator");
const actor_1 = require("../../helpers/custom-validators/activitypub/actor");
const misc_1 = require("../../helpers/custom-validators/misc");
const application_1 = require("../../models/application/application");
const servers_1 = require("../../helpers/custom-validators/servers");
const logger_1 = require("../../helpers/logger");
const constants_1 = require("../../initializers/constants");
const account_blocklist_1 = require("../../models/account/account-blocklist");
const server_1 = require("../../models/server/server");
const server_blocklist_1 = require("../../models/server/server-blocklist");
const shared_1 = require("./shared");
const blockAccountValidator = [
    (0, express_validator_1.body)('accountName').exists().withMessage('Should have an account name with host'),
    (req, res, next) => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
        logger_1.logger.debug('Checking blockAccountByAccountValidator parameters', { parameters: req.body });
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        if (!(yield (0, shared_1.doesAccountNameWithHostExist)(req.body.accountName, res)))
            return;
        const user = res.locals.oauth.token.User;
        const accountToBlock = res.locals.account;
        if (user.Account.id === accountToBlock.id) {
            res.fail({
                status: 409,
                message: 'You cannot block yourself.'
            });
            return;
        }
        return next();
    })
];
exports.blockAccountValidator = blockAccountValidator;
const unblockAccountByAccountValidator = [
    (0, express_validator_1.param)('accountName').exists().withMessage('Should have an account name with host'),
    (req, res, next) => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
        logger_1.logger.debug('Checking unblockAccountByAccountValidator parameters', { parameters: req.params });
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        if (!(yield (0, shared_1.doesAccountNameWithHostExist)(req.params.accountName, res)))
            return;
        const user = res.locals.oauth.token.User;
        const targetAccount = res.locals.account;
        if (!(yield doesUnblockAccountExist(user.Account.id, targetAccount.id, res)))
            return;
        return next();
    })
];
exports.unblockAccountByAccountValidator = unblockAccountByAccountValidator;
const unblockAccountByServerValidator = [
    (0, express_validator_1.param)('accountName').exists().withMessage('Should have an account name with host'),
    (req, res, next) => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
        logger_1.logger.debug('Checking unblockAccountByServerValidator parameters', { parameters: req.params });
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        if (!(yield (0, shared_1.doesAccountNameWithHostExist)(req.params.accountName, res)))
            return;
        const serverActor = yield (0, application_1.getServerActor)();
        const targetAccount = res.locals.account;
        if (!(yield doesUnblockAccountExist(serverActor.Account.id, targetAccount.id, res)))
            return;
        return next();
    })
];
exports.unblockAccountByServerValidator = unblockAccountByServerValidator;
const blockServerValidator = [
    (0, express_validator_1.body)('host').custom(servers_1.isHostValid).withMessage('Should have a valid host'),
    (req, res, next) => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
        logger_1.logger.debug('Checking serverGetValidator parameters', { parameters: req.body });
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        const host = req.body.host;
        if (host === constants_1.WEBSERVER.HOST) {
            return res.fail({
                status: 409,
                message: 'You cannot block your own server.'
            });
        }
        const server = yield server_1.ServerModel.loadOrCreateByHost(host);
        res.locals.server = server;
        return next();
    })
];
exports.blockServerValidator = blockServerValidator;
const unblockServerByAccountValidator = [
    (0, express_validator_1.param)('host').custom(servers_1.isHostValid).withMessage('Should have an account name with host'),
    (req, res, next) => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
        logger_1.logger.debug('Checking unblockServerByAccountValidator parameters', { parameters: req.params });
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        const user = res.locals.oauth.token.User;
        if (!(yield doesUnblockServerExist(user.Account.id, req.params.host, res)))
            return;
        return next();
    })
];
exports.unblockServerByAccountValidator = unblockServerByAccountValidator;
const unblockServerByServerValidator = [
    (0, express_validator_1.param)('host').custom(servers_1.isHostValid).withMessage('Should have an account name with host'),
    (req, res, next) => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
        logger_1.logger.debug('Checking unblockServerByServerValidator parameters', { parameters: req.params });
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        const serverActor = yield (0, application_1.getServerActor)();
        if (!(yield doesUnblockServerExist(serverActor.Account.id, req.params.host, res)))
            return;
        return next();
    })
];
exports.unblockServerByServerValidator = unblockServerByServerValidator;
const blocklistStatusValidator = [
    (0, express_validator_1.query)('hosts')
        .optional()
        .customSanitizer(misc_1.toArray)
        .custom(servers_1.isEachUniqueHostValid).withMessage('Should have a valid hosts array'),
    (0, express_validator_1.query)('accounts')
        .optional()
        .customSanitizer(misc_1.toArray)
        .custom(actor_1.areValidActorHandles).withMessage('Should have a valid accounts array'),
    (req, res, next) => {
        logger_1.logger.debug('Checking blocklistStatusValidator parameters', { query: req.query });
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        return next();
    }
];
exports.blocklistStatusValidator = blocklistStatusValidator;
function doesUnblockAccountExist(accountId, targetAccountId, res) {
    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
        const accountBlock = yield account_blocklist_1.AccountBlocklistModel.loadByAccountAndTarget(accountId, targetAccountId);
        if (!accountBlock) {
            res.fail({
                status: 404,
                message: 'Account block entry not found.'
            });
            return false;
        }
        res.locals.accountBlock = accountBlock;
        return true;
    });
}
function doesUnblockServerExist(accountId, host, res) {
    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
        const serverBlock = yield server_blocklist_1.ServerBlocklistModel.loadByAccountAndHost(accountId, host);
        if (!serverBlock) {
            res.fail({
                status: 404,
                message: 'Server block entry not found.'
            });
            return false;
        }
        res.locals.serverBlock = serverBlock;
        return true;
    });
}
//# sourceMappingURL=blocklist.js.map