2022-08-17 08:52:23 -04:00
|
|
|
import { pick } from '@shared/core-utils'
|
2021-07-16 08:27:30 -04:00
|
|
|
import { HttpStatusCode, ResultList, VideoComment, VideoCommentThreads, VideoCommentThreadTree } from '@shared/models'
|
2021-07-09 08:15:11 -04:00
|
|
|
import { unwrapBody } from '../requests'
|
|
|
|
import { AbstractCommand, OverrideCommandOptions } from '../shared'
|
|
|
|
|
|
|
|
export class CommentsCommand extends AbstractCommand {
|
|
|
|
|
2021-07-20 08:15:15 -04:00
|
|
|
private lastVideoId: number | string
|
|
|
|
private lastThreadId: number
|
|
|
|
private lastReplyId: number
|
|
|
|
|
2021-07-09 08:15:11 -04:00
|
|
|
listForAdmin (options: OverrideCommandOptions & {
|
|
|
|
start?: number
|
|
|
|
count?: number
|
|
|
|
sort?: string
|
|
|
|
isLocal?: boolean
|
2022-08-12 05:05:11 -04:00
|
|
|
onLocalVideo?: boolean
|
2021-07-09 08:15:11 -04:00
|
|
|
search?: string
|
|
|
|
searchAccount?: string
|
|
|
|
searchVideo?: string
|
|
|
|
} = {}) {
|
|
|
|
const { sort = '-createdAt' } = options
|
|
|
|
const path = '/api/v1/videos/comments'
|
|
|
|
|
2022-08-12 05:05:11 -04:00
|
|
|
const query = { sort, ...pick(options, [ 'start', 'count', 'isLocal', 'onLocalVideo', 'search', 'searchAccount', 'searchVideo' ]) }
|
2021-07-09 08:15:11 -04:00
|
|
|
|
|
|
|
return this.getRequestBody<ResultList<VideoComment>>({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path,
|
|
|
|
query,
|
|
|
|
implicitToken: true,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.OK_200
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
listThreads (options: OverrideCommandOptions & {
|
|
|
|
videoId: number | string
|
2023-06-29 03:48:55 -04:00
|
|
|
videoPassword?: string
|
2021-07-09 08:15:11 -04:00
|
|
|
start?: number
|
|
|
|
count?: number
|
|
|
|
sort?: string
|
|
|
|
}) {
|
2023-06-29 03:48:55 -04:00
|
|
|
const { start, count, sort, videoId, videoPassword } = options
|
2021-07-09 08:15:11 -04:00
|
|
|
const path = '/api/v1/videos/' + videoId + '/comment-threads'
|
|
|
|
|
|
|
|
return this.getRequestBody<VideoCommentThreads>({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path,
|
|
|
|
query: { start, count, sort },
|
2023-06-29 03:48:55 -04:00
|
|
|
headers: this.buildVideoPasswordHeader(videoPassword),
|
2021-07-09 08:15:11 -04:00
|
|
|
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<VideoCommentThreadTree>({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path,
|
|
|
|
implicitToken: false,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.OK_200
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async createThread (options: OverrideCommandOptions & {
|
|
|
|
videoId: number | string
|
|
|
|
text: string
|
2023-06-29 03:48:55 -04:00
|
|
|
videoPassword?: string
|
2021-07-09 08:15:11 -04:00
|
|
|
}) {
|
2023-06-29 03:48:55 -04:00
|
|
|
const { videoId, text, videoPassword } = options
|
2021-07-09 08:15:11 -04:00
|
|
|
const path = '/api/v1/videos/' + videoId + '/comment-threads'
|
|
|
|
|
|
|
|
const body = await unwrapBody<{ comment: VideoComment }>(this.postBodyRequest({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path,
|
|
|
|
fields: { text },
|
2023-06-29 03:48:55 -04:00
|
|
|
headers: this.buildVideoPasswordHeader(videoPassword),
|
2021-07-09 08:15:11 -04:00
|
|
|
implicitToken: true,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.OK_200
|
|
|
|
}))
|
|
|
|
|
2021-07-21 07:58:35 -04:00
|
|
|
this.lastThreadId = body.comment?.id
|
2021-07-20 08:15:15 -04:00
|
|
|
this.lastVideoId = videoId
|
|
|
|
|
2021-07-09 08:15:11 -04:00
|
|
|
return body.comment
|
|
|
|
}
|
|
|
|
|
|
|
|
async addReply (options: OverrideCommandOptions & {
|
|
|
|
videoId: number | string
|
|
|
|
toCommentId: number
|
|
|
|
text: string
|
2023-06-29 03:48:55 -04:00
|
|
|
videoPassword?: string
|
2021-07-09 08:15:11 -04:00
|
|
|
}) {
|
2023-06-29 03:48:55 -04:00
|
|
|
const { videoId, toCommentId, text, videoPassword } = options
|
2021-07-09 08:15:11 -04:00
|
|
|
const path = '/api/v1/videos/' + videoId + '/comments/' + toCommentId
|
|
|
|
|
|
|
|
const body = await unwrapBody<{ comment: VideoComment }>(this.postBodyRequest({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path,
|
|
|
|
fields: { text },
|
2023-06-29 03:48:55 -04:00
|
|
|
headers: this.buildVideoPasswordHeader(videoPassword),
|
2021-07-09 08:15:11 -04:00
|
|
|
implicitToken: true,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.OK_200
|
|
|
|
}))
|
|
|
|
|
2021-07-21 07:58:35 -04:00
|
|
|
this.lastReplyId = body.comment?.id
|
2021-07-20 08:15:15 -04:00
|
|
|
|
2021-07-09 08:15:11 -04:00
|
|
|
return body.comment
|
|
|
|
}
|
|
|
|
|
2021-07-20 08:15:15 -04:00
|
|
|
async addReplyToLastReply (options: OverrideCommandOptions & {
|
|
|
|
text: string
|
|
|
|
}) {
|
|
|
|
return this.addReply({ ...options, videoId: this.lastVideoId, toCommentId: this.lastReplyId })
|
|
|
|
}
|
|
|
|
|
|
|
|
async addReplyToLastThread (options: OverrideCommandOptions & {
|
|
|
|
text: string
|
|
|
|
}) {
|
|
|
|
return this.addReply({ ...options, videoId: this.lastVideoId, toCommentId: this.lastThreadId })
|
|
|
|
}
|
|
|
|
|
2021-07-09 08:15:11 -04:00
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|