1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { makeGetRequest, makePostBodyRequest, makePutBodyRequest } from '../requests/requests'
4 import { UserNotification, UserNotificationSetting, UserNotificationType } from '../../models/users'
5 import { ServerInfo } from '..'
6 import { expect } from 'chai'
7 import { inspect } from 'util'
9 function updateMyNotificationSettings (url: string, token: string, settings: UserNotificationSetting, statusCodeExpected = 204) {
10 const path = '/api/v1/users/me/notification-settings'
12 return makePutBodyRequest({
21 async function getUserNotifications (
28 statusCodeExpected = 200
30 const path = '/api/v1/users/me/notifications'
32 return makeGetRequest({
46 function markAsReadNotifications (url: string, token: string, ids: number[], statusCodeExpected = 204) {
47 const path = '/api/v1/users/me/notifications/read'
49 return makePostBodyRequest({
58 function markAsReadAllNotifications (url: string, token: string, statusCodeExpected = 204) {
59 const path = '/api/v1/users/me/notifications/read-all'
61 return makePostBodyRequest({
69 async function getLastNotification (serverUrl: string, accessToken: string) {
70 const res = await getUserNotifications(serverUrl, accessToken, 0, 1, undefined, '-createdAt')
72 if (res.body.total === 0) return undefined
74 return res.body.data[0] as UserNotification
77 type CheckerBaseParams = {
80 socketNotifications: UserNotification[]
82 check?: { web: boolean, mail: boolean }
85 type CheckerType = 'presence' | 'absence'
87 async function checkNotification (
88 base: CheckerBaseParams,
89 notificationChecker: (notification: UserNotification, type: CheckerType) => void,
90 emailNotificationFinder: (email: object) => boolean,
91 checkType: CheckerType
93 const check = base.check || { web: true, mail: true }
96 const notification = await getLastNotification(base.server.url, base.token)
98 if (notification || checkType !== 'absence') {
99 notificationChecker(notification, checkType)
102 const socketNotification = base.socketNotifications.find(n => {
104 notificationChecker(n, 'presence')
111 if (checkType === 'presence') {
112 const obj = inspect(base.socketNotifications, { depth: 5 })
113 expect(socketNotification, 'The socket notification is absent. ' + obj).to.not.be.undefined
115 const obj = inspect(socketNotification, { depth: 5 })
116 expect(socketNotification, 'The socket notification is present. ' + obj).to.be.undefined
122 const email = base.emails
125 .find(e => emailNotificationFinder(e))
127 if (checkType === 'presence') {
128 expect(email, 'The email is absent. ' + inspect(base.emails)).to.not.be.undefined
130 expect(email, 'The email is present. ' + inspect(email)).to.be.undefined
135 function checkVideo (video: any, videoName?: string, videoUUID?: string) {
136 expect(video.name).to.be.a('string')
137 expect(video.name).to.not.be.empty
138 if (videoName) expect(video.name).to.equal(videoName)
140 expect(video.uuid).to.be.a('string')
141 expect(video.uuid).to.not.be.empty
142 if (videoUUID) expect(video.uuid).to.equal(videoUUID)
144 expect(video.id).to.be.a('number')
147 function checkActor (actor: any) {
148 expect(actor.displayName).to.be.a('string')
149 expect(actor.displayName).to.not.be.empty
150 expect(actor.host).to.not.be.undefined
153 function checkComment (comment: any, commentId: number, threadId: number) {
154 expect(comment.id).to.equal(commentId)
155 expect(comment.threadId).to.equal(threadId)
158 async function checkNewVideoFromSubscription (base: CheckerBaseParams, videoName: string, videoUUID: string, type: CheckerType) {
159 const notificationType = UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION
161 function notificationChecker (notification: UserNotification, type: CheckerType) {
162 if (type === 'presence') {
163 expect(notification).to.not.be.undefined
164 expect(notification.type).to.equal(notificationType)
166 checkVideo(notification.video, videoName, videoUUID)
167 checkActor(notification.video.channel)
169 expect(notification).to.satisfy((n: UserNotification) => {
170 return n === undefined || n.type !== UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION || n.video.name !== videoName
175 function emailFinder (email: object) {
176 const text = email['text']
177 return text.indexOf(videoUUID) !== -1 && text.indexOf('Your subscription') !== -1
180 await checkNotification(base, notificationChecker, emailFinder, type)
183 async function checkVideoIsPublished (base: CheckerBaseParams, videoName: string, videoUUID: string, type: CheckerType) {
184 const notificationType = UserNotificationType.MY_VIDEO_PUBLISHED
186 function notificationChecker (notification: UserNotification, type: CheckerType) {
187 if (type === 'presence') {
188 expect(notification).to.not.be.undefined
189 expect(notification.type).to.equal(notificationType)
191 checkVideo(notification.video, videoName, videoUUID)
192 checkActor(notification.video.channel)
194 expect(notification.video).to.satisfy(v => v === undefined || v.name !== videoName)
198 function emailFinder (email: object) {
199 const text: string = email['text']
200 return text.includes(videoUUID) && text.includes('Your video')
203 await checkNotification(base, notificationChecker, emailFinder, type)
206 async function checkMyVideoImportIsFinished (
207 base: CheckerBaseParams,
214 const notificationType = success ? UserNotificationType.MY_VIDEO_IMPORT_SUCCESS : UserNotificationType.MY_VIDEO_IMPORT_ERROR
216 function notificationChecker (notification: UserNotification, type: CheckerType) {
217 if (type === 'presence') {
218 expect(notification).to.not.be.undefined
219 expect(notification.type).to.equal(notificationType)
221 expect(notification.videoImport.targetUrl).to.equal(url)
223 if (success) checkVideo(notification.videoImport.video, videoName, videoUUID)
225 expect(notification.videoImport).to.satisfy(i => i === undefined || i.targetUrl !== url)
229 function emailFinder (email: object) {
230 const text: string = email['text']
231 const toFind = success ? ' finished' : ' error'
233 return text.includes(url) && text.includes(toFind)
236 await checkNotification(base, notificationChecker, emailFinder, type)
239 async function checkUserRegistered (base: CheckerBaseParams, username: string, type: CheckerType) {
240 const notificationType = UserNotificationType.NEW_USER_REGISTRATION
242 function notificationChecker (notification: UserNotification, type: CheckerType) {
243 if (type === 'presence') {
244 expect(notification).to.not.be.undefined
245 expect(notification.type).to.equal(notificationType)
247 checkActor(notification.account)
248 expect(notification.account.name).to.equal(username)
250 expect(notification).to.satisfy(n => n.type !== notificationType || n.account.name !== username)
254 function emailFinder (email: object) {
255 const text: string = email['text']
257 return text.includes(' registered ') && text.includes(username)
260 await checkNotification(base, notificationChecker, emailFinder, type)
263 async function checkNewActorFollow (
264 base: CheckerBaseParams,
265 followType: 'channel' | 'account',
266 followerName: string,
267 followerDisplayName: string,
268 followingDisplayName: string,
271 const notificationType = UserNotificationType.NEW_FOLLOW
273 function notificationChecker (notification: UserNotification, type: CheckerType) {
274 if (type === 'presence') {
275 expect(notification).to.not.be.undefined
276 expect(notification.type).to.equal(notificationType)
278 checkActor(notification.actorFollow.follower)
279 expect(notification.actorFollow.follower.displayName).to.equal(followerDisplayName)
280 expect(notification.actorFollow.follower.name).to.equal(followerName)
281 expect(notification.actorFollow.follower.host).to.not.be.undefined
283 const following = notification.actorFollow.following
284 expect(following.displayName).to.equal(followingDisplayName)
285 expect(following.type).to.equal(followType)
287 expect(notification).to.satisfy(n => {
288 return n.type !== notificationType ||
289 (n.actorFollow.follower.name !== followerName && n.actorFollow.following !== followingDisplayName)
294 function emailFinder (email: object) {
295 const text: string = email['text']
297 return text.includes('Your ' + followType) && text.includes(followingDisplayName) && text.includes(followerDisplayName)
300 await checkNotification(base, notificationChecker, emailFinder, type)
303 async function checkNewInstanceFollower (base: CheckerBaseParams, followerHost: string, type: CheckerType) {
304 const notificationType = UserNotificationType.NEW_INSTANCE_FOLLOWER
306 function notificationChecker (notification: UserNotification, type: CheckerType) {
307 if (type === 'presence') {
308 expect(notification).to.not.be.undefined
309 expect(notification.type).to.equal(notificationType)
311 checkActor(notification.actorFollow.follower)
312 expect(notification.actorFollow.follower.name).to.equal('peertube')
313 expect(notification.actorFollow.follower.host).to.equal(followerHost)
315 expect(notification.actorFollow.following.name).to.equal('peertube')
317 expect(notification).to.satisfy(n => {
318 return n.type !== notificationType || n.actorFollow.follower.host !== followerHost
323 function emailFinder (email: object) {
324 const text: string = email['text']
326 return text.includes('instance has a new follower') && text.includes(followerHost)
329 await checkNotification(base, notificationChecker, emailFinder, type)
332 async function checkAutoInstanceFollowing (base: CheckerBaseParams, followerHost: string, followingHost: string, type: CheckerType) {
333 const notificationType = UserNotificationType.AUTO_INSTANCE_FOLLOWING
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)
340 const following = notification.actorFollow.following
341 checkActor(following)
342 expect(following.name).to.equal('peertube')
343 expect(following.host).to.equal(followingHost)
345 expect(notification.actorFollow.follower.name).to.equal('peertube')
346 expect(notification.actorFollow.follower.host).to.equal(followerHost)
348 expect(notification).to.satisfy(n => {
349 return n.type !== notificationType || n.actorFollow.following.host !== followingHost
354 function emailFinder (email: object) {
355 const text: string = email['text']
357 return text.includes(' automatically followed a new instance') && text.includes(followingHost)
360 await checkNotification(base, notificationChecker, emailFinder, type)
363 async function checkCommentMention (
364 base: CheckerBaseParams,
368 byAccountDisplayName: string,
371 const notificationType = UserNotificationType.COMMENT_MENTION
373 function notificationChecker (notification: UserNotification, type: CheckerType) {
374 if (type === 'presence') {
375 expect(notification).to.not.be.undefined
376 expect(notification.type).to.equal(notificationType)
378 checkComment(notification.comment, commentId, threadId)
379 checkActor(notification.comment.account)
380 expect(notification.comment.account.displayName).to.equal(byAccountDisplayName)
382 checkVideo(notification.comment.video, undefined, uuid)
384 expect(notification).to.satisfy(n => n.type !== notificationType || n.comment.id !== commentId)
388 function emailFinder (email: object) {
389 const text: string = email['text']
391 return text.includes(' mentioned ') && text.includes(uuid) && text.includes(byAccountDisplayName)
394 await checkNotification(base, notificationChecker, emailFinder, type)
397 let lastEmailCount = 0
399 async function checkNewCommentOnMyVideo (base: CheckerBaseParams, uuid: string, commentId: number, threadId: number, type: CheckerType) {
400 const notificationType = UserNotificationType.NEW_COMMENT_ON_MY_VIDEO
402 function notificationChecker (notification: UserNotification, type: CheckerType) {
403 if (type === 'presence') {
404 expect(notification).to.not.be.undefined
405 expect(notification.type).to.equal(notificationType)
407 checkComment(notification.comment, commentId, threadId)
408 checkActor(notification.comment.account)
409 checkVideo(notification.comment.video, undefined, uuid)
411 expect(notification).to.satisfy((n: UserNotification) => {
412 return n === undefined || n.comment === undefined || n.comment.id !== commentId
417 const commentUrl = `http://localhost:${base.server.port}/videos/watch/${uuid};threadId=${threadId}`
419 function emailFinder (email: object) {
420 return email['text'].indexOf(commentUrl) !== -1
423 await checkNotification(base, notificationChecker, emailFinder, type)
425 if (type === 'presence') {
426 // We cannot detect email duplicates, so check we received another email
427 expect(base.emails).to.have.length.above(lastEmailCount)
428 lastEmailCount = base.emails.length
432 async function checkNewVideoAbuseForModerators (base: CheckerBaseParams, videoUUID: string, videoName: string, type: CheckerType) {
433 const notificationType = UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS
435 function notificationChecker (notification: UserNotification, type: CheckerType) {
436 if (type === 'presence') {
437 expect(notification).to.not.be.undefined
438 expect(notification.type).to.equal(notificationType)
440 expect(notification.videoAbuse.id).to.be.a('number')
441 checkVideo(notification.videoAbuse.video, videoName, videoUUID)
443 expect(notification).to.satisfy((n: UserNotification) => {
444 return n === undefined || n.videoAbuse === undefined || n.videoAbuse.video.uuid !== videoUUID
449 function emailFinder (email: object) {
450 const text = email['text']
451 return text.indexOf(videoUUID) !== -1 && text.indexOf('abuse') !== -1
454 await checkNotification(base, notificationChecker, emailFinder, type)
457 async function checkVideoAutoBlacklistForModerators (base: CheckerBaseParams, videoUUID: string, videoName: string, type: CheckerType) {
458 const notificationType = UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS
460 function notificationChecker (notification: UserNotification, type: CheckerType) {
461 if (type === 'presence') {
462 expect(notification).to.not.be.undefined
463 expect(notification.type).to.equal(notificationType)
465 expect(notification.videoBlacklist.video.id).to.be.a('number')
466 checkVideo(notification.videoBlacklist.video, videoName, videoUUID)
468 expect(notification).to.satisfy((n: UserNotification) => {
469 return n === undefined || n.video === undefined || n.video.uuid !== videoUUID
474 function emailFinder (email: object) {
475 const text = email['text']
476 return text.indexOf(videoUUID) !== -1 && email['text'].indexOf('video-auto-blacklist/list') !== -1
479 await checkNotification(base, notificationChecker, emailFinder, type)
482 async function checkNewBlacklistOnMyVideo (
483 base: CheckerBaseParams,
486 blacklistType: 'blacklist' | 'unblacklist'
488 const notificationType = blacklistType === 'blacklist'
489 ? UserNotificationType.BLACKLIST_ON_MY_VIDEO
490 : UserNotificationType.UNBLACKLIST_ON_MY_VIDEO
492 function notificationChecker (notification: UserNotification) {
493 expect(notification).to.not.be.undefined
494 expect(notification.type).to.equal(notificationType)
496 const video = blacklistType === 'blacklist' ? notification.videoBlacklist.video : notification.video
498 checkVideo(video, videoName, videoUUID)
501 function emailFinder (email: object) {
502 const text = email['text']
503 return text.indexOf(videoUUID) !== -1 && text.indexOf(' ' + blacklistType) !== -1
506 await checkNotification(base, notificationChecker, emailFinder, 'presence')
509 // ---------------------------------------------------------------------------
515 markAsReadAllNotifications,
516 checkMyVideoImportIsFinished,
518 checkAutoInstanceFollowing,
519 checkVideoIsPublished,
520 checkNewVideoFromSubscription,
522 checkNewCommentOnMyVideo,
523 checkNewBlacklistOnMyVideo,
525 updateMyNotificationSettings,
526 checkNewVideoAbuseForModerators,
527 checkVideoAutoBlacklistForModerators,
528 getUserNotifications,
529 markAsReadNotifications,
531 checkNewInstanceFollower