import { pick } from 'lodash' ; import { HttpStatusCode , ResultList , VideoComment , VideoCommentThreads , VideoCommentThreadTree } from '@shared/models' ; import { unwrapBody } from '../requests' ; import { AbstractCommand , OverrideCommandOptions } from '../shared' ; export class CommentsCommand extends AbstractCommand { private lastVideoId : number | string ; private lastThreadId : number ; private lastReplyId : number ; listForAdmin ( options = { } ) { const { sort = '-createdAt' } = options ; const path = '/api/v1/videos/comments' ; const query = { sort , ... pick ( options , [ 'start' , 'count' , 'isLocal' , 'search' , 'searchAccount' , 'searchVideo' ] ) } ; return this . getRequestBody < ResultList < VideoComment > > ( { ... options , path , query , implicitToken : true , defaultExpectedStatus : HttpStatusCode . OK_200 } ) ; } listThreads ( options ) { const { start , count , sort , videoId } = options ; const path = '/api/v1/videos/' + videoId + '/comment-threads' ; return this . getRequestBody < VideoCommentThreads > ( { ... options , path , query : { start , count , sort } , implicitToken : false , defaultExpectedStatus : HttpStatusCode . OK_200 } ) ; } getThread ( options ) { 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 ) { const { videoId , text } = options ; const path = '/api/v1/videos/' + videoId + '/comment-threads' ; const body = await unwrapBody < { comment : VideoComment ; } > ( this . postBodyRequest ( { ... options , path , fields : { text } , implicitToken : true , defaultExpectedStatus : HttpStatusCode . OK_200 } ) ) ; this . lastThreadId = body . comment ?. id ; this . lastVideoId = videoId ; return body . comment ; } async addReply ( options ) { const { videoId , toCommentId , text } = options ; const path = '/api/v1/videos/' + videoId + '/comments/' + toCommentId ; const body = await unwrapBody < { comment : VideoComment ; } > ( this . postBodyRequest ( { ... options , path , fields : { text } , implicitToken : true , defaultExpectedStatus : HttpStatusCode . OK_200 } ) ) ; this . lastReplyId = body . comment ?. id ; return body . comment ; } async addReplyToLastReply ( options ) { return this . addReply ( { ... options , videoId : this . lastVideoId , toCommentId : this . lastReplyId } ) ; } async addReplyToLastThread ( options ) { return this . addReply ( { ... options , videoId : this . lastVideoId , toCommentId : this . lastThreadId } ) ; } async findCommentId ( options ) { const { videoId , text } = options ; const { data } = await this . listThreads ( { videoId , count : 25 , sort : '-createdAt' } ) ; return data . find ( c => c . text === text ) . id ; } delete ( options ) { const { videoId , commentId } = options ; const path = '/api/v1/videos/' + videoId + '/comments/' + commentId ; return this . deleteRequest ( { ... options , path , implicitToken : true , defaultExpectedStatus : HttpStatusCode . NO_CONTENT_204 } ) ; } }