From 12edc1495a36b2199f1bf1ba37f50c7b694be382 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 9 Jul 2021 14:15:11 +0200 Subject: Introduce comments command --- shared/extra-utils/server/servers.ts | 3 + shared/extra-utils/videos/comments-command.ts | 132 ++++++++++++++++++++ shared/extra-utils/videos/index.ts | 3 +- .../videos/streaming-playlists-command.ts | 2 +- shared/extra-utils/videos/video-comments.ts | 138 --------------------- shared/models/videos/comment/index.ts | 1 + .../videos/comment/video-comment-create.model.ts | 3 + .../models/videos/comment/video-comment.model.ts | 7 +- 8 files changed, 145 insertions(+), 144 deletions(-) create mode 100644 shared/extra-utils/videos/comments-command.ts delete mode 100644 shared/extra-utils/videos/video-comments.ts create mode 100644 shared/models/videos/comment/video-comment-create.model.ts (limited to 'shared') 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 -} -- cgit v1.2.3