"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const chai_1 = require("chai");
const shared_1 = require("../../shared");
const core_utils_1 = require("../../../../shared/core-utils");
const extra_utils_1 = require("../../../../shared/extra-utils");
const server_commands_1 = require("../../../../shared/server-commands");
describe('Test user notifications', function () {
    let servers = [];
    let userAccessToken;
    let userNotifications = [];
    let adminNotifications = [];
    let adminNotificationsServer2 = [];
    let emails = [];
    let channelId;
    before(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(120000);
            const res = yield (0, shared_1.prepareNotificationsTest)(3);
            emails = res.emails;
            userAccessToken = res.userAccessToken;
            servers = res.servers;
            userNotifications = res.userNotifications;
            adminNotifications = res.adminNotifications;
            adminNotificationsServer2 = res.adminNotificationsServer2;
            channelId = res.channelId;
        });
    });
    describe('New video from my subscription notification', function () {
        let baseParams;
        before(() => {
            baseParams = {
                server: servers[0],
                emails,
                socketNotifications: userNotifications,
                token: userAccessToken
            };
        });
        it('Should not send notifications if the user does not follow the video publisher', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(50000);
                yield (0, shared_1.uploadRandomVideoOnServers)(servers, 1);
                const notification = yield servers[0].notifications.getLatest({ token: userAccessToken });
                (0, chai_1.expect)(notification).to.be.undefined;
                (0, chai_1.expect)(emails).to.have.lengthOf(0);
                (0, chai_1.expect)(userNotifications).to.have.lengthOf(0);
            });
        });
        it('Should send a new video notification if the user follows the local video publisher', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(15000);
                yield servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'root_channel@' + servers[0].host });
                yield (0, server_commands_1.waitJobs)(servers);
                const { name, shortUUID } = yield (0, shared_1.uploadRandomVideoOnServers)(servers, 1);
                yield (0, shared_1.checkNewVideoFromSubscription)(Object.assign(Object.assign({}, baseParams), { videoName: name, shortUUID, checkType: 'presence' }));
            });
        });
        it('Should send a new video notification from a remote account', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(150000);
                yield servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'root_channel@' + servers[1].host });
                yield (0, server_commands_1.waitJobs)(servers);
                const { name, shortUUID } = yield (0, shared_1.uploadRandomVideoOnServers)(servers, 2);
                yield (0, shared_1.checkNewVideoFromSubscription)(Object.assign(Object.assign({}, baseParams), { videoName: name, shortUUID, checkType: 'presence' }));
            });
        });
        it('Should send a new video notification on a scheduled publication', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(50000);
                const updateAt = new Date(new Date().getTime() + 2000);
                const data = {
                    privacy: 3,
                    scheduleUpdate: {
                        updateAt: updateAt.toISOString(),
                        privacy: 1
                    }
                };
                const { name, shortUUID } = yield (0, shared_1.uploadRandomVideoOnServers)(servers, 1, data);
                yield (0, core_utils_1.wait)(6000);
                yield (0, shared_1.checkNewVideoFromSubscription)(Object.assign(Object.assign({}, baseParams), { videoName: name, shortUUID, checkType: 'presence' }));
            });
        });
        it('Should send a new video notification on a remote scheduled publication', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(100000);
                const updateAt = new Date(new Date().getTime() + 2000);
                const data = {
                    privacy: 3,
                    scheduleUpdate: {
                        updateAt: updateAt.toISOString(),
                        privacy: 1
                    }
                };
                const { name, shortUUID } = yield (0, shared_1.uploadRandomVideoOnServers)(servers, 2, data);
                yield (0, server_commands_1.waitJobs)(servers);
                yield (0, core_utils_1.wait)(6000);
                yield (0, shared_1.checkNewVideoFromSubscription)(Object.assign(Object.assign({}, baseParams), { videoName: name, shortUUID, checkType: 'presence' }));
            });
        });
        it('Should not send a notification before the video is published', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(150000);
                const updateAt = new Date(new Date().getTime() + 1000000);
                const data = {
                    privacy: 3,
                    scheduleUpdate: {
                        updateAt: updateAt.toISOString(),
                        privacy: 1
                    }
                };
                const { name, shortUUID } = yield (0, shared_1.uploadRandomVideoOnServers)(servers, 1, data);
                yield (0, core_utils_1.wait)(6000);
                yield (0, shared_1.checkNewVideoFromSubscription)(Object.assign(Object.assign({}, baseParams), { videoName: name, shortUUID, checkType: 'absence' }));
            });
        });
        it('Should send a new video notification when a video becomes public', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(50000);
                const data = { privacy: 3 };
                const { name, uuid, shortUUID } = yield (0, shared_1.uploadRandomVideoOnServers)(servers, 1, data);
                yield (0, shared_1.checkNewVideoFromSubscription)(Object.assign(Object.assign({}, baseParams), { videoName: name, shortUUID, checkType: 'absence' }));
                yield servers[0].videos.update({ id: uuid, attributes: { privacy: 1 } });
                yield (0, server_commands_1.waitJobs)(servers);
                yield (0, shared_1.checkNewVideoFromSubscription)(Object.assign(Object.assign({}, baseParams), { videoName: name, shortUUID, checkType: 'presence' }));
            });
        });
        it('Should send a new video notification when a remote video becomes public', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(120000);
                const data = { privacy: 3 };
                const { name, uuid, shortUUID } = yield (0, shared_1.uploadRandomVideoOnServers)(servers, 2, data);
                yield (0, shared_1.checkNewVideoFromSubscription)(Object.assign(Object.assign({}, baseParams), { videoName: name, shortUUID, checkType: 'absence' }));
                yield servers[1].videos.update({ id: uuid, attributes: { privacy: 1 } });
                yield (0, server_commands_1.waitJobs)(servers);
                yield (0, shared_1.checkNewVideoFromSubscription)(Object.assign(Object.assign({}, baseParams), { videoName: name, shortUUID, checkType: 'presence' }));
            });
        });
        it('Should not send a new video notification when a video becomes unlisted', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(50000);
                const data = { privacy: 3 };
                const { name, uuid, shortUUID } = yield (0, shared_1.uploadRandomVideoOnServers)(servers, 1, data);
                yield servers[0].videos.update({ id: uuid, attributes: { privacy: 2 } });
                yield (0, shared_1.checkNewVideoFromSubscription)(Object.assign(Object.assign({}, baseParams), { videoName: name, shortUUID, checkType: 'absence' }));
            });
        });
        it('Should not send a new video notification when a remote video becomes unlisted', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(100000);
                const data = { privacy: 3 };
                const { name, uuid, shortUUID } = yield (0, shared_1.uploadRandomVideoOnServers)(servers, 2, data);
                yield servers[1].videos.update({ id: uuid, attributes: { privacy: 2 } });
                yield (0, server_commands_1.waitJobs)(servers);
                yield (0, shared_1.checkNewVideoFromSubscription)(Object.assign(Object.assign({}, baseParams), { videoName: name, shortUUID, checkType: 'absence' }));
            });
        });
        it('Should send a new video notification after a video import', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(100000);
                const name = 'video import ' + (0, extra_utils_1.buildUUID)();
                const attributes = {
                    name,
                    channelId,
                    privacy: 1,
                    targetUrl: shared_1.FIXTURE_URLS.goodVideo
                };
                const { video } = yield servers[0].imports.importVideo({ attributes });
                yield (0, server_commands_1.waitJobs)(servers);
                yield (0, shared_1.checkNewVideoFromSubscription)(Object.assign(Object.assign({}, baseParams), { videoName: name, shortUUID: video.shortUUID, checkType: 'presence' }));
            });
        });
    });
    describe('My video is published', function () {
        let baseParams;
        before(() => {
            baseParams = {
                server: servers[1],
                emails,
                socketNotifications: adminNotificationsServer2,
                token: servers[1].accessToken
            };
        });
        it('Should not send a notification if transcoding is not enabled', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(50000);
                const { name, shortUUID } = yield (0, shared_1.uploadRandomVideoOnServers)(servers, 1);
                yield (0, server_commands_1.waitJobs)(servers);
                yield (0, shared_1.checkVideoIsPublished)(Object.assign(Object.assign({}, baseParams), { videoName: name, shortUUID, checkType: 'absence' }));
            });
        });
        it('Should not send a notification if the wait transcoding is false', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(100000);
                yield (0, shared_1.uploadRandomVideoOnServers)(servers, 2, { waitTranscoding: false });
                yield (0, server_commands_1.waitJobs)(servers);
                const notification = yield servers[0].notifications.getLatest({ token: userAccessToken });
                if (notification) {
                    (0, chai_1.expect)(notification.type).to.not.equal(6);
                }
            });
        });
        it('Should send a notification even if the video is not transcoded in other resolutions', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(100000);
                const { name, shortUUID } = yield (0, shared_1.uploadRandomVideoOnServers)(servers, 2, { waitTranscoding: true, fixture: 'video_short_240p.mp4' });
                yield (0, server_commands_1.waitJobs)(servers);
                yield (0, shared_1.checkVideoIsPublished)(Object.assign(Object.assign({}, baseParams), { videoName: name, shortUUID, checkType: 'presence' }));
            });
        });
        it('Should send a notification with a transcoded video', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(100000);
                const { name, shortUUID } = yield (0, shared_1.uploadRandomVideoOnServers)(servers, 2, { waitTranscoding: true });
                yield (0, server_commands_1.waitJobs)(servers);
                yield (0, shared_1.checkVideoIsPublished)(Object.assign(Object.assign({}, baseParams), { videoName: name, shortUUID, checkType: 'presence' }));
            });
        });
        it('Should send a notification when an imported video is transcoded', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(120000);
                const name = 'video import ' + (0, extra_utils_1.buildUUID)();
                const attributes = {
                    name,
                    channelId,
                    privacy: 1,
                    targetUrl: shared_1.FIXTURE_URLS.goodVideo,
                    waitTranscoding: true
                };
                const { video } = yield servers[1].imports.importVideo({ attributes });
                yield (0, server_commands_1.waitJobs)(servers);
                yield (0, shared_1.checkVideoIsPublished)(Object.assign(Object.assign({}, baseParams), { videoName: name, shortUUID: video.shortUUID, checkType: 'presence' }));
            });
        });
        it('Should send a notification when the scheduled update has been proceeded', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(70000);
                const updateAt = new Date(new Date().getTime() + 2000);
                const data = {
                    privacy: 3,
                    scheduleUpdate: {
                        updateAt: updateAt.toISOString(),
                        privacy: 1
                    }
                };
                const { name, shortUUID } = yield (0, shared_1.uploadRandomVideoOnServers)(servers, 2, data);
                yield (0, core_utils_1.wait)(6000);
                yield (0, shared_1.checkVideoIsPublished)(Object.assign(Object.assign({}, baseParams), { videoName: name, shortUUID, checkType: 'presence' }));
            });
        });
        it('Should not send a notification before the video is published', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(150000);
                const updateAt = new Date(new Date().getTime() + 1000000);
                const data = {
                    privacy: 3,
                    scheduleUpdate: {
                        updateAt: updateAt.toISOString(),
                        privacy: 1
                    }
                };
                const { name, shortUUID } = yield (0, shared_1.uploadRandomVideoOnServers)(servers, 2, data);
                yield (0, core_utils_1.wait)(6000);
                yield (0, shared_1.checkVideoIsPublished)(Object.assign(Object.assign({}, baseParams), { videoName: name, shortUUID, checkType: 'absence' }));
            });
        });
    });
    describe('My live replay is published', function () {
        let baseParams;
        before(() => {
            baseParams = {
                server: servers[1],
                emails,
                socketNotifications: adminNotificationsServer2,
                token: servers[1].accessToken
            };
        });
        it('Should send a notification is a live replay of a non permanent live is published', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(120000);
                const { shortUUID } = yield servers[1].live.create({
                    fields: {
                        name: 'non permanent live',
                        privacy: 1,
                        channelId: servers[1].store.channel.id,
                        saveReplay: true,
                        permanentLive: false
                    }
                });
                const ffmpegCommand = yield servers[1].live.sendRTMPStreamInVideo({ videoId: shortUUID });
                yield (0, server_commands_1.waitJobs)(servers);
                yield servers[1].live.waitUntilPublished({ videoId: shortUUID });
                yield (0, server_commands_1.stopFfmpeg)(ffmpegCommand);
                yield servers[1].live.waitUntilReplacedByReplay({ videoId: shortUUID });
                yield (0, server_commands_1.waitJobs)(servers);
                yield (0, shared_1.checkVideoIsPublished)(Object.assign(Object.assign({}, baseParams), { videoName: 'non permanent live', shortUUID, checkType: 'presence' }));
            });
        });
        it('Should send a notification is a live replay of a permanent live is published', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(120000);
                const { shortUUID } = yield servers[1].live.create({
                    fields: {
                        name: 'permanent live',
                        privacy: 1,
                        channelId: servers[1].store.channel.id,
                        saveReplay: true,
                        permanentLive: true
                    }
                });
                const ffmpegCommand = yield servers[1].live.sendRTMPStreamInVideo({ videoId: shortUUID });
                yield (0, server_commands_1.waitJobs)(servers);
                yield servers[1].live.waitUntilPublished({ videoId: shortUUID });
                const liveDetails = yield servers[1].videos.get({ id: shortUUID });
                yield (0, server_commands_1.stopFfmpeg)(ffmpegCommand);
                yield servers[1].live.waitUntilWaiting({ videoId: shortUUID });
                yield (0, server_commands_1.waitJobs)(servers);
                const video = yield (0, server_commands_1.findExternalSavedVideo)(servers[1], liveDetails);
                (0, chai_1.expect)(video).to.exist;
                yield (0, shared_1.checkVideoIsPublished)(Object.assign(Object.assign({}, baseParams), { videoName: video.name, shortUUID: video.shortUUID, checkType: 'presence' }));
            });
        });
    });
    describe('Video studio', function () {
        let baseParams;
        before(() => {
            baseParams = {
                server: servers[1],
                emails,
                socketNotifications: adminNotificationsServer2,
                token: servers[1].accessToken
            };
        });
        it('Should send a notification after studio edition', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(240000);
                const { name, shortUUID, id } = yield (0, shared_1.uploadRandomVideoOnServers)(servers, 2, { waitTranscoding: true });
                yield (0, server_commands_1.waitJobs)(servers);
                yield (0, shared_1.checkVideoIsPublished)(Object.assign(Object.assign({}, baseParams), { videoName: name, shortUUID, checkType: 'presence' }));
                const tasks = [
                    {
                        name: 'cut',
                        options: {
                            start: 0,
                            end: 1
                        }
                    }
                ];
                yield servers[1].videoStudio.createEditionTasks({ videoId: id, tasks });
                yield (0, server_commands_1.waitJobs)(servers);
                yield (0, shared_1.checkVideoStudioEditionIsFinished)(Object.assign(Object.assign({}, baseParams), { videoName: name, shortUUID, checkType: 'presence' }));
            });
        });
    });
    describe('My video is imported', function () {
        let baseParams;
        before(() => {
            baseParams = {
                server: servers[0],
                emails,
                socketNotifications: adminNotifications,
                token: servers[0].accessToken
            };
        });
        it('Should send a notification when the video import failed', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(70000);
                const name = 'video import ' + (0, extra_utils_1.buildUUID)();
                const attributes = {
                    name,
                    channelId,
                    privacy: 3,
                    targetUrl: shared_1.FIXTURE_URLS.badVideo
                };
                const { video: { shortUUID } } = yield servers[0].imports.importVideo({ attributes });
                yield (0, server_commands_1.waitJobs)(servers);
                const url = shared_1.FIXTURE_URLS.badVideo;
                yield (0, shared_1.checkMyVideoImportIsFinished)(Object.assign(Object.assign({}, baseParams), { videoName: name, shortUUID, url, success: false, checkType: 'presence' }));
            });
        });
        it('Should send a notification when the video import succeeded', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(70000);
                const name = 'video import ' + (0, extra_utils_1.buildUUID)();
                const attributes = {
                    name,
                    channelId,
                    privacy: 3,
                    targetUrl: shared_1.FIXTURE_URLS.goodVideo
                };
                const { video: { shortUUID } } = yield servers[0].imports.importVideo({ attributes });
                yield (0, server_commands_1.waitJobs)(servers);
                const url = shared_1.FIXTURE_URLS.goodVideo;
                yield (0, shared_1.checkMyVideoImportIsFinished)(Object.assign(Object.assign({}, baseParams), { videoName: name, shortUUID, url, success: true, checkType: 'presence' }));
            });
        });
    });
    describe('New actor follow', function () {
        let baseParams;
        const myChannelName = 'super channel name';
        const myUserName = 'super user name';
        before(() => tslib_1.__awaiter(this, void 0, void 0, function* () {
            baseParams = {
                server: servers[0],
                emails,
                socketNotifications: userNotifications,
                token: userAccessToken
            };
            yield servers[0].users.updateMe({ displayName: 'super root name' });
            yield servers[0].users.updateMe({
                token: userAccessToken,
                displayName: myUserName
            });
            yield servers[1].users.updateMe({ displayName: 'super root 2 name' });
            yield servers[0].channels.update({
                token: userAccessToken,
                channelName: 'user_1_channel',
                attributes: { displayName: myChannelName }
            });
        }));
        it('Should notify when a local channel is following one of our channel', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(50000);
                yield servers[0].subscriptions.add({ targetUri: 'user_1_channel@' + servers[0].host });
                yield (0, server_commands_1.waitJobs)(servers);
                yield (0, shared_1.checkNewActorFollow)(Object.assign(Object.assign({}, baseParams), { followType: 'channel', followerName: 'root', followerDisplayName: 'super root name', followingDisplayName: myChannelName, checkType: 'presence' }));
                yield servers[0].subscriptions.remove({ uri: 'user_1_channel@' + servers[0].host });
            });
        });
        it('Should notify when a remote channel is following one of our channel', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(50000);
                yield servers[1].subscriptions.add({ targetUri: 'user_1_channel@' + servers[0].host });
                yield (0, server_commands_1.waitJobs)(servers);
                yield (0, shared_1.checkNewActorFollow)(Object.assign(Object.assign({}, baseParams), { followType: 'channel', followerName: 'root', followerDisplayName: 'super root 2 name', followingDisplayName: myChannelName, checkType: 'presence' }));
                yield servers[1].subscriptions.remove({ uri: 'user_1_channel@' + servers[0].host });
            });
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            shared_1.MockSmtpServer.Instance.kill();
            yield (0, server_commands_1.cleanupTests)(servers);
        });
    });
});
//# sourceMappingURL=user-notifications.js.map