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