"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.botsRouter = void 0;
const tslib_1 = require("tslib");
const application_1 = require("../models/application/application");
const express_1 = (0, tslib_1.__importDefault)(require("express"));
const lodash_1 = require("lodash");
const sitemap_1 = require("sitemap");
const express_utils_1 = require("../helpers/express-utils");
const constants_1 = require("../initializers/constants");
const middlewares_1 = require("../middlewares");
const cache_1 = require("../middlewares/cache/cache");
const account_1 = require("../models/account/account");
const video_1 = require("../models/video/video");
const video_channel_1 = require("../models/video/video-channel");
const botsRouter = express_1.default.Router();
exports.botsRouter = botsRouter;
botsRouter.use('/sitemap.xml', (0, cache_1.cacheRoute)(constants_1.ROUTE_CACHE_LIFETIME.SITEMAP), (0, middlewares_1.asyncMiddleware)(getSitemap));
function getSitemap(req, res) {
    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
        let urls = getSitemapBasicUrls();
        urls = urls.concat(yield getSitemapLocalVideoUrls());
        urls = urls.concat(yield getSitemapVideoChannelUrls());
        urls = urls.concat(yield getSitemapAccountUrls());
        const sitemapStream = new sitemap_1.SitemapStream({ hostname: constants_1.WEBSERVER.URL });
        for (const urlObj of urls) {
            sitemapStream.write(urlObj);
        }
        sitemapStream.end();
        const xml = yield (0, sitemap_1.streamToPromise)(sitemapStream);
        res.header('Content-Type', 'application/xml');
        res.send(xml);
    });
}
function getSitemapVideoChannelUrls() {
    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
        const rows = yield video_channel_1.VideoChannelModel.listLocalsForSitemap('createdAt');
        return rows.map(channel => ({
            url: constants_1.WEBSERVER.URL + '/video-channels/' + channel.Actor.preferredUsername
        }));
    });
}
function getSitemapAccountUrls() {
    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
        const rows = yield account_1.AccountModel.listLocalsForSitemap('createdAt');
        return rows.map(channel => ({
            url: constants_1.WEBSERVER.URL + '/accounts/' + channel.Actor.preferredUsername
        }));
    });
}
function getSitemapLocalVideoUrls() {
    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
        const serverActor = yield (0, application_1.getServerActor)();
        const { data } = yield video_1.VideoModel.listForApi({
            start: 0,
            count: undefined,
            sort: 'createdAt',
            displayOnlyForFollower: {
                actorId: serverActor.id,
                orLocalVideos: true
            },
            isLocal: true,
            nsfw: (0, express_utils_1.buildNSFWFilter)(),
            countVideos: false
        });
        return data.map(v => ({
            url: constants_1.WEBSERVER.URL + v.getWatchStaticPath(),
            video: [
                {
                    title: v.name,
                    description: (0, lodash_1.truncate)(v.description || v.name, { length: 2000, omission: '...' }),
                    player_loc: constants_1.WEBSERVER.URL + v.getEmbedStaticPath(),
                    thumbnail_loc: constants_1.WEBSERVER.URL + v.getMiniatureStaticPath()
                }
            ]
        }));
    });
}
function getSitemapBasicUrls() {
    const paths = [
        '/about/instance',
        '/videos/local'
    ];
    return paths.map(p => ({ url: constants_1.WEBSERVER.URL + p }));
}
//# sourceMappingURL=bots.js.map