"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchRemoteActor = exports.fetchActorFollowsCount = void 0;
const tslib_1 = require("tslib");
const actor_1 = require("../../../../helpers/custom-validators/activitypub/actor");
const logger_1 = require("../../../../helpers/logger");
const requests_1 = require("../../../../helpers/requests");
const url_1 = require("../../url");
function fetchRemoteActor(actorUrl) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        logger_1.logger.info('Fetching remote actor %s.', actorUrl);
        const { body, statusCode } = yield (0, requests_1.doJSONRequest)(actorUrl, { activityPub: true });
        if ((0, actor_1.sanitizeAndCheckActorObject)(body) === false) {
            logger_1.logger.debug('Remote actor JSON is not valid.', { actorJSON: body });
            return { actorObject: undefined, statusCode };
        }
        if ((0, url_1.checkUrlsSameHost)(body.id, actorUrl) !== true) {
            logger_1.logger.warn('Actor url %s has not the same host than its AP id %s', actorUrl, body.id);
            return { actorObject: undefined, statusCode };
        }
        return {
            statusCode,
            actorObject: body
        };
    });
}
exports.fetchRemoteActor = fetchRemoteActor;
function fetchActorFollowsCount(actorObject) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        let followersCount = 0;
        let followingCount = 0;
        if (actorObject.followers)
            followersCount = yield fetchActorTotalItems(actorObject.followers);
        if (actorObject.following)
            followingCount = yield fetchActorTotalItems(actorObject.following);
        return { followersCount, followingCount };
    });
}
exports.fetchActorFollowsCount = fetchActorFollowsCount;
function fetchActorTotalItems(url) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        try {
            const { body } = yield (0, requests_1.doJSONRequest)(url, { activityPub: true });
            return body.totalItems || 0;
        }
        catch (err) {
            logger_1.logger.info('Cannot fetch remote actor count %s.', url, { err });
            return 0;
        }
    });
}
//# sourceMappingURL=url-to-object.js.map