1 import { pick } from 'lodash'
2 import { ResultList, VideoComment, VideoCommentThreads, VideoCommentThreadTree } from '@shared/models'
3 import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes'
4 import { unwrapBody } from '../requests'
5 import { AbstractCommand, OverrideCommandOptions } from '../shared'
7 export class CommentsCommand extends AbstractCommand {
9 listForAdmin (options: OverrideCommandOptions & {
15 searchAccount?: string
18 const { sort = '-createdAt' } = options
19 const path = '/api/v1/videos/comments'
21 const query = { sort, ...pick(options, [ 'start', 'count', 'isLocal', 'search', 'searchAccount', 'searchVideo' ]) }
23 return this.getRequestBody<ResultList<VideoComment>>({
29 defaultExpectedStatus: HttpStatusCode.OK_200
33 listThreads (options: OverrideCommandOptions & {
34 videoId: number | string
39 const { start, count, sort, videoId } = options
40 const path = '/api/v1/videos/' + videoId + '/comment-threads'
42 return this.getRequestBody<VideoCommentThreads>({
46 query: { start, count, sort },
48 defaultExpectedStatus: HttpStatusCode.OK_200
52 getThread (options: OverrideCommandOptions & {
53 videoId: number | string
56 const { videoId, threadId } = options
57 const path = '/api/v1/videos/' + videoId + '/comment-threads/' + threadId
59 return this.getRequestBody<VideoCommentThreadTree>({
64 defaultExpectedStatus: HttpStatusCode.OK_200
68 async createThread (options: OverrideCommandOptions & {
69 videoId: number | string
72 const { videoId, text } = options
73 const path = '/api/v1/videos/' + videoId + '/comment-threads'
75 const body = await unwrapBody<{ comment: VideoComment }>(this.postBodyRequest({
81 defaultExpectedStatus: HttpStatusCode.OK_200
87 async addReply (options: OverrideCommandOptions & {
88 videoId: number | string
92 const { videoId, toCommentId, text } = options
93 const path = '/api/v1/videos/' + videoId + '/comments/' + toCommentId
95 const body = await unwrapBody<{ comment: VideoComment }>(this.postBodyRequest({
101 defaultExpectedStatus: HttpStatusCode.OK_200
107 async findCommentId (options: OverrideCommandOptions & {
108 videoId: number | string
111 const { videoId, text } = options
112 const { data } = await this.listThreads({ videoId, count: 25, sort: '-createdAt' })
114 return data.find(c => c.text === text).id
117 delete (options: OverrideCommandOptions & {
118 videoId: number | string
121 const { videoId, commentId } = options
122 const path = '/api/v1/videos/' + videoId + '/comments/' + commentId
124 return this.deleteRequest({
129 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204