aboutsummaryrefslogtreecommitdiffhomepage
path: root/packages/server-commands/src/videos/comments-command.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/server-commands/src/videos/comments-command.ts')
-rw-r--r--packages/server-commands/src/videos/comments-command.ts159
1 files changed, 159 insertions, 0 deletions
diff --git a/packages/server-commands/src/videos/comments-command.ts b/packages/server-commands/src/videos/comments-command.ts
new file mode 100644
index 000000000..4835ae1fb
--- /dev/null
+++ b/packages/server-commands/src/videos/comments-command.ts
@@ -0,0 +1,159 @@
1import { pick } from '@peertube/peertube-core-utils'
2import { HttpStatusCode, ResultList, VideoComment, VideoCommentThreads, VideoCommentThreadTree } from '@peertube/peertube-models'
3import { unwrapBody } from '../requests/index.js'
4import { AbstractCommand, OverrideCommandOptions } from '../shared/index.js'
5
6export class CommentsCommand extends AbstractCommand {
7
8 private lastVideoId: number | string
9 private lastThreadId: number
10 private lastReplyId: number
11
12 listForAdmin (options: OverrideCommandOptions & {
13 start?: number
14 count?: number
15 sort?: string
16 isLocal?: boolean
17 onLocalVideo?: boolean
18 search?: string
19 searchAccount?: string
20 searchVideo?: string
21 } = {}) {
22 const { sort = '-createdAt' } = options
23 const path = '/api/v1/videos/comments'
24
25 const query = { sort, ...pick(options, [ 'start', 'count', 'isLocal', 'onLocalVideo', 'search', 'searchAccount', 'searchVideo' ]) }
26
27 return this.getRequestBody<ResultList<VideoComment>>({
28 ...options,
29
30 path,
31 query,
32 implicitToken: true,
33 defaultExpectedStatus: HttpStatusCode.OK_200
34 })
35 }
36
37 listThreads (options: OverrideCommandOptions & {
38 videoId: number | string
39 videoPassword?: string
40 start?: number
41 count?: number
42 sort?: string
43 }) {
44 const { start, count, sort, videoId, videoPassword } = options
45 const path = '/api/v1/videos/' + videoId + '/comment-threads'
46
47 return this.getRequestBody<VideoCommentThreads>({
48 ...options,
49
50 path,
51 query: { start, count, sort },
52 headers: this.buildVideoPasswordHeader(videoPassword),
53 implicitToken: false,
54 defaultExpectedStatus: HttpStatusCode.OK_200
55 })
56 }
57
58 getThread (options: OverrideCommandOptions & {
59 videoId: number | string
60 threadId: number
61 }) {
62 const { videoId, threadId } = options
63 const path = '/api/v1/videos/' + videoId + '/comment-threads/' + threadId
64
65 return this.getRequestBody<VideoCommentThreadTree>({
66 ...options,
67
68 path,
69 implicitToken: false,
70 defaultExpectedStatus: HttpStatusCode.OK_200
71 })
72 }
73
74 async createThread (options: OverrideCommandOptions & {
75 videoId: number | string
76 text: string
77 videoPassword?: string
78 }) {
79 const { videoId, text, videoPassword } = options
80 const path = '/api/v1/videos/' + videoId + '/comment-threads'
81
82 const body = await unwrapBody<{ comment: VideoComment }>(this.postBodyRequest({
83 ...options,
84
85 path,
86 fields: { text },
87 headers: this.buildVideoPasswordHeader(videoPassword),
88 implicitToken: true,
89 defaultExpectedStatus: HttpStatusCode.OK_200
90 }))
91
92 this.lastThreadId = body.comment?.id
93 this.lastVideoId = videoId
94
95 return body.comment
96 }
97
98 async addReply (options: OverrideCommandOptions & {
99 videoId: number | string
100 toCommentId: number
101 text: string
102 videoPassword?: string
103 }) {
104 const { videoId, toCommentId, text, videoPassword } = options
105 const path = '/api/v1/videos/' + videoId + '/comments/' + toCommentId
106
107 const body = await unwrapBody<{ comment: VideoComment }>(this.postBodyRequest({
108 ...options,
109
110 path,
111 fields: { text },
112 headers: this.buildVideoPasswordHeader(videoPassword),
113 implicitToken: true,
114 defaultExpectedStatus: HttpStatusCode.OK_200
115 }))
116
117 this.lastReplyId = body.comment?.id
118
119 return body.comment
120 }
121
122 async addReplyToLastReply (options: OverrideCommandOptions & {
123 text: string
124 }) {
125 return this.addReply({ ...options, videoId: this.lastVideoId, toCommentId: this.lastReplyId })
126 }
127
128 async addReplyToLastThread (options: OverrideCommandOptions & {
129 text: string
130 }) {
131 return this.addReply({ ...options, videoId: this.lastVideoId, toCommentId: this.lastThreadId })
132 }
133
134 async findCommentId (options: OverrideCommandOptions & {
135 videoId: number | string
136 text: string
137 }) {
138 const { videoId, text } = options
139 const { data } = await this.listThreads({ videoId, count: 25, sort: '-createdAt' })
140
141 return data.find(c => c.text === text).id
142 }
143
144 delete (options: OverrideCommandOptions & {
145 videoId: number | string
146 commentId: number
147 }) {
148 const { videoId, commentId } = options
149 const path = '/api/v1/videos/' + videoId + '/comments/' + commentId
150
151 return this.deleteRequest({
152 ...options,
153
154 path,
155 implicitToken: true,
156 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
157 })
158 }
159}