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