"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.accountsRouter = void 0;
const tslib_1 = require("tslib");
const express_1 = tslib_1.__importDefault(require("express"));
const query_1 = require("../../helpers/query");
const actor_follow_1 = require("../../models/actor/actor-follow");
const application_1 = require("../../models/application/application");
const video_format_utils_1 = require("../../models/video/formatter/video-format-utils");
const video_channel_sync_1 = require("../../models/video/video-channel-sync");
const express_utils_1 = require("../../helpers/express-utils");
const utils_1 = require("../../helpers/utils");
const job_queue_1 = require("../../lib/job-queue");
const hooks_1 = require("../../lib/plugins/hooks");
const middlewares_1 = require("../../middlewares");
const validators_1 = require("../../middlewares/validators");
const video_playlists_1 = require("../../middlewares/validators/videos/video-playlists");
const account_1 = require("../../models/account/account");
const account_video_rate_1 = require("../../models/account/account-video-rate");
const video_1 = require("../../models/video/video");
const video_channel_1 = require("../../models/video/video-channel");
const video_playlist_1 = require("../../models/video/video-playlist");
const accountsRouter = express_1.default.Router();
exports.accountsRouter = accountsRouter;
accountsRouter.get('/', middlewares_1.paginationValidator, validators_1.accountsSortValidator, middlewares_1.setDefaultSort, middlewares_1.setDefaultPagination, (0, middlewares_1.asyncMiddleware)(listAccounts));
accountsRouter.get('/:accountName', (0, middlewares_1.asyncMiddleware)(validators_1.accountNameWithHostGetValidator), getAccount);
accountsRouter.get('/:accountName/videos', (0, middlewares_1.asyncMiddleware)(validators_1.accountNameWithHostGetValidator), middlewares_1.paginationValidator, validators_1.videosSortValidator, middlewares_1.setDefaultVideosSort, middlewares_1.setDefaultPagination, middlewares_1.optionalAuthenticate, middlewares_1.commonVideosFiltersValidator, (0, middlewares_1.asyncMiddleware)(listAccountVideos));
accountsRouter.get('/:accountName/video-channels', (0, middlewares_1.asyncMiddleware)(validators_1.accountNameWithHostGetValidator), validators_1.videoChannelStatsValidator, middlewares_1.paginationValidator, validators_1.videoChannelsSortValidator, middlewares_1.setDefaultSort, middlewares_1.setDefaultPagination, (0, middlewares_1.asyncMiddleware)(listAccountChannels));
accountsRouter.get('/:accountName/video-channel-syncs', middlewares_1.authenticate, (0, middlewares_1.asyncMiddleware)(validators_1.accountNameWithHostGetValidator), validators_1.ensureCanManageChannelOrAccount, middlewares_1.paginationValidator, validators_1.videoChannelSyncsSortValidator, middlewares_1.setDefaultSort, middlewares_1.setDefaultPagination, (0, middlewares_1.asyncMiddleware)(listAccountChannelsSync));
accountsRouter.get('/:accountName/video-playlists', middlewares_1.optionalAuthenticate, (0, middlewares_1.asyncMiddleware)(validators_1.accountNameWithHostGetValidator), middlewares_1.paginationValidator, middlewares_1.videoPlaylistsSortValidator, middlewares_1.setDefaultSort, middlewares_1.setDefaultPagination, video_playlists_1.commonVideoPlaylistFiltersValidator, video_playlists_1.videoPlaylistsSearchValidator, (0, middlewares_1.asyncMiddleware)(listAccountPlaylists));
accountsRouter.get('/:accountName/ratings', middlewares_1.authenticate, (0, middlewares_1.asyncMiddleware)(validators_1.accountNameWithHostGetValidator), validators_1.ensureAuthUserOwnsAccountValidator, middlewares_1.paginationValidator, middlewares_1.videoRatesSortValidator, middlewares_1.setDefaultSort, middlewares_1.setDefaultPagination, middlewares_1.videoRatingValidator, (0, middlewares_1.asyncMiddleware)(listAccountRatings));
accountsRouter.get('/:accountName/followers', middlewares_1.authenticate, (0, middlewares_1.asyncMiddleware)(validators_1.accountNameWithHostGetValidator), validators_1.ensureAuthUserOwnsAccountValidator, middlewares_1.paginationValidator, validators_1.accountsFollowersSortValidator, middlewares_1.setDefaultSort, middlewares_1.setDefaultPagination, (0, middlewares_1.asyncMiddleware)(listAccountFollowers));
function getAccount(req, res) {
    const account = res.locals.account;
    if (account.isOutdated()) {
        job_queue_1.JobQueue.Instance.createJobAsync({ type: 'activitypub-refresher', payload: { type: 'actor', url: account.Actor.url } });
    }
    return res.json(account.toFormattedJSON());
}
function listAccounts(req, res) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const resultList = yield account_1.AccountModel.listForApi(req.query.start, req.query.count, req.query.sort);
        return res.json((0, utils_1.getFormattedObjects)(resultList.data, resultList.total));
    });
}
function listAccountChannels(req, res) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const options = {
            accountId: res.locals.account.id,
            start: req.query.start,
            count: req.query.count,
            sort: req.query.sort,
            withStats: req.query.withStats,
            search: req.query.search
        };
        const resultList = yield video_channel_1.VideoChannelModel.listByAccountForAPI(options);
        return res.json((0, utils_1.getFormattedObjects)(resultList.data, resultList.total));
    });
}
function listAccountChannelsSync(req, res) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const options = {
            accountId: res.locals.account.id,
            start: req.query.start,
            count: req.query.count,
            sort: req.query.sort,
            search: req.query.search
        };
        const resultList = yield video_channel_sync_1.VideoChannelSyncModel.listByAccountForAPI(options);
        return res.json((0, utils_1.getFormattedObjects)(resultList.data, resultList.total));
    });
}
function listAccountPlaylists(req, res) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const serverActor = yield (0, application_1.getServerActor)();
        let listMyPlaylists = false;
        if (res.locals.oauth && res.locals.oauth.token.User.Account.id === res.locals.account.id) {
            listMyPlaylists = true;
        }
        const resultList = yield video_playlist_1.VideoPlaylistModel.listForApi({
            search: req.query.search,
            followerActorId: serverActor.id,
            start: req.query.start,
            count: req.query.count,
            sort: req.query.sort,
            accountId: res.locals.account.id,
            listMyPlaylists,
            type: req.query.playlistType
        });
        return res.json((0, utils_1.getFormattedObjects)(resultList.data, resultList.total));
    });
}
function listAccountVideos(req, res) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const serverActor = yield (0, application_1.getServerActor)();
        const account = res.locals.account;
        const displayOnlyForFollower = (0, express_utils_1.isUserAbleToSearchRemoteURI)(res)
            ? null
            : {
                actorId: serverActor.id,
                orLocalVideos: true
            };
        const countVideos = (0, express_utils_1.getCountVideos)(req);
        const query = (0, query_1.pickCommonVideoQuery)(req.query);
        const apiOptions = yield hooks_1.Hooks.wrapObject(Object.assign(Object.assign({}, query), { displayOnlyForFollower, nsfw: (0, express_utils_1.buildNSFWFilter)(res, query.nsfw), accountId: account.id, user: res.locals.oauth ? res.locals.oauth.token.User : undefined, countVideos }), 'filter:api.accounts.videos.list.params');
        const resultList = yield hooks_1.Hooks.wrapPromiseFun(video_1.VideoModel.listForApi, apiOptions, 'filter:api.accounts.videos.list.result');
        return res.json((0, utils_1.getFormattedObjects)(resultList.data, resultList.total, (0, video_format_utils_1.guessAdditionalAttributesFromQuery)(query)));
    });
}
function listAccountRatings(req, res) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const account = res.locals.account;
        const resultList = yield account_video_rate_1.AccountVideoRateModel.listByAccountForApi({
            accountId: account.id,
            start: req.query.start,
            count: req.query.count,
            sort: req.query.sort,
            type: req.query.rating
        });
        return res.json((0, utils_1.getFormattedObjects)(resultList.data, resultList.total));
    });
}
function listAccountFollowers(req, res) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const account = res.locals.account;
        const channels = yield video_channel_1.VideoChannelModel.listAllByAccount(account.id);
        const actorIds = [account.actorId].concat(channels.map(c => c.actorId));
        const resultList = yield actor_follow_1.ActorFollowModel.listFollowersForApi({
            actorIds,
            start: req.query.start,
            count: req.query.count,
            sort: req.query.sort,
            search: req.query.search,
            state: 'accepted'
        });
        return res.json((0, utils_1.getFormattedObjects)(resultList.data, resultList.total));
    });
}
//# sourceMappingURL=accounts.js.map