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 private lastVideoId: number | string
9 private lastThreadId: number
10 private lastReplyId: number
12 listForAdmin (options: OverrideCommandOptions & {
18 searchAccount?: string
21 const { sort = '-createdAt' } = options
22 const path = '/api/v1/videos/comments'
24 const query = { sort, ...pick(options, [ 'start', 'count', 'isLocal', 'search', 'searchAccount', 'searchVideo' ]) }
26 return this.getRequestBody<ResultList<VideoComment>>({
32 defaultExpectedStatus: HttpStatusCode.OK_200
36 listThreads (options: OverrideCommandOptions & {
37 videoId: number | string
42 const { start, count, sort, videoId } = options
43 const path = '/api/v1/videos/' + videoId + '/comment-threads'
45 return this.getRequestBody<VideoCommentThreads>({
49 query: { start, count, sort },
51 defaultExpectedStatus: HttpStatusCode.OK_200
55 getThread (options: OverrideCommandOptions & {
56 videoId: number | string
59 const { videoId, threadId } = options
60 const path = '/api/v1/videos/' + videoId + '/comment-threads/' + threadId
62 return this.getRequestBody<VideoCommentThreadTree>({
67 defaultExpectedStatus: HttpStatusCode.OK_200
71 async createThread (options: OverrideCommandOptions & {
72 videoId: number | string
75 const { videoId, text } = options
76 const path = '/api/v1/videos/' + videoId + '/comment-threads'
78 const body = await unwrapBody<{ comment: VideoComment }>(this.postBodyRequest({
84 defaultExpectedStatus: HttpStatusCode.OK_200
87 this.lastThreadId = body.comment.id
88 this.lastVideoId = videoId
93 async addReply (options: OverrideCommandOptions & {
94 videoId: number | string
98 const { videoId, toCommentId, text } = options
99 const path = '/api/v1/videos/' + videoId + '/comments/' + toCommentId
101 const body = await unwrapBody<{ comment: VideoComment }>(this.postBodyRequest({
107 defaultExpectedStatus: HttpStatusCode.OK_200
110 this.lastReplyId = body.comment.id
115 async addReplyToLastReply (options: OverrideCommandOptions & {
118 return this.addReply({ ...options, videoId: this.lastVideoId, toCommentId: this.lastReplyId })
121 async addReplyToLastThread (options: OverrideCommandOptions & {
124 return this.addReply({ ...options, videoId: this.lastVideoId, toCommentId: this.lastThreadId })
127 async findCommentId (options: OverrideCommandOptions & {
128 videoId: number | string
131 const { videoId, text } = options
132 const { data } = await this.listThreads({ videoId, count: 25, sort: '-createdAt' })
134 return data.find(c => c.text === text).id
137 delete (options: OverrideCommandOptions & {
138 videoId: number | string
141 const { videoId, commentId } = options
142 const path = '/api/v1/videos/' + videoId + '/comments/' + commentId
144 return this.deleteRequest({
149 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204