"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkUserExist = exports.checkUserNameOrEmailDoesNotAlreadyExist = exports.checkUserEmailExist = exports.checkUserIdExist = void 0;
const tslib_1 = require("tslib");
const actor_1 = require("../../../models/actor/actor");
const user_1 = require("../../../models/user/user");
const core_utils_1 = require("../../../../shared/core-utils");
function checkUserIdExist(idArg, res, withStats = false) {
    const id = (0, core_utils_1.forceNumber)(idArg);
    return checkUserExist(() => user_1.UserModel.loadByIdWithChannels(id, withStats), res);
}
exports.checkUserIdExist = checkUserIdExist;
function checkUserEmailExist(email, res, abortResponse = true) {
    return checkUserExist(() => user_1.UserModel.loadByEmail(email), res, abortResponse);
}
exports.checkUserEmailExist = checkUserEmailExist;
function checkUserNameOrEmailDoesNotAlreadyExist(username, email, res) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const user = yield user_1.UserModel.loadByUsernameOrEmail(username, email);
        if (user) {
            res.fail({
                status: 409,
                message: 'User with this username or email already exists.'
            });
            return false;
        }
        const actor = yield actor_1.ActorModel.loadLocalByName(username);
        if (actor) {
            res.fail({
                status: 409,
                message: 'Another actor (account/channel) with this name on this instance already exists or has already existed.'
            });
            return false;
        }
        return true;
    });
}
exports.checkUserNameOrEmailDoesNotAlreadyExist = checkUserNameOrEmailDoesNotAlreadyExist;
function checkUserExist(finder, res, abortResponse = true) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const user = yield finder();
        if (!user) {
            if (abortResponse === true) {
                res.fail({
                    status: 404,
                    message: 'User not found'
                });
            }
            return false;
        }
        res.locals.user = user;
        return true;
    });
}
exports.checkUserExist = checkUserExist;
//# sourceMappingURL=users.js.map