]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/extra-utils/videos/comments-command.ts
Reorganize imports
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / videos / comments-command.ts
CommitLineData
12edc149 1import { pick } from 'lodash'
4c7e60bc 2import { HttpStatusCode, ResultList, VideoComment, VideoCommentThreads, VideoCommentThreadTree } from '@shared/models'
12edc149
C
3import { unwrapBody } from '../requests'
4import { AbstractCommand, OverrideCommandOptions } from '../shared'
5
6export class CommentsCommand extends AbstractCommand {
7
8 listForAdmin (options: OverrideCommandOptions & {
9 start?: number
10 count?: number
11 sort?: string
12 isLocal?: boolean
13 search?: string
14 searchAccount?: string
15 searchVideo?: string
16 } = {}) {
17 const { sort = '-createdAt' } = options
18 const path = '/api/v1/videos/comments'
19
20 const query = { sort, ...pick(options, [ 'start', 'count', 'isLocal', 'search', 'searchAccount', 'searchVideo' ]) }
21
22 return this.getRequestBody<ResultList<VideoComment>>({
23 ...options,
24
25 path,
26 query,
27 implicitToken: true,
28 defaultExpectedStatus: HttpStatusCode.OK_200
29 })
30 }
31
32 listThreads (options: OverrideCommandOptions & {
33 videoId: number | string
34 start?: number
35 count?: number
36 sort?: string
37 }) {
38 const { start, count, sort, videoId } = options
39 const path = '/api/v1/videos/' + videoId + '/comment-threads'
40
41 return this.getRequestBody<VideoCommentThreads>({
42 ...options,
43
44 path,
45 query: { start, count, sort },
46 implicitToken: false,
47 defaultExpectedStatus: HttpStatusCode.OK_200
48 })
49 }
50
51 getThread (options: OverrideCommandOptions & {
52 videoId: number | string
53 threadId: number
54 }) {
55 const { videoId, threadId } = options
56 const path = '/api/v1/videos/' + videoId + '/comment-threads/' + threadId
57
58 return this.getRequestBody<VideoCommentThreadTree>({
59 ...options,
60
61 path,
62 implicitToken: false,
63 defaultExpectedStatus: HttpStatusCode.OK_200
64 })
65 }
66
67 async createThread (options: OverrideCommandOptions & {
68 videoId: number | string
69 text: string
70 }) {
71 const { videoId, text } = options
72 const path = '/api/v1/videos/' + videoId + '/comment-threads'
73
74 const body = await unwrapBody<{ comment: VideoComment }>(this.postBodyRequest({
75 ...options,
76
77 path,
78 fields: { text },
79 implicitToken: true,
80 defaultExpectedStatus: HttpStatusCode.OK_200
81 }))
82
83 return body.comment
84 }
85
86 async addReply (options: OverrideCommandOptions & {
87 videoId: number | string
88 toCommentId: number
89 text: string
90 }) {
91 const { videoId, toCommentId, text } = options
92 const path = '/api/v1/videos/' + videoId + '/comments/' + toCommentId
93
94 const body = await unwrapBody<{ comment: VideoComment }>(this.postBodyRequest({
95 ...options,
96
97 path,
98 fields: { text },
99 implicitToken: true,
100 defaultExpectedStatus: HttpStatusCode.OK_200
101 }))
102
103 return body.comment
104 }
105
106 async findCommentId (options: OverrideCommandOptions & {
107 videoId: number | string
108 text: string
109 }) {
110 const { videoId, text } = options
111 const { data } = await this.listThreads({ videoId, count: 25, sort: '-createdAt' })
112
113 return data.find(c => c.text === text).id
114 }
115
116 delete (options: OverrideCommandOptions & {
117 videoId: number | string
118 commentId: number
119 }) {
120 const { videoId, commentId } = options
121 const path = '/api/v1/videos/' + videoId + '/comments/' + commentId
122
123 return this.deleteRequest({
124 ...options,
125
126 path,
127 implicitToken: true,
128 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
129 })
130 }
131}