"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.miscRouter = void 0;
const tslib_1 = require("tslib");
const cors_1 = tslib_1.__importDefault(require("cors"));
const express_1 = tslib_1.__importDefault(require("express"));
const config_1 = require("../initializers/config");
const client_html_1 = require("../lib/client-html");
const server_config_manager_1 = require("../lib/server-config-manager");
const constants_1 = require("../initializers/constants");
const theme_utils_1 = require("../lib/plugins/theme-utils");
const middlewares_1 = require("../middlewares");
const cache_1 = require("../middlewares/cache/cache");
const user_1 = require("../models/user/user");
const video_1 = require("../models/video/video");
const video_comment_1 = require("../models/video/video-comment");
const miscRouter = express_1.default.Router();
exports.miscRouter = miscRouter;
miscRouter.use((0, cors_1.default)());
miscRouter.use('/nodeinfo/:version.json', (0, cache_1.cacheRoute)(constants_1.ROUTE_CACHE_LIFETIME.NODEINFO), (0, middlewares_1.asyncMiddleware)(generateNodeinfo));
miscRouter.get('/robots.txt', (0, cache_1.cacheRoute)(constants_1.ROUTE_CACHE_LIFETIME.ROBOTS), (_, res) => {
    res.type('text/plain');
    return res.send(config_1.CONFIG.INSTANCE.ROBOTS);
});
miscRouter.all('/teapot', getCup, (0, middlewares_1.asyncMiddleware)(client_html_1.serveIndexHTML));
miscRouter.get('/security.txt', (_, res) => {
    return res.redirect(301, '/.well-known/security.txt');
});
function generateNodeinfo(req, res) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const { totalVideos } = yield video_1.VideoModel.getStats();
        const { totalLocalVideoComments } = yield video_comment_1.VideoCommentModel.getStats();
        const { totalUsers, totalMonthlyActiveUsers, totalHalfYearActiveUsers } = yield user_1.UserModel.getStats();
        if (!req.params.version || req.params.version !== '2.0') {
            return res.fail({
                status: 404,
                message: 'Nodeinfo schema version not handled'
            });
        }
        const json = {
            version: '2.0',
            software: {
                name: 'peertube',
                version: constants_1.PEERTUBE_VERSION
            },
            protocols: [
                'activitypub'
            ],
            services: {
                inbound: [],
                outbound: [
                    'atom1.0',
                    'rss2.0'
                ]
            },
            openRegistrations: config_1.CONFIG.SIGNUP.ENABLED,
            usage: {
                users: {
                    total: totalUsers,
                    activeMonth: totalMonthlyActiveUsers,
                    activeHalfyear: totalHalfYearActiveUsers
                },
                localPosts: totalVideos,
                localComments: totalLocalVideoComments
            },
            metadata: {
                taxonomy: {
                    postsName: 'Videos'
                },
                nodeName: config_1.CONFIG.INSTANCE.NAME,
                nodeDescription: config_1.CONFIG.INSTANCE.SHORT_DESCRIPTION,
                nodeConfig: {
                    search: {
                        remoteUri: {
                            users: config_1.CONFIG.SEARCH.REMOTE_URI.USERS,
                            anonymous: config_1.CONFIG.SEARCH.REMOTE_URI.ANONYMOUS
                        }
                    },
                    plugin: {
                        registered: server_config_manager_1.ServerConfigManager.Instance.getRegisteredPlugins()
                    },
                    theme: {
                        registered: server_config_manager_1.ServerConfigManager.Instance.getRegisteredThemes(),
                        default: (0, theme_utils_1.getThemeOrDefault)(config_1.CONFIG.THEME.DEFAULT, constants_1.DEFAULT_THEME_NAME)
                    },
                    email: {
                        enabled: (0, config_1.isEmailEnabled)()
                    },
                    contactForm: {
                        enabled: config_1.CONFIG.CONTACT_FORM.ENABLED
                    },
                    transcoding: {
                        hls: {
                            enabled: config_1.CONFIG.TRANSCODING.HLS.ENABLED
                        },
                        webtorrent: {
                            enabled: config_1.CONFIG.TRANSCODING.WEBTORRENT.ENABLED
                        },
                        enabledResolutions: server_config_manager_1.ServerConfigManager.Instance.getEnabledResolutions('vod')
                    },
                    live: {
                        enabled: config_1.CONFIG.LIVE.ENABLED,
                        transcoding: {
                            enabled: config_1.CONFIG.LIVE.TRANSCODING.ENABLED,
                            enabledResolutions: server_config_manager_1.ServerConfigManager.Instance.getEnabledResolutions('live')
                        }
                    },
                    import: {
                        videos: {
                            http: {
                                enabled: config_1.CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
                            },
                            torrent: {
                                enabled: config_1.CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED
                            }
                        }
                    },
                    autoBlacklist: {
                        videos: {
                            ofUsers: {
                                enabled: config_1.CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED
                            }
                        }
                    },
                    avatar: {
                        file: {
                            size: {
                                max: constants_1.CONSTRAINTS_FIELDS.ACTORS.IMAGE.FILE_SIZE.max
                            },
                            extensions: constants_1.CONSTRAINTS_FIELDS.ACTORS.IMAGE.EXTNAME
                        }
                    },
                    video: {
                        image: {
                            extensions: constants_1.CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME,
                            size: {
                                max: constants_1.CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max
                            }
                        },
                        file: {
                            extensions: constants_1.CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
                        }
                    },
                    videoCaption: {
                        file: {
                            size: {
                                max: constants_1.CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max
                            },
                            extensions: constants_1.CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME
                        }
                    },
                    user: {
                        videoQuota: config_1.CONFIG.USER.VIDEO_QUOTA,
                        videoQuotaDaily: config_1.CONFIG.USER.VIDEO_QUOTA_DAILY
                    },
                    trending: {
                        videos: {
                            intervalDays: config_1.CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS
                        }
                    },
                    tracker: {
                        enabled: config_1.CONFIG.TRACKER.ENABLED
                    }
                }
            }
        };
        res.contentType('application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"')
            .send(json)
            .end();
    });
}
function getCup(req, res, next) {
    res.status(418);
    res.setHeader('Accept-Additions', 'Non-Dairy;1,Sugar;1');
    res.setHeader('Safe', 'if-sepia-awake');
    return next();
}
//# sourceMappingURL=misc.js.map