]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tests/api/users/blocklist.ts
Block comments from muted accounts/servers
[github/Chocobozzz/PeerTube.git] / server / tests / api / users / blocklist.ts
index 21b9ae4f801b8b70b307eef763bce01cfe57a456..8c9107a509c3c216aa04ec3410468b920ce5ab0d 100644 (file)
@@ -2,7 +2,7 @@
 
 import * as chai from 'chai'
 import 'mocha'
-import { AccountBlock, ServerBlock, Video } from '../../../../shared/index'
+import { AccountBlock, ServerBlock, Video, UserNotification, UserNotificationType } from '../../../../shared/index'
 import {
   cleanupTests,
   createUser,
@@ -11,7 +11,9 @@ import {
   flushAndRunMultipleServers,
   ServerInfo,
   uploadVideo,
-  userLogin
+  userLogin,
+  follow,
+  unfollow
 } from '../../../../shared/extra-utils/index'
 import { setAccessTokensToServers } from '../../../../shared/extra-utils/users/login'
 import { getVideosList, getVideosListWithToken } from '../../../../shared/extra-utils/videos/videos'
@@ -19,7 +21,8 @@ import {
   addVideoCommentReply,
   addVideoCommentThread,
   getVideoCommentThreads,
-  getVideoThreadComments
+  getVideoThreadComments,
+  findCommentId
 } from '../../../../shared/extra-utils/videos/video-comments'
 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
 import { VideoComment, VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model'
@@ -45,13 +48,13 @@ async function checkAllVideos (url: string, token: string) {
   {
     const res = await getVideosListWithToken(url, token)
 
-    expect(res.body.data).to.have.lengthOf(4)
+    expect(res.body.data).to.have.lengthOf(5)
   }
 
   {
     const res = await getVideosList(url)
 
-    expect(res.body.data).to.have.lengthOf(4)
+    expect(res.body.data).to.have.lengthOf(5)
   }
 }
 
@@ -76,13 +79,15 @@ async function checkCommentNotification (
   check: 'presence' | 'absence'
 ) {
   const resComment = await addVideoCommentThread(comment.server.url, comment.token, comment.videoUUID, comment.text)
-  const threadId = resComment.body.comment.id
+  const created = resComment.body.comment as VideoComment
+  const threadId = created.id
+  const createdAt = created.createdAt
 
   await waitJobs([ mainServer, comment.server ])
 
   const res = await getUserNotifications(mainServer.url, mainServer.accessToken, 0, 30)
-  const commentNotifications = res.body.data
-                                  .filter(n => n.comment && n.comment.id === threadId)
+  const commentNotifications = (res.body.data as UserNotification[])
+                                  .filter(n => n.comment && n.comment.video.uuid === comment.videoUUID && n.createdAt >= createdAt)
 
   if (check === 'presence') expect(commentNotifications).to.have.lengthOf(1)
   else expect(commentNotifications).to.have.lengthOf(0)
@@ -96,6 +101,7 @@ describe('Test blocklist', function () {
   let servers: ServerInfo[]
   let videoUUID1: string
   let videoUUID2: string
+  let videoUUID3: string
   let userToken1: string
   let userModeratorToken: string
   let userToken2: string
@@ -103,7 +109,7 @@ describe('Test blocklist', function () {
   before(async function () {
     this.timeout(60000)
 
-    servers = await flushAndRunMultipleServers(2)
+    servers = await flushAndRunMultipleServers(3)
     await setAccessTokensToServers(servers)
 
     {
@@ -139,7 +145,13 @@ describe('Test blocklist', function () {
       videoUUID2 = res.body.video.uuid
     }
 
+    {
+      const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video 2 server 1' })
+      videoUUID3 = res.body.video.uuid
+    }
+
     await doubleFollow(servers[0], servers[1])
+    await doubleFollow(servers[0], servers[2])
 
     {
       const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID1, 'comment root 1')
@@ -174,7 +186,7 @@ describe('Test blocklist', function () {
         const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
 
         const videos: Video[] = res.body.data
-        expect(videos).to.have.lengthOf(3)
+        expect(videos).to.have.lengthOf(4)
 
         const v = videos.find(v => v.name === 'video user 2')
         expect(v).to.be.undefined
@@ -188,14 +200,14 @@ describe('Test blocklist', function () {
         const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
 
         const videos: Video[] = res.body.data
-        expect(videos).to.have.lengthOf(2)
+        expect(videos).to.have.lengthOf(3)
 
         const v = videos.find(v => v.name === 'video user 1')
         expect(v).to.be.undefined
       })
 
       it('Should hide its comments', async function () {
-        const resThreads = await getVideoCommentThreads(servers[0].url, videoUUID1, 0, 5, '-createdAt', servers[0].accessToken)
+        const resThreads = await getVideoCommentThreads(servers[0].url, videoUUID1, 0, 25, '-createdAt', servers[0].accessToken)
 
         const threads: VideoComment[] = resThreads.body.data
         expect(threads).to.have.lengthOf(1)
@@ -235,10 +247,6 @@ describe('Test blocklist', function () {
         return checkAllVideos(servers[0].url, userToken1)
       })
 
-      it('Should list all the comments with another user', async function () {
-        return checkAllComments(servers[0].url, userToken1, videoUUID1)
-      })
-
       it('Should list blocked accounts', async function () {
         {
           const res = await getAccountBlocklistByAccount(servers[0].url, servers[0].accessToken, 0, 1, 'createdAt')
@@ -269,6 +277,61 @@ describe('Test blocklist', function () {
         }
       })
 
+      it('Should not allow a remote blocked user to comment my videos', async function () {
+        this.timeout(60000)
+
+        {
+          await addVideoCommentThread(servers[1].url, userToken2, videoUUID3, 'comment user 2')
+          await waitJobs(servers)
+
+          await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID3, 'uploader')
+          await waitJobs(servers)
+
+          const commentId = await findCommentId(servers[1].url, videoUUID3, 'uploader')
+          const message = 'reply by user 2'
+          const resReply = await addVideoCommentReply(servers[1].url, userToken2, videoUUID3, commentId, message)
+          await addVideoCommentReply(servers[1].url, servers[1].accessToken, videoUUID3, resReply.body.comment.id, 'another reply')
+
+          await waitJobs(servers)
+        }
+
+        // Server 2 has all the comments
+        {
+          const resThreads = await getVideoCommentThreads(servers[1].url, videoUUID3, 0, 25, '-createdAt')
+          const threads: VideoComment[] = resThreads.body.data
+
+          expect(threads).to.have.lengthOf(2)
+          expect(threads[0].text).to.equal('uploader')
+          expect(threads[1].text).to.equal('comment user 2')
+
+          const resReplies = await getVideoThreadComments(servers[1].url, videoUUID3, threads[0].id)
+
+          const tree: VideoCommentThreadTree = resReplies.body
+          expect(tree.children).to.have.lengthOf(1)
+          expect(tree.children[0].comment.text).to.equal('reply by user 2')
+          expect(tree.children[0].children).to.have.lengthOf(1)
+          expect(tree.children[0].children[0].comment.text).to.equal('another reply')
+        }
+
+        // Server 1 and 3 should only have uploader comments
+        for (const server of [ servers[0], servers[2] ]) {
+          const resThreads = await getVideoCommentThreads(server.url, videoUUID3, 0, 25, '-createdAt')
+          const threads: VideoComment[] = resThreads.body.data
+
+          expect(threads).to.have.lengthOf(1)
+          expect(threads[0].text).to.equal('uploader')
+
+          const resReplies = await getVideoThreadComments(server.url, videoUUID3, threads[0].id)
+
+          const tree: VideoCommentThreadTree = resReplies.body
+          if (server.serverNumber === 1) {
+            expect(tree.children).to.have.lengthOf(0)
+          } else {
+            expect(tree.children).to.have.lengthOf(1)
+          }
+        }
+      })
+
       it('Should unblock the remote account', async function () {
         await removeAccountFromAccountBlocklist(servers[0].url, servers[0].accessToken, 'user2@localhost:' + servers[1].port)
       })
@@ -277,12 +340,37 @@ describe('Test blocklist', function () {
         const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
 
         const videos: Video[] = res.body.data
-        expect(videos).to.have.lengthOf(3)
+        expect(videos).to.have.lengthOf(4)
 
         const v = videos.find(v => v.name === 'video user 2')
         expect(v).not.to.be.undefined
       })
 
+      it('Should display its comments on my video', async function () {
+        for (const server of servers) {
+          const resThreads = await getVideoCommentThreads(server.url, videoUUID3, 0, 25, '-createdAt')
+          const threads: VideoComment[] = resThreads.body.data
+
+          // Server 3 should not have 2 comment threads, because server 1 did not forward the server 2 comment
+          if (server.serverNumber === 3) {
+            expect(threads).to.have.lengthOf(1)
+            continue
+          }
+
+          expect(threads).to.have.lengthOf(2)
+          expect(threads[0].text).to.equal('uploader')
+          expect(threads[1].text).to.equal('comment user 2')
+
+          const resReplies = await getVideoThreadComments(server.url, videoUUID3, threads[0].id)
+
+          const tree: VideoCommentThreadTree = resReplies.body
+          expect(tree.children).to.have.lengthOf(1)
+          expect(tree.children[0].comment.text).to.equal('reply by user 2')
+          expect(tree.children[0].children).to.have.lengthOf(1)
+          expect(tree.children[0].children[0].comment.text).to.equal('another reply')
+        }
+      })
+
       it('Should unblock the local account', async function () {
         await removeAccountFromAccountBlocklist(servers[0].url, servers[0].accessToken, 'user1')
       })
@@ -328,7 +416,7 @@ describe('Test blocklist', function () {
         const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
 
         const videos: Video[] = res.body.data
-        expect(videos).to.have.lengthOf(2)
+        expect(videos).to.have.lengthOf(3)
 
         const v1 = videos.find(v => v.name === 'video user 2')
         const v2 = videos.find(v => v.name === 'video server 2')
@@ -442,7 +530,7 @@ describe('Test blocklist', function () {
           const res = await getVideosListWithToken(servers[0].url, token)
 
           const videos: Video[] = res.body.data
-          expect(videos).to.have.lengthOf(3)
+          expect(videos).to.have.lengthOf(4)
 
           const v = videos.find(v => v.name === 'video user 2')
           expect(v).to.be.undefined
@@ -458,7 +546,7 @@ describe('Test blocklist', function () {
           const res = await getVideosListWithToken(servers[0].url, token)
 
           const videos: Video[] = res.body.data
-          expect(videos).to.have.lengthOf(2)
+          expect(videos).to.have.lengthOf(3)
 
           const v = videos.find(v => v.name === 'video user 1')
           expect(v).to.be.undefined
@@ -467,9 +555,11 @@ describe('Test blocklist', function () {
 
       it('Should hide its comments', async function () {
         for (const token of [ userModeratorToken, servers[0].accessToken ]) {
-          const resThreads = await getVideoCommentThreads(servers[0].url, videoUUID1, 0, 5, '-createdAt', token)
+          const resThreads = await getVideoCommentThreads(servers[0].url, videoUUID1, 0, 20, '-createdAt', token)
+
+          let threads: VideoComment[] = resThreads.body.data
+          threads = threads.filter(t => t.isDeleted === false)
 
-          const threads: VideoComment[] = resThreads.body.data
           expect(threads).to.have.lengthOf(1)
           expect(threads[0].totalReplies).to.equal(0)
 
@@ -543,7 +633,7 @@ describe('Test blocklist', function () {
           const res = await getVideosListWithToken(servers[0].url, token)
 
           const videos: Video[] = res.body.data
-          expect(videos).to.have.lengthOf(3)
+          expect(videos).to.have.lengthOf(4)
 
           const v = videos.find(v => v.name === 'video user 2')
           expect(v).not.to.be.undefined
@@ -604,7 +694,7 @@ describe('Test blocklist', function () {
 
           for (const res of [ res1, res2 ]) {
             const videos: Video[] = res.body.data
-            expect(videos).to.have.lengthOf(2)
+            expect(videos).to.have.lengthOf(3)
 
             const v1 = videos.find(v => v.name === 'video user 2')
             const v2 = videos.find(v => v.name === 'video server 2')
@@ -629,7 +719,7 @@ describe('Test blocklist', function () {
       })
 
       it('Should not have notification from blocked instances by instance', async function () {
-        this.timeout(20000)
+        this.timeout(50000)
 
         {
           const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'hidden comment' }
@@ -645,6 +735,24 @@ describe('Test blocklist', function () {
           }
           await checkCommentNotification(servers[0], comment, 'absence')
         }
+
+        {
+          const now = new Date()
+          await unfollow(servers[1].url, servers[1].accessToken, servers[0])
+          await waitJobs(servers)
+          await follow(servers[1].url, [ servers[0].host ], servers[1].accessToken)
+
+          await waitJobs(servers)
+
+          const res = await getUserNotifications(servers[0].url, servers[0].accessToken, 0, 30)
+          const commentNotifications = (res.body.data as UserNotification[])
+                                          .filter(n => {
+                                            return n.type === UserNotificationType.NEW_INSTANCE_FOLLOWER &&
+                                            n.createdAt >= now.toISOString()
+                                          })
+
+          expect(commentNotifications).to.have.lengthOf(0)
+        }
       })
 
       it('Should list blocked servers', async function () {
@@ -676,7 +784,7 @@ describe('Test blocklist', function () {
       })
 
       it('Should have notification from unblocked instances', async function () {
-        this.timeout(20000)
+        this.timeout(50000)
 
         {
           const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'displayed comment' }
@@ -692,6 +800,24 @@ describe('Test blocklist', function () {
           }
           await checkCommentNotification(servers[0], comment, 'presence')
         }
+
+        {
+          const now = new Date()
+          await unfollow(servers[1].url, servers[1].accessToken, servers[0])
+          await waitJobs(servers)
+          await follow(servers[1].url, [ servers[0].host ], servers[1].accessToken)
+
+          await waitJobs(servers)
+
+          const res = await getUserNotifications(servers[0].url, servers[0].accessToken, 0, 30)
+          const commentNotifications = (res.body.data as UserNotification[])
+                                          .filter(n => {
+                                            return n.type === UserNotificationType.NEW_INSTANCE_FOLLOWER &&
+                                            n.createdAt >= now.toISOString()
+                                          })
+
+          expect(commentNotifications).to.have.lengthOf(1)
+        }
       })
     })
   })