From: Chocobozzz Date: Fri, 9 Jul 2021 12:15:11 +0000 (+0200) Subject: Introduce comments command X-Git-Tag: v3.4.0-rc.1~200^2~20 X-Git-Url: https://git.immae.eu/?a=commitdiff_plain;h=12edc1495a36b2199f1bf1ba37f50c7b694be382;hp=a54618880c394ad7571f3f3222dc96ec2dd10d9a;p=github%2FChocobozzz%2FPeerTube.git Introduce comments command --- diff --git a/scripts/benchmark.ts b/scripts/benchmark.ts index fcfc67bf7..321b07c94 100644 --- a/scripts/benchmark.ts +++ b/scripts/benchmark.ts @@ -1,19 +1,10 @@ -import { registerTSPaths } from '../server/helpers/register-ts-paths' -registerTSPaths() - import * as autocannon from 'autocannon' -import { - addVideoCommentReply, - addVideoCommentThread, - flushAndRunServer, - getVideosList, - killallServers, - ServerInfo, - setAccessTokensToServers, - uploadVideo -} from '@shared/extra-utils' -import { Video, VideoPrivacy } from '@shared/models' import { writeJson } from 'fs-extra' +import { flushAndRunServer, getVideosList, killallServers, ServerInfo, setAccessTokensToServers, uploadVideo } from '@shared/extra-utils' +import { Video, VideoPrivacy } from '@shared/models' +import { registerTSPaths } from '../server/helpers/register-ts-paths' + +registerTSPaths() let server: ServerInfo let video: Video @@ -228,18 +219,17 @@ async function prepare () { for (let i = 0; i < 10; i++) { const text = 'my super first comment' - const res = await addVideoCommentThread(server.url, server.accessToken, video.id, text) - threadId = res.body.comment.id + const created = await server.commentsCommand.createThread({ videoId: video.id, text }) + threadId = created.id const text1 = 'my super answer to thread 1' - const childCommentRes = await addVideoCommentReply(server.url, server.accessToken, video.id, threadId, text1) - const childCommentId = childCommentRes.body.comment.id + const child = await server.commentsCommand.addReply({ videoId: video.id, toCommentId: threadId, text: text1 }) const text2 = 'my super answer to answer of thread 1' - await addVideoCommentReply(server.url, server.accessToken, video.id, childCommentId, text2) + await server.commentsCommand.addReply({ videoId: video.id, toCommentId: child.id, text: text2 }) const text3 = 'my second answer to thread 1' - await addVideoCommentReply(server.url, server.accessToken, video.id, threadId, text3) + await server.commentsCommand.addReply({ videoId: video.id, toCommentId: threadId, text: text3 }) } for (const caption of [ 'ar', 'fr', 'en', 'zh' ]) { diff --git a/server/controllers/api/videos/comment.ts b/server/controllers/api/videos/comment.ts index e6f28c1cb..6a25511e5 100644 --- a/server/controllers/api/videos/comment.ts +++ b/server/controllers/api/videos/comment.ts @@ -1,7 +1,7 @@ import * as express from 'express' import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' -import { ResultList, ThreadsResultList, UserRight } from '../../../../shared/models' -import { VideoCommentCreate } from '../../../../shared/models/videos/comment/video-comment.model' +import { ResultList, ThreadsResultList, UserRight, VideoCommentCreate } from '../../../../shared/models' +import { VideoCommentThreads } from '../../../../shared/models/videos/comment/video-comment.model' import { auditLoggerFactory, CommentAuditView, getAuditIdFromRes } from '../../../helpers/audit-logger' import { getFormattedObjects } from '../../../helpers/utils' import { sequelizeTypescript } from '../../../initializers/database' @@ -136,7 +136,7 @@ async function listVideoThreads (req: express.Request, res: express.Response) { return res.json({ ...getFormattedObjects(resultList.data, resultList.total), totalNotDeletedComments: resultList.totalNotDeletedComments - }) + } as VideoCommentThreads) } async function listVideoThreadComments (req: express.Request, res: express.Response) { diff --git a/server/lib/moderation.ts b/server/lib/moderation.ts index 14e00518e..a42ab5b7f 100644 --- a/server/lib/moderation.ts +++ b/server/lib/moderation.ts @@ -23,7 +23,7 @@ import { ActivityCreate } from '../../shared/models/activitypub' import { VideoObject } from '../../shared/models/activitypub/objects' import { VideoCommentObject } from '../../shared/models/activitypub/objects/video-comment-object' import { LiveVideoCreate, VideoCreate, VideoImportCreate } from '../../shared/models/videos' -import { VideoCommentCreate } from '../../shared/models/videos/comment/video-comment.model' +import { VideoCommentCreate } from '../../shared/models/videos/comment' import { ActorModel } from '../models/actor/actor' import { UserModel } from '../models/user/user' import { VideoModel } from '../models/video/video' diff --git a/server/tests/api/activitypub/cleaner.ts b/server/tests/api/activitypub/cleaner.ts index 75ef56ce3..27f17b4d6 100644 --- a/server/tests/api/activitypub/cleaner.ts +++ b/server/tests/api/activitypub/cleaner.ts @@ -7,16 +7,19 @@ import { closeAllSequelize, deleteAll, doubleFollow, + flushAndRunMultipleServers, getCount, + getVideo, + rateVideo, selectQuery, + ServerInfo, + setAccessTokensToServers, setVideoField, updateQuery, - wait -} from '../../../../shared/extra-utils' -import { flushAndRunMultipleServers, ServerInfo, setAccessTokensToServers } from '../../../../shared/extra-utils/index' -import { waitJobs } from '../../../../shared/extra-utils/server/jobs' -import { addVideoCommentThread, getVideoCommentThreads } from '../../../../shared/extra-utils/videos/video-comments' -import { getVideo, rateVideo, uploadVideoAndGetId } from '../../../../shared/extra-utils/videos/videos' + uploadVideoAndGetId, + wait, + waitJobs +} from '@shared/extra-utils' const expect = chai.expect @@ -63,7 +66,7 @@ describe('Test AP cleaner', function () { for (const server of servers) { for (const uuid of videoUUIDs) { await rateVideo(server.url, server.accessToken, uuid, 'like') - await addVideoCommentThread(server.url, server.accessToken, uuid, 'comment') + await server.commentsCommand.createThread({ videoId: uuid, text: 'comment' }) } } @@ -172,8 +175,8 @@ describe('Test AP cleaner', function () { this.timeout(20000) { - const res = await getVideoCommentThreads(servers[0].url, videoUUID1, 0, 5) - expect(res.body.total).to.equal(3) + const { total } = await servers[0].commentsCommand.listThreads({ videoId: videoUUID1 }) + expect(total).to.equal(3) } await deleteAll(servers[2].internalServerNumber, 'videoComment') @@ -182,8 +185,8 @@ describe('Test AP cleaner', function () { await waitJobs(servers) { - const res = await getVideoCommentThreads(servers[0].url, videoUUID1, 0, 5) - expect(res.body.total).to.equal(2) + const { total } = await servers[0].commentsCommand.listThreads({ videoId: videoUUID1 }) + expect(total).to.equal(2) } }) diff --git a/server/tests/api/check-params/video-comments.ts b/server/tests/api/check-params/video-comments.ts index a38420851..ff94645cb 100644 --- a/server/tests/api/check-params/video-comments.ts +++ b/server/tests/api/check-params/video-comments.ts @@ -2,9 +2,11 @@ import 'mocha' import * as chai from 'chai' -import { VideoCreateResult } from '@shared/models' -import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' +import { HttpStatusCode } from '@shared/core-utils' import { + checkBadCountPagination, + checkBadSortPagination, + checkBadStartPagination, cleanupTests, createUser, flushAndRunServer, @@ -15,13 +17,8 @@ import { setAccessTokensToServers, uploadVideo, userLogin -} from '../../../../shared/extra-utils' -import { - checkBadCountPagination, - checkBadSortPagination, - checkBadStartPagination -} from '../../../../shared/extra-utils/requests/check-api-params' -import { addVideoCommentThread } from '../../../../shared/extra-utils/videos/video-comments' +} from '@shared/extra-utils' +import { VideoCreateResult } from '@shared/models' const expect = chai.expect @@ -50,8 +47,8 @@ describe('Test video comments API validator', function () { } { - const res = await addVideoCommentThread(server.url, server.accessToken, video.uuid, 'coucou') - commentId = res.body.comment.id + const created = await server.commentsCommand.createThread({ videoId: video.uuid, text: 'coucou' }) + commentId = created.id pathComment = '/api/v1/videos/' + video.uuid + '/comments/' + commentId } @@ -281,8 +278,8 @@ describe('Test video comments API validator', function () { let commentToDelete: number { - const res = await addVideoCommentThread(server.url, userAccessToken, video.uuid, 'hello') - commentToDelete = res.body.comment.id + const created = await server.commentsCommand.createThread({ videoId: video.uuid, text: 'hello' }) + commentToDelete = created.id } const path = '/api/v1/videos/' + video.uuid + '/comments/' + commentToDelete @@ -301,8 +298,8 @@ describe('Test video comments API validator', function () { } { - const res = await addVideoCommentThread(server.url, server.accessToken, anotherVideoUUID, 'hello') - commentToDelete = res.body.comment.id + const created = await server.commentsCommand.createThread({ videoId: anotherVideoUUID, text: 'hello' }) + commentToDelete = created.id } const path = '/api/v1/videos/' + anotherVideoUUID + '/comments/' + commentToDelete diff --git a/server/tests/api/moderation/abuses.ts b/server/tests/api/moderation/abuses.ts index a2bd07b12..e428cf1a8 100644 --- a/server/tests/api/moderation/abuses.ts +++ b/server/tests/api/moderation/abuses.ts @@ -4,14 +4,11 @@ import 'mocha' import * as chai from 'chai' import { AbusesCommand, - addVideoCommentThread, cleanupTests, createUser, - deleteVideoComment, doubleFollow, flushAndRunMultipleServers, generateUserAccessToken, - getVideoCommentThreads, getVideoIdFromUUID, getVideosList, removeUser, @@ -23,7 +20,7 @@ import { userLogin, waitJobs } from '@shared/extra-utils' -import { AbuseMessage, AbusePredefinedReasonsString, AbuseState, AdminAbuse, UserAbuse, VideoComment } from '@shared/models' +import { AbuseMessage, AbusePredefinedReasonsString, AbuseState, AdminAbuse, UserAbuse } from '@shared/models' const expect = chai.expect @@ -399,14 +396,14 @@ describe('Test abuses', function () { describe('Comment abuses', function () { - async function getComment (url: string, videoIdArg: number | string) { + async function getComment (server: ServerInfo, videoIdArg: number | string) { const videoId = typeof videoIdArg === 'string' - ? await getVideoIdFromUUID(url, videoIdArg) + ? await getVideoIdFromUUID(server.url, videoIdArg) : videoIdArg - const res = await getVideoCommentThreads(url, videoId, 0, 5) + const { data } = await server.commentsCommand.listThreads({ videoId }) - return res.body.data[0] as VideoComment + return data[0] } before(async function () { @@ -415,8 +412,8 @@ describe('Test abuses', function () { servers[0].video = await uploadVideoAndGetId({ server: servers[0], videoName: 'server 1' }) servers[1].video = await uploadVideoAndGetId({ server: servers[1], videoName: 'server 2' }) - await addVideoCommentThread(servers[0].url, servers[0].accessToken, servers[0].video.id, 'comment server 1') - await addVideoCommentThread(servers[1].url, servers[1].accessToken, servers[1].video.id, 'comment server 2') + await servers[0].commentsCommand.createThread({ videoId: servers[0].video.id, text: 'comment server 1' }) + await servers[1].commentsCommand.createThread({ videoId: servers[1].video.id, text: 'comment server 2' }) await waitJobs(servers) }) @@ -424,7 +421,7 @@ describe('Test abuses', function () { it('Should report abuse on a comment', async function () { this.timeout(15000) - const comment = await getComment(servers[0].url, servers[0].video.id) + const comment = await getComment(servers[0], servers[0].video.id) const reason = 'it is a bad comment' await commands[0].report({ commentId: comment.id, reason }) @@ -434,7 +431,7 @@ describe('Test abuses', function () { it('Should have 1 comment abuse on server 1 and 0 on server 2', async function () { { - const comment = await getComment(servers[0].url, servers[0].video.id) + const comment = await getComment(servers[0], servers[0].video.id) const body = await commands[0].getAdminList({ filter: 'comment' }) expect(body.total).to.equal(1) @@ -469,7 +466,7 @@ describe('Test abuses', function () { it('Should report abuse on a remote comment', async function () { this.timeout(10000) - const comment = await getComment(servers[0].url, servers[1].video.uuid) + const comment = await getComment(servers[0], servers[1].video.uuid) const reason = 'it is a really bad comment' await commands[0].report({ commentId: comment.id, reason }) @@ -478,7 +475,7 @@ describe('Test abuses', function () { }) it('Should have 2 comment abuses on server 1 and 1 on server 2', async function () { - const commentServer2 = await getComment(servers[0].url, servers[1].video.id) + const commentServer2 = await getComment(servers[0], servers[1].video.id) { const body = await commands[0].getAdminList({ filter: 'comment' }) @@ -537,9 +534,9 @@ describe('Test abuses', function () { it('Should keep the comment abuse when deleting the comment', async function () { this.timeout(10000) - const commentServer2 = await getComment(servers[0].url, servers[1].video.id) + const commentServer2 = await getComment(servers[0], servers[1].video.id) - await deleteVideoComment(servers[0].url, servers[0].accessToken, servers[1].video.uuid, commentServer2.id) + await servers[0].commentsCommand.delete({ videoId: servers[1].video.uuid, commentId: commentServer2.id }) await waitJobs(servers) diff --git a/server/tests/api/moderation/blocklist-notification.ts b/server/tests/api/moderation/blocklist-notification.ts index 5b9699816..a077d8739 100644 --- a/server/tests/api/moderation/blocklist-notification.ts +++ b/server/tests/api/moderation/blocklist-notification.ts @@ -3,7 +3,6 @@ import 'mocha' import * as chai from 'chai' import { - addVideoCommentThread, cleanupTests, createUser, doubleFollow, @@ -59,7 +58,11 @@ describe('Test blocklist', function () { } { - await addVideoCommentThread(servers[1].url, remoteUserToken, videoUUID, '@user2@' + servers[0].host + ' hello') + await servers[1].commentsCommand.createThread({ + token: remoteUserToken, + videoId: videoUUID, + text: '@user2@' + servers[0].host + ' hello' + }) } { diff --git a/server/tests/api/moderation/blocklist.ts b/server/tests/api/moderation/blocklist.ts index 1b8860571..00cb6c65c 100644 --- a/server/tests/api/moderation/blocklist.ts +++ b/server/tests/api/moderation/blocklist.ts @@ -3,55 +3,47 @@ import 'mocha' import * as chai from 'chai' import { - addVideoCommentReply, - addVideoCommentThread, BlocklistCommand, cleanupTests, + CommentsCommand, createUser, - deleteVideoComment, doubleFollow, - findCommentId, flushAndRunMultipleServers, getUserNotifications, - getVideoCommentThreads, getVideosList, getVideosListWithToken, - getVideoThreadComments, ServerInfo, setAccessTokensToServers, uploadVideo, userLogin, waitJobs } from '@shared/extra-utils' -import { UserNotification, UserNotificationType, Video, VideoComment, VideoCommentThreadTree } from '@shared/models' +import { UserNotification, UserNotificationType, Video } from '@shared/models' const expect = chai.expect -async function checkAllVideos (url: string, token: string) { +async function checkAllVideos (server: ServerInfo, token: string) { { - const res = await getVideosListWithToken(url, token) + const res = await getVideosListWithToken(server.url, token) expect(res.body.data).to.have.lengthOf(5) } { - const res = await getVideosList(url) + const res = await getVideosList(server.url) expect(res.body.data).to.have.lengthOf(5) } } -async function checkAllComments (url: string, token: string, videoUUID: string) { - const resThreads = await getVideoCommentThreads(url, videoUUID, 0, 25, '-createdAt', token) +async function checkAllComments (server: ServerInfo, token: string, videoUUID: string) { + const { data } = await server.commentsCommand.listThreads({ videoId: videoUUID, start: 0, count: 25, sort: '-createdAt', token }) - const allThreads: VideoComment[] = resThreads.body.data - const threads = allThreads.filter(t => t.isDeleted === false) + const threads = data.filter(t => t.isDeleted === false) expect(threads).to.have.lengthOf(2) for (const thread of threads) { - const res = await getVideoThreadComments(url, videoUUID, thread.id, token) - - const tree: VideoCommentThreadTree = res.body + const tree = await server.commentsCommand.getThread({ videoId: videoUUID, threadId: thread.id, token }) expect(tree.children).to.have.lengthOf(1) } } @@ -61,10 +53,9 @@ async function checkCommentNotification ( comment: { server: ServerInfo, token: string, videoUUID: string, text: string }, check: 'presence' | 'absence' ) { - const resComment = await addVideoCommentThread(comment.server.url, comment.token, comment.videoUUID, comment.text) - const created = resComment.body.comment as VideoComment - const threadId = created.id - const createdAt = created.createdAt + const command = comment.server.commentsCommand + + const { threadId, createdAt } = await command.createThread({ token: comment.token, videoId: comment.videoUUID, text: comment.text }) await waitJobs([ mainServer, comment.server ]) @@ -75,7 +66,7 @@ async function checkCommentNotification ( if (check === 'presence') expect(commentNotifications).to.have.lengthOf(1) else expect(commentNotifications).to.have.lengthOf(0) - await deleteVideoComment(comment.server.url, comment.token, comment.videoUUID, threadId) + await command.delete({ token: comment.token, videoId: comment.videoUUID, commentId: threadId }) await waitJobs([ mainServer, comment.server ]) } @@ -90,6 +81,7 @@ describe('Test blocklist', function () { let userToken2: string let command: BlocklistCommand + let commentsCommand: CommentsCommand[] before(async function () { this.timeout(120000) @@ -97,6 +89,9 @@ describe('Test blocklist', function () { servers = await flushAndRunMultipleServers(3) await setAccessTokensToServers(servers) + command = servers[0].blocklistCommand + commentsCommand = servers.map(s => s.commentsCommand) + { const user = { username: 'user1', password: 'password' } await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password }) @@ -139,30 +134,33 @@ describe('Test blocklist', function () { await doubleFollow(servers[0], servers[2]) { - const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID1, 'comment root 1') - const resReply = await addVideoCommentReply(servers[0].url, userToken1, videoUUID1, resComment.body.comment.id, 'comment user 1') - await addVideoCommentReply(servers[0].url, servers[0].accessToken, videoUUID1, resReply.body.comment.id, 'comment root 1') + const created = await commentsCommand[0].createThread({ videoId: videoUUID1, text: 'comment root 1' }) + const reply = await commentsCommand[0].addReply({ + token: userToken1, + videoId: videoUUID1, + toCommentId: created.id, + text: 'comment user 1' + }) + await commentsCommand[0].addReply({ videoId: videoUUID1, toCommentId: reply.id, text: 'comment root 1' }) } { - const resComment = await addVideoCommentThread(servers[0].url, userToken1, videoUUID1, 'comment user 1') - await addVideoCommentReply(servers[0].url, servers[0].accessToken, videoUUID1, resComment.body.comment.id, 'comment root 1') + const created = await commentsCommand[0].createThread({ token: userToken1, videoId: videoUUID1, text: 'comment user 1' }) + await commentsCommand[0].addReply({ videoId: videoUUID1, toCommentId: created.id, text: 'comment root 1' }) } await waitJobs(servers) - - command = servers[0].blocklistCommand }) describe('User blocklist', function () { describe('When managing account blocklist', function () { it('Should list all videos', function () { - return checkAllVideos(servers[0].url, servers[0].accessToken) + return checkAllVideos(servers[0], servers[0].accessToken) }) it('Should list the comments', function () { - return checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1) + return checkAllComments(servers[0], servers[0].accessToken, videoUUID1) }) it('Should block a remote account', async function () { @@ -194,19 +192,26 @@ describe('Test blocklist', function () { }) it('Should hide its comments', async function () { - 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) - expect(threads[0].totalReplies).to.equal(1) - - const t = threads.find(t => t.text === 'comment user 1') + const { data } = await commentsCommand[0].listThreads({ + token: servers[0].accessToken, + videoId: videoUUID1, + start: 0, + count: 25, + sort: '-createdAt' + }) + + expect(data).to.have.lengthOf(1) + expect(data[0].totalReplies).to.equal(1) + + const t = data.find(t => t.text === 'comment user 1') expect(t).to.be.undefined - for (const thread of threads) { - const res = await getVideoThreadComments(servers[0].url, videoUUID1, thread.id, servers[0].accessToken) - - const tree: VideoCommentThreadTree = res.body + for (const thread of data) { + const tree = await commentsCommand[0].getThread({ + videoId: videoUUID1, + threadId: thread.id, + token: servers[0].accessToken + }) expect(tree.children).to.have.lengthOf(0) } }) @@ -231,7 +236,7 @@ describe('Test blocklist', function () { }) it('Should list all the videos with another user', async function () { - return checkAllVideos(servers[0].url, userToken1) + return checkAllVideos(servers[0], userToken1) }) it('Should list blocked accounts', async function () { @@ -264,32 +269,29 @@ describe('Test blocklist', function () { this.timeout(60000) { - await addVideoCommentThread(servers[1].url, userToken2, videoUUID3, 'comment user 2') + await commentsCommand[1].createThread({ token: userToken2, videoId: videoUUID3, text: 'comment user 2' }) await waitJobs(servers) - await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID3, 'uploader') + await commentsCommand[0].createThread({ token: servers[0].accessToken, videoId: videoUUID3, text: 'uploader' }) await waitJobs(servers) - const commentId = await findCommentId(servers[1].url, videoUUID3, 'uploader') + const commentId = await commentsCommand[1].findCommentId({ videoId: videoUUID3, text: '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') + const reply = await commentsCommand[1].addReply({ token: userToken2, videoId: videoUUID3, toCommentId: commentId, text: message }) + await commentsCommand[1].addReply({ videoId: videoUUID3, toCommentId: reply.id, text: '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 { data } = await commentsCommand[1].listThreads({ videoId: videoUUID3, count: 25, sort: '-createdAt' }) - const resReplies = await getVideoThreadComments(servers[1].url, videoUUID3, threads[0].id) + expect(data).to.have.lengthOf(2) + expect(data[0].text).to.equal('uploader') + expect(data[1].text).to.equal('comment user 2') - const tree: VideoCommentThreadTree = resReplies.body + const tree = await commentsCommand[1].getThread({ videoId: videoUUID3, threadId: data[0].id }) 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) @@ -298,20 +300,15 @@ describe('Test blocklist', function () { // 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 + const { data } = await server.commentsCommand.listThreads({ videoId: videoUUID3, count: 25, sort: '-createdAt' }) - expect(threads).to.have.lengthOf(1) - expect(threads[0].text).to.equal('uploader') + expect(data).to.have.lengthOf(1) + expect(data[0].text).to.equal('uploader') - const resReplies = await getVideoThreadComments(server.url, videoUUID3, threads[0].id) + const tree = await server.commentsCommand.getThread({ videoId: videoUUID3, threadId: data[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) - } + if (server.serverNumber === 1) expect(tree.children).to.have.lengthOf(0) + else expect(tree.children).to.have.lengthOf(1) } }) @@ -331,22 +328,19 @@ describe('Test blocklist', function () { 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 + const { data } = await server.commentsCommand.listThreads({ videoId: videoUUID3, count: 25, sort: '-createdAt' }) // 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) + expect(data).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) + expect(data).to.have.lengthOf(2) + expect(data[0].text).to.equal('uploader') + expect(data[1].text).to.equal('comment user 2') - const tree: VideoCommentThreadTree = resReplies.body + const tree = await server.commentsCommand.getThread({ videoId: videoUUID3, threadId: data[0].id }) 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) @@ -359,7 +353,7 @@ describe('Test blocklist', function () { }) it('Should display its comments', function () { - return checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1) + return checkAllComments(servers[0], servers[0].accessToken, videoUUID1) }) it('Should have a notification from a non blocked account', async function () { @@ -385,11 +379,11 @@ describe('Test blocklist', function () { describe('When managing server blocklist', function () { it('Should list all videos', function () { - return checkAllVideos(servers[0].url, servers[0].accessToken) + return checkAllVideos(servers[0], servers[0].accessToken) }) it('Should list the comments', function () { - return checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1) + return checkAllComments(servers[0], servers[0].accessToken, videoUUID1) }) it('Should block a remote server', async function () { @@ -410,20 +404,19 @@ describe('Test blocklist', function () { }) it('Should list all the videos with another user', async function () { - return checkAllVideos(servers[0].url, userToken1) + return checkAllVideos(servers[0], userToken1) }) it('Should hide its comments', async function () { this.timeout(10000) - const resThreads = await addVideoCommentThread(servers[1].url, userToken2, videoUUID1, 'hidden comment 2') - const threadId = resThreads.body.comment.id + const { id } = await commentsCommand[1].createThread({ token: userToken2, videoId: videoUUID1, text: 'hidden comment 2' }) await waitJobs(servers) - await checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1) + await checkAllComments(servers[0], servers[0].accessToken, videoUUID1) - await deleteVideoComment(servers[1].url, userToken2, videoUUID1, threadId) + await commentsCommand[1].delete({ token: userToken2, videoId: videoUUID1, commentId: id }) }) it('Should not have notifications from blocked server', async function () { @@ -460,11 +453,11 @@ describe('Test blocklist', function () { }) it('Should display its videos', function () { - return checkAllVideos(servers[0].url, servers[0].accessToken) + return checkAllVideos(servers[0], servers[0].accessToken) }) it('Should display its comments', function () { - return checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1) + return checkAllComments(servers[0], servers[0].accessToken, videoUUID1) }) it('Should have notification from unblocked server', async function () { @@ -493,13 +486,13 @@ describe('Test blocklist', function () { describe('When managing account blocklist', function () { it('Should list all videos', async function () { for (const token of [ userModeratorToken, servers[0].accessToken ]) { - await checkAllVideos(servers[0].url, token) + await checkAllVideos(servers[0], token) } }) it('Should list the comments', async function () { for (const token of [ userModeratorToken, servers[0].accessToken ]) { - await checkAllComments(servers[0].url, token, videoUUID1) + await checkAllComments(servers[0], token, videoUUID1) } }) @@ -537,10 +530,8 @@ 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, 20, '-createdAt', token) - - let threads: VideoComment[] = resThreads.body.data - threads = threads.filter(t => t.isDeleted === false) + const { data } = await commentsCommand[0].listThreads({ videoId: videoUUID1, count: 20, sort: '-createdAt', token }) + const threads = data.filter(t => t.isDeleted === false) expect(threads).to.have.lengthOf(1) expect(threads[0].totalReplies).to.equal(1) @@ -549,9 +540,7 @@ describe('Test blocklist', function () { expect(t).to.be.undefined for (const thread of threads) { - const res = await getVideoThreadComments(servers[0].url, videoUUID1, thread.id, token) - - const tree: VideoCommentThreadTree = res.body + const tree = await commentsCommand[0].getThread({ videoId: videoUUID1, threadId: thread.id, token }) expect(tree.children).to.have.lengthOf(0) } } @@ -624,7 +613,7 @@ describe('Test blocklist', function () { it('Should display its comments', async function () { for (const token of [ userModeratorToken, servers[0].accessToken ]) { - await checkAllComments(servers[0].url, token, videoUUID1) + await checkAllComments(servers[0], token, videoUUID1) } }) @@ -651,13 +640,13 @@ describe('Test blocklist', function () { describe('When managing server blocklist', function () { it('Should list all videos', async function () { for (const token of [ userModeratorToken, servers[0].accessToken ]) { - await checkAllVideos(servers[0].url, token) + await checkAllVideos(servers[0], token) } }) it('Should list the comments', async function () { for (const token of [ userModeratorToken, servers[0].accessToken ]) { - await checkAllComments(servers[0].url, token, videoUUID1) + await checkAllComments(servers[0], token, videoUUID1) } }) @@ -686,14 +675,13 @@ describe('Test blocklist', function () { it('Should hide its comments', async function () { this.timeout(10000) - const resThreads = await addVideoCommentThread(servers[1].url, userToken2, videoUUID1, 'hidden comment 2') - const threadId = resThreads.body.comment.id + const { id } = await commentsCommand[1].createThread({ token: userToken2, videoId: videoUUID1, text: 'hidden comment 2' }) await waitJobs(servers) - await checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1) + await checkAllComments(servers[0], servers[0].accessToken, videoUUID1) - await deleteVideoComment(servers[1].url, userToken2, videoUUID1, threadId) + await commentsCommand[1].delete({ token: userToken2, videoId: videoUUID1, commentId: id }) }) it('Should not have notification from blocked instances by instance', async function () { @@ -749,13 +737,13 @@ describe('Test blocklist', function () { it('Should list all videos', async function () { for (const token of [ userModeratorToken, servers[0].accessToken ]) { - await checkAllVideos(servers[0].url, token) + await checkAllVideos(servers[0], token) } }) it('Should list the comments', async function () { for (const token of [ userModeratorToken, servers[0].accessToken ]) { - await checkAllComments(servers[0].url, token, videoUUID1) + await checkAllComments(servers[0], token, videoUUID1) } }) diff --git a/server/tests/api/notifications/comments-notifications.ts b/server/tests/api/notifications/comments-notifications.ts index 13fcee843..ea6055386 100644 --- a/server/tests/api/notifications/comments-notifications.ts +++ b/server/tests/api/notifications/comments-notifications.ts @@ -3,14 +3,10 @@ import 'mocha' import * as chai from 'chai' import { - addVideoCommentReply, - addVideoCommentThread, checkCommentMention, CheckerBaseParams, checkNewCommentOnMyVideo, cleanupTests, - getVideoCommentThreads, - getVideoThreadComments, MockSmtpServer, prepareNotificationsTest, ServerInfo, @@ -18,13 +14,13 @@ import { uploadVideo, waitJobs } from '@shared/extra-utils' -import { UserNotification, VideoCommentThreadTree } from '@shared/models' +import { UserNotification } from '@shared/models' const expect = chai.expect describe('Test comments notifications', function () { let servers: ServerInfo[] = [] - let userAccessToken: string + let userToken: string let userNotifications: UserNotification[] = [] let emails: object[] = [] @@ -38,7 +34,7 @@ describe('Test comments notifications', function () { const res = await prepareNotificationsTest(2) emails = res.emails - userAccessToken = res.userAccessToken + userToken = res.userAccessToken servers = res.servers userNotifications = res.userNotifications }) @@ -51,7 +47,7 @@ describe('Test comments notifications', function () { server: servers[0], emails, socketNotifications: userNotifications, - token: userAccessToken + token: userToken } }) @@ -61,8 +57,8 @@ describe('Test comments notifications', function () { const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) const uuid = resVideo.body.video.uuid - const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment') - const commentId = resComment.body.comment.id + const created = await servers[0].commentsCommand.createThread({ videoId: uuid, text: 'comment' }) + const commentId = created.id await waitJobs(servers) await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence') @@ -71,11 +67,11 @@ describe('Test comments notifications', function () { it('Should not send a new comment notification if I comment my own video', async function () { this.timeout(20000) - const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) + const resVideo = await uploadVideo(servers[0].url, userToken, { name: 'super video' }) const uuid = resVideo.body.video.uuid - const resComment = await addVideoCommentThread(servers[0].url, userAccessToken, uuid, 'comment') - const commentId = resComment.body.comment.id + const created = await servers[0].commentsCommand.createThread({ token: userToken, videoId: uuid, text: 'comment' }) + const commentId = created.id await waitJobs(servers) await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence') @@ -84,28 +80,28 @@ describe('Test comments notifications', function () { it('Should not send a new comment notification if the account is muted', async function () { this.timeout(20000) - await servers[0].blocklistCommand.addToMyBlocklist({ token: userAccessToken, account: 'root' }) + await servers[0].blocklistCommand.addToMyBlocklist({ token: userToken, account: 'root' }) - const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) + const resVideo = await uploadVideo(servers[0].url, userToken, { name: 'super video' }) const uuid = resVideo.body.video.uuid - const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment') - const commentId = resComment.body.comment.id + const created = await servers[0].commentsCommand.createThread({ videoId: uuid, text: 'comment' }) + const commentId = created.id await waitJobs(servers) await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence') - await servers[0].blocklistCommand.removeFromMyBlocklist({ token: userAccessToken, account: 'root' }) + await servers[0].blocklistCommand.removeFromMyBlocklist({ token: userToken, account: 'root' }) }) it('Should send a new comment notification after a local comment on my video', async function () { this.timeout(20000) - const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) + const resVideo = await uploadVideo(servers[0].url, userToken, { name: 'super video' }) const uuid = resVideo.body.video.uuid - const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment') - const commentId = resComment.body.comment.id + const created = await servers[0].commentsCommand.createThread({ videoId: uuid, text: 'comment' }) + const commentId = created.id await waitJobs(servers) await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'presence') @@ -114,33 +110,31 @@ describe('Test comments notifications', function () { it('Should send a new comment notification after a remote comment on my video', async function () { this.timeout(20000) - const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) + const resVideo = await uploadVideo(servers[0].url, userToken, { name: 'super video' }) const uuid = resVideo.body.video.uuid await waitJobs(servers) - await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, 'comment') + await servers[1].commentsCommand.createThread({ videoId: uuid, text: 'comment' }) await waitJobs(servers) - const resComment = await getVideoCommentThreads(servers[0].url, uuid, 0, 5) - expect(resComment.body.data).to.have.lengthOf(1) - const commentId = resComment.body.data[0].id + const { data } = await servers[0].commentsCommand.listThreads({ videoId: uuid }) + expect(data).to.have.lengthOf(1) + const commentId = data[0].id await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'presence') }) it('Should send a new comment notification after a local reply on my video', async function () { this.timeout(20000) - const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) + const resVideo = await uploadVideo(servers[0].url, userToken, { name: 'super video' }) const uuid = resVideo.body.video.uuid - const resThread = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment') - const threadId = resThread.body.comment.id + const { id: threadId } = await servers[0].commentsCommand.createThread({ videoId: uuid, text: 'comment' }) - const resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, uuid, threadId, 'reply') - const commentId = resComment.body.comment.id + const { id: commentId } = await servers[0].commentsCommand.addReply({ videoId: uuid, toCommentId: threadId, text: 'reply' }) await waitJobs(servers) await checkNewCommentOnMyVideo(baseParams, uuid, commentId, threadId, 'presence') @@ -149,24 +143,23 @@ describe('Test comments notifications', function () { it('Should send a new comment notification after a remote reply on my video', async function () { this.timeout(20000) - const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) + const resVideo = await uploadVideo(servers[0].url, userToken, { name: 'super video' }) const uuid = resVideo.body.video.uuid await waitJobs(servers) { - const resThread = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, 'comment') - const threadId = resThread.body.comment.id - await addVideoCommentReply(servers[1].url, servers[1].accessToken, uuid, threadId, 'reply') + const created = await servers[1].commentsCommand.createThread({ videoId: uuid, text: 'comment' }) + const threadId = created.id + await servers[1].commentsCommand.addReply({ videoId: uuid, toCommentId: threadId, text: 'reply' }) } await waitJobs(servers) - const resThread = await getVideoCommentThreads(servers[0].url, uuid, 0, 5) - expect(resThread.body.data).to.have.lengthOf(1) - const threadId = resThread.body.data[0].id + const { data } = await servers[0].commentsCommand.listThreads({ videoId: uuid }) + expect(data).to.have.lengthOf(1) - const resComments = await getVideoThreadComments(servers[0].url, uuid, threadId) - const tree = resComments.body as VideoCommentThreadTree + const threadId = data[0].id + const tree = await servers[0].commentsCommand.getThread({ videoId: uuid, threadId }) expect(tree.children).to.have.lengthOf(1) const commentId = tree.children[0].comment.id @@ -177,10 +170,10 @@ describe('Test comments notifications', function () { it('Should convert markdown in comment to html', async function () { this.timeout(20000) - const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'cool video' }) + const resVideo = await uploadVideo(servers[0].url, userToken, { name: 'cool video' }) const uuid = resVideo.body.video.uuid - await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, commentText) + await servers[0].commentsCommand.createThread({ videoId: uuid, text: commentText }) await waitJobs(servers) @@ -197,7 +190,7 @@ describe('Test comments notifications', function () { server: servers[0], emails, socketNotifications: userNotifications, - token: userAccessToken + token: userToken } await updateMyUser({ @@ -216,11 +209,10 @@ describe('Test comments notifications', function () { it('Should not send a new mention comment notification if I mention the video owner', async function () { this.timeout(10000) - const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) + const resVideo = await uploadVideo(servers[0].url, userToken, { name: 'super video' }) const uuid = resVideo.body.video.uuid - const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello') - const commentId = resComment.body.comment.id + const { id: commentId } = await servers[0].commentsCommand.createThread({ videoId: uuid, text: '@user_1 hello' }) await waitJobs(servers) await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence') @@ -232,8 +224,7 @@ describe('Test comments notifications', function () { const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) const uuid = resVideo.body.video.uuid - const resComment = await addVideoCommentThread(servers[0].url, userAccessToken, uuid, '@user_1 hello') - const commentId = resComment.body.comment.id + const { id: commentId } = await servers[0].commentsCommand.createThread({ token: userToken, videoId: uuid, text: '@user_1 hello' }) await waitJobs(servers) await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence') @@ -242,18 +233,17 @@ describe('Test comments notifications', function () { it('Should not send a new mention notification if the account is muted', async function () { this.timeout(10000) - await servers[0].blocklistCommand.addToMyBlocklist({ token: userAccessToken, account: 'root' }) + await servers[0].blocklistCommand.addToMyBlocklist({ token: userToken, account: 'root' }) const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) const uuid = resVideo.body.video.uuid - const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello') - const commentId = resComment.body.comment.id + const { id: commentId } = await servers[0].commentsCommand.createThread({ videoId: uuid, text: '@user_1 hello' }) await waitJobs(servers) await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence') - await servers[0].blocklistCommand.removeFromMyBlocklist({ token: userAccessToken, account: 'root' }) + await servers[0].blocklistCommand.removeFromMyBlocklist({ token: userToken, account: 'root' }) }) it('Should not send a new mention notification if the remote account mention a local account', async function () { @@ -263,8 +253,7 @@ describe('Test comments notifications', function () { const uuid = resVideo.body.video.uuid await waitJobs(servers) - const resThread = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, '@user_1 hello') - const threadId = resThread.body.comment.id + const { id: threadId } = await servers[1].commentsCommand.createThread({ videoId: uuid, text: '@user_1 hello' }) await waitJobs(servers) await checkCommentMention(baseParams, uuid, threadId, threadId, 'super root 2 name', 'absence') @@ -276,14 +265,12 @@ describe('Test comments notifications', function () { const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) const uuid = resVideo.body.video.uuid - const resThread = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello 1') - const threadId = resThread.body.comment.id + const { id: threadId } = await servers[0].commentsCommand.createThread({ videoId: uuid, text: '@user_1 hellotext: 1' }) await waitJobs(servers) await checkCommentMention(baseParams, uuid, threadId, threadId, 'super root name', 'presence') - const resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, uuid, threadId, 'hello 2 @user_1') - const commentId = resComment.body.comment.id + const { id: commentId } = await servers[0].commentsCommand.addReply({ videoId: uuid, toCommentId: threadId, text: 'hello 2 @user_1' }) await waitJobs(servers) await checkCommentMention(baseParams, uuid, commentId, threadId, 'super root name', 'presence') @@ -298,23 +285,22 @@ describe('Test comments notifications', function () { await waitJobs(servers) const text1 = `hello @user_1@localhost:${servers[0].port} 1` - const resThread = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, text1) - const server2ThreadId = resThread.body.comment.id + const { id: server2ThreadId } = await servers[1].commentsCommand.createThread({ videoId: uuid, text: text1 }) await waitJobs(servers) - const resThread2 = await getVideoCommentThreads(servers[0].url, uuid, 0, 5) - expect(resThread2.body.data).to.have.lengthOf(1) - const server1ThreadId = resThread2.body.data[0].id + const { data } = await servers[0].commentsCommand.listThreads({ videoId: uuid }) + expect(data).to.have.lengthOf(1) + + const server1ThreadId = data[0].id await checkCommentMention(baseParams, uuid, server1ThreadId, server1ThreadId, 'super root 2 name', 'presence') const text2 = `@user_1@localhost:${servers[0].port} hello 2 @root@localhost:${servers[0].port}` - await addVideoCommentReply(servers[1].url, servers[1].accessToken, uuid, server2ThreadId, text2) + await servers[1].commentsCommand.addReply({ videoId: uuid, toCommentId: server2ThreadId, text: text2 }) await waitJobs(servers) - const resComments = await getVideoThreadComments(servers[0].url, uuid, server1ThreadId) - const tree = resComments.body as VideoCommentThreadTree + const tree = await servers[0].commentsCommand.getThread({ videoId: uuid, threadId: server1ThreadId }) expect(tree.children).to.have.lengthOf(1) const commentId = tree.children[0].comment.id @@ -328,10 +314,9 @@ describe('Test comments notifications', function () { const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) const uuid = resVideo.body.video.uuid - const resThread = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello 1') - const threadId = resThread.body.comment.id + const { id: threadId } = await servers[0].commentsCommand.createThread({ videoId: uuid, text: '@user_1 hello 1' }) - await addVideoCommentReply(servers[0].url, servers[0].accessToken, uuid, threadId, '@user_1 ' + commentText) + await servers[0].commentsCommand.addReply({ videoId: uuid, toCommentId: threadId, text: '@user_1 ' + commentText }) await waitJobs(servers) diff --git a/server/tests/api/notifications/moderation-notifications.ts b/server/tests/api/notifications/moderation-notifications.ts index 52ade0548..229f78811 100644 --- a/server/tests/api/notifications/moderation-notifications.ts +++ b/server/tests/api/notifications/moderation-notifications.ts @@ -3,7 +3,6 @@ import 'mocha' import { buildUUID } from '@server/helpers/uuid' import { - addVideoCommentThread, checkAbuseStateChange, checkAutoInstanceFollowing, CheckerBaseParams, @@ -20,7 +19,6 @@ import { cleanupTests, createUser, generateUserAccessToken, - getVideoCommentThreads, getVideoIdFromUUID, immutableAssign, MockInstancesIndex, @@ -101,8 +99,11 @@ describe('Test moderation notifications', function () { const name = 'video for abuse ' + buildUUID() const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name }) const video = resVideo.body.video - const resComment = await addVideoCommentThread(servers[0].url, userAccessToken, video.id, 'comment abuse ' + buildUUID()) - const comment = resComment.body.comment + const comment = await servers[0].commentsCommand.createThread({ + token: userAccessToken, + videoId: video.id, + text: 'comment abuse ' + buildUUID() + }) await waitJobs(servers) @@ -118,12 +119,17 @@ describe('Test moderation notifications', function () { const name = 'video for abuse ' + buildUUID() const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name }) const video = resVideo.body.video - await addVideoCommentThread(servers[0].url, userAccessToken, video.id, 'comment abuse ' + buildUUID()) + + await servers[0].commentsCommand.createThread({ + token: userAccessToken, + videoId: video.id, + text: 'comment abuse ' + buildUUID() + }) await waitJobs(servers) - const resComments = await getVideoCommentThreads(servers[1].url, video.uuid, 0, 5) - const commentId = resComments.body.data[0].id + const { data } = await servers[1].commentsCommand.listThreads({ videoId: video.uuid }) + const commentId = data[0].id await servers[1].abusesCommand.report({ commentId, reason: 'super reason' }) await waitJobs(servers) diff --git a/server/tests/api/server/bulk.ts b/server/tests/api/server/bulk.ts index ca98a2eb2..5c8673e00 100644 --- a/server/tests/api/server/bulk.ts +++ b/server/tests/api/server/bulk.ts @@ -2,16 +2,13 @@ import 'mocha' import * as chai from 'chai' -import { Video, VideoComment } from '@shared/models' +import { Video } from '@shared/models' import { - addVideoCommentReply, - addVideoCommentThread, BulkCommand, cleanupTests, createUser, doubleFollow, flushAndRunMultipleServers, - getVideoCommentThreads, getVideosList, ServerInfo, setAccessTokensToServers, @@ -26,9 +23,9 @@ describe('Test bulk actions', function () { const commentsUser3: { videoId: number, commentId: number }[] = [] let servers: ServerInfo[] = [] - let user1AccessToken: string - let user2AccessToken: string - let user3AccessToken: string + let user1Token: string + let user2Token: string + let user3Token: string let bulkCommand: BulkCommand @@ -44,21 +41,21 @@ describe('Test bulk actions', function () { const user = { username: 'user1', password: 'password' } await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password }) - user1AccessToken = await userLogin(servers[0], user) + user1Token = await userLogin(servers[0], user) } { const user = { username: 'user2', password: 'password' } await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password }) - user2AccessToken = await userLogin(servers[0], user) + user2Token = await userLogin(servers[0], user) } { const user = { username: 'user3', password: 'password' } await createUser({ url: servers[1].url, accessToken: servers[1].accessToken, username: user.username, password: user.password }) - user3AccessToken = await userLogin(servers[1], user) + user3Token = await userLogin(servers[1], user) } await doubleFollow(servers[0], servers[1]) @@ -74,9 +71,8 @@ describe('Test bulk actions', function () { // Server 1 should not have these comments anymore for (const video of videos) { - const resThreads = await getVideoCommentThreads(servers[0].url, video.id, 0, 10) - const comments = resThreads.body.data as VideoComment[] - const comment = comments.find(c => c.text === 'comment by user 3') + const { data } = await servers[0].commentsCommand.listThreads({ videoId: video.id }) + const comment = data.find(c => c.text === 'comment by user 3') expect(comment).to.not.exist } @@ -88,9 +84,8 @@ describe('Test bulk actions', function () { // Server 1 should not have these comments on videos of server 1 for (const video of videos) { - const resThreads = await getVideoCommentThreads(servers[1].url, video.id, 0, 10) - const comments = resThreads.body.data as VideoComment[] - const comment = comments.find(c => c.text === 'comment by user 3') + const { data } = await servers[1].commentsCommand.listThreads({ videoId: video.id }) + const comment = data.find(c => c.text === 'comment by user 3') if (video.account.host === 'localhost:' + servers[0].port) { expect(comment).to.not.exist @@ -106,7 +101,7 @@ describe('Test bulk actions', function () { await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video 1 server 1' }) await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video 2 server 1' }) - await uploadVideo(servers[0].url, user1AccessToken, { name: 'video 3 server 1' }) + await uploadVideo(servers[0].url, user1Token, { name: 'video 3 server 1' }) await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video 1 server 2' }) @@ -115,19 +110,20 @@ describe('Test bulk actions', function () { { const res = await getVideosList(servers[0].url) for (const video of res.body.data) { - await addVideoCommentThread(servers[0].url, servers[0].accessToken, video.id, 'comment by root server 1') - await addVideoCommentThread(servers[0].url, user1AccessToken, video.id, 'comment by user 1') - await addVideoCommentThread(servers[0].url, user2AccessToken, video.id, 'comment by user 2') + await servers[0].commentsCommand.createThread({ videoId: video.id, text: 'comment by root server 1' }) + await servers[0].commentsCommand.createThread({ token: user1Token, videoId: video.id, text: 'comment by user 1' }) + await servers[0].commentsCommand.createThread({ token: user2Token, videoId: video.id, text: 'comment by user 2' }) } } { const res = await getVideosList(servers[1].url) + for (const video of res.body.data) { - await addVideoCommentThread(servers[1].url, servers[1].accessToken, video.id, 'comment by root server 2') + await servers[1].commentsCommand.createThread({ videoId: video.id, text: 'comment by root server 2' }) - const res = await addVideoCommentThread(servers[1].url, user3AccessToken, video.id, 'comment by user 3') - commentsUser3.push({ videoId: video.id, commentId: res.body.comment.id }) + const comment = await servers[1].commentsCommand.createThread({ token: user3Token, videoId: video.id, text: 'comment by user 3' }) + commentsUser3.push({ videoId: video.id, commentId: comment.id }) } } @@ -138,7 +134,7 @@ describe('Test bulk actions', function () { this.timeout(60000) await bulkCommand.removeCommentsOf({ - token: user1AccessToken, + token: user1Token, attributes: { accountName: 'user2', scope: 'my-videos' @@ -151,15 +147,11 @@ describe('Test bulk actions', function () { const res = await getVideosList(server.url) for (const video of res.body.data) { - const resThreads = await getVideoCommentThreads(server.url, video.id, 0, 10) - const comments = resThreads.body.data as VideoComment[] - const comment = comments.find(c => c.text === 'comment by user 2') + const { data } = await server.commentsCommand.listThreads({ videoId: video.id }) + const comment = data.find(c => c.text === 'comment by user 2') - if (video.name === 'video 3 server 1') { - expect(comment).to.not.exist - } else { - expect(comment).to.exist - } + if (video.name === 'video 3 server 1') expect(comment).to.not.exist + else expect(comment).to.exist } } }) @@ -183,7 +175,12 @@ describe('Test bulk actions', function () { this.timeout(60000) for (const obj of commentsUser3) { - await addVideoCommentReply(servers[1].url, user3AccessToken, obj.videoId, obj.commentId, 'comment by user 3 bis') + await servers[1].commentsCommand.addReply({ + token: user3Token, + videoId: obj.videoId, + toCommentId: obj.commentId, + text: 'comment by user 3 bis' + }) } await waitJobs(servers) diff --git a/server/tests/api/server/follows.ts b/server/tests/api/server/follows.ts index 520442c6e..4a9ed2d05 100644 --- a/server/tests/api/server/follows.ts +++ b/server/tests/api/server/follows.ts @@ -3,19 +3,14 @@ import 'mocha' import * as chai from 'chai' import { - addVideoCommentReply, - addVideoCommentThread, cleanupTests, completeVideoCheck, createUser, dateIsValid, - deleteVideoComment, expectAccountFollows, flushAndRunMultipleServers, FollowsCommand, - getVideoCommentThreads, getVideosList, - getVideoThreadComments, rateVideo, ServerInfo, setAccessTokensToServers, @@ -24,7 +19,7 @@ import { userLogin, waitJobs } from '@shared/extra-utils' -import { Video, VideoComment, VideoCommentThreadTree, VideoPrivacy } from '@shared/models' +import { Video, VideoPrivacy } from '@shared/models' const expect = chai.expect @@ -348,37 +343,35 @@ describe('Test follows', function () { { { const text = 'my super first comment' - const res = await addVideoCommentThread(servers[2].url, servers[2].accessToken, video4.id, text) - const threadId = res.body.comment.id + const created = await servers[2].commentsCommand.createThread({ videoId: video4.id, text }) + const threadId = created.id const text1 = 'my super answer to thread 1' - const childCommentRes = await addVideoCommentReply(servers[2].url, servers[2].accessToken, video4.id, threadId, text1) - const childCommentId = childCommentRes.body.comment.id + const childComment = await servers[2].commentsCommand.addReply({ videoId: video4.id, toCommentId: threadId, text: text1 }) const text2 = 'my super answer to answer of thread 1' - await addVideoCommentReply(servers[2].url, servers[2].accessToken, video4.id, childCommentId, text2) + await servers[2].commentsCommand.addReply({ videoId: video4.id, toCommentId: childComment.id, text: text2 }) const text3 = 'my second answer to thread 1' - await addVideoCommentReply(servers[2].url, servers[2].accessToken, video4.id, threadId, text3) + await servers[2].commentsCommand.addReply({ videoId: video4.id, toCommentId: threadId, text: text3 }) } { const text = 'will be deleted' - const res = await addVideoCommentThread(servers[2].url, servers[2].accessToken, video4.id, text) - const threadId = res.body.comment.id + const created = await servers[2].commentsCommand.createThread({ videoId: video4.id, text }) + const threadId = created.id const text1 = 'answer to deleted' - await addVideoCommentReply(servers[2].url, servers[2].accessToken, video4.id, threadId, text1) + await servers[2].commentsCommand.addReply({ videoId: video4.id, toCommentId: threadId, text: text1 }) const text2 = 'will also be deleted' - const childCommentRes = await addVideoCommentReply(servers[2].url, servers[2].accessToken, video4.id, threadId, text2) - const childCommentId = childCommentRes.body.comment.id + const childComment = await servers[2].commentsCommand.addReply({ videoId: video4.id, toCommentId: threadId, text: text2 }) const text3 = 'my second answer to deleted' - await addVideoCommentReply(servers[2].url, servers[2].accessToken, video4.id, childCommentId, text3) + await servers[2].commentsCommand.addReply({ videoId: video4.id, toCommentId: childComment.id, text: text3 }) - await deleteVideoComment(servers[2].url, servers[2].accessToken, video4.id, threadId) - await deleteVideoComment(servers[2].url, servers[2].accessToken, video4.id, childCommentId) + await servers[2].commentsCommand.delete({ videoId: video4.id, commentId: threadId }) + await servers[2].commentsCommand.delete({ videoId: video4.id, commentId: childComment.id }) } } @@ -462,14 +455,14 @@ describe('Test follows', function () { }) it('Should have propagated comments', async function () { - const res1 = await getVideoCommentThreads(servers[0].url, video4.id, 0, 5, 'createdAt') + const { total, data } = await servers[0].commentsCommand.listThreads({ videoId: video4.id, sort: 'createdAt' }) - expect(res1.body.total).to.equal(2) - expect(res1.body.data).to.be.an('array') - expect(res1.body.data).to.have.lengthOf(2) + expect(total).to.equal(2) + expect(data).to.be.an('array') + expect(data).to.have.lengthOf(2) { - const comment: VideoComment = res1.body.data[0] + const comment = data[0] expect(comment.inReplyToCommentId).to.be.null expect(comment.text).equal('my super first comment') expect(comment.videoId).to.equal(video4.id) @@ -482,9 +475,7 @@ describe('Test follows', function () { const threadId = comment.threadId - const res2 = await getVideoThreadComments(servers[0].url, video4.id, threadId) - - const tree: VideoCommentThreadTree = res2.body + const tree = await servers[0].commentsCommand.getThread({ videoId: video4.id, threadId }) expect(tree.comment.text).equal('my super first comment') expect(tree.children).to.have.lengthOf(2) @@ -502,7 +493,7 @@ describe('Test follows', function () { } { - const deletedComment: VideoComment = res1.body.data[1] + const deletedComment = data[1] expect(deletedComment).to.not.be.undefined expect(deletedComment.isDeleted).to.be.true expect(deletedComment.deletedAt).to.not.be.null @@ -512,9 +503,7 @@ describe('Test follows', function () { expect(deletedComment.totalReplies).to.equal(2) expect(dateIsValid(deletedComment.deletedAt as string)).to.be.true - const res2 = await getVideoThreadComments(servers[0].url, video4.id, deletedComment.threadId) - - const tree: VideoCommentThreadTree = res2.body + const tree = await servers[0].commentsCommand.getThread({ videoId: video4.id, threadId: deletedComment.threadId }) const [ commentRoot, deletedChildRoot ] = tree.children expect(deletedChildRoot).to.not.be.undefined diff --git a/server/tests/api/server/handle-down.ts b/server/tests/api/server/handle-down.ts index c6202fdaa..94496a159 100644 --- a/server/tests/api/server/handle-down.ts +++ b/server/tests/api/server/handle-down.ts @@ -4,16 +4,13 @@ import 'mocha' import * as chai from 'chai' import { HttpStatusCode } from '@shared/core-utils' import { - addVideoCommentReply, - addVideoCommentThread, cleanupTests, closeAllSequelize, + CommentsCommand, completeVideoCheck, flushAndRunMultipleServers, getVideo, - getVideoCommentThreads, getVideosList, - getVideoThreadComments, immutableAssign, killallServers, reRunServer, @@ -26,7 +23,7 @@ import { wait, waitJobs } from '@shared/extra-utils' -import { JobState, Video, VideoCommentThreadTree, VideoPrivacy } from '@shared/models' +import { JobState, Video, VideoPrivacy } from '@shared/models' const expect = chai.expect @@ -62,10 +59,13 @@ describe('Test handle downs', function () { let checkAttributes: any let unlistedCheckAttributes: any + let commentCommands: CommentsCommand[] + before(async function () { this.timeout(30000) servers = await flushAndRunMultipleServers(3) + commentCommands = servers.map(s => s.commentsCommand) checkAttributes = { name: 'my super name for server 1', @@ -154,15 +154,13 @@ describe('Test handle downs', function () { // Add comments to video 2 { const text = 'thread 1' - let resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, missedVideo2.uuid, text) - let comment = resComment.body.comment + let comment = await commentCommands[0].createThread({ videoId: missedVideo2.uuid, text }) threadIdServer1 = comment.id - resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, missedVideo2.uuid, comment.id, 'comment 1-1') - comment = resComment.body.comment + comment = await commentCommands[0].addReply({ videoId: missedVideo2.uuid, toCommentId: comment.id, text: 'comment 1-1' }) - resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, missedVideo2.uuid, comment.id, 'comment 1-2') - commentIdServer1 = resComment.body.comment.id + const created = await commentCommands[0].addReply({ videoId: missedVideo2.uuid, toCommentId: comment.id, text: 'comment 1-2' }) + commentIdServer1 = created.id } await waitJobs(servers[0]) @@ -235,7 +233,7 @@ describe('Test handle downs', function () { it('Should send comments on a video to server 3, and automatically fetch the video', async function () { this.timeout(25000) - await addVideoCommentReply(servers[0].url, servers[0].accessToken, missedVideo2.uuid, commentIdServer1, 'comment 1-3') + await commentCommands[0].addReply({ videoId: missedVideo2.uuid, toCommentId: commentIdServer1, text: 'comment 1-3' }) await waitJobs(servers) @@ -243,15 +241,13 @@ describe('Test handle downs', function () { expect(resVideo.body).not.to.be.undefined { - let resComment = await getVideoCommentThreads(servers[2].url, missedVideo2.uuid, 0, 5) - expect(resComment.body.data).to.be.an('array') - expect(resComment.body.data).to.have.lengthOf(1) - - threadIdServer2 = resComment.body.data[0].id + const { data } = await servers[2].commentsCommand.listThreads({ videoId: missedVideo2.uuid }) + expect(data).to.be.an('array') + expect(data).to.have.lengthOf(1) - resComment = await getVideoThreadComments(servers[2].url, missedVideo2.uuid, threadIdServer2) + threadIdServer2 = data[0].id - const tree: VideoCommentThreadTree = resComment.body + const tree = await servers[2].commentsCommand.getThread({ videoId: missedVideo2.uuid, threadId: threadIdServer2 }) expect(tree.comment.text).equal('thread 1') expect(tree.children).to.have.lengthOf(1) @@ -274,33 +270,30 @@ describe('Test handle downs', function () { it('Should correctly reply to the comment', async function () { this.timeout(15000) - await addVideoCommentReply(servers[2].url, servers[2].accessToken, missedVideo2.uuid, commentIdServer2, 'comment 1-4') + await servers[2].commentsCommand.addReply({ videoId: missedVideo2.uuid, toCommentId: commentIdServer2, text: 'comment 1-4' }) await waitJobs(servers) - { - const resComment = await getVideoThreadComments(servers[0].url, missedVideo2.uuid, threadIdServer1) + const tree = await commentCommands[0].getThread({ videoId: missedVideo2.uuid, threadId: threadIdServer1 }) - const tree: VideoCommentThreadTree = resComment.body - expect(tree.comment.text).equal('thread 1') - expect(tree.children).to.have.lengthOf(1) + expect(tree.comment.text).equal('thread 1') + expect(tree.children).to.have.lengthOf(1) - const firstChild = tree.children[0] - expect(firstChild.comment.text).to.equal('comment 1-1') - expect(firstChild.children).to.have.lengthOf(1) + const firstChild = tree.children[0] + expect(firstChild.comment.text).to.equal('comment 1-1') + expect(firstChild.children).to.have.lengthOf(1) - const childOfFirstChild = firstChild.children[0] - expect(childOfFirstChild.comment.text).to.equal('comment 1-2') - expect(childOfFirstChild.children).to.have.lengthOf(1) + const childOfFirstChild = firstChild.children[0] + expect(childOfFirstChild.comment.text).to.equal('comment 1-2') + expect(childOfFirstChild.children).to.have.lengthOf(1) - const childOfChildFirstChild = childOfFirstChild.children[0] - expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3') - expect(childOfChildFirstChild.children).to.have.lengthOf(1) + const childOfChildFirstChild = childOfFirstChild.children[0] + expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3') + expect(childOfChildFirstChild.children).to.have.lengthOf(1) - const childOfChildOfChildOfFirstChild = childOfChildFirstChild.children[0] - expect(childOfChildOfChildOfFirstChild.comment.text).to.equal('comment 1-4') - expect(childOfChildOfChildOfFirstChild.children).to.have.lengthOf(0) - } + const childOfChildOfChildOfFirstChild = childOfChildFirstChild.children[0] + expect(childOfChildOfChildOfFirstChild.comment.text).to.equal('comment 1-4') + expect(childOfChildOfChildOfFirstChild.children).to.have.lengthOf(0) }) it('Should upload many videos on server 1', async function () { diff --git a/server/tests/api/server/stats.ts b/server/tests/api/server/stats.ts index 9c954c347..ded305899 100644 --- a/server/tests/api/server/stats.ts +++ b/server/tests/api/server/stats.ts @@ -3,7 +3,6 @@ import 'mocha' import * as chai from 'chai' import { - addVideoCommentThread, cleanupTests, createUser, doubleFollow, @@ -42,7 +41,7 @@ describe('Test stats (excluding redundancy)', function () { const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { fixture: 'video_short.webm' }) const videoUUID = resVideo.body.video.uuid - await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'comment') + await servers[0].commentsCommand.createThread({ videoId: videoUUID, text: 'comment' }) await viewVideo(servers[0].url, videoUUID) diff --git a/server/tests/api/users/users.ts b/server/tests/api/users/users.ts index ed670b3c9..4d255f340 100644 --- a/server/tests/api/users/users.ts +++ b/server/tests/api/users/users.ts @@ -4,7 +4,6 @@ import 'mocha' import * as chai from 'chai' import { HttpStatusCode } from '@shared/core-utils' import { - addVideoCommentThread, blockUser, cleanupTests, closeAllSequelize, @@ -983,7 +982,7 @@ describe('Test users', function () { it('Should report correct video comments for user', async function () { const text = 'super comment' - await addVideoCommentThread(server.url, user17AccessToken, videoId, text) + await server.commentsCommand.createThread({ token: user17AccessToken, videoId, text }) const res = await getUserInformation(server.url, server.accessToken, user17Id, true) const user: User = res.body diff --git a/server/tests/api/videos/multiple-servers.ts b/server/tests/api/videos/multiple-servers.ts index a59d5a858..2148d8e1c 100644 --- a/server/tests/api/videos/multiple-servers.ts +++ b/server/tests/api/videos/multiple-servers.ts @@ -3,7 +3,7 @@ import 'mocha' import * as chai from 'chai' import * as request from 'supertest' -import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' +import { HttpStatusCode } from '@shared/core-utils' import { buildAbsoluteFixturePath, checkTmpIsEmpty, @@ -27,18 +27,10 @@ import { userLogin, viewVideo, wait, + waitJobs, webtorrentAdd -} from '../../../../shared/extra-utils' -import { waitJobs } from '../../../../shared/extra-utils/server/jobs' -import { - addVideoCommentReply, - addVideoCommentThread, - deleteVideoComment, - findCommentId, - getVideoCommentThreads, - getVideoThreadComments -} from '../../../../shared/extra-utils/videos/video-comments' -import { VideoComment, VideoCommentThreadTree, VideoPrivacy } from '../../../../shared/models/videos' +} from '@shared/extra-utils' +import { VideoCommentThreadTree, VideoPrivacy } from '@shared/models' const expect = chai.expect @@ -762,36 +754,36 @@ describe('Test multiple servers', function () { { const text = 'my super first comment' - await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, text) + await servers[0].commentsCommand.createThread({ videoId: videoUUID, text }) } { const text = 'my super second comment' - await addVideoCommentThread(servers[2].url, servers[2].accessToken, videoUUID, text) + await servers[2].commentsCommand.createThread({ videoId: videoUUID, text }) } await waitJobs(servers) { - const threadId = await findCommentId(servers[1].url, videoUUID, 'my super first comment') + const threadId = await servers[1].commentsCommand.findCommentId({ videoId: videoUUID, text: 'my super first comment' }) const text = 'my super answer to thread 1' - await addVideoCommentReply(servers[1].url, servers[1].accessToken, videoUUID, threadId, text) + await servers[1].commentsCommand.addReply({ videoId: videoUUID, toCommentId: threadId, text }) } await waitJobs(servers) { - const threadId = await findCommentId(servers[2].url, videoUUID, 'my super first comment') + const threadId = await servers[2].commentsCommand.findCommentId({ videoId: videoUUID, text: 'my super first comment' }) - const res2 = await getVideoThreadComments(servers[2].url, videoUUID, threadId) - const childCommentId = res2.body.children[0].comment.id + const body = await servers[2].commentsCommand.getThread({ videoId: videoUUID, threadId }) + const childCommentId = body.children[0].comment.id const text3 = 'my second answer to thread 1' - await addVideoCommentReply(servers[2].url, servers[2].accessToken, videoUUID, threadId, text3) + await servers[2].commentsCommand.addReply({ videoId: videoUUID, toCommentId: threadId, text: text3 }) const text2 = 'my super answer to answer of thread 1' - await addVideoCommentReply(servers[2].url, servers[2].accessToken, videoUUID, childCommentId, text2) + await servers[2].commentsCommand.addReply({ videoId: videoUUID, toCommentId: childCommentId, text: text2 }) } await waitJobs(servers) @@ -799,14 +791,14 @@ describe('Test multiple servers', function () { it('Should have these threads', async function () { for (const server of servers) { - const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5) + const body = await server.commentsCommand.listThreads({ videoId: videoUUID }) - expect(res.body.total).to.equal(2) - expect(res.body.data).to.be.an('array') - expect(res.body.data).to.have.lengthOf(2) + expect(body.total).to.equal(2) + expect(body.data).to.be.an('array') + expect(body.data).to.have.lengthOf(2) { - const comment: VideoComment = res.body.data.find(c => c.text === 'my super first comment') + const comment = body.data.find(c => c.text === 'my super first comment') expect(comment).to.not.be.undefined expect(comment.inReplyToCommentId).to.be.null expect(comment.account.name).to.equal('root') @@ -817,7 +809,7 @@ describe('Test multiple servers', function () { } { - const comment: VideoComment = res.body.data.find(c => c.text === 'my super second comment') + const comment = body.data.find(c => c.text === 'my super second comment') expect(comment).to.not.be.undefined expect(comment.inReplyToCommentId).to.be.null expect(comment.account.name).to.equal('root') @@ -831,12 +823,11 @@ describe('Test multiple servers', function () { it('Should have these comments', async function () { for (const server of servers) { - const res1 = await getVideoCommentThreads(server.url, videoUUID, 0, 5) - const threadId = res1.body.data.find(c => c.text === 'my super first comment').id + const body = await server.commentsCommand.listThreads({ videoId: videoUUID }) + const threadId = body.data.find(c => c.text === 'my super first comment').id - const res2 = await getVideoThreadComments(server.url, videoUUID, threadId) + const tree = await server.commentsCommand.getThread({ videoId: videoUUID, threadId }) - const tree: VideoCommentThreadTree = res2.body expect(tree.comment.text).equal('my super first comment') expect(tree.comment.account.name).equal('root') expect(tree.comment.account.host).equal('localhost:' + servers[0].port) @@ -865,19 +856,17 @@ describe('Test multiple servers', function () { it('Should delete a reply', async function () { this.timeout(10000) - await deleteVideoComment(servers[2].url, servers[2].accessToken, videoUUID, childOfFirstChild.comment.id) + await servers[2].commentsCommand.delete({ videoId: videoUUID, commentId: childOfFirstChild.comment.id }) await waitJobs(servers) }) it('Should have this comment marked as deleted', async function () { for (const server of servers) { - const res1 = await getVideoCommentThreads(server.url, videoUUID, 0, 5) - const threadId = res1.body.data.find(c => c.text === 'my super first comment').id - - const res2 = await getVideoThreadComments(server.url, videoUUID, threadId) + const { data } = await server.commentsCommand.listThreads({ videoId: videoUUID }) + const threadId = data.find(c => c.text === 'my super first comment').id - const tree: VideoCommentThreadTree = res2.body + const tree = await server.commentsCommand.getThread({ videoId: videoUUID, threadId }) expect(tree.comment.text).equal('my super first comment') const firstChild = tree.children[0] @@ -898,23 +887,23 @@ describe('Test multiple servers', function () { it('Should delete the thread comments', async function () { this.timeout(10000) - const res = await getVideoCommentThreads(servers[0].url, videoUUID, 0, 5) - const threadId = res.body.data.find(c => c.text === 'my super first comment').id - await deleteVideoComment(servers[0].url, servers[0].accessToken, videoUUID, threadId) + const { data } = await servers[0].commentsCommand.listThreads({ videoId: videoUUID }) + const commentId = data.find(c => c.text === 'my super first comment').id + await servers[0].commentsCommand.delete({ videoId: videoUUID, commentId }) await waitJobs(servers) }) it('Should have the threads marked as deleted on other servers too', async function () { for (const server of servers) { - const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5) + const body = await server.commentsCommand.listThreads({ videoId: videoUUID }) - expect(res.body.total).to.equal(2) - expect(res.body.data).to.be.an('array') - expect(res.body.data).to.have.lengthOf(2) + expect(body.total).to.equal(2) + expect(body.data).to.be.an('array') + expect(body.data).to.have.lengthOf(2) { - const comment: VideoComment = res.body.data[0] + const comment = body.data[0] expect(comment).to.not.be.undefined expect(comment.inReplyToCommentId).to.be.null expect(comment.account.name).to.equal('root') @@ -925,7 +914,7 @@ describe('Test multiple servers', function () { } { - const deletedComment: VideoComment = res.body.data[1] + const deletedComment = body.data[1] expect(deletedComment).to.not.be.undefined expect(deletedComment.isDeleted).to.be.true expect(deletedComment.deletedAt).to.not.be.null @@ -943,22 +932,22 @@ describe('Test multiple servers', function () { it('Should delete a remote thread by the origin server', async function () { this.timeout(5000) - const res = await getVideoCommentThreads(servers[0].url, videoUUID, 0, 5) - const threadId = res.body.data.find(c => c.text === 'my super second comment').id - await deleteVideoComment(servers[0].url, servers[0].accessToken, videoUUID, threadId) + const { data } = await servers[0].commentsCommand.listThreads({ videoId: videoUUID }) + const commentId = data.find(c => c.text === 'my super second comment').id + await servers[0].commentsCommand.delete({ videoId: videoUUID, commentId }) await waitJobs(servers) }) it('Should have the threads marked as deleted on other servers too', async function () { for (const server of servers) { - const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5) + const body = await server.commentsCommand.listThreads({ videoId: videoUUID }) - expect(res.body.total).to.equal(2) - expect(res.body.data).to.have.lengthOf(2) + expect(body.total).to.equal(2) + expect(body.data).to.have.lengthOf(2) { - const comment: VideoComment = res.body.data[0] + const comment = body.data[0] expect(comment.text).to.equal('') expect(comment.isDeleted).to.be.true expect(comment.createdAt).to.not.be.null @@ -968,7 +957,7 @@ describe('Test multiple servers', function () { } { - const comment: VideoComment = res.body.data[1] + const comment = body.data[1] expect(comment.text).to.equal('') expect(comment.isDeleted).to.be.true expect(comment.createdAt).to.not.be.null @@ -997,7 +986,7 @@ describe('Test multiple servers', function () { expect(res.body.downloadEnabled).to.be.false const text = 'my super forbidden comment' - await addVideoCommentThread(server.url, server.accessToken, videoUUID, text, HttpStatusCode.CONFLICT_409) + await server.commentsCommand.createThread({ videoId: videoUUID, text, expectedStatus: HttpStatusCode.CONFLICT_409 }) } }) }) diff --git a/server/tests/api/videos/video-comments.ts b/server/tests/api/videos/video-comments.ts index b6b002307..548d9fbf5 100644 --- a/server/tests/api/videos/video-comments.ts +++ b/server/tests/api/videos/video-comments.ts @@ -2,38 +2,33 @@ import 'mocha' import * as chai from 'chai' -import { VideoComment, VideoCommentAdmin, VideoCommentThreadTree } from '@shared/models' -import { cleanupTests, testImage } from '../../../../shared/extra-utils' import { + cleanupTests, + CommentsCommand, createUser, dateIsValid, flushAndRunServer, getAccessToken, ServerInfo, setAccessTokensToServers, + testImage, updateMyAvatar, uploadVideo -} from '../../../../shared/extra-utils/index' -import { - addVideoCommentReply, - addVideoCommentThread, - deleteVideoComment, - getAdminVideoComments, - getVideoCommentThreads, - getVideoThreadComments -} from '../../../../shared/extra-utils/videos/video-comments' +} from '@shared/extra-utils' const expect = chai.expect describe('Test video comments', function () { let server: ServerInfo - let videoId - let videoUUID - let threadId + let videoId: number + let videoUUID: string + let threadId: number let replyToDeleteId: number let userAccessTokenServer1: string + let command: CommentsCommand + before(async function () { this.timeout(30000) @@ -58,24 +53,25 @@ describe('Test video comments', function () { password: 'password' }) userAccessTokenServer1 = await getAccessToken(server.url, 'user1', 'password') + + command = server.commentsCommand }) describe('User comments', function () { it('Should not have threads on this video', async function () { - const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5) + const body = await command.listThreads({ videoId: videoUUID }) - expect(res.body.total).to.equal(0) - expect(res.body.totalNotDeletedComments).to.equal(0) - expect(res.body.data).to.be.an('array') - expect(res.body.data).to.have.lengthOf(0) + expect(body.total).to.equal(0) + expect(body.totalNotDeletedComments).to.equal(0) + expect(body.data).to.be.an('array') + expect(body.data).to.have.lengthOf(0) }) it('Should create a thread in this video', async function () { const text = 'my super first comment' - const res = await addVideoCommentThread(server.url, server.accessToken, videoUUID, text) - const comment = res.body.comment + const comment = await command.createThread({ videoId: videoUUID, text }) expect(comment.inReplyToCommentId).to.be.null expect(comment.text).equal('my super first comment') @@ -91,14 +87,14 @@ describe('Test video comments', function () { }) it('Should list threads of this video', async function () { - const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5) + const body = await command.listThreads({ videoId: videoUUID }) - expect(res.body.total).to.equal(1) - expect(res.body.totalNotDeletedComments).to.equal(1) - expect(res.body.data).to.be.an('array') - expect(res.body.data).to.have.lengthOf(1) + expect(body.total).to.equal(1) + expect(body.totalNotDeletedComments).to.equal(1) + expect(body.data).to.be.an('array') + expect(body.data).to.have.lengthOf(1) - const comment: VideoComment = res.body.data[0] + const comment = body.data[0] expect(comment.inReplyToCommentId).to.be.null expect(comment.text).equal('my super first comment') expect(comment.videoId).to.equal(videoId) @@ -117,9 +113,9 @@ describe('Test video comments', function () { }) it('Should get all the thread created', async function () { - const res = await getVideoThreadComments(server.url, videoUUID, threadId) + const body = await command.getThread({ videoId: videoUUID, threadId }) - const rootComment = res.body.comment + const rootComment = body.comment expect(rootComment.inReplyToCommentId).to.be.null expect(rootComment.text).equal('my super first comment') expect(rootComment.videoId).to.equal(videoId) @@ -129,20 +125,19 @@ describe('Test video comments', function () { it('Should create multiple replies in this thread', async function () { const text1 = 'my super answer to thread 1' - const childCommentRes = await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text1) - const childCommentId = childCommentRes.body.comment.id + const created = await command.addReply({ videoId, toCommentId: threadId, text: text1 }) + const childCommentId = created.id const text2 = 'my super answer to answer of thread 1' - await addVideoCommentReply(server.url, server.accessToken, videoId, childCommentId, text2) + await command.addReply({ videoId, toCommentId: childCommentId, text: text2 }) const text3 = 'my second answer to thread 1' - await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text3) + await command.addReply({ videoId, toCommentId: threadId, text: text3 }) }) it('Should get correctly the replies', async function () { - const res = await getVideoThreadComments(server.url, videoUUID, threadId) + const tree = await command.getThread({ videoId: videoUUID, threadId }) - const tree: VideoCommentThreadTree = res.body expect(tree.comment.text).equal('my super first comment') expect(tree.children).to.have.lengthOf(2) @@ -163,42 +158,41 @@ describe('Test video comments', function () { it('Should create other threads', async function () { const text1 = 'super thread 2' - await addVideoCommentThread(server.url, server.accessToken, videoUUID, text1) + await command.createThread({ videoId: videoUUID, text: text1 }) const text2 = 'super thread 3' - await addVideoCommentThread(server.url, server.accessToken, videoUUID, text2) + await command.createThread({ videoId: videoUUID, text: text2 }) }) it('Should list the threads', async function () { - const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt') - - expect(res.body.total).to.equal(3) - expect(res.body.totalNotDeletedComments).to.equal(6) - expect(res.body.data).to.be.an('array') - expect(res.body.data).to.have.lengthOf(3) - - expect(res.body.data[0].text).to.equal('my super first comment') - expect(res.body.data[0].totalReplies).to.equal(3) - expect(res.body.data[1].text).to.equal('super thread 2') - expect(res.body.data[1].totalReplies).to.equal(0) - expect(res.body.data[2].text).to.equal('super thread 3') - expect(res.body.data[2].totalReplies).to.equal(0) + const body = await command.listThreads({ videoId: videoUUID, sort: 'createdAt' }) + + expect(body.total).to.equal(3) + expect(body.totalNotDeletedComments).to.equal(6) + expect(body.data).to.be.an('array') + expect(body.data).to.have.lengthOf(3) + + expect(body.data[0].text).to.equal('my super first comment') + expect(body.data[0].totalReplies).to.equal(3) + expect(body.data[1].text).to.equal('super thread 2') + expect(body.data[1].totalReplies).to.equal(0) + expect(body.data[2].text).to.equal('super thread 3') + expect(body.data[2].totalReplies).to.equal(0) }) it('Should delete a reply', async function () { - await deleteVideoComment(server.url, server.accessToken, videoId, replyToDeleteId) + await command.delete({ videoId, commentId: replyToDeleteId }) { - const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt') + const body = await command.listThreads({ videoId: videoUUID, sort: 'createdAt' }) - expect(res.body.total).to.equal(3) - expect(res.body.totalNotDeletedComments).to.equal(5) + expect(body.total).to.equal(3) + expect(body.totalNotDeletedComments).to.equal(5) } { - const res = await getVideoThreadComments(server.url, videoUUID, threadId) + const tree = await command.getThread({ videoId: videoUUID, threadId }) - const tree: VideoCommentThreadTree = res.body expect(tree.comment.text).equal('my super first comment') expect(tree.children).to.have.lengthOf(2) @@ -220,99 +214,88 @@ describe('Test video comments', function () { }) it('Should delete a complete thread', async function () { - await deleteVideoComment(server.url, server.accessToken, videoId, threadId) - - const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt') - expect(res.body.total).to.equal(3) - expect(res.body.data).to.be.an('array') - expect(res.body.data).to.have.lengthOf(3) - - expect(res.body.data[0].text).to.equal('') - expect(res.body.data[0].isDeleted).to.be.true - expect(res.body.data[0].deletedAt).to.not.be.null - expect(res.body.data[0].account).to.be.null - expect(res.body.data[0].totalReplies).to.equal(2) - expect(res.body.data[1].text).to.equal('super thread 2') - expect(res.body.data[1].totalReplies).to.equal(0) - expect(res.body.data[2].text).to.equal('super thread 3') - expect(res.body.data[2].totalReplies).to.equal(0) + await command.delete({ videoId, commentId: threadId }) + + const body = await command.listThreads({ videoId: videoUUID, sort: 'createdAt' }) + expect(body.total).to.equal(3) + expect(body.data).to.be.an('array') + expect(body.data).to.have.lengthOf(3) + + expect(body.data[0].text).to.equal('') + expect(body.data[0].isDeleted).to.be.true + expect(body.data[0].deletedAt).to.not.be.null + expect(body.data[0].account).to.be.null + expect(body.data[0].totalReplies).to.equal(2) + expect(body.data[1].text).to.equal('super thread 2') + expect(body.data[1].totalReplies).to.equal(0) + expect(body.data[2].text).to.equal('super thread 3') + expect(body.data[2].totalReplies).to.equal(0) }) it('Should count replies from the video author correctly', async function () { - const text = 'my super first comment' - await addVideoCommentThread(server.url, server.accessToken, videoUUID, text) - let res = await getVideoCommentThreads(server.url, videoUUID, 0, 5) - const comment: VideoComment = res.body.data[0] - const threadId2 = comment.threadId + await command.createThread({ videoId: videoUUID, text: 'my super first comment' }) + + const { data } = await command.listThreads({ videoId: videoUUID }) + const threadId2 = data[0].threadId const text2 = 'a first answer to thread 4 by a third party' - await addVideoCommentReply(server.url, userAccessTokenServer1, videoId, threadId2, text2) + await command.addReply({ token: userAccessTokenServer1, videoId, toCommentId: threadId2, text: text2 }) const text3 = 'my second answer to thread 4' - await addVideoCommentReply(server.url, server.accessToken, videoId, threadId2, text3) + await command.addReply({ videoId, toCommentId: threadId2, text: text3 }) - res = await getVideoThreadComments(server.url, videoUUID, threadId2) - const tree: VideoCommentThreadTree = res.body + const tree = await command.getThread({ videoId: videoUUID, threadId: threadId2 }) expect(tree.comment.totalReplies).to.equal(tree.comment.totalRepliesFromVideoAuthor + 1) }) }) describe('All instance comments', function () { - async function getComments (options: any = {}) { - const res = await getAdminVideoComments(Object.assign({ - url: server.url, - token: server.accessToken, - start: 0, - count: 10 - }, options)) - - return { comments: res.body.data as VideoCommentAdmin[], total: res.body.total as number } - } it('Should list instance comments as admin', async function () { - const { comments } = await getComments({ start: 0, count: 1 }) + const { data } = await command.listForAdmin({ start: 0, count: 1 }) - expect(comments[0].text).to.equal('my second answer to thread 4') + expect(data[0].text).to.equal('my second answer to thread 4') }) it('Should filter instance comments by isLocal', async function () { - const { total, comments } = await getComments({ isLocal: false }) + const { total, data } = await command.listForAdmin({ isLocal: false }) - expect(comments).to.have.lengthOf(0) + expect(data).to.have.lengthOf(0) expect(total).to.equal(0) }) it('Should search instance comments by account', async function () { - const { total, comments } = await getComments({ searchAccount: 'user' }) + const { total, data } = await command.listForAdmin({ searchAccount: 'user' }) - expect(comments).to.have.lengthOf(1) + expect(data).to.have.lengthOf(1) expect(total).to.equal(1) - expect(comments[0].text).to.equal('a first answer to thread 4 by a third party') + expect(data[0].text).to.equal('a first answer to thread 4 by a third party') }) it('Should search instance comments by video', async function () { { - const { total, comments } = await getComments({ searchVideo: 'video' }) + const { total, data } = await command.listForAdmin({ searchVideo: 'video' }) - expect(comments).to.have.lengthOf(7) + expect(data).to.have.lengthOf(7) expect(total).to.equal(7) } { - const { total, comments } = await getComments({ searchVideo: 'hello' }) + const { total, data } = await command.listForAdmin({ searchVideo: 'hello' }) - expect(comments).to.have.lengthOf(0) + expect(data).to.have.lengthOf(0) expect(total).to.equal(0) } }) it('Should search instance comments', async function () { - const { total, comments } = await getComments({ search: 'super thread 3' }) + const { total, data } = await command.listForAdmin({ search: 'super thread 3' }) - expect(comments).to.have.lengthOf(1) expect(total).to.equal(1) - expect(comments[0].text).to.equal('super thread 3') + + expect(data).to.have.lengthOf(1) + expect(data[0].text).to.equal('super thread 3') }) }) diff --git a/server/tests/cli/update-host.ts b/server/tests/cli/update-host.ts index 09a3dd1e7..61a6c403a 100644 --- a/server/tests/cli/update-host.ts +++ b/server/tests/cli/update-host.ts @@ -2,7 +2,6 @@ import 'mocha' import { - addVideoCommentThread, cleanupTests, createUser, flushAndRunServer, @@ -55,7 +54,7 @@ describe('Test update host scripts', function () { // Create comments const text = 'my super first comment' - await addVideoCommentThread(server.url, server.accessToken, video1UUID, text) + await server.commentsCommand.createThread({ videoId: video1UUID, text }) await waitJobs(server) }) diff --git a/server/tests/feeds/feeds.ts b/server/tests/feeds/feeds.ts index 6ee22340b..610849105 100644 --- a/server/tests/feeds/feeds.ts +++ b/server/tests/feeds/feeds.ts @@ -5,7 +5,6 @@ import * as chai from 'chai' import * as xmlParser from 'fast-xml-parser' import { HttpStatusCode } from '@shared/core-utils' import { - addVideoCommentThread, cleanupTests, createUser, doubleFollow, @@ -90,8 +89,8 @@ describe('Test syndication feeds', () => { const res = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes) const videoId = res.body.video.id - await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoId, 'super comment 1') - await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoId, 'super comment 2') + await servers[0].commentsCommand.createThread({ videoId, text: 'super comment 1' }) + await servers[0].commentsCommand.createThread({ videoId, text: 'super comment 2' }) } { @@ -99,7 +98,7 @@ describe('Test syndication feeds', () => { const res = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes) const videoId = res.body.video.id - await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoId, 'comment on unlisted video') + await servers[0].commentsCommand.createThread({ videoId, text: 'comment on unlisted video' }) } await waitJobs(servers) @@ -277,7 +276,7 @@ describe('Test syndication feeds', () => { { const videoUUID = (await uploadVideoAndGetId({ server: servers[1], videoName: 'server 2' })).uuid await waitJobs(servers) - await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'super comment') + await servers[0].commentsCommand.createThread({ videoId: videoUUID, text: 'super comment' }) await waitJobs(servers) const json = await servers[1].feedCommand.getJSON({ feed: 'video-comments', query: { version: 3 } }) diff --git a/server/tests/plugins/action-hooks.ts b/server/tests/plugins/action-hooks.ts index fd83bf2ac..6975ca4bb 100644 --- a/server/tests/plugins/action-hooks.ts +++ b/server/tests/plugins/action-hooks.ts @@ -3,11 +3,8 @@ import 'mocha' import { ServerHookName, VideoPlaylistPrivacy, VideoPrivacy } from '@shared/models' import { - addVideoCommentReply, - addVideoCommentThread, blockUser, createUser, - deleteVideoComment, PluginsCommand, registerUser, removeUser, @@ -101,20 +98,20 @@ describe('Test plugin action hooks', function () { describe('Comments hooks', function () { it('Should run action:api.video-thread.created', async function () { - const res = await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'thread') - threadId = res.body.comment.id + const created = await servers[0].commentsCommand.createThread({ videoId: videoUUID, text: 'thread' }) + threadId = created.id await checkHook('action:api.video-thread.created') }) it('Should run action:api.video-comment-reply.created', async function () { - await addVideoCommentReply(servers[0].url, servers[0].accessToken, videoUUID, threadId, 'reply') + await servers[0].commentsCommand.addReply({ videoId: videoUUID, toCommentId: threadId, text: 'reply' }) await checkHook('action:api.video-comment-reply.created') }) it('Should run action:api.video-comment.deleted', async function () { - await deleteVideoComment(servers[0].url, servers[0].accessToken, videoUUID, threadId) + await servers[0].commentsCommand.delete({ videoId: videoUUID, commentId: threadId }) await checkHook('action:api.video-comment.deleted') }) diff --git a/server/tests/plugins/filter-hooks.ts b/server/tests/plugins/filter-hooks.ts index a3c3c0551..c235508e8 100644 --- a/server/tests/plugins/filter-hooks.ts +++ b/server/tests/plugins/filter-hooks.ts @@ -4,8 +4,6 @@ import 'mocha' import * as chai from 'chai' import { HttpStatusCode } from '@shared/core-utils' import { - addVideoCommentReply, - addVideoCommentThread, cleanupTests, doubleFollow, flushAndRunMultipleServers, @@ -13,10 +11,8 @@ import { getMyVideos, getVideo, getVideoChannelVideos, - getVideoCommentThreads, getVideosList, getVideosListPagination, - getVideoThreadComments, getVideoWithToken, ImportsCommand, makeRawRequest, @@ -31,7 +27,7 @@ import { waitJobs, waitUntilLog } from '@shared/extra-utils' -import { VideoCommentThreadTree, VideoDetails, VideoImportState, VideoPlaylist, VideoPlaylistPrivacy, VideoPrivacy } from '@shared/models' +import { VideoDetails, VideoImportState, VideoPlaylist, VideoPlaylistPrivacy, VideoPrivacy } from '@shared/models' const expect = chai.expect @@ -226,44 +222,50 @@ describe('Test plugin filter hooks', function () { }) it('Should run filter:api.video-thread.create.accept.result', async function () { - await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'comment with bad word', HttpStatusCode.FORBIDDEN_403) + await servers[0].commentsCommand.createThread({ + videoId: videoUUID, + text: 'comment with bad word', + expectedStatus: HttpStatusCode.FORBIDDEN_403 + }) }) it('Should run filter:api.video-comment-reply.create.accept.result', async function () { - const res = await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'thread') - threadId = res.body.comment.id - - await addVideoCommentReply( - servers[0].url, - servers[0].accessToken, - videoUUID, - threadId, - 'comment with bad word', - HttpStatusCode.FORBIDDEN_403 - ) - await addVideoCommentReply(servers[0].url, servers[0].accessToken, videoUUID, threadId, 'comment with good word', HttpStatusCode.OK_200) + const created = await servers[0].commentsCommand.createThread({ videoId: videoUUID, text: 'thread' }) + threadId = created.id + + await servers[0].commentsCommand.addReply({ + videoId: videoUUID, + toCommentId: threadId, + text: 'comment with bad word', + expectedStatus: HttpStatusCode.FORBIDDEN_403 + }) + await servers[0].commentsCommand.addReply({ + videoId: videoUUID, + toCommentId: threadId, + text: 'comment with good word', + expectedStatus: HttpStatusCode.OK_200 + }) }) it('Should run filter:api.video-threads.list.params', async function () { - const res = await getVideoCommentThreads(servers[0].url, videoUUID, 0, 0) + const { data } = await servers[0].commentsCommand.listThreads({ videoId: videoUUID, start: 0, count: 0 }) // our plugin do +1 to the count parameter - expect(res.body.data).to.have.lengthOf(1) + expect(data).to.have.lengthOf(1) }) it('Should run filter:api.video-threads.list.result', async function () { - const res = await getVideoCommentThreads(servers[0].url, videoUUID, 0, 0) + const { total } = await servers[0].commentsCommand.listThreads({ videoId: videoUUID, start: 0, count: 0 }) // Plugin do +1 to the total result - expect(res.body.total).to.equal(2) + expect(total).to.equal(2) }) it('Should run filter:api.video-thread-comments.list.params') it('Should run filter:api.video-thread-comments.list.result', async function () { - const res = await getVideoThreadComments(servers[0].url, videoUUID, threadId) + const thread = await servers[0].commentsCommand.getThread({ videoId: videoUUID, threadId }) - const thread = res.body as VideoCommentThreadTree expect(thread.comment.text.endsWith(' <3')).to.be.true }) diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts index 68e10af5f..8e80a9842 100644 --- a/shared/extra-utils/server/servers.ts +++ b/shared/extra-utils/server/servers.ts @@ -30,6 +30,7 @@ import { ServicesCommand, StreamingPlaylistsCommand } from '../videos' +import { CommentsCommand } from '../videos/comments-command' import { ConfigCommand } from './config-command' import { ContactFormCommand } from './contact-form-command' import { DebugCommand } from './debug-command' @@ -121,6 +122,7 @@ interface ServerInfo { importsCommand?: ImportsCommand streamingPlaylistsCommand?: StreamingPlaylistsCommand channelsCommand?: ChannelsCommand + commentsCommand?: CommentsCommand } function parallelTests () { @@ -356,6 +358,7 @@ async function runServer (server: ServerInfo, configOverrideArg?: any, args = [] server.importsCommand = new ImportsCommand(server) server.streamingPlaylistsCommand = new StreamingPlaylistsCommand(server) server.channelsCommand = new ChannelsCommand(server) + server.commentsCommand = new CommentsCommand(server) res(server) }) diff --git a/shared/extra-utils/videos/comments-command.ts b/shared/extra-utils/videos/comments-command.ts new file mode 100644 index 000000000..b31f3e2dd --- /dev/null +++ b/shared/extra-utils/videos/comments-command.ts @@ -0,0 +1,132 @@ +import { pick } from 'lodash' +import { ResultList, VideoComment, VideoCommentThreads, VideoCommentThreadTree } from '@shared/models' +import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' +import { unwrapBody } from '../requests' +import { AbstractCommand, OverrideCommandOptions } from '../shared' + +export class CommentsCommand extends AbstractCommand { + + listForAdmin (options: OverrideCommandOptions & { + start?: number + count?: number + sort?: string + isLocal?: boolean + search?: string + searchAccount?: string + searchVideo?: string + } = {}) { + const { sort = '-createdAt' } = options + const path = '/api/v1/videos/comments' + + const query = { sort, ...pick(options, [ 'start', 'count', 'isLocal', 'search', 'searchAccount', 'searchVideo' ]) } + + return this.getRequestBody>({ + ...options, + + path, + query, + implicitToken: true, + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + } + + listThreads (options: OverrideCommandOptions & { + videoId: number | string + start?: number + count?: number + sort?: string + }) { + const { start, count, sort, videoId } = options + const path = '/api/v1/videos/' + videoId + '/comment-threads' + + return this.getRequestBody({ + ...options, + + path, + query: { start, count, sort }, + implicitToken: false, + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + } + + getThread (options: OverrideCommandOptions & { + videoId: number | string + threadId: number + }) { + const { videoId, threadId } = options + const path = '/api/v1/videos/' + videoId + '/comment-threads/' + threadId + + return this.getRequestBody({ + ...options, + + path, + implicitToken: false, + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + } + + async createThread (options: OverrideCommandOptions & { + videoId: number | string + text: string + }) { + const { videoId, text } = options + const path = '/api/v1/videos/' + videoId + '/comment-threads' + + const body = await unwrapBody<{ comment: VideoComment }>(this.postBodyRequest({ + ...options, + + path, + fields: { text }, + implicitToken: true, + defaultExpectedStatus: HttpStatusCode.OK_200 + })) + + return body.comment + } + + async addReply (options: OverrideCommandOptions & { + videoId: number | string + toCommentId: number + text: string + }) { + const { videoId, toCommentId, text } = options + const path = '/api/v1/videos/' + videoId + '/comments/' + toCommentId + + const body = await unwrapBody<{ comment: VideoComment }>(this.postBodyRequest({ + ...options, + + path, + fields: { text }, + implicitToken: true, + defaultExpectedStatus: HttpStatusCode.OK_200 + })) + + return body.comment + } + + async findCommentId (options: OverrideCommandOptions & { + videoId: number | string + text: string + }) { + const { videoId, text } = options + const { data } = await this.listThreads({ videoId, count: 25, sort: '-createdAt' }) + + return data.find(c => c.text === text).id + } + + delete (options: OverrideCommandOptions & { + videoId: number | string + commentId: number + }) { + const { videoId, commentId } = options + const path = '/api/v1/videos/' + videoId + '/comments/' + commentId + + return this.deleteRequest({ + ...options, + + path, + implicitToken: true, + defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 + }) + } +} diff --git a/shared/extra-utils/videos/index.ts b/shared/extra-utils/videos/index.ts index 3bc219281..652d82842 100644 --- a/shared/extra-utils/videos/index.ts +++ b/shared/extra-utils/videos/index.ts @@ -4,6 +4,7 @@ export * from './captions' export * from './change-ownership-command' export * from './channels' export * from './channels-command' +export * from './comments-command' export * from './history-command' export * from './imports-command' export * from './live-command' @@ -13,5 +14,5 @@ export * from './playlists' export * from './services-command' export * from './streaming-playlists-command' export * from './streaming-playlists' -export * from './video-comments' +export * from './comments-command' export * from './videos' diff --git a/shared/extra-utils/videos/streaming-playlists-command.ts b/shared/extra-utils/videos/streaming-playlists-command.ts index 4caec7137..b109597c9 100644 --- a/shared/extra-utils/videos/streaming-playlists-command.ts +++ b/shared/extra-utils/videos/streaming-playlists-command.ts @@ -27,7 +27,7 @@ export class StreamingPlaylistsCommand extends AbstractCommand { url: options.url, range: options.range, implicitToken: false, - defaultExpectedStatus: HttpStatusCode.OK_200, + defaultExpectedStatus: HttpStatusCode.OK_200 })) } diff --git a/shared/extra-utils/videos/video-comments.ts b/shared/extra-utils/videos/video-comments.ts deleted file mode 100644 index 71b9f875a..000000000 --- a/shared/extra-utils/videos/video-comments.ts +++ /dev/null @@ -1,138 +0,0 @@ -/* eslint-disable @typescript-eslint/no-floating-promises */ - -import * as request from 'supertest' -import { makeDeleteRequest, makeGetRequest } from '../requests/requests' -import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' - -function getAdminVideoComments (options: { - url: string - token: string - start: number - count: number - sort?: string - isLocal?: boolean - search?: string - searchAccount?: string - searchVideo?: string -}) { - const { url, token, start, count, sort, isLocal, search, searchAccount, searchVideo } = options - const path = '/api/v1/videos/comments' - - const query = { - start, - count, - sort: sort || '-createdAt' - } - - if (isLocal !== undefined) Object.assign(query, { isLocal }) - if (search !== undefined) Object.assign(query, { search }) - if (searchAccount !== undefined) Object.assign(query, { searchAccount }) - if (searchVideo !== undefined) Object.assign(query, { searchVideo }) - - return makeGetRequest({ - url, - path, - token, - query, - statusCodeExpected: HttpStatusCode.OK_200 - }) -} - -function getVideoCommentThreads (url: string, videoId: number | string, start: number, count: number, sort?: string, token?: string) { - const path = '/api/v1/videos/' + videoId + '/comment-threads' - - const req = request(url) - .get(path) - .query({ start: start }) - .query({ count: count }) - - if (sort) req.query({ sort }) - if (token) req.set('Authorization', 'Bearer ' + token) - - return req.set('Accept', 'application/json') - .expect(HttpStatusCode.OK_200) - .expect('Content-Type', /json/) -} - -function getVideoThreadComments (url: string, videoId: number | string, threadId: number, token?: string) { - const path = '/api/v1/videos/' + videoId + '/comment-threads/' + threadId - - const req = request(url) - .get(path) - .set('Accept', 'application/json') - - if (token) req.set('Authorization', 'Bearer ' + token) - - return req.expect(HttpStatusCode.OK_200) - .expect('Content-Type', /json/) -} - -function addVideoCommentThread ( - url: string, - token: string, - videoId: number | string, - text: string, - expectedStatus = HttpStatusCode.OK_200 -) { - const path = '/api/v1/videos/' + videoId + '/comment-threads' - - return request(url) - .post(path) - .send({ text }) - .set('Accept', 'application/json') - .set('Authorization', 'Bearer ' + token) - .expect(expectedStatus) -} - -function addVideoCommentReply ( - url: string, - token: string, - videoId: number | string, - inReplyToCommentId: number, - text: string, - expectedStatus = HttpStatusCode.OK_200 -) { - const path = '/api/v1/videos/' + videoId + '/comments/' + inReplyToCommentId - - return request(url) - .post(path) - .send({ text }) - .set('Accept', 'application/json') - .set('Authorization', 'Bearer ' + token) - .expect(expectedStatus) -} - -async function findCommentId (url: string, videoId: number | string, text: string) { - const res = await getVideoCommentThreads(url, videoId, 0, 25, '-createdAt') - - return res.body.data.find(c => c.text === text).id as number -} - -function deleteVideoComment ( - url: string, - token: string, - videoId: number | string, - commentId: number, - statusCodeExpected = HttpStatusCode.NO_CONTENT_204 -) { - const path = '/api/v1/videos/' + videoId + '/comments/' + commentId - - return makeDeleteRequest({ - url, - path, - token, - statusCodeExpected - }) -} - -// --------------------------------------------------------------------------- - -export { - getVideoCommentThreads, - getAdminVideoComments, - getVideoThreadComments, - addVideoCommentThread, - addVideoCommentReply, - findCommentId, - deleteVideoComment -} diff --git a/shared/models/videos/comment/index.ts b/shared/models/videos/comment/index.ts index 7b9261a36..80c6c0724 100644 --- a/shared/models/videos/comment/index.ts +++ b/shared/models/videos/comment/index.ts @@ -1 +1,2 @@ +export * from './video-comment-create.model' export * from './video-comment.model' diff --git a/shared/models/videos/comment/video-comment-create.model.ts b/shared/models/videos/comment/video-comment-create.model.ts new file mode 100644 index 000000000..1f0135405 --- /dev/null +++ b/shared/models/videos/comment/video-comment-create.model.ts @@ -0,0 +1,3 @@ +export interface VideoCommentCreate { + text: string +} diff --git a/shared/models/videos/comment/video-comment.model.ts b/shared/models/videos/comment/video-comment.model.ts index 79c0e4c0a..5a96f9d4a 100644 --- a/shared/models/videos/comment/video-comment.model.ts +++ b/shared/models/videos/comment/video-comment.model.ts @@ -1,3 +1,4 @@ +import { ResultList } from '@shared/models/common' import { Account } from '../../actors' export interface VideoComment { @@ -36,11 +37,9 @@ export interface VideoCommentAdmin { } } +export type VideoCommentThreads = ResultList & { totalNotDeletedComments: number } + export interface VideoCommentThreadTree { comment: VideoComment children: VideoCommentThreadTree[] } - -export interface VideoCommentCreate { - text: string -}