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 & {
17 onLocalVideo?: boolean
19 searchAccount?: string
22 const { sort = '-createdAt' } = options
23 const path = '/api/v1/videos/comments'
25 const query = { sort, ...pick(options, [ 'start', 'count', 'isLocal', 'onLocalVideo', 'search', 'searchAccount', 'searchVideo' ]) }
27 return this.getRequestBody<ResultList<VideoComment>>({
33 defaultExpectedStatus: HttpStatusCode.OK_200
37 listThreads (options: OverrideCommandOptions & {
38 videoId: number | string
43 const { start, count, sort, videoId } = options
44 const path = '/api/v1/videos/' + videoId + '/comment-threads'
46 return this.getRequestBody<VideoCommentThreads>({
50 query: { start, count, sort },
52 defaultExpectedStatus: HttpStatusCode.OK_200
56 getThread (options: OverrideCommandOptions & {
57 videoId: number | string
60 const { videoId, threadId } = options
61 const path = '/api/v1/videos/' + videoId + '/comment-threads/' + threadId
63 return this.getRequestBody<VideoCommentThreadTree>({
68 defaultExpectedStatus: HttpStatusCode.OK_200
72 async createThread (options: OverrideCommandOptions & {
73 videoId: number | string
76 const { videoId, text } = options
77 const path = '/api/v1/videos/' + videoId + '/comment-threads'
79 const body = await unwrapBody<{ comment: VideoComment }>(this.postBodyRequest({
85 defaultExpectedStatus: HttpStatusCode.OK_200
88 this.lastThreadId = body.comment?.id
89 this.lastVideoId = videoId
94 async addReply (options: OverrideCommandOptions & {
95 videoId: number | string
99 const { videoId, toCommentId, text } = options
100 const path = '/api/v1/videos/' + videoId + '/comments/' + toCommentId
102 const body = await unwrapBody<{ comment: VideoComment }>(this.postBodyRequest({
108 defaultExpectedStatus: HttpStatusCode.OK_200
111 this.lastReplyId = body.comment?.id
116 async addReplyToLastReply (options: OverrideCommandOptions & {
119 return this.addReply({ ...options, videoId: this.lastVideoId, toCommentId: this.lastReplyId })
122 async addReplyToLastThread (options: OverrideCommandOptions & {
125 return this.addReply({ ...options, videoId: this.lastVideoId, toCommentId: this.lastThreadId })
128 async findCommentId (options: OverrideCommandOptions & {
129 videoId: number | string
132 const { videoId, text } = options
133 const { data } = await this.listThreads({ videoId, count: 25, sort: '-createdAt' })
135 return data.find(c => c.text === text).id
138 delete (options: OverrideCommandOptions & {
139 videoId: number | string
142 const { videoId, commentId } = options
143 const path = '/api/v1/videos/' + videoId + '/comments/' + commentId
145 return this.deleteRequest({
150 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204