]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/extra-utils/users/notifications.ts
Introduce notifications command
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / users / notifications.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
cef534ed 2
cef534ed 3import { expect } from 'chai'
dc133480 4import { inspect } from 'util'
32a18cbf 5import { AbuseState, PluginType } from '@shared/models'
8eb07b01 6import { UserNotification, UserNotificationSetting, UserNotificationSettingValue, UserNotificationType } from '../../models/users'
8ef9457f 7import { MockSmtpServer } from '../mock-servers/mock-email'
8eb07b01
C
8import { doubleFollow } from '../server/follows'
9import { flushAndRunMultipleServers, ServerInfo } from '../server/servers'
8eb07b01
C
10import { setAccessTokensToServers, userLogin } from './login'
11import { createUser, getMyUserInformation } from './users'
cef534ed 12
dd0ebb71
C
13function getAllNotificationsSettings (): UserNotificationSetting {
14 return {
15 newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
16 newCommentOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
17 abuseAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
18 videoAutoBlacklistAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
19 blacklistOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
20 myVideoImportFinished: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
21 myVideoPublished: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
22 commentMention: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
23 newFollow: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
24 newUserRegistration: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
25 newInstanceFollower: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
26 abuseNewMessage: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
27 abuseStateChange: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
28 autoInstanceFollowing: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
29 newPeerTubeVersion: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
30 newPluginVersion: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
31 }
cef534ed
C
32}
33
34type CheckerBaseParams = {
35 server: ServerInfo
8eb07b01 36 emails: any[]
cef534ed 37 socketNotifications: UserNotification[]
a1587156 38 token: string
cef534ed
C
39 check?: { web: boolean, mail: boolean }
40}
41
42type CheckerType = 'presence' | 'absence'
43
44async function checkNotification (
45 base: CheckerBaseParams,
dc133480 46 notificationChecker: (notification: UserNotification, type: CheckerType) => void,
cef534ed 47 emailNotificationFinder: (email: object) => boolean,
dc133480 48 checkType: CheckerType
cef534ed
C
49) {
50 const check = base.check || { web: true, mail: true }
51
52 if (check.web) {
dd0ebb71 53 const notification = await base.server.notificationsCommand.getLastest({ token: base.token })
cef534ed 54
dc133480
C
55 if (notification || checkType !== 'absence') {
56 notificationChecker(notification, checkType)
57 }
cef534ed 58
dc133480
C
59 const socketNotification = base.socketNotifications.find(n => {
60 try {
61 notificationChecker(n, 'presence')
62 return true
63 } catch {
64 return false
65 }
66 })
67
68 if (checkType === 'presence') {
f7cc67b4 69 const obj = inspect(base.socketNotifications, { depth: 5 })
8eb07b01 70 expect(socketNotification, 'The socket notification is absent when it should be present. ' + obj).to.not.be.undefined
dc133480 71 } else {
f7cc67b4 72 const obj = inspect(socketNotification, { depth: 5 })
8eb07b01 73 expect(socketNotification, 'The socket notification is present when it should not be present. ' + obj).to.be.undefined
dc133480 74 }
cef534ed
C
75 }
76
77 if (check.mail) {
78 // Last email
79 const email = base.emails
80 .slice()
81 .reverse()
82 .find(e => emailNotificationFinder(e))
83
dc133480 84 if (checkType === 'presence') {
8eb07b01
C
85 const emails = base.emails.map(e => e.text)
86 expect(email, 'The email is absent when is should be present. ' + inspect(emails)).to.not.be.undefined
dc133480 87 } else {
df4c603d 88 expect(email, 'The email is present when is should not be present. ' + inspect(email)).to.be.undefined
dc133480 89 }
cef534ed
C
90 }
91}
92
dc133480 93function checkVideo (video: any, videoName?: string, videoUUID?: string) {
310b5219
C
94 if (videoName) {
95 expect(video.name).to.be.a('string')
96 expect(video.name).to.not.be.empty
97 expect(video.name).to.equal(videoName)
98 }
dc133480 99
310b5219
C
100 if (videoUUID) {
101 expect(video.uuid).to.be.a('string')
102 expect(video.uuid).to.not.be.empty
103 expect(video.uuid).to.equal(videoUUID)
104 }
dc133480
C
105
106 expect(video.id).to.be.a('number')
107}
108
f7cc67b4
C
109function checkActor (actor: any) {
110 expect(actor.displayName).to.be.a('string')
111 expect(actor.displayName).to.not.be.empty
38967f7b 112 expect(actor.host).to.not.be.undefined
dc133480
C
113}
114
115function checkComment (comment: any, commentId: number, threadId: number) {
116 expect(comment.id).to.equal(commentId)
117 expect(comment.threadId).to.equal(threadId)
118}
119
cef534ed
C
120async function checkNewVideoFromSubscription (base: CheckerBaseParams, videoName: string, videoUUID: string, type: CheckerType) {
121 const notificationType = UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION
122
dc133480 123 function notificationChecker (notification: UserNotification, type: CheckerType) {
cef534ed
C
124 if (type === 'presence') {
125 expect(notification).to.not.be.undefined
126 expect(notification.type).to.equal(notificationType)
dc133480
C
127
128 checkVideo(notification.video, videoName, videoUUID)
129 checkActor(notification.video.channel)
cef534ed 130 } else {
7ccddd7b
JM
131 expect(notification).to.satisfy((n: UserNotification) => {
132 return n === undefined || n.type !== UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION || n.video.name !== videoName
133 })
cef534ed
C
134 }
135 }
136
df4c603d 137 function emailNotificationFinder (email: object) {
a1587156 138 const text = email['text']
7ccddd7b 139 return text.indexOf(videoUUID) !== -1 && text.indexOf('Your subscription') !== -1
dc133480
C
140 }
141
df4c603d 142 await checkNotification(base, notificationChecker, emailNotificationFinder, type)
dc133480
C
143}
144
145async function checkVideoIsPublished (base: CheckerBaseParams, videoName: string, videoUUID: string, type: CheckerType) {
146 const notificationType = UserNotificationType.MY_VIDEO_PUBLISHED
147
148 function notificationChecker (notification: UserNotification, type: CheckerType) {
149 if (type === 'presence') {
150 expect(notification).to.not.be.undefined
151 expect(notification.type).to.equal(notificationType)
152
153 checkVideo(notification.video, videoName, videoUUID)
154 checkActor(notification.video.channel)
155 } else {
156 expect(notification.video).to.satisfy(v => v === undefined || v.name !== videoName)
157 }
cef534ed
C
158 }
159
df4c603d 160 function emailNotificationFinder (email: object) {
a1587156 161 const text: string = email['text']
dc133480 162 return text.includes(videoUUID) && text.includes('Your video')
cef534ed
C
163 }
164
df4c603d 165 await checkNotification(base, notificationChecker, emailNotificationFinder, type)
dc133480
C
166}
167
168async function checkMyVideoImportIsFinished (
169 base: CheckerBaseParams,
170 videoName: string,
171 videoUUID: string,
172 url: string,
173 success: boolean,
174 type: CheckerType
175) {
176 const notificationType = success ? UserNotificationType.MY_VIDEO_IMPORT_SUCCESS : UserNotificationType.MY_VIDEO_IMPORT_ERROR
177
178 function notificationChecker (notification: UserNotification, type: CheckerType) {
179 if (type === 'presence') {
180 expect(notification).to.not.be.undefined
181 expect(notification.type).to.equal(notificationType)
182
183 expect(notification.videoImport.targetUrl).to.equal(url)
184
185 if (success) checkVideo(notification.videoImport.video, videoName, videoUUID)
186 } else {
187 expect(notification.videoImport).to.satisfy(i => i === undefined || i.targetUrl !== url)
188 }
189 }
190
df4c603d 191 function emailNotificationFinder (email: object) {
a1587156 192 const text: string = email['text']
dc133480
C
193 const toFind = success ? ' finished' : ' error'
194
195 return text.includes(url) && text.includes(toFind)
196 }
197
df4c603d 198 await checkNotification(base, notificationChecker, emailNotificationFinder, type)
cef534ed
C
199}
200
f7cc67b4
C
201async function checkUserRegistered (base: CheckerBaseParams, username: string, type: CheckerType) {
202 const notificationType = UserNotificationType.NEW_USER_REGISTRATION
203
204 function notificationChecker (notification: UserNotification, type: CheckerType) {
205 if (type === 'presence') {
206 expect(notification).to.not.be.undefined
207 expect(notification.type).to.equal(notificationType)
208
209 checkActor(notification.account)
210 expect(notification.account.name).to.equal(username)
211 } else {
212 expect(notification).to.satisfy(n => n.type !== notificationType || n.account.name !== username)
213 }
214 }
215
df4c603d 216 function emailNotificationFinder (email: object) {
a1587156 217 const text: string = email['text']
f7cc67b4 218
df4c603d 219 return text.includes(' registered.') && text.includes(username)
f7cc67b4
C
220 }
221
df4c603d 222 await checkNotification(base, notificationChecker, emailNotificationFinder, type)
f7cc67b4
C
223}
224
225async function checkNewActorFollow (
226 base: CheckerBaseParams,
227 followType: 'channel' | 'account',
228 followerName: string,
229 followerDisplayName: string,
230 followingDisplayName: string,
231 type: CheckerType
232) {
233 const notificationType = UserNotificationType.NEW_FOLLOW
234
235 function notificationChecker (notification: UserNotification, type: CheckerType) {
236 if (type === 'presence') {
237 expect(notification).to.not.be.undefined
238 expect(notification.type).to.equal(notificationType)
239
240 checkActor(notification.actorFollow.follower)
241 expect(notification.actorFollow.follower.displayName).to.equal(followerDisplayName)
242 expect(notification.actorFollow.follower.name).to.equal(followerName)
ebff55d8 243 expect(notification.actorFollow.follower.host).to.not.be.undefined
f7cc67b4 244
8424c402
C
245 const following = notification.actorFollow.following
246 expect(following.displayName).to.equal(followingDisplayName)
247 expect(following.type).to.equal(followType)
f7cc67b4
C
248 } else {
249 expect(notification).to.satisfy(n => {
250 return n.type !== notificationType ||
251 (n.actorFollow.follower.name !== followerName && n.actorFollow.following !== followingDisplayName)
252 })
253 }
254 }
255
df4c603d 256 function emailNotificationFinder (email: object) {
a1587156 257 const text: string = email['text']
f7cc67b4 258
df4c603d 259 return text.includes(followType) && text.includes(followingDisplayName) && text.includes(followerDisplayName)
f7cc67b4
C
260 }
261
df4c603d 262 await checkNotification(base, notificationChecker, emailNotificationFinder, type)
f7cc67b4
C
263}
264
883993c8
C
265async function checkNewInstanceFollower (base: CheckerBaseParams, followerHost: string, type: CheckerType) {
266 const notificationType = UserNotificationType.NEW_INSTANCE_FOLLOWER
267
268 function notificationChecker (notification: UserNotification, type: CheckerType) {
269 if (type === 'presence') {
270 expect(notification).to.not.be.undefined
271 expect(notification.type).to.equal(notificationType)
272
273 checkActor(notification.actorFollow.follower)
274 expect(notification.actorFollow.follower.name).to.equal('peertube')
275 expect(notification.actorFollow.follower.host).to.equal(followerHost)
276
277 expect(notification.actorFollow.following.name).to.equal('peertube')
278 } else {
279 expect(notification).to.satisfy(n => {
280 return n.type !== notificationType || n.actorFollow.follower.host !== followerHost
281 })
282 }
283 }
284
df4c603d 285 function emailNotificationFinder (email: object) {
a1587156 286 const text: string = email['text']
883993c8
C
287
288 return text.includes('instance has a new follower') && text.includes(followerHost)
289 }
290
df4c603d 291 await checkNotification(base, notificationChecker, emailNotificationFinder, type)
883993c8
C
292}
293
8424c402
C
294async function checkAutoInstanceFollowing (base: CheckerBaseParams, followerHost: string, followingHost: string, type: CheckerType) {
295 const notificationType = UserNotificationType.AUTO_INSTANCE_FOLLOWING
296
297 function notificationChecker (notification: UserNotification, type: CheckerType) {
298 if (type === 'presence') {
299 expect(notification).to.not.be.undefined
300 expect(notification.type).to.equal(notificationType)
301
302 const following = notification.actorFollow.following
303 checkActor(following)
304 expect(following.name).to.equal('peertube')
305 expect(following.host).to.equal(followingHost)
306
307 expect(notification.actorFollow.follower.name).to.equal('peertube')
308 expect(notification.actorFollow.follower.host).to.equal(followerHost)
309 } else {
310 expect(notification).to.satisfy(n => {
311 return n.type !== notificationType || n.actorFollow.following.host !== followingHost
312 })
313 }
314 }
315
df4c603d 316 function emailNotificationFinder (email: object) {
a1587156 317 const text: string = email['text']
8424c402
C
318
319 return text.includes(' automatically followed a new instance') && text.includes(followingHost)
320 }
321
df4c603d 322 await checkNotification(base, notificationChecker, emailNotificationFinder, type)
8424c402
C
323}
324
f7cc67b4
C
325async function checkCommentMention (
326 base: CheckerBaseParams,
327 uuid: string,
328 commentId: number,
329 threadId: number,
330 byAccountDisplayName: string,
331 type: CheckerType
332) {
333 const notificationType = UserNotificationType.COMMENT_MENTION
334
335 function notificationChecker (notification: UserNotification, type: CheckerType) {
336 if (type === 'presence') {
337 expect(notification).to.not.be.undefined
338 expect(notification.type).to.equal(notificationType)
339
340 checkComment(notification.comment, commentId, threadId)
341 checkActor(notification.comment.account)
342 expect(notification.comment.account.displayName).to.equal(byAccountDisplayName)
343
344 checkVideo(notification.comment.video, undefined, uuid)
345 } else {
346 expect(notification).to.satisfy(n => n.type !== notificationType || n.comment.id !== commentId)
347 }
348 }
349
df4c603d 350 function emailNotificationFinder (email: object) {
a1587156 351 const text: string = email['text']
f7cc67b4
C
352
353 return text.includes(' mentioned ') && text.includes(uuid) && text.includes(byAccountDisplayName)
354 }
355
df4c603d 356 await checkNotification(base, notificationChecker, emailNotificationFinder, type)
f7cc67b4
C
357}
358
cef534ed 359let lastEmailCount = 0
a1587156 360
cef534ed
C
361async function checkNewCommentOnMyVideo (base: CheckerBaseParams, uuid: string, commentId: number, threadId: number, type: CheckerType) {
362 const notificationType = UserNotificationType.NEW_COMMENT_ON_MY_VIDEO
363
dc133480 364 function notificationChecker (notification: UserNotification, type: CheckerType) {
cef534ed
C
365 if (type === 'presence') {
366 expect(notification).to.not.be.undefined
367 expect(notification.type).to.equal(notificationType)
dc133480
C
368
369 checkComment(notification.comment, commentId, threadId)
370 checkActor(notification.comment.account)
371 checkVideo(notification.comment.video, undefined, uuid)
cef534ed
C
372 } else {
373 expect(notification).to.satisfy((n: UserNotification) => {
374 return n === undefined || n.comment === undefined || n.comment.id !== commentId
375 })
376 }
377 }
378
0fc98432 379 const commentUrl = `http://localhost:${base.server.port}/w/${uuid};threadId=${threadId}`
a1587156 380
df4c603d 381 function emailNotificationFinder (email: object) {
a1587156 382 return email['text'].indexOf(commentUrl) !== -1
cef534ed
C
383 }
384
df4c603d 385 await checkNotification(base, notificationChecker, emailNotificationFinder, type)
cef534ed
C
386
387 if (type === 'presence') {
388 // We cannot detect email duplicates, so check we received another email
389 expect(base.emails).to.have.length.above(lastEmailCount)
390 lastEmailCount = base.emails.length
391 }
392}
393
394async function checkNewVideoAbuseForModerators (base: CheckerBaseParams, videoUUID: string, videoName: string, type: CheckerType) {
310b5219 395 const notificationType = UserNotificationType.NEW_ABUSE_FOR_MODERATORS
cef534ed 396
dc133480 397 function notificationChecker (notification: UserNotification, type: CheckerType) {
cef534ed
C
398 if (type === 'presence') {
399 expect(notification).to.not.be.undefined
400 expect(notification.type).to.equal(notificationType)
dc133480 401
d95d1559
C
402 expect(notification.abuse.id).to.be.a('number')
403 checkVideo(notification.abuse.video, videoName, videoUUID)
cef534ed
C
404 } else {
405 expect(notification).to.satisfy((n: UserNotification) => {
d95d1559 406 return n === undefined || n.abuse === undefined || n.abuse.video.uuid !== videoUUID
cef534ed
C
407 })
408 }
409 }
410
df4c603d 411 function emailNotificationFinder (email: object) {
a1587156 412 const text = email['text']
cef534ed
C
413 return text.indexOf(videoUUID) !== -1 && text.indexOf('abuse') !== -1
414 }
415
df4c603d 416 await checkNotification(base, notificationChecker, emailNotificationFinder, type)
cef534ed
C
417}
418
594d3e48
C
419async function checkNewAbuseMessage (base: CheckerBaseParams, abuseId: number, message: string, toEmail: string, type: CheckerType) {
420 const notificationType = UserNotificationType.ABUSE_NEW_MESSAGE
421
422 function notificationChecker (notification: UserNotification, type: CheckerType) {
423 if (type === 'presence') {
424 expect(notification).to.not.be.undefined
425 expect(notification.type).to.equal(notificationType)
426
427 expect(notification.abuse.id).to.equal(abuseId)
428 } else {
429 expect(notification).to.satisfy((n: UserNotification) => {
430 return n === undefined || n.type !== notificationType || n.abuse === undefined || n.abuse.id !== abuseId
431 })
432 }
433 }
434
435 function emailNotificationFinder (email: object) {
436 const text = email['text']
437 const to = email['to'].filter(t => t.address === toEmail)
438
439 return text.indexOf(message) !== -1 && to.length !== 0
440 }
441
442 await checkNotification(base, notificationChecker, emailNotificationFinder, type)
443}
444
445async function checkAbuseStateChange (base: CheckerBaseParams, abuseId: number, state: AbuseState, type: CheckerType) {
446 const notificationType = UserNotificationType.ABUSE_STATE_CHANGE
447
448 function notificationChecker (notification: UserNotification, type: CheckerType) {
449 if (type === 'presence') {
450 expect(notification).to.not.be.undefined
451 expect(notification.type).to.equal(notificationType)
452
453 expect(notification.abuse.id).to.equal(abuseId)
454 expect(notification.abuse.state).to.equal(state)
455 } else {
456 expect(notification).to.satisfy((n: UserNotification) => {
457 return n === undefined || n.abuse === undefined || n.abuse.id !== abuseId
458 })
459 }
460 }
461
462 function emailNotificationFinder (email: object) {
463 const text = email['text']
464
465 const contains = state === AbuseState.ACCEPTED
466 ? ' accepted'
467 : ' rejected'
468
469 return text.indexOf(contains) !== -1
470 }
471
472 await checkNotification(base, notificationChecker, emailNotificationFinder, type)
473}
474
310b5219
C
475async function checkNewCommentAbuseForModerators (base: CheckerBaseParams, videoUUID: string, videoName: string, type: CheckerType) {
476 const notificationType = UserNotificationType.NEW_ABUSE_FOR_MODERATORS
477
478 function notificationChecker (notification: UserNotification, type: CheckerType) {
479 if (type === 'presence') {
480 expect(notification).to.not.be.undefined
481 expect(notification.type).to.equal(notificationType)
482
483 expect(notification.abuse.id).to.be.a('number')
484 checkVideo(notification.abuse.comment.video, videoName, videoUUID)
485 } else {
486 expect(notification).to.satisfy((n: UserNotification) => {
487 return n === undefined || n.abuse === undefined || n.abuse.comment.video.uuid !== videoUUID
488 })
489 }
490 }
491
492 function emailNotificationFinder (email: object) {
493 const text = email['text']
494 return text.indexOf(videoUUID) !== -1 && text.indexOf('abuse') !== -1
495 }
496
497 await checkNotification(base, notificationChecker, emailNotificationFinder, type)
498}
499
500async function checkNewAccountAbuseForModerators (base: CheckerBaseParams, displayName: string, type: CheckerType) {
501 const notificationType = UserNotificationType.NEW_ABUSE_FOR_MODERATORS
502
503 function notificationChecker (notification: UserNotification, type: CheckerType) {
504 if (type === 'presence') {
505 expect(notification).to.not.be.undefined
506 expect(notification.type).to.equal(notificationType)
507
508 expect(notification.abuse.id).to.be.a('number')
509 expect(notification.abuse.account.displayName).to.equal(displayName)
510 } else {
511 expect(notification).to.satisfy((n: UserNotification) => {
512 return n === undefined || n.abuse === undefined || n.abuse.account.displayName !== displayName
513 })
514 }
515 }
516
517 function emailNotificationFinder (email: object) {
518 const text = email['text']
519 return text.indexOf(displayName) !== -1 && text.indexOf('abuse') !== -1
520 }
521
522 await checkNotification(base, notificationChecker, emailNotificationFinder, type)
523}
524
7ccddd7b
JM
525async function checkVideoAutoBlacklistForModerators (base: CheckerBaseParams, videoUUID: string, videoName: string, type: CheckerType) {
526 const notificationType = UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS
527
528 function notificationChecker (notification: UserNotification, type: CheckerType) {
529 if (type === 'presence') {
530 expect(notification).to.not.be.undefined
531 expect(notification.type).to.equal(notificationType)
532
8424c402
C
533 expect(notification.videoBlacklist.video.id).to.be.a('number')
534 checkVideo(notification.videoBlacklist.video, videoName, videoUUID)
7ccddd7b
JM
535 } else {
536 expect(notification).to.satisfy((n: UserNotification) => {
537 return n === undefined || n.video === undefined || n.video.uuid !== videoUUID
538 })
539 }
540 }
541
df4c603d 542 function emailNotificationFinder (email: object) {
a1587156
C
543 const text = email['text']
544 return text.indexOf(videoUUID) !== -1 && email['text'].indexOf('video-auto-blacklist/list') !== -1
7ccddd7b
JM
545 }
546
df4c603d 547 await checkNotification(base, notificationChecker, emailNotificationFinder, type)
7ccddd7b
JM
548}
549
cef534ed
C
550async function checkNewBlacklistOnMyVideo (
551 base: CheckerBaseParams,
552 videoUUID: string,
553 videoName: string,
554 blacklistType: 'blacklist' | 'unblacklist'
555) {
556 const notificationType = blacklistType === 'blacklist'
557 ? UserNotificationType.BLACKLIST_ON_MY_VIDEO
558 : UserNotificationType.UNBLACKLIST_ON_MY_VIDEO
559
dc133480 560 function notificationChecker (notification: UserNotification) {
cef534ed
C
561 expect(notification).to.not.be.undefined
562 expect(notification.type).to.equal(notificationType)
563
564 const video = blacklistType === 'blacklist' ? notification.videoBlacklist.video : notification.video
565
dc133480 566 checkVideo(video, videoName, videoUUID)
cef534ed
C
567 }
568
df4c603d 569 function emailNotificationFinder (email: object) {
a1587156 570 const text = email['text']
cef534ed
C
571 return text.indexOf(videoUUID) !== -1 && text.indexOf(' ' + blacklistType) !== -1
572 }
573
df4c603d 574 await checkNotification(base, notificationChecker, emailNotificationFinder, 'presence')
cef534ed
C
575}
576
32a18cbf
C
577async function checkNewPeerTubeVersion (base: CheckerBaseParams, latestVersion: string, type: CheckerType) {
578 const notificationType = UserNotificationType.NEW_PEERTUBE_VERSION
579
580 function notificationChecker (notification: UserNotification, type: CheckerType) {
581 if (type === 'presence') {
582 expect(notification).to.not.be.undefined
583 expect(notification.type).to.equal(notificationType)
584
585 expect(notification.peertube).to.exist
586 expect(notification.peertube.latestVersion).to.equal(latestVersion)
587 } else {
588 expect(notification).to.satisfy((n: UserNotification) => {
589 return n === undefined || n.peertube === undefined || n.peertube.latestVersion !== latestVersion
590 })
591 }
592 }
593
594 function emailNotificationFinder (email: object) {
595 const text = email['text']
596
597 return text.includes(latestVersion)
598 }
599
600 await checkNotification(base, notificationChecker, emailNotificationFinder, type)
601}
602
603async function checkNewPluginVersion (base: CheckerBaseParams, pluginType: PluginType, pluginName: string, type: CheckerType) {
604 const notificationType = UserNotificationType.NEW_PLUGIN_VERSION
605
606 function notificationChecker (notification: UserNotification, type: CheckerType) {
607 if (type === 'presence') {
608 expect(notification).to.not.be.undefined
609 expect(notification.type).to.equal(notificationType)
610
611 expect(notification.plugin.name).to.equal(pluginName)
612 expect(notification.plugin.type).to.equal(pluginType)
613 } else {
614 expect(notification).to.satisfy((n: UserNotification) => {
615 return n === undefined || n.plugin === undefined || n.plugin.name !== pluginName
616 })
617 }
618 }
619
620 function emailNotificationFinder (email: object) {
621 const text = email['text']
622
623 return text.includes(pluginName)
624 }
625
626 await checkNotification(base, notificationChecker, emailNotificationFinder, type)
627}
628
32a18cbf 629async function prepareNotificationsTest (serversCount = 3, overrideConfigArg: any = {}) {
8eb07b01
C
630 const userNotifications: UserNotification[] = []
631 const adminNotifications: UserNotification[] = []
632 const adminNotificationsServer2: UserNotification[] = []
633 const emails: object[] = []
634
635 const port = await MockSmtpServer.Instance.collectEmails(emails)
636
637 const overrideConfig = {
638 smtp: {
639 hostname: 'localhost',
640 port
310b5219
C
641 },
642 signup: {
643 limit: 20
8eb07b01
C
644 }
645 }
32a18cbf 646 const servers = await flushAndRunMultipleServers(serversCount, Object.assign(overrideConfig, overrideConfigArg))
8eb07b01
C
647
648 await setAccessTokensToServers(servers)
49919ca1
C
649
650 if (serversCount > 1) {
651 await doubleFollow(servers[0], servers[1])
652 }
8eb07b01
C
653
654 const user = {
655 username: 'user_1',
656 password: 'super password'
657 }
658 await createUser({
659 url: servers[0].url,
660 accessToken: servers[0].accessToken,
661 username: user.username,
662 password: user.password,
663 videoQuota: 10 * 1000 * 1000
664 })
665 const userAccessToken = await userLogin(servers[0], user)
666
dd0ebb71
C
667 await servers[0].notificationsCommand.updateMySettings({ token: userAccessToken, settings: getAllNotificationsSettings() })
668 await servers[0].notificationsCommand.updateMySettings({ settings: getAllNotificationsSettings() })
8eb07b01
C
669
670 if (serversCount > 1) {
dd0ebb71 671 await servers[1].notificationsCommand.updateMySettings({ settings: getAllNotificationsSettings() })
8eb07b01
C
672 }
673
674 {
87e2635a 675 const socket = servers[0].socketIOCommand.getUserNotificationSocket({ token: userAccessToken })
8eb07b01
C
676 socket.on('new-notification', n => userNotifications.push(n))
677 }
678 {
87e2635a 679 const socket = servers[0].socketIOCommand.getUserNotificationSocket()
8eb07b01
C
680 socket.on('new-notification', n => adminNotifications.push(n))
681 }
682
683 if (serversCount > 1) {
87e2635a 684 const socket = servers[1].socketIOCommand.getUserNotificationSocket()
8eb07b01
C
685 socket.on('new-notification', n => adminNotificationsServer2.push(n))
686 }
687
688 const resChannel = await getMyUserInformation(servers[0].url, servers[0].accessToken)
689 const channelId = resChannel.body.videoChannels[0].id
690
691 return {
692 userNotifications,
693 adminNotifications,
694 adminNotificationsServer2,
695 userAccessToken,
696 emails,
697 servers,
698 channelId
699 }
700}
701
cef534ed
C
702// ---------------------------------------------------------------------------
703
704export {
dd0ebb71
C
705 getAllNotificationsSettings,
706
cef534ed
C
707 CheckerBaseParams,
708 CheckerType,
709 checkNotification,
dc133480 710 checkMyVideoImportIsFinished,
f7cc67b4 711 checkUserRegistered,
8424c402 712 checkAutoInstanceFollowing,
dc133480 713 checkVideoIsPublished,
cef534ed 714 checkNewVideoFromSubscription,
f7cc67b4 715 checkNewActorFollow,
cef534ed
C
716 checkNewCommentOnMyVideo,
717 checkNewBlacklistOnMyVideo,
f7cc67b4 718 checkCommentMention,
cef534ed 719 checkNewVideoAbuseForModerators,
7ccddd7b 720 checkVideoAutoBlacklistForModerators,
594d3e48
C
721 checkNewAbuseMessage,
722 checkAbuseStateChange,
8eb07b01 723 checkNewInstanceFollower,
310b5219
C
724 prepareNotificationsTest,
725 checkNewCommentAbuseForModerators,
32a18cbf
C
726 checkNewAccountAbuseForModerators,
727 checkNewPeerTubeVersion,
728 checkNewPluginVersion
cef534ed 729}