Add notifications in the client
[oweals/peertube.git] / server / tests / api / users / user-notifications.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import {
6   addVideoToBlacklist,
7   createUser,
8   doubleFollow,
9   flushAndRunMultipleServers,
10   flushTests,
11   getMyUserInformation,
12   immutableAssign,
13   registerUser,
14   removeVideoFromBlacklist,
15   reportVideoAbuse,
16   updateMyUser,
17   updateVideo,
18   updateVideoChannel,
19   userLogin,
20   wait
21 } from '../../../../shared/utils'
22 import { killallServers, ServerInfo, uploadVideo } from '../../../../shared/utils/index'
23 import { setAccessTokensToServers } from '../../../../shared/utils/users/login'
24 import { waitJobs } from '../../../../shared/utils/server/jobs'
25 import { getUserNotificationSocket } from '../../../../shared/utils/socket/socket-io'
26 import {
27   checkCommentMention,
28   CheckerBaseParams,
29   checkMyVideoImportIsFinished,
30   checkNewActorFollow,
31   checkNewBlacklistOnMyVideo,
32   checkNewCommentOnMyVideo,
33   checkNewVideoAbuseForModerators,
34   checkNewVideoFromSubscription,
35   checkUserRegistered,
36   checkVideoIsPublished,
37   getLastNotification,
38   getUserNotifications,
39   markAsReadNotifications,
40   updateMyNotificationSettings,
41   markAsReadAllNotifications
42 } from '../../../../shared/utils/users/user-notifications'
43 import {
44   User,
45   UserNotification,
46   UserNotificationSetting,
47   UserNotificationSettingValue,
48   UserNotificationType
49 } from '../../../../shared/models/users'
50 import { MockSmtpServer } from '../../../../shared/utils/miscs/email'
51 import { addUserSubscription, removeUserSubscription } from '../../../../shared/utils/users/user-subscriptions'
52 import { VideoPrivacy } from '../../../../shared/models/videos'
53 import { getBadVideoUrl, getYoutubeVideoUrl, importVideo } from '../../../../shared/utils/videos/video-imports'
54 import { addVideoCommentReply, addVideoCommentThread } from '../../../../shared/utils/videos/video-comments'
55 import * as uuidv4 from 'uuid/v4'
56 import { addAccountToAccountBlocklist, removeAccountFromAccountBlocklist } from '../../../../shared/utils/users/blocklist'
57
58 const expect = chai.expect
59
60 async function uploadVideoByRemoteAccount (servers: ServerInfo[], additionalParams: any = {}) {
61   const name = 'remote video ' + uuidv4()
62
63   const data = Object.assign({ name }, additionalParams)
64   const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, data)
65
66   await waitJobs(servers)
67
68   return { uuid: res.body.video.uuid, name }
69 }
70
71 async function uploadVideoByLocalAccount (servers: ServerInfo[], additionalParams: any = {}) {
72   const name = 'local video ' + uuidv4()
73
74   const data = Object.assign({ name }, additionalParams)
75   const res = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, data)
76
77   await waitJobs(servers)
78
79   return { uuid: res.body.video.uuid, name }
80 }
81
82 describe('Test users notifications', function () {
83   let servers: ServerInfo[] = []
84   let userAccessToken: string
85   let userNotifications: UserNotification[] = []
86   let adminNotifications: UserNotification[] = []
87   let adminNotificationsServer2: UserNotification[] = []
88   const emails: object[] = []
89   let channelId: number
90
91   const allNotificationSettings: UserNotificationSetting = {
92     newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
93     newCommentOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
94     videoAbuseAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
95     blacklistOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
96     myVideoImportFinished: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
97     myVideoPublished: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
98     commentMention: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
99     newFollow: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
100     newUserRegistration: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
101   }
102
103   before(async function () {
104     this.timeout(120000)
105
106     await MockSmtpServer.Instance.collectEmails(emails)
107
108     await flushTests()
109
110     const overrideConfig = {
111       smtp: {
112         hostname: 'localhost'
113       }
114     }
115     servers = await flushAndRunMultipleServers(2, overrideConfig)
116
117     // Get the access tokens
118     await setAccessTokensToServers(servers)
119
120     // Server 1 and server 2 follow each other
121     await doubleFollow(servers[0], servers[1])
122
123     await waitJobs(servers)
124
125     const user = {
126       username: 'user_1',
127       password: 'super password'
128     }
129     await createUser(servers[0].url, servers[0].accessToken, user.username, user.password, 10 * 1000 * 1000)
130     userAccessToken = await userLogin(servers[0], user)
131
132     await updateMyNotificationSettings(servers[0].url, userAccessToken, allNotificationSettings)
133     await updateMyNotificationSettings(servers[0].url, servers[0].accessToken, allNotificationSettings)
134     await updateMyNotificationSettings(servers[1].url, servers[1].accessToken, allNotificationSettings)
135
136     {
137       const socket = getUserNotificationSocket(servers[ 0 ].url, userAccessToken)
138       socket.on('new-notification', n => userNotifications.push(n))
139     }
140     {
141       const socket = getUserNotificationSocket(servers[ 0 ].url, servers[0].accessToken)
142       socket.on('new-notification', n => adminNotifications.push(n))
143     }
144     {
145       const socket = getUserNotificationSocket(servers[ 1 ].url, servers[1].accessToken)
146       socket.on('new-notification', n => adminNotificationsServer2.push(n))
147     }
148
149     {
150       const resChannel = await getMyUserInformation(servers[0].url, servers[0].accessToken)
151       channelId = resChannel.body.videoChannels[0].id
152     }
153   })
154
155   describe('New video from my subscription notification', function () {
156     let baseParams: CheckerBaseParams
157
158     before(() => {
159       baseParams = {
160         server: servers[0],
161         emails,
162         socketNotifications: userNotifications,
163         token: userAccessToken
164       }
165     })
166
167     it('Should not send notifications if the user does not follow the video publisher', async function () {
168       await uploadVideoByLocalAccount(servers)
169
170       const notification = await getLastNotification(servers[ 0 ].url, userAccessToken)
171       expect(notification).to.be.undefined
172
173       expect(emails).to.have.lengthOf(0)
174       expect(userNotifications).to.have.lengthOf(0)
175     })
176
177     it('Should send a new video notification if the user follows the local video publisher', async function () {
178       this.timeout(10000)
179
180       await addUserSubscription(servers[0].url, userAccessToken, 'root_channel@localhost:9001')
181       await waitJobs(servers)
182
183       const { name, uuid } = await uploadVideoByLocalAccount(servers)
184       await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
185     })
186
187     it('Should send a new video notification from a remote account', async function () {
188       this.timeout(50000) // Server 2 has transcoding enabled
189
190       await addUserSubscription(servers[0].url, userAccessToken, 'root_channel@localhost:9002')
191       await waitJobs(servers)
192
193       const { name, uuid } = await uploadVideoByRemoteAccount(servers)
194       await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
195     })
196
197     it('Should send a new video notification on a scheduled publication', async function () {
198       this.timeout(20000)
199
200       // In 2 seconds
201       let updateAt = new Date(new Date().getTime() + 2000)
202
203       const data = {
204         privacy: VideoPrivacy.PRIVATE,
205         scheduleUpdate: {
206           updateAt: updateAt.toISOString(),
207           privacy: VideoPrivacy.PUBLIC
208         }
209       }
210       const { name, uuid } = await uploadVideoByLocalAccount(servers, data)
211
212       await wait(6000)
213       await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
214     })
215
216     it('Should send a new video notification on a remote scheduled publication', async function () {
217       this.timeout(20000)
218
219       // In 2 seconds
220       let updateAt = new Date(new Date().getTime() + 2000)
221
222       const data = {
223         privacy: VideoPrivacy.PRIVATE,
224         scheduleUpdate: {
225           updateAt: updateAt.toISOString(),
226           privacy: VideoPrivacy.PUBLIC
227         }
228       }
229       const { name, uuid } = await uploadVideoByRemoteAccount(servers, data)
230       await waitJobs(servers)
231
232       await wait(6000)
233       await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
234     })
235
236     it('Should not send a notification before the video is published', async function () {
237       this.timeout(20000)
238
239       let updateAt = new Date(new Date().getTime() + 100000)
240
241       const data = {
242         privacy: VideoPrivacy.PRIVATE,
243         scheduleUpdate: {
244           updateAt: updateAt.toISOString(),
245           privacy: VideoPrivacy.PUBLIC
246         }
247       }
248       const { name, uuid } = await uploadVideoByLocalAccount(servers, data)
249
250       await wait(6000)
251       await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
252     })
253
254     it('Should send a new video notification when a video becomes public', async function () {
255       this.timeout(10000)
256
257       const data = { privacy: VideoPrivacy.PRIVATE }
258       const { name, uuid } = await uploadVideoByLocalAccount(servers, data)
259
260       await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
261
262       await updateVideo(servers[0].url, servers[0].accessToken, uuid, { privacy: VideoPrivacy.PUBLIC })
263
264       await wait(500)
265       await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
266     })
267
268     it('Should send a new video notification when a remote video becomes public', async function () {
269       this.timeout(20000)
270
271       const data = { privacy: VideoPrivacy.PRIVATE }
272       const { name, uuid } = await uploadVideoByRemoteAccount(servers, data)
273
274       await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
275
276       await updateVideo(servers[1].url, servers[1].accessToken, uuid, { privacy: VideoPrivacy.PUBLIC })
277
278       await waitJobs(servers)
279       await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
280     })
281
282     it('Should not send a new video notification when a video becomes unlisted', async function () {
283       this.timeout(20000)
284
285       const data = { privacy: VideoPrivacy.PRIVATE }
286       const { name, uuid } = await uploadVideoByLocalAccount(servers, data)
287
288       await updateVideo(servers[0].url, servers[0].accessToken, uuid, { privacy: VideoPrivacy.UNLISTED })
289
290       await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
291     })
292
293     it('Should not send a new video notification when a remote video becomes unlisted', async function () {
294       this.timeout(20000)
295
296       const data = { privacy: VideoPrivacy.PRIVATE }
297       const { name, uuid } = await uploadVideoByRemoteAccount(servers, data)
298
299       await updateVideo(servers[1].url, servers[1].accessToken, uuid, { privacy: VideoPrivacy.UNLISTED })
300
301       await waitJobs(servers)
302       await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
303     })
304
305     it('Should send a new video notification after a video import', async function () {
306       this.timeout(30000)
307
308       const name = 'video import ' + uuidv4()
309
310       const attributes = {
311         name,
312         channelId,
313         privacy: VideoPrivacy.PUBLIC,
314         targetUrl: getYoutubeVideoUrl()
315       }
316       const res = await importVideo(servers[0].url, servers[0].accessToken, attributes)
317       const uuid = res.body.video.uuid
318
319       await waitJobs(servers)
320
321       await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
322     })
323   })
324
325   describe('Comment on my video notifications', function () {
326     let baseParams: CheckerBaseParams
327
328     before(() => {
329       baseParams = {
330         server: servers[0],
331         emails,
332         socketNotifications: userNotifications,
333         token: userAccessToken
334       }
335     })
336
337     it('Should not send a new comment notification after a comment on another video', async function () {
338       this.timeout(10000)
339
340       const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' })
341       const uuid = resVideo.body.video.uuid
342
343       const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment')
344       const commentId = resComment.body.comment.id
345
346       await wait(500)
347       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence')
348     })
349
350     it('Should not send a new comment notification if I comment my own video', async function () {
351       this.timeout(10000)
352
353       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
354       const uuid = resVideo.body.video.uuid
355
356       const resComment = await addVideoCommentThread(servers[0].url, userAccessToken, uuid, 'comment')
357       const commentId = resComment.body.comment.id
358
359       await wait(500)
360       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence')
361     })
362
363     it('Should not send a new comment notification if the account is muted', async function () {
364       this.timeout(10000)
365
366       await addAccountToAccountBlocklist(servers[ 0 ].url, userAccessToken, 'root')
367
368       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
369       const uuid = resVideo.body.video.uuid
370
371       const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment')
372       const commentId = resComment.body.comment.id
373
374       await wait(500)
375       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence')
376
377       await removeAccountFromAccountBlocklist(servers[ 0 ].url, userAccessToken, 'root')
378     })
379
380     it('Should send a new comment notification after a local comment on my video', async function () {
381       this.timeout(10000)
382
383       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
384       const uuid = resVideo.body.video.uuid
385
386       const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment')
387       const commentId = resComment.body.comment.id
388
389       await wait(500)
390       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'presence')
391     })
392
393     it('Should send a new comment notification after a remote comment on my video', async function () {
394       this.timeout(10000)
395
396       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
397       const uuid = resVideo.body.video.uuid
398
399       await waitJobs(servers)
400
401       const resComment = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, 'comment')
402       const commentId = resComment.body.comment.id
403
404       await waitJobs(servers)
405       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'presence')
406     })
407
408     it('Should send a new comment notification after a local reply on my video', async function () {
409       this.timeout(10000)
410
411       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
412       const uuid = resVideo.body.video.uuid
413
414       const resThread = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment')
415       const threadId = resThread.body.comment.id
416
417       const resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, uuid, threadId, 'reply')
418       const commentId = resComment.body.comment.id
419
420       await wait(500)
421       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, threadId, 'presence')
422     })
423
424     it('Should send a new comment notification after a remote reply on my video', async function () {
425       this.timeout(10000)
426
427       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
428       const uuid = resVideo.body.video.uuid
429       await waitJobs(servers)
430
431       const resThread = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, 'comment')
432       const threadId = resThread.body.comment.id
433
434       const resComment = await addVideoCommentReply(servers[1].url, servers[1].accessToken, uuid, threadId, 'reply')
435       const commentId = resComment.body.comment.id
436
437       await waitJobs(servers)
438       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, threadId, 'presence')
439     })
440   })
441
442   describe('Mention notifications', function () {
443     let baseParams: CheckerBaseParams
444
445     before(async () => {
446       baseParams = {
447         server: servers[0],
448         emails,
449         socketNotifications: userNotifications,
450         token: userAccessToken
451       }
452
453       await updateMyUser({
454         url: servers[0].url,
455         accessToken: servers[0].accessToken,
456         displayName: 'super root name'
457       })
458
459       await updateMyUser({
460         url: servers[1].url,
461         accessToken: servers[1].accessToken,
462         displayName: 'super root 2 name'
463       })
464     })
465
466     it('Should not send a new mention comment notification if I mention the video owner', async function () {
467       this.timeout(10000)
468
469       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
470       const uuid = resVideo.body.video.uuid
471
472       const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello')
473       const commentId = resComment.body.comment.id
474
475       await wait(500)
476       await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence')
477     })
478
479     it('Should not send a new mention comment notification if I mention myself', async function () {
480       this.timeout(10000)
481
482       const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' })
483       const uuid = resVideo.body.video.uuid
484
485       const resComment = await addVideoCommentThread(servers[0].url, userAccessToken, uuid, '@user_1 hello')
486       const commentId = resComment.body.comment.id
487
488       await wait(500)
489       await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence')
490     })
491
492     it('Should not send a new mention notification if the account is muted', async function () {
493       this.timeout(10000)
494
495       await addAccountToAccountBlocklist(servers[ 0 ].url, userAccessToken, 'root')
496
497       const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' })
498       const uuid = resVideo.body.video.uuid
499
500       const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello')
501       const commentId = resComment.body.comment.id
502
503       await wait(500)
504       await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence')
505
506       await removeAccountFromAccountBlocklist(servers[ 0 ].url, userAccessToken, 'root')
507     })
508
509     it('Should send a new mention notification after local comments', async function () {
510       this.timeout(10000)
511
512       const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' })
513       const uuid = resVideo.body.video.uuid
514
515       const resThread = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello 1')
516       const threadId = resThread.body.comment.id
517
518       await wait(500)
519       await checkCommentMention(baseParams, uuid, threadId, threadId, 'super root name', 'presence')
520
521       const resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, uuid, threadId, 'hello 2 @user_1')
522       const commentId = resComment.body.comment.id
523
524       await wait(500)
525       await checkCommentMention(baseParams, uuid, commentId, threadId, 'super root name', 'presence')
526     })
527
528     it('Should send a new mention notification after remote comments', async function () {
529       this.timeout(20000)
530
531       const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' })
532       const uuid = resVideo.body.video.uuid
533
534       await waitJobs(servers)
535       const resThread = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, 'hello @user_1@localhost:9001 1')
536       const threadId = resThread.body.comment.id
537
538       await waitJobs(servers)
539       await checkCommentMention(baseParams, uuid, threadId, threadId, 'super root 2 name', 'presence')
540
541       const text = '@user_1@localhost:9001 hello 2 @root@localhost:9001'
542       const resComment = await addVideoCommentReply(servers[1].url, servers[1].accessToken, uuid, threadId, text)
543       const commentId = resComment.body.comment.id
544
545       await waitJobs(servers)
546       await checkCommentMention(baseParams, uuid, commentId, threadId, 'super root 2 name', 'presence')
547     })
548   })
549
550   describe('Video abuse for moderators notification' , function () {
551     let baseParams: CheckerBaseParams
552
553     before(() => {
554       baseParams = {
555         server: servers[0],
556         emails,
557         socketNotifications: adminNotifications,
558         token: servers[0].accessToken
559       }
560     })
561
562     it('Should send a notification to moderators on local video abuse', async function () {
563       this.timeout(10000)
564
565       const name = 'video for abuse ' + uuidv4()
566       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
567       const uuid = resVideo.body.video.uuid
568
569       await reportVideoAbuse(servers[0].url, servers[0].accessToken, uuid, 'super reason')
570
571       await waitJobs(servers)
572       await checkNewVideoAbuseForModerators(baseParams, uuid, name, 'presence')
573     })
574
575     it('Should send a notification to moderators on remote video abuse', async function () {
576       this.timeout(10000)
577
578       const name = 'video for abuse ' + uuidv4()
579       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
580       const uuid = resVideo.body.video.uuid
581
582       await waitJobs(servers)
583
584       await reportVideoAbuse(servers[1].url, servers[1].accessToken, uuid, 'super reason')
585
586       await waitJobs(servers)
587       await checkNewVideoAbuseForModerators(baseParams, uuid, name, 'presence')
588     })
589   })
590
591   describe('Video blacklist on my video', function () {
592     let baseParams: CheckerBaseParams
593
594     before(() => {
595       baseParams = {
596         server: servers[0],
597         emails,
598         socketNotifications: userNotifications,
599         token: userAccessToken
600       }
601     })
602
603     it('Should send a notification to video owner on blacklist', async function () {
604       this.timeout(10000)
605
606       const name = 'video for abuse ' + uuidv4()
607       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
608       const uuid = resVideo.body.video.uuid
609
610       await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid)
611
612       await waitJobs(servers)
613       await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'blacklist')
614     })
615
616     it('Should send a notification to video owner on unblacklist', async function () {
617       this.timeout(10000)
618
619       const name = 'video for abuse ' + uuidv4()
620       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
621       const uuid = resVideo.body.video.uuid
622
623       await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid)
624
625       await waitJobs(servers)
626       await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid)
627       await waitJobs(servers)
628
629       await wait(500)
630       await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'unblacklist')
631     })
632   })
633
634   describe('My video is published', function () {
635     let baseParams: CheckerBaseParams
636
637     before(() => {
638       baseParams = {
639         server: servers[1],
640         emails,
641         socketNotifications: adminNotificationsServer2,
642         token: servers[1].accessToken
643       }
644     })
645
646     it('Should not send a notification if transcoding is not enabled', async function () {
647       const { name, uuid } = await uploadVideoByLocalAccount(servers)
648       await waitJobs(servers)
649
650       await checkVideoIsPublished(baseParams, name, uuid, 'absence')
651     })
652
653     it('Should not send a notification if the wait transcoding is false', async function () {
654       this.timeout(50000)
655
656       await uploadVideoByRemoteAccount(servers, { waitTranscoding: false })
657       await waitJobs(servers)
658
659       const notification = await getLastNotification(servers[ 0 ].url, userAccessToken)
660       if (notification) {
661         expect(notification.type).to.not.equal(UserNotificationType.MY_VIDEO_PUBLISHED)
662       }
663     })
664
665     it('Should send a notification even if the video is not transcoded in other resolutions', async function () {
666       this.timeout(50000)
667
668       const { name, uuid } = await uploadVideoByRemoteAccount(servers, { waitTranscoding: true, fixture: 'video_short_240p.mp4' })
669       await waitJobs(servers)
670
671       await checkVideoIsPublished(baseParams, name, uuid, 'presence')
672     })
673
674     it('Should send a notification with a transcoded video', async function () {
675       this.timeout(50000)
676
677       const { name, uuid } = await uploadVideoByRemoteAccount(servers, { waitTranscoding: true })
678       await waitJobs(servers)
679
680       await checkVideoIsPublished(baseParams, name, uuid, 'presence')
681     })
682
683     it('Should send a notification when an imported video is transcoded', async function () {
684       this.timeout(50000)
685
686       const name = 'video import ' + uuidv4()
687
688       const attributes = {
689         name,
690         channelId,
691         privacy: VideoPrivacy.PUBLIC,
692         targetUrl: getYoutubeVideoUrl(),
693         waitTranscoding: true
694       }
695       const res = await importVideo(servers[1].url, servers[1].accessToken, attributes)
696       const uuid = res.body.video.uuid
697
698       await waitJobs(servers)
699       await checkVideoIsPublished(baseParams, name, uuid, 'presence')
700     })
701
702     it('Should send a notification when the scheduled update has been proceeded', async function () {
703       this.timeout(70000)
704
705       // In 2 seconds
706       let updateAt = new Date(new Date().getTime() + 2000)
707
708       const data = {
709         privacy: VideoPrivacy.PRIVATE,
710         scheduleUpdate: {
711           updateAt: updateAt.toISOString(),
712           privacy: VideoPrivacy.PUBLIC
713         }
714       }
715       const { name, uuid } = await uploadVideoByRemoteAccount(servers, data)
716
717       await wait(6000)
718       await checkVideoIsPublished(baseParams, name, uuid, 'presence')
719     })
720   })
721
722   describe('My video is imported', function () {
723     let baseParams: CheckerBaseParams
724
725     before(() => {
726       baseParams = {
727         server: servers[0],
728         emails,
729         socketNotifications: adminNotifications,
730         token: servers[0].accessToken
731       }
732     })
733
734     it('Should send a notification when the video import failed', async function () {
735       this.timeout(70000)
736
737       const name = 'video import ' + uuidv4()
738
739       const attributes = {
740         name,
741         channelId,
742         privacy: VideoPrivacy.PRIVATE,
743         targetUrl: getBadVideoUrl()
744       }
745       const res = await importVideo(servers[0].url, servers[0].accessToken, attributes)
746       const uuid = res.body.video.uuid
747
748       await waitJobs(servers)
749       await checkMyVideoImportIsFinished(baseParams, name, uuid, getBadVideoUrl(), false, 'presence')
750     })
751
752     it('Should send a notification when the video import succeeded', async function () {
753       this.timeout(70000)
754
755       const name = 'video import ' + uuidv4()
756
757       const attributes = {
758         name,
759         channelId,
760         privacy: VideoPrivacy.PRIVATE,
761         targetUrl: getYoutubeVideoUrl()
762       }
763       const res = await importVideo(servers[0].url, servers[0].accessToken, attributes)
764       const uuid = res.body.video.uuid
765
766       await waitJobs(servers)
767       await checkMyVideoImportIsFinished(baseParams, name, uuid, getYoutubeVideoUrl(), true, 'presence')
768     })
769   })
770
771   describe('New registration', function () {
772     let baseParams: CheckerBaseParams
773
774     before(() => {
775       baseParams = {
776         server: servers[0],
777         emails,
778         socketNotifications: adminNotifications,
779         token: servers[0].accessToken
780       }
781     })
782
783     it('Should send a notification only to moderators when a user registers on the instance', async function () {
784       await registerUser(servers[0].url, 'user_45', 'password')
785
786       await waitJobs(servers)
787
788       await checkUserRegistered(baseParams, 'user_45', 'presence')
789
790       const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
791       await checkUserRegistered(immutableAssign(baseParams, userOverride), 'user_45', 'absence')
792     })
793   })
794
795   describe('New actor follow', function () {
796     let baseParams: CheckerBaseParams
797     let myChannelName = 'super channel name'
798     let myUserName = 'super user name'
799
800     before(async () => {
801       baseParams = {
802         server: servers[0],
803         emails,
804         socketNotifications: userNotifications,
805         token: userAccessToken
806       }
807
808       await updateMyUser({
809         url: servers[0].url,
810         accessToken: servers[0].accessToken,
811         displayName: 'super root name'
812       })
813
814       await updateMyUser({
815         url: servers[0].url,
816         accessToken: userAccessToken,
817         displayName: myUserName
818       })
819
820       await updateMyUser({
821         url: servers[1].url,
822         accessToken: servers[1].accessToken,
823         displayName: 'super root 2 name'
824       })
825
826       await updateVideoChannel(servers[0].url, userAccessToken, 'user_1_channel', { displayName: myChannelName })
827     })
828
829     it('Should notify when a local channel is following one of our channel', async function () {
830       this.timeout(10000)
831
832       await addUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:9001')
833       await waitJobs(servers)
834
835       await checkNewActorFollow(baseParams, 'channel', 'root', 'super root name', myChannelName, 'presence')
836
837       await removeUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:9001')
838     })
839
840     it('Should notify when a remote channel is following one of our channel', async function () {
841       this.timeout(10000)
842
843       await addUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:9001')
844       await waitJobs(servers)
845
846       await checkNewActorFollow(baseParams, 'channel', 'root', 'super root 2 name', myChannelName, 'presence')
847
848       await removeUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:9001')
849     })
850
851     it('Should notify when a local account is following one of our channel', async function () {
852       await addUserSubscription(servers[0].url, servers[0].accessToken, 'user_1@localhost:9001')
853
854       await waitJobs(servers)
855
856       await checkNewActorFollow(baseParams, 'account', 'root', 'super root name', myUserName, 'presence')
857     })
858
859     it('Should notify when a remote account is following one of our channel', async function () {
860       await addUserSubscription(servers[1].url, servers[1].accessToken, 'user_1@localhost:9001')
861
862       await waitJobs(servers)
863
864       await checkNewActorFollow(baseParams, 'account', 'root', 'super root 2 name', myUserName, 'presence')
865     })
866   })
867
868   describe('Mark as read', function () {
869     it('Should mark as read some notifications', async function () {
870       const res = await getUserNotifications(servers[ 0 ].url, userAccessToken, 2, 3)
871       const ids = res.body.data.map(n => n.id)
872
873       await markAsReadNotifications(servers[ 0 ].url, userAccessToken, ids)
874     })
875
876     it('Should have the notifications marked as read', async function () {
877       const res = await getUserNotifications(servers[ 0 ].url, userAccessToken, 0, 10)
878
879       const notifications = res.body.data as UserNotification[]
880       expect(notifications[ 0 ].read).to.be.false
881       expect(notifications[ 1 ].read).to.be.false
882       expect(notifications[ 2 ].read).to.be.true
883       expect(notifications[ 3 ].read).to.be.true
884       expect(notifications[ 4 ].read).to.be.true
885       expect(notifications[ 5 ].read).to.be.false
886     })
887
888     it('Should only list read notifications', async function () {
889       const res = await getUserNotifications(servers[ 0 ].url, userAccessToken, 0, 10, false)
890
891       const notifications = res.body.data as UserNotification[]
892       for (const notification of notifications) {
893         expect(notification.read).to.be.true
894       }
895     })
896
897     it('Should only list unread notifications', async function () {
898       const res = await getUserNotifications(servers[ 0 ].url, userAccessToken, 0, 10, true)
899
900       const notifications = res.body.data as UserNotification[]
901       for (const notification of notifications) {
902         expect(notification.read).to.be.false
903       }
904     })
905
906     it('Should mark as read all notifications', async function () {
907       await markAsReadAllNotifications(servers[ 0 ].url, userAccessToken)
908
909       const res = await getUserNotifications(servers[ 0 ].url, userAccessToken, 0, 10, true)
910
911       expect(res.body.total).to.equal(0)
912       expect(res.body.data).to.have.lengthOf(0)
913     })
914   })
915
916   describe('Notification settings', function () {
917     let baseParams: CheckerBaseParams
918
919     before(() => {
920       baseParams = {
921         server: servers[0],
922         emails,
923         socketNotifications: userNotifications,
924         token: userAccessToken
925       }
926     })
927
928     it('Should not have notifications', async function () {
929       await updateMyNotificationSettings(servers[0].url, userAccessToken, immutableAssign(allNotificationSettings, {
930         newVideoFromSubscription: UserNotificationSettingValue.NONE
931       }))
932
933       {
934         const res = await getMyUserInformation(servers[0].url, userAccessToken)
935         const info = res.body as User
936         expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.NONE)
937       }
938
939       const { name, uuid } = await uploadVideoByLocalAccount(servers)
940
941       const check = { web: true, mail: true }
942       await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence')
943     })
944
945     it('Should only have web notifications', async function () {
946       await updateMyNotificationSettings(servers[0].url, userAccessToken, immutableAssign(allNotificationSettings, {
947         newVideoFromSubscription: UserNotificationSettingValue.WEB
948       }))
949
950       {
951         const res = await getMyUserInformation(servers[0].url, userAccessToken)
952         const info = res.body as User
953         expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.WEB)
954       }
955
956       const { name, uuid } = await uploadVideoByLocalAccount(servers)
957
958       {
959         const check = { mail: true, web: false }
960         await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence')
961       }
962
963       {
964         const check = { mail: false, web: true }
965         await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'presence')
966       }
967     })
968
969     it('Should only have mail notifications', async function () {
970       await updateMyNotificationSettings(servers[0].url, userAccessToken, immutableAssign(allNotificationSettings, {
971         newVideoFromSubscription: UserNotificationSettingValue.EMAIL
972       }))
973
974       {
975         const res = await getMyUserInformation(servers[0].url, userAccessToken)
976         const info = res.body as User
977         expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.EMAIL)
978       }
979
980       const { name, uuid } = await uploadVideoByLocalAccount(servers)
981
982       {
983         const check = { mail: false, web: true }
984         await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence')
985       }
986
987       {
988         const check = { mail: true, web: false }
989         await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'presence')
990       }
991     })
992
993     it('Should have email and web notifications', async function () {
994       await updateMyNotificationSettings(servers[0].url, userAccessToken, immutableAssign(allNotificationSettings, {
995         newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
996       }))
997
998       {
999         const res = await getMyUserInformation(servers[0].url, userAccessToken)
1000         const info = res.body as User
1001         expect(info.notificationSettings.newVideoFromSubscription).to.equal(
1002           UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
1003         )
1004       }
1005
1006       const { name, uuid } = await uploadVideoByLocalAccount(servers)
1007
1008       await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
1009     })
1010   })
1011
1012   after(async function () {
1013     killallServers(servers)
1014   })
1015 })