1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
7 checkMyVideoImportIsFinished,
9 checkNewVideoFromSubscription,
10 checkVideoIsPublished,
11 checkVideoStudioEditionIsFinished,
14 prepareNotificationsTest,
15 uploadRandomVideoOnServers
16 } from '@server/tests/shared'
17 import { wait } from '@shared/core-utils'
18 import { buildUUID } from '@shared/extra-utils'
19 import { UserNotification, UserNotificationType, VideoPrivacy, VideoStudioTask } from '@shared/models'
20 import { cleanupTests, findExternalSavedVideo, PeerTubeServer, stopFfmpeg, waitJobs } from '@shared/server-commands'
22 const expect = chai.expect
24 describe('Test user notifications', function () {
25 let servers: PeerTubeServer[] = []
26 let userAccessToken: string
28 let userNotifications: UserNotification[] = []
29 let adminNotifications: UserNotification[] = []
30 let adminNotificationsServer2: UserNotification[] = []
31 let emails: object[] = []
35 before(async function () {
38 const res = await prepareNotificationsTest(3)
40 userAccessToken = res.userAccessToken
42 userNotifications = res.userNotifications
43 adminNotifications = res.adminNotifications
44 adminNotificationsServer2 = res.adminNotificationsServer2
45 channelId = res.channelId
48 describe('New video from my subscription notification', function () {
49 let baseParams: CheckerBaseParams
55 socketNotifications: userNotifications,
56 token: userAccessToken
60 it('Should not send notifications if the user does not follow the video publisher', async function () {
63 await uploadRandomVideoOnServers(servers, 1)
65 const notification = await servers[0].notifications.getLatest({ token: userAccessToken })
66 expect(notification).to.be.undefined
68 expect(emails).to.have.lengthOf(0)
69 expect(userNotifications).to.have.lengthOf(0)
72 it('Should send a new video notification if the user follows the local video publisher', async function () {
75 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'root_channel@localhost:' + servers[0].port })
76 await waitJobs(servers)
78 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 1)
79 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
82 it('Should send a new video notification from a remote account', async function () {
83 this.timeout(150000) // Server 2 has transcoding enabled
85 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'root_channel@localhost:' + servers[1].port })
86 await waitJobs(servers)
88 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 2)
89 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
92 it('Should send a new video notification on a scheduled publication', async function () {
96 const updateAt = new Date(new Date().getTime() + 2000)
99 privacy: VideoPrivacy.PRIVATE,
101 updateAt: updateAt.toISOString(),
102 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
105 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 1, data)
108 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
111 it('Should send a new video notification on a remote scheduled publication', async function () {
115 const updateAt = new Date(new Date().getTime() + 2000)
118 privacy: VideoPrivacy.PRIVATE,
120 updateAt: updateAt.toISOString(),
121 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
124 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 2, data)
125 await waitJobs(servers)
128 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
131 it('Should not send a notification before the video is published', async function () {
134 const updateAt = new Date(new Date().getTime() + 1000000)
137 privacy: VideoPrivacy.PRIVATE,
139 updateAt: updateAt.toISOString(),
140 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
143 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 1, data)
146 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
149 it('Should send a new video notification when a video becomes public', async function () {
152 const data = { privacy: VideoPrivacy.PRIVATE }
153 const { name, uuid, shortUUID } = await uploadRandomVideoOnServers(servers, 1, data)
155 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
157 await servers[0].videos.update({ id: uuid, attributes: { privacy: VideoPrivacy.PUBLIC } })
159 await waitJobs(servers)
160 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
163 it('Should send a new video notification when a remote video becomes public', async function () {
166 const data = { privacy: VideoPrivacy.PRIVATE }
167 const { name, uuid, shortUUID } = await uploadRandomVideoOnServers(servers, 2, data)
169 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
171 await servers[1].videos.update({ id: uuid, attributes: { privacy: VideoPrivacy.PUBLIC } })
173 await waitJobs(servers)
174 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
177 it('Should not send a new video notification when a video becomes unlisted', async function () {
180 const data = { privacy: VideoPrivacy.PRIVATE }
181 const { name, uuid, shortUUID } = await uploadRandomVideoOnServers(servers, 1, data)
183 await servers[0].videos.update({ id: uuid, attributes: { privacy: VideoPrivacy.UNLISTED } })
185 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
188 it('Should not send a new video notification when a remote video becomes unlisted', async function () {
191 const data = { privacy: VideoPrivacy.PRIVATE }
192 const { name, uuid, shortUUID } = await uploadRandomVideoOnServers(servers, 2, data)
194 await servers[1].videos.update({ id: uuid, attributes: { privacy: VideoPrivacy.UNLISTED } })
196 await waitJobs(servers)
197 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
200 it('Should send a new video notification after a video import', async function () {
203 const name = 'video import ' + buildUUID()
208 privacy: VideoPrivacy.PUBLIC,
209 targetUrl: FIXTURE_URLS.goodVideo
211 const { video } = await servers[0].imports.importVideo({ attributes })
213 await waitJobs(servers)
215 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID: video.shortUUID, checkType: 'presence' })
219 describe('My video is published', function () {
220 let baseParams: CheckerBaseParams
226 socketNotifications: adminNotificationsServer2,
227 token: servers[1].accessToken
231 it('Should not send a notification if transcoding is not enabled', async function () {
234 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 1)
235 await waitJobs(servers)
237 await checkVideoIsPublished({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
240 it('Should not send a notification if the wait transcoding is false', async function () {
241 this.timeout(100_000)
243 await uploadRandomVideoOnServers(servers, 2, { waitTranscoding: false })
244 await waitJobs(servers)
246 const notification = await servers[0].notifications.getLatest({ token: userAccessToken })
248 expect(notification.type).to.not.equal(UserNotificationType.MY_VIDEO_PUBLISHED)
252 it('Should send a notification even if the video is not transcoded in other resolutions', async function () {
255 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 2, { waitTranscoding: true, fixture: 'video_short_240p.mp4' })
256 await waitJobs(servers)
258 await checkVideoIsPublished({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
261 it('Should send a notification with a transcoded video', async function () {
264 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 2, { waitTranscoding: true })
265 await waitJobs(servers)
267 await checkVideoIsPublished({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
270 it('Should send a notification when an imported video is transcoded', async function () {
273 const name = 'video import ' + buildUUID()
278 privacy: VideoPrivacy.PUBLIC,
279 targetUrl: FIXTURE_URLS.goodVideo,
280 waitTranscoding: true
282 const { video } = await servers[1].imports.importVideo({ attributes })
284 await waitJobs(servers)
285 await checkVideoIsPublished({ ...baseParams, videoName: name, shortUUID: video.shortUUID, checkType: 'presence' })
288 it('Should send a notification when the scheduled update has been proceeded', async function () {
292 const updateAt = new Date(new Date().getTime() + 2000)
295 privacy: VideoPrivacy.PRIVATE,
297 updateAt: updateAt.toISOString(),
298 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
301 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 2, data)
304 await checkVideoIsPublished({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
307 it('Should not send a notification before the video is published', async function () {
310 const updateAt = new Date(new Date().getTime() + 1000000)
313 privacy: VideoPrivacy.PRIVATE,
315 updateAt: updateAt.toISOString(),
316 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
319 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 2, data)
322 await checkVideoIsPublished({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
326 describe('My live replay is published', function () {
328 let baseParams: CheckerBaseParams
334 socketNotifications: adminNotificationsServer2,
335 token: servers[1].accessToken
339 it('Should send a notification is a live replay of a non permanent live is published', async function () {
342 const { shortUUID } = await servers[1].live.create({
344 name: 'non permanent live',
345 privacy: VideoPrivacy.PUBLIC,
346 channelId: servers[1].store.channel.id,
352 const ffmpegCommand = await servers[1].live.sendRTMPStreamInVideo({ videoId: shortUUID })
354 await waitJobs(servers)
355 await servers[1].live.waitUntilPublished({ videoId: shortUUID })
357 await stopFfmpeg(ffmpegCommand)
358 await servers[1].live.waitUntilReplacedByReplay({ videoId: shortUUID })
360 await waitJobs(servers)
361 await checkVideoIsPublished({ ...baseParams, videoName: 'non permanent live', shortUUID, checkType: 'presence' })
364 it('Should send a notification is a live replay of a permanent live is published', async function () {
367 const { shortUUID } = await servers[1].live.create({
369 name: 'permanent live',
370 privacy: VideoPrivacy.PUBLIC,
371 channelId: servers[1].store.channel.id,
377 const ffmpegCommand = await servers[1].live.sendRTMPStreamInVideo({ videoId: shortUUID })
379 await waitJobs(servers)
380 await servers[1].live.waitUntilPublished({ videoId: shortUUID })
382 const liveDetails = await servers[1].videos.get({ id: shortUUID })
384 await stopFfmpeg(ffmpegCommand)
386 await servers[1].live.waitUntilWaiting({ videoId: shortUUID })
387 await waitJobs(servers)
389 const video = await findExternalSavedVideo(servers[1], liveDetails)
390 expect(video).to.exist
392 await checkVideoIsPublished({ ...baseParams, videoName: video.name, shortUUID: video.shortUUID, checkType: 'presence' })
396 describe('Video studio', function () {
397 let baseParams: CheckerBaseParams
403 socketNotifications: adminNotificationsServer2,
404 token: servers[1].accessToken
408 it('Should send a notification after studio edition', async function () {
411 const { name, shortUUID, id } = await uploadRandomVideoOnServers(servers, 2, { waitTranscoding: true })
413 await waitJobs(servers)
414 await checkVideoIsPublished({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
416 const tasks: VideoStudioTask[] = [
425 await servers[1].videoStudio.createEditionTasks({ videoId: id, tasks })
426 await waitJobs(servers)
428 await checkVideoStudioEditionIsFinished({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
432 describe('My video is imported', function () {
433 let baseParams: CheckerBaseParams
439 socketNotifications: adminNotifications,
440 token: servers[0].accessToken
444 it('Should send a notification when the video import failed', async function () {
447 const name = 'video import ' + buildUUID()
452 privacy: VideoPrivacy.PRIVATE,
453 targetUrl: FIXTURE_URLS.badVideo
455 const { video: { shortUUID } } = await servers[0].imports.importVideo({ attributes })
457 await waitJobs(servers)
459 const url = FIXTURE_URLS.badVideo
460 await checkMyVideoImportIsFinished({ ...baseParams, videoName: name, shortUUID, url, success: false, checkType: 'presence' })
463 it('Should send a notification when the video import succeeded', async function () {
466 const name = 'video import ' + buildUUID()
471 privacy: VideoPrivacy.PRIVATE,
472 targetUrl: FIXTURE_URLS.goodVideo
474 const { video: { shortUUID } } = await servers[0].imports.importVideo({ attributes })
476 await waitJobs(servers)
478 const url = FIXTURE_URLS.goodVideo
479 await checkMyVideoImportIsFinished({ ...baseParams, videoName: name, shortUUID, url, success: true, checkType: 'presence' })
483 describe('New actor follow', function () {
484 let baseParams: CheckerBaseParams
485 const myChannelName = 'super channel name'
486 const myUserName = 'super user name'
492 socketNotifications: userNotifications,
493 token: userAccessToken
496 await servers[0].users.updateMe({ displayName: 'super root name' })
498 await servers[0].users.updateMe({
499 token: userAccessToken,
500 displayName: myUserName
503 await servers[1].users.updateMe({ displayName: 'super root 2 name' })
505 await servers[0].channels.update({
506 token: userAccessToken,
507 channelName: 'user_1_channel',
508 attributes: { displayName: myChannelName }
512 it('Should notify when a local channel is following one of our channel', async function () {
515 await servers[0].subscriptions.add({ targetUri: 'user_1_channel@localhost:' + servers[0].port })
516 await waitJobs(servers)
518 await checkNewActorFollow({
520 followType: 'channel',
521 followerName: 'root',
522 followerDisplayName: 'super root name',
523 followingDisplayName: myChannelName,
524 checkType: 'presence'
527 await servers[0].subscriptions.remove({ uri: 'user_1_channel@localhost:' + servers[0].port })
530 it('Should notify when a remote channel is following one of our channel', async function () {
533 await servers[1].subscriptions.add({ targetUri: 'user_1_channel@localhost:' + servers[0].port })
534 await waitJobs(servers)
536 await checkNewActorFollow({
538 followType: 'channel',
539 followerName: 'root',
540 followerDisplayName: 'super root 2 name',
541 followingDisplayName: myChannelName,
542 checkType: 'presence'
545 await servers[1].subscriptions.remove({ uri: 'user_1_channel@localhost:' + servers[0].port })
548 // PeerTube does not support account -> account follows
549 // it('Should notify when a local account is following one of our channel', async function () {
550 // this.timeout(50000)
552 // await addUserSubscription(servers[0].url, servers[0].accessToken, 'user_1@localhost:' + servers[0].port)
554 // await waitJobs(servers)
556 // await checkNewActorFollow(baseParams, 'account', 'root', 'super root name', myUserName, 'presence')
559 // it('Should notify when a remote account is following one of our channel', async function () {
560 // this.timeout(50000)
562 // await addUserSubscription(servers[1].url, servers[1].accessToken, 'user_1@localhost:' + servers[0].port)
564 // await waitJobs(servers)
566 // await checkNewActorFollow(baseParams, 'account', 'root', 'super root 2 name', myUserName, 'presence')
570 after(async function () {
571 MockSmtpServer.Instance.kill()
573 await cleanupTests(servers)