"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const chai_1 = require("chai");
const core_utils_1 = require("../../../../shared/core-utils");
const server_commands_1 = require("../../../../shared/server-commands");
describe('Test videos filter', function () {
    let servers;
    let paths;
    let remotePaths;
    before(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(240000);
            servers = yield (0, server_commands_1.createMultipleServers)(2);
            yield (0, server_commands_1.setAccessTokensToServers)(servers);
            yield (0, server_commands_1.setDefaultVideoChannel)(servers);
            yield (0, server_commands_1.setDefaultAccountAvatar)(servers);
            for (const server of servers) {
                const moderator = { username: 'moderator', password: 'my super password' };
                yield server.users.create({ username: moderator.username, password: moderator.password, role: 1 });
                server['moderatorAccessToken'] = yield server.login.getAccessToken(moderator);
                yield server.videos.upload({ attributes: { name: 'public ' + server.serverNumber } });
                {
                    const attributes = { name: 'unlisted ' + server.serverNumber, privacy: 2 };
                    yield server.videos.upload({ attributes });
                }
                {
                    const attributes = { name: 'private ' + server.serverNumber, privacy: 3 };
                    yield server.videos.upload({ attributes });
                }
            }
            yield (0, server_commands_1.doubleFollow)(servers[0], servers[1]);
            paths = [
                `/api/v1/video-channels/root_channel/videos`,
                `/api/v1/accounts/root/videos`,
                '/api/v1/videos',
                '/api/v1/search/videos'
            ];
            remotePaths = [
                `/api/v1/video-channels/root_channel@${servers[1].host}/videos`,
                `/api/v1/accounts/root@${servers[1].host}/videos`,
                '/api/v1/videos',
                '/api/v1/search/videos'
            ];
        });
    });
    describe('Check deprecated videos filter', function () {
        function getVideosNames(server, token, filter, expectedStatus = 200) {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const videosResults = [];
                for (const path of paths) {
                    const res = yield (0, server_commands_1.makeGetRequest)({
                        url: server.url,
                        path,
                        token,
                        query: {
                            sort: 'createdAt',
                            filter
                        },
                        expectedStatus
                    });
                    videosResults.push(res.body.data.map(v => v.name));
                }
                return videosResults;
            });
        }
        it('Should display local videos', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                for (const server of servers) {
                    const namesResults = yield getVideosNames(server, server.accessToken, 'local');
                    for (const names of namesResults) {
                        (0, chai_1.expect)(names).to.have.lengthOf(1);
                        (0, chai_1.expect)(names[0]).to.equal('public ' + server.serverNumber);
                    }
                }
            });
        });
        it('Should display all local videos by the admin or the moderator', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                for (const server of servers) {
                    for (const token of [server.accessToken, server['moderatorAccessToken']]) {
                        const namesResults = yield getVideosNames(server, token, 'all-local');
                        for (const names of namesResults) {
                            (0, chai_1.expect)(names).to.have.lengthOf(3);
                            (0, chai_1.expect)(names[0]).to.equal('public ' + server.serverNumber);
                            (0, chai_1.expect)(names[1]).to.equal('unlisted ' + server.serverNumber);
                            (0, chai_1.expect)(names[2]).to.equal('private ' + server.serverNumber);
                        }
                    }
                }
            });
        });
        it('Should display all videos by the admin or the moderator', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                for (const server of servers) {
                    for (const token of [server.accessToken, server['moderatorAccessToken']]) {
                        const [channelVideos, accountVideos, videos, searchVideos] = yield getVideosNames(server, token, 'all');
                        (0, chai_1.expect)(channelVideos).to.have.lengthOf(3);
                        (0, chai_1.expect)(accountVideos).to.have.lengthOf(3);
                        (0, chai_1.expect)(videos).to.have.lengthOf(5);
                        (0, chai_1.expect)(searchVideos).to.have.lengthOf(5);
                    }
                }
            });
        });
    });
    describe('Check videos filters', function () {
        function listVideos(options) {
            var _a, _b;
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const res = yield (0, server_commands_1.makeGetRequest)({
                    url: options.server.url,
                    path: options.path,
                    token: (_a = options.token) !== null && _a !== void 0 ? _a : options.server.accessToken,
                    query: Object.assign(Object.assign({}, (0, core_utils_1.pick)(options, ['isLocal', 'include', 'category', 'tagsAllOf', 'hasWebtorrentFiles', 'hasHLSFiles', 'privacyOneOf'])), { sort: 'createdAt' }),
                    expectedStatus: (_b = options.expectedStatus) !== null && _b !== void 0 ? _b : 200
                });
                return res.body.data;
            });
        }
        function getVideosNames(options) {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const videosResults = [];
                for (const path of paths) {
                    const videos = yield listVideos(Object.assign(Object.assign({}, options), { path }));
                    videosResults.push(videos.map(v => v.name));
                }
                return videosResults;
            });
        }
        it('Should display local videos', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                for (const server of servers) {
                    const namesResults = yield getVideosNames({ server, isLocal: true });
                    for (const names of namesResults) {
                        (0, chai_1.expect)(names).to.have.lengthOf(1);
                        (0, chai_1.expect)(names[0]).to.equal('public ' + server.serverNumber);
                    }
                }
            });
        });
        it('Should display local videos with hidden privacy by the admin or the moderator', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                for (const server of servers) {
                    for (const token of [server.accessToken, server['moderatorAccessToken']]) {
                        const namesResults = yield getVideosNames({
                            server,
                            token,
                            isLocal: true,
                            privacyOneOf: [2, 1, 3]
                        });
                        for (const names of namesResults) {
                            (0, chai_1.expect)(names).to.have.lengthOf(3);
                            (0, chai_1.expect)(names[0]).to.equal('public ' + server.serverNumber);
                            (0, chai_1.expect)(names[1]).to.equal('unlisted ' + server.serverNumber);
                            (0, chai_1.expect)(names[2]).to.equal('private ' + server.serverNumber);
                        }
                    }
                }
            });
        });
        it('Should display all videos by the admin or the moderator', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                for (const server of servers) {
                    for (const token of [server.accessToken, server['moderatorAccessToken']]) {
                        const [channelVideos, accountVideos, videos, searchVideos] = yield getVideosNames({
                            server,
                            token,
                            privacyOneOf: [2, 1, 3]
                        });
                        (0, chai_1.expect)(channelVideos).to.have.lengthOf(3);
                        (0, chai_1.expect)(accountVideos).to.have.lengthOf(3);
                        (0, chai_1.expect)(videos).to.have.lengthOf(5);
                        (0, chai_1.expect)(searchVideos).to.have.lengthOf(5);
                    }
                }
            });
        });
        it('Should display only remote videos', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(120000);
                yield servers[1].videos.upload({ attributes: { name: 'remote video' } });
                yield (0, server_commands_1.waitJobs)(servers);
                const finder = (videos) => videos.find(v => v.name === 'remote video');
                for (const path of remotePaths) {
                    {
                        const videos = yield listVideos({ server: servers[0], path });
                        const video = finder(videos);
                        (0, chai_1.expect)(video).to.exist;
                    }
                    {
                        const videos = yield listVideos({ server: servers[0], path, isLocal: false });
                        const video = finder(videos);
                        (0, chai_1.expect)(video).to.exist;
                    }
                    {
                        const videos = yield listVideos({ server: servers[0], path, isLocal: true });
                        const video = finder(videos);
                        (0, chai_1.expect)(video).to.not.exist;
                    }
                }
            });
        });
        it('Should include not published videos', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield servers[0].config.enableLive({ allowReplay: false, transcoding: false });
                yield servers[0].live.create({ fields: { name: 'live video', channelId: servers[0].store.channel.id, privacy: 1 } });
                const finder = (videos) => videos.find(v => v.name === 'live video');
                for (const path of paths) {
                    {
                        const videos = yield listVideos({ server: servers[0], path });
                        const video = finder(videos);
                        (0, chai_1.expect)(video).to.not.exist;
                        (0, chai_1.expect)(videos[0].state).to.not.exist;
                        (0, chai_1.expect)(videos[0].waitTranscoding).to.not.exist;
                    }
                    {
                        const videos = yield listVideos({ server: servers[0], path, include: 1 });
                        const video = finder(videos);
                        (0, chai_1.expect)(video).to.exist;
                        (0, chai_1.expect)(video.state).to.exist;
                    }
                }
            });
        });
        it('Should include blacklisted videos', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const { id } = yield servers[0].videos.upload({ attributes: { name: 'blacklisted' } });
                yield servers[0].blacklist.add({ videoId: id });
                const finder = (videos) => videos.find(v => v.name === 'blacklisted');
                for (const path of paths) {
                    {
                        const videos = yield listVideos({ server: servers[0], path });
                        const video = finder(videos);
                        (0, chai_1.expect)(video).to.not.exist;
                        (0, chai_1.expect)(videos[0].blacklisted).to.not.exist;
                    }
                    {
                        const videos = yield listVideos({ server: servers[0], path, include: 2 });
                        const video = finder(videos);
                        (0, chai_1.expect)(video).to.exist;
                        (0, chai_1.expect)(video.blacklisted).to.be.true;
                    }
                }
            });
        });
        it('Should include videos from muted account', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const finder = (videos) => videos.find(v => v.name === 'remote video');
                yield servers[0].blocklist.addToServerBlocklist({ account: 'root@' + servers[1].host });
                for (const path of remotePaths) {
                    {
                        const videos = yield listVideos({ server: servers[0], path });
                        const video = finder(videos);
                        (0, chai_1.expect)(video).to.not.exist;
                        if (videos[0]) {
                            (0, chai_1.expect)(videos[0].blockedOwner).to.not.exist;
                            (0, chai_1.expect)(videos[0].blockedServer).to.not.exist;
                        }
                    }
                    {
                        const videos = yield listVideos({ server: servers[0], path, include: 4 });
                        const video = finder(videos);
                        (0, chai_1.expect)(video).to.exist;
                        (0, chai_1.expect)(video.blockedServer).to.be.false;
                        (0, chai_1.expect)(video.blockedOwner).to.be.true;
                    }
                }
                yield servers[0].blocklist.removeFromServerBlocklist({ account: 'root@' + servers[1].host });
            });
        });
        it('Should include videos from muted server', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const finder = (videos) => videos.find(v => v.name === 'remote video');
                yield servers[0].blocklist.addToServerBlocklist({ server: servers[1].host });
                for (const path of remotePaths) {
                    {
                        const videos = yield listVideos({ server: servers[0], path });
                        const video = finder(videos);
                        (0, chai_1.expect)(video).to.not.exist;
                        if (videos[0]) {
                            (0, chai_1.expect)(videos[0].blockedOwner).to.not.exist;
                            (0, chai_1.expect)(videos[0].blockedServer).to.not.exist;
                        }
                    }
                    {
                        const videos = yield listVideos({ server: servers[0], path, include: 4 });
                        const video = finder(videos);
                        (0, chai_1.expect)(video).to.exist;
                        (0, chai_1.expect)(video.blockedServer).to.be.true;
                        (0, chai_1.expect)(video.blockedOwner).to.be.false;
                    }
                }
                yield servers[0].blocklist.removeFromServerBlocklist({ server: servers[1].host });
            });
        });
        it('Should include video files', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                for (const path of paths) {
                    {
                        const videos = yield listVideos({ server: servers[0], path });
                        for (const video of videos) {
                            const videoWithFiles = video;
                            (0, chai_1.expect)(videoWithFiles.files).to.not.exist;
                            (0, chai_1.expect)(videoWithFiles.streamingPlaylists).to.not.exist;
                        }
                    }
                    {
                        const videos = yield listVideos({ server: servers[0], path, include: 8 });
                        for (const video of videos) {
                            const videoWithFiles = video;
                            (0, chai_1.expect)(videoWithFiles.files).to.exist;
                            (0, chai_1.expect)(videoWithFiles.files).to.have.length.at.least(1);
                        }
                    }
                }
            });
        });
        it('Should filter by tags and category', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield servers[0].videos.upload({ attributes: { name: 'tag filter', tags: ['tag1', 'tag2'] } });
                yield servers[0].videos.upload({ attributes: { name: 'tag filter with category', tags: ['tag3'], category: 4 } });
                for (const path of paths) {
                    {
                        const videos = yield listVideos({ server: servers[0], path, tagsAllOf: ['tag1', 'tag2'] });
                        (0, chai_1.expect)(videos).to.have.lengthOf(1);
                        (0, chai_1.expect)(videos[0].name).to.equal('tag filter');
                    }
                    {
                        const videos = yield listVideos({ server: servers[0], path, tagsAllOf: ['tag1', 'tag3'] });
                        (0, chai_1.expect)(videos).to.have.lengthOf(0);
                    }
                    {
                        const { data, total } = yield servers[0].videos.list({ tagsAllOf: ['tag3'], categoryOneOf: [4] });
                        (0, chai_1.expect)(total).to.equal(1);
                        (0, chai_1.expect)(data[0].name).to.equal('tag filter with category');
                    }
                    {
                        const { total } = yield servers[0].videos.list({ tagsAllOf: ['tag4'], categoryOneOf: [4] });
                        (0, chai_1.expect)(total).to.equal(0);
                    }
                }
            });
        });
        it('Should filter by HLS or WebTorrent files', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(360000);
                const finderFactory = (name) => (videos) => videos.some(v => v.name === name);
                yield servers[0].config.enableTranscoding(true, false);
                yield servers[0].videos.upload({ attributes: { name: 'webtorrent video' } });
                const hasWebtorrent = finderFactory('webtorrent video');
                yield (0, server_commands_1.waitJobs)(servers);
                yield servers[0].config.enableTranscoding(false, true);
                yield servers[0].videos.upload({ attributes: { name: 'hls video' } });
                const hasHLS = finderFactory('hls video');
                yield (0, server_commands_1.waitJobs)(servers);
                yield servers[0].config.enableTranscoding(true, true);
                yield servers[0].videos.upload({ attributes: { name: 'hls and webtorrent video' } });
                const hasBoth = finderFactory('hls and webtorrent video');
                yield (0, server_commands_1.waitJobs)(servers);
                for (const path of paths) {
                    {
                        const videos = yield listVideos({ server: servers[0], path, hasWebtorrentFiles: true });
                        (0, chai_1.expect)(hasWebtorrent(videos)).to.be.true;
                        (0, chai_1.expect)(hasHLS(videos)).to.be.false;
                        (0, chai_1.expect)(hasBoth(videos)).to.be.true;
                    }
                    {
                        const videos = yield listVideos({ server: servers[0], path, hasWebtorrentFiles: false });
                        (0, chai_1.expect)(hasWebtorrent(videos)).to.be.false;
                        (0, chai_1.expect)(hasHLS(videos)).to.be.true;
                        (0, chai_1.expect)(hasBoth(videos)).to.be.false;
                    }
                    {
                        const videos = yield listVideos({ server: servers[0], path, hasHLSFiles: true });
                        (0, chai_1.expect)(hasWebtorrent(videos)).to.be.false;
                        (0, chai_1.expect)(hasHLS(videos)).to.be.true;
                        (0, chai_1.expect)(hasBoth(videos)).to.be.true;
                    }
                    {
                        const videos = yield listVideos({ server: servers[0], path, hasHLSFiles: false });
                        (0, chai_1.expect)(hasWebtorrent(videos)).to.be.true;
                        (0, chai_1.expect)(hasHLS(videos)).to.be.false;
                        (0, chai_1.expect)(hasBoth(videos)).to.be.false;
                    }
                    {
                        const videos = yield listVideos({ server: servers[0], path, hasHLSFiles: false, hasWebtorrentFiles: false });
                        (0, chai_1.expect)(hasWebtorrent(videos)).to.be.false;
                        (0, chai_1.expect)(hasHLS(videos)).to.be.false;
                        (0, chai_1.expect)(hasBoth(videos)).to.be.false;
                    }
                    {
                        const videos = yield listVideos({ server: servers[0], path, hasHLSFiles: true, hasWebtorrentFiles: true });
                        (0, chai_1.expect)(hasWebtorrent(videos)).to.be.false;
                        (0, chai_1.expect)(hasHLS(videos)).to.be.false;
                        (0, chai_1.expect)(hasBoth(videos)).to.be.true;
                    }
                }
            });
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)(servers);
        });
    });
});
//# sourceMappingURL=videos-common-filters.js.map