cd3ed2f70639e4a192ec035c38a5d9080af336dd
[oweals/peertube.git] / server / tests / api / notifications / comments-notifications.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import { cleanupTests, getVideoCommentThreads, getVideoThreadComments, updateMyUser, wait } from '../../../../shared/extra-utils'
6 import { ServerInfo, uploadVideo } from '../../../../shared/extra-utils/index'
7 import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email'
8 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
9 import { addAccountToAccountBlocklist, removeAccountFromAccountBlocklist } from '../../../../shared/extra-utils/users/blocklist'
10 import {
11   checkCommentMention,
12   CheckerBaseParams,
13   checkNewCommentOnMyVideo,
14   prepareNotificationsTest
15 } from '../../../../shared/extra-utils/users/user-notifications'
16 import { addVideoCommentReply, addVideoCommentThread } from '../../../../shared/extra-utils/videos/video-comments'
17 import { UserNotification } from '../../../../shared/models/users'
18 import { VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model'
19
20 const expect = chai.expect
21
22 describe('Test comments notifications', function () {
23   let servers: ServerInfo[] = []
24   let userAccessToken: string
25   let userNotifications: UserNotification[] = []
26   let emails: object[] = []
27
28   before(async function () {
29     this.timeout(120000)
30
31     const res = await prepareNotificationsTest(2)
32     emails = res.emails
33     userAccessToken = res.userAccessToken
34     servers = res.servers
35     userNotifications = res.userNotifications
36   })
37
38   describe('Comment on my video notifications', function () {
39     let baseParams: CheckerBaseParams
40
41     before(() => {
42       baseParams = {
43         server: servers[0],
44         emails,
45         socketNotifications: userNotifications,
46         token: userAccessToken
47       }
48     })
49
50     it('Should not send a new comment notification after a comment on another video', async function () {
51       this.timeout(10000)
52
53       const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' })
54       const uuid = resVideo.body.video.uuid
55
56       const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment')
57       const commentId = resComment.body.comment.id
58
59       await wait(500)
60       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence')
61     })
62
63     it('Should not send a new comment notification if I comment my own video', async function () {
64       this.timeout(10000)
65
66       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
67       const uuid = resVideo.body.video.uuid
68
69       const resComment = await addVideoCommentThread(servers[0].url, userAccessToken, uuid, 'comment')
70       const commentId = resComment.body.comment.id
71
72       await wait(500)
73       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence')
74     })
75
76     it('Should not send a new comment notification if the account is muted', async function () {
77       this.timeout(10000)
78
79       await addAccountToAccountBlocklist(servers[0].url, userAccessToken, 'root')
80
81       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
82       const uuid = resVideo.body.video.uuid
83
84       const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment')
85       const commentId = resComment.body.comment.id
86
87       await wait(500)
88       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence')
89
90       await removeAccountFromAccountBlocklist(servers[0].url, userAccessToken, 'root')
91     })
92
93     it('Should send a new comment notification after a local comment on my video', async function () {
94       this.timeout(10000)
95
96       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
97       const uuid = resVideo.body.video.uuid
98
99       const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment')
100       const commentId = resComment.body.comment.id
101
102       await wait(500)
103       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'presence')
104     })
105
106     it('Should send a new comment notification after a remote comment on my video', async function () {
107       this.timeout(10000)
108
109       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
110       const uuid = resVideo.body.video.uuid
111
112       await waitJobs(servers)
113
114       await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, 'comment')
115
116       await waitJobs(servers)
117
118       const resComment = await getVideoCommentThreads(servers[0].url, uuid, 0, 5)
119       expect(resComment.body.data).to.have.lengthOf(1)
120       const commentId = resComment.body.data[0].id
121
122       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'presence')
123     })
124
125     it('Should send a new comment notification after a local reply on my video', async function () {
126       this.timeout(10000)
127
128       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
129       const uuid = resVideo.body.video.uuid
130
131       const resThread = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment')
132       const threadId = resThread.body.comment.id
133
134       const resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, uuid, threadId, 'reply')
135       const commentId = resComment.body.comment.id
136
137       await wait(500)
138       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, threadId, 'presence')
139     })
140
141     it('Should send a new comment notification after a remote reply on my video', async function () {
142       this.timeout(10000)
143
144       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
145       const uuid = resVideo.body.video.uuid
146       await waitJobs(servers)
147
148       {
149         const resThread = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, 'comment')
150         const threadId = resThread.body.comment.id
151         await addVideoCommentReply(servers[1].url, servers[1].accessToken, uuid, threadId, 'reply')
152       }
153
154       await waitJobs(servers)
155
156       const resThread = await getVideoCommentThreads(servers[0].url, uuid, 0, 5)
157       expect(resThread.body.data).to.have.lengthOf(1)
158       const threadId = resThread.body.data[0].id
159
160       const resComments = await getVideoThreadComments(servers[0].url, uuid, threadId)
161       const tree = resComments.body as VideoCommentThreadTree
162
163       expect(tree.children).to.have.lengthOf(1)
164       const commentId = tree.children[0].comment.id
165
166       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, threadId, 'presence')
167     })
168   })
169
170   describe('Mention notifications', function () {
171     let baseParams: CheckerBaseParams
172
173     before(async () => {
174       baseParams = {
175         server: servers[0],
176         emails,
177         socketNotifications: userNotifications,
178         token: userAccessToken
179       }
180
181       await updateMyUser({
182         url: servers[0].url,
183         accessToken: servers[0].accessToken,
184         displayName: 'super root name'
185       })
186
187       await updateMyUser({
188         url: servers[1].url,
189         accessToken: servers[1].accessToken,
190         displayName: 'super root 2 name'
191       })
192     })
193
194     it('Should not send a new mention comment notification if I mention the video owner', async function () {
195       this.timeout(10000)
196
197       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
198       const uuid = resVideo.body.video.uuid
199
200       const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello')
201       const commentId = resComment.body.comment.id
202
203       await wait(500)
204       await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence')
205     })
206
207     it('Should not send a new mention comment notification if I mention myself', async function () {
208       this.timeout(10000)
209
210       const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' })
211       const uuid = resVideo.body.video.uuid
212
213       const resComment = await addVideoCommentThread(servers[0].url, userAccessToken, uuid, '@user_1 hello')
214       const commentId = resComment.body.comment.id
215
216       await wait(500)
217       await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence')
218     })
219
220     it('Should not send a new mention notification if the account is muted', async function () {
221       this.timeout(10000)
222
223       await addAccountToAccountBlocklist(servers[0].url, userAccessToken, 'root')
224
225       const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' })
226       const uuid = resVideo.body.video.uuid
227
228       const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello')
229       const commentId = resComment.body.comment.id
230
231       await wait(500)
232       await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence')
233
234       await removeAccountFromAccountBlocklist(servers[0].url, userAccessToken, 'root')
235     })
236
237     it('Should not send a new mention notification if the remote account mention a local account', async function () {
238       this.timeout(20000)
239
240       const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' })
241       const uuid = resVideo.body.video.uuid
242
243       await waitJobs(servers)
244       const resThread = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, '@user_1 hello')
245       const threadId = resThread.body.comment.id
246
247       await waitJobs(servers)
248       await checkCommentMention(baseParams, uuid, threadId, threadId, 'super root 2 name', 'absence')
249     })
250
251     it('Should send a new mention notification after local comments', async function () {
252       this.timeout(10000)
253
254       const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' })
255       const uuid = resVideo.body.video.uuid
256
257       const resThread = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello 1')
258       const threadId = resThread.body.comment.id
259
260       await wait(500)
261       await checkCommentMention(baseParams, uuid, threadId, threadId, 'super root name', 'presence')
262
263       const resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, uuid, threadId, 'hello 2 @user_1')
264       const commentId = resComment.body.comment.id
265
266       await wait(500)
267       await checkCommentMention(baseParams, uuid, commentId, threadId, 'super root name', 'presence')
268     })
269
270     it('Should send a new mention notification after remote comments', async function () {
271       this.timeout(20000)
272
273       const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' })
274       const uuid = resVideo.body.video.uuid
275
276       await waitJobs(servers)
277
278       const text1 = `hello @user_1@localhost:${servers[0].port} 1`
279       const resThread = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, text1)
280       const server2ThreadId = resThread.body.comment.id
281
282       await waitJobs(servers)
283
284       const resThread2 = await getVideoCommentThreads(servers[0].url, uuid, 0, 5)
285       expect(resThread2.body.data).to.have.lengthOf(1)
286       const server1ThreadId = resThread2.body.data[0].id
287       await checkCommentMention(baseParams, uuid, server1ThreadId, server1ThreadId, 'super root 2 name', 'presence')
288
289       const text2 = `@user_1@localhost:${servers[0].port} hello 2 @root@localhost:${servers[0].port}`
290       await addVideoCommentReply(servers[1].url, servers[1].accessToken, uuid, server2ThreadId, text2)
291
292       await waitJobs(servers)
293
294       const resComments = await getVideoThreadComments(servers[0].url, uuid, server1ThreadId)
295       const tree = resComments.body as VideoCommentThreadTree
296
297       expect(tree.children).to.have.lengthOf(1)
298       const commentId = tree.children[0].comment.id
299
300       await checkCommentMention(baseParams, uuid, commentId, server1ThreadId, 'super root 2 name', 'presence')
301     })
302   })
303
304   after(async function () {
305     MockSmtpServer.Instance.kill()
306
307     await cleanupTests(servers)
308   })
309 })