"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.listFollowsValidator = exports.rejectFollowerValidator = exports.acceptFollowerValidator = exports.getFollowerValidator = exports.removeFollowingValidator = exports.followValidator = void 0;
const tslib_1 = require("tslib");
const express_validator_1 = require("express-validator");
const core_utils_1 = require("../../helpers/core-utils");
const follows_1 = require("../../helpers/custom-validators/follows");
const actors_1 = require("../../lib/activitypub/actors");
const follow_1 = require("../../lib/activitypub/follow");
const application_1 = require("../../models/application/application");
const actor_1 = require("../../helpers/custom-validators/activitypub/actor");
const servers_1 = require("../../helpers/custom-validators/servers");
const logger_1 = require("../../helpers/logger");
const constants_1 = require("../../initializers/constants");
const actor_2 = require("../../models/actor/actor");
const actor_follow_1 = require("../../models/actor/actor-follow");
const shared_1 = require("./shared");
const listFollowsValidator = [
    (0, express_validator_1.query)('state')
        .optional()
        .custom(follows_1.isFollowStateValid),
    (0, express_validator_1.query)('actorType')
        .optional()
        .custom(actor_1.isActorTypeValid),
    (req, res, next) => {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        return next();
    }
];
exports.listFollowsValidator = listFollowsValidator;
const followValidator = [
    (0, express_validator_1.body)('hosts')
        .toArray()
        .custom(servers_1.isEachUniqueHostValid).withMessage('Should have an array of unique hosts'),
    (0, express_validator_1.body)('handles')
        .toArray()
        .custom(follows_1.isEachUniqueHandleValid).withMessage('Should have an array of handles'),
    (req, res, next) => {
        if ((0, core_utils_1.isProdInstance)() && constants_1.WEBSERVER.SCHEME === 'http') {
            return res
                .status(500)
                .json({
                error: 'Cannot follow on a non HTTPS web server.'
            });
        }
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        const body = req.body;
        if (body.hosts.length === 0 && body.handles.length === 0) {
            return res
                .status(400)
                .json({
                error: 'You must provide at least one handle or one host.'
            });
        }
        return next();
    }
];
exports.followValidator = followValidator;
const removeFollowingValidator = [
    (0, express_validator_1.param)('hostOrHandle')
        .custom(value => (0, servers_1.isHostValid)(value) || (0, follows_1.isRemoteHandleValid)(value)),
    (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        const serverActor = yield (0, application_1.getServerActor)();
        const { name, host } = (0, follow_1.getRemoteNameAndHost)(req.params.hostOrHandle);
        const follow = yield actor_follow_1.ActorFollowModel.loadByActorAndTargetNameAndHostForAPI({
            actorId: serverActor.id,
            targetName: name,
            targetHost: host
        });
        if (!follow) {
            return res.fail({
                status: 404,
                message: `Follow ${req.params.hostOrHandle} not found.`
            });
        }
        res.locals.follow = follow;
        return next();
    })
];
exports.removeFollowingValidator = removeFollowingValidator;
const getFollowerValidator = [
    (0, express_validator_1.param)('nameWithHost')
        .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 follow;
        try {
            const actorUrl = yield (0, actors_1.loadActorUrlOrGetFromWebfinger)(req.params.nameWithHost);
            const actor = yield actor_2.ActorModel.loadByUrl(actorUrl);
            const serverActor = yield (0, application_1.getServerActor)();
            follow = yield actor_follow_1.ActorFollowModel.loadByActorAndTarget(actor.id, serverActor.id);
        }
        catch (err) {
            logger_1.logger.warn('Cannot get actor from handle.', { handle: req.params.nameWithHost, err });
        }
        if (!follow) {
            return res.fail({
                status: 404,
                message: `Follower ${req.params.nameWithHost} not found.`
            });
        }
        res.locals.follow = follow;
        return next();
    })
];
exports.getFollowerValidator = getFollowerValidator;
const acceptFollowerValidator = [
    (req, res, next) => {
        const follow = res.locals.follow;
        if (follow.state !== 'pending' && follow.state !== 'rejected') {
            return res.fail({ message: 'Follow is not in pending/rejected state.' });
        }
        return next();
    }
];
exports.acceptFollowerValidator = acceptFollowerValidator;
const rejectFollowerValidator = [
    (req, res, next) => {
        const follow = res.locals.follow;
        if (follow.state !== 'pending' && follow.state !== 'accepted') {
            return res.fail({ message: 'Follow is not in pending/accepted state.' });
        }
        return next();
    }
];
exports.rejectFollowerValidator = rejectFollowerValidator;
//# sourceMappingURL=follows.js.map