"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.blocklistRouter = void 0;
const tslib_1 = require("tslib");
const express_1 = (0, tslib_1.__importDefault)(require("express"));
const actors_1 = require("../../helpers/actors");
const account_blocklist_1 = require("../../models/account/account-blocklist");
const application_1 = require("../../models/application/application");
const server_blocklist_1 = require("../../models/server/server-blocklist");
const middlewares_1 = require("../../middlewares");
const logger_1 = require("../../helpers/logger");
const blocklistRouter = express_1.default.Router();
exports.blocklistRouter = blocklistRouter;
blocklistRouter.get('/status', middlewares_1.optionalAuthenticate, middlewares_1.blocklistStatusValidator, (0, middlewares_1.asyncMiddleware)(getBlocklistStatus));
function getBlocklistStatus(req, res) {
    var _a;
    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
        const hosts = req.query.hosts;
        const accounts = req.query.accounts;
        const user = (_a = res.locals.oauth) === null || _a === void 0 ? void 0 : _a.token.User;
        const serverActor = yield (0, application_1.getServerActor)();
        const byAccountIds = [serverActor.Account.id];
        if (user)
            byAccountIds.push(user.Account.id);
        const status = {
            accounts: {},
            hosts: {}
        };
        const baseOptions = {
            byAccountIds,
            user,
            serverActor,
            status
        };
        yield Promise.all([
            populateServerBlocklistStatus(Object.assign(Object.assign({}, baseOptions), { hosts })),
            populateAccountBlocklistStatus(Object.assign(Object.assign({}, baseOptions), { accounts }))
        ]);
        return res.json(status);
    });
}
function populateServerBlocklistStatus(options) {
    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
        const { byAccountIds, user, serverActor, hosts, status } = options;
        if (!hosts || hosts.length === 0)
            return;
        const serverBlocklistStatus = yield server_blocklist_1.ServerBlocklistModel.getBlockStatus(byAccountIds, hosts);
        logger_1.logger.debug('Got server blocklist status.', { serverBlocklistStatus, byAccountIds, hosts });
        for (const host of hosts) {
            const block = serverBlocklistStatus.find(b => b.host === host);
            status.hosts[host] = getStatus(block, serverActor, user);
        }
    });
}
function populateAccountBlocklistStatus(options) {
    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
        const { byAccountIds, user, serverActor, accounts, status } = options;
        if (!accounts || accounts.length === 0)
            return;
        const accountBlocklistStatus = yield account_blocklist_1.AccountBlocklistModel.getBlockStatus(byAccountIds, accounts);
        logger_1.logger.debug('Got account blocklist status.', { accountBlocklistStatus, byAccountIds, accounts });
        for (const account of accounts) {
            const sanitizedHandle = (0, actors_1.handleToNameAndHost)(account);
            const block = accountBlocklistStatus.find(b => b.name === sanitizedHandle.name && b.host === sanitizedHandle.host);
            status.accounts[sanitizedHandle.handle] = getStatus(block, serverActor, user);
        }
    });
}
function getStatus(block, serverActor, user) {
    return {
        blockedByServer: !!(block && block.accountId === serverActor.Account.id),
        blockedByUser: !!(block && user && block.accountId === user.Account.id)
    };
}
//# sourceMappingURL=blocklist.js.map