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