From 696d83fd1377486dd03cc1bd02a21d9b6ddd9fcd Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 22 May 2020 17:06:26 +0200 Subject: Block comments from muted accounts/servers Add better control for users of comments displayed on their videos: * Do not forward comments from muted remote accounts/servers (muted by the current server or by the video owner) * Do not list threads and hide replies (with their children) of accounts/servers muted by the video owner * Hide from RSS comments of muted accounts/servers by video owners Use case: * Try to limit spam propagation in the federation * Add ability for users to automatically hide comments on their videos from undesirable accounts/servers (the comment section belongs to videomakers, so they choose what's posted there) --- server/tests/api/users/blocklist.ts | 170 ++++++++++++++++++++++++---- server/tests/api/videos/multiple-servers.ts | 9 +- 2 files changed, 151 insertions(+), 28 deletions(-) (limited to 'server/tests/api') diff --git a/server/tests/api/users/blocklist.ts b/server/tests/api/users/blocklist.ts index e37dbb5a4..8c9107a50 100644 --- a/server/tests/api/users/blocklist.ts +++ b/server/tests/api/users/blocklist.ts @@ -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,7 +200,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 v = videos.find(v => v.name === 'video user 1') expect(v).to.be.undefined @@ -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 @@ -545,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 @@ -606,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') @@ -631,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' } @@ -647,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 () { @@ -678,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' } @@ -694,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) + } }) }) }) diff --git a/server/tests/api/videos/multiple-servers.ts b/server/tests/api/videos/multiple-servers.ts index e3029f1ae..d7b04373f 100644 --- a/server/tests/api/videos/multiple-servers.ts +++ b/server/tests/api/videos/multiple-servers.ts @@ -37,7 +37,8 @@ import { addVideoCommentThread, deleteVideoComment, getVideoCommentThreads, - getVideoThreadComments + getVideoThreadComments, + findCommentId } from '../../../../shared/extra-utils/videos/video-comments' import { waitJobs } from '../../../../shared/extra-utils/server/jobs' @@ -773,8 +774,7 @@ describe('Test multiple servers', function () { await waitJobs(servers) { - const res = await getVideoCommentThreads(servers[1].url, videoUUID, 0, 5) - const threadId = res.body.data.find(c => c.text === 'my super first comment').id + const threadId = await findCommentId(servers[1].url, videoUUID, 'my super first comment') const text = 'my super answer to thread 1' await addVideoCommentReply(servers[1].url, servers[1].accessToken, videoUUID, threadId, text) @@ -783,8 +783,7 @@ describe('Test multiple servers', function () { await waitJobs(servers) { - const res1 = await getVideoCommentThreads(servers[2].url, videoUUID, 0, 5) - const threadId = res1.body.data.find(c => c.text === 'my super first comment').id + const threadId = await findCommentId(servers[2].url, videoUUID, 'my super first comment') const res2 = await getVideoThreadComments(servers[2].url, videoUUID, threadId) const childCommentId = res2.body.children[0].comment.id -- cgit v1.2.3