1 import { pick } from 'lodash'
2 import { HttpStatusCode, ResultList, VideoComment, VideoCommentThreads, VideoCommentThreadTree } from '@shared/models'
3 import { unwrapBody } from '../requests'
4 import { AbstractCommand, OverrideCommandOptions } from '../shared'
6 export class CommentsCommand extends AbstractCommand {
8 listForAdmin (options: OverrideCommandOptions & {
14 searchAccount?: string
17 const { sort = '-createdAt' } = options
18 const path = '/api/v1/videos/comments'
20 const query = { sort, ...pick(options, [ 'start', 'count', 'isLocal', 'search', 'searchAccount', 'searchVideo' ]) }
22 return this.getRequestBody<ResultList<VideoComment>>({
28 defaultExpectedStatus: HttpStatusCode.OK_200
32 listThreads (options: OverrideCommandOptions & {
33 videoId: number | string
38 const { start, count, sort, videoId } = options
39 const path = '/api/v1/videos/' + videoId + '/comment-threads'
41 return this.getRequestBody<VideoCommentThreads>({
45 query: { start, count, sort },
47 defaultExpectedStatus: HttpStatusCode.OK_200
51 getThread (options: OverrideCommandOptions & {
52 videoId: number | string
55 const { videoId, threadId } = options
56 const path = '/api/v1/videos/' + videoId + '/comment-threads/' + threadId
58 return this.getRequestBody<VideoCommentThreadTree>({
63 defaultExpectedStatus: HttpStatusCode.OK_200
67 async createThread (options: OverrideCommandOptions & {
68 videoId: number | string
71 const { videoId, text } = options
72 const path = '/api/v1/videos/' + videoId + '/comment-threads'
74 const body = await unwrapBody<{ comment: VideoComment }>(this.postBodyRequest({
80 defaultExpectedStatus: HttpStatusCode.OK_200
86 async addReply (options: OverrideCommandOptions & {
87 videoId: number | string
91 const { videoId, toCommentId, text } = options
92 const path = '/api/v1/videos/' + videoId + '/comments/' + toCommentId
94 const body = await unwrapBody<{ comment: VideoComment }>(this.postBodyRequest({
100 defaultExpectedStatus: HttpStatusCode.OK_200
106 async findCommentId (options: OverrideCommandOptions & {
107 videoId: number | string
110 const { videoId, text } = options
111 const { data } = await this.listThreads({ videoId, count: 25, sort: '-createdAt' })
113 return data.find(c => c.text === text).id
116 delete (options: OverrideCommandOptions & {
117 videoId: number | string
120 const { videoId, commentId } = options
121 const path = '/api/v1/videos/' + videoId + '/comments/' + commentId
123 return this.deleteRequest({
128 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204