aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/server-commands/videos/comments-command.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-07-31 14:34:36 +0200
committerChocobozzz <me@florianbigard.com>2023-08-11 15:02:33 +0200
commit3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch)
treee4510b39bdac9c318fdb4b47018d08f15368b8f0 /shared/server-commands/videos/comments-command.ts
parent04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff)
downloadPeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge conflicts, but it's a major step forward: * Server can be faster at startup because imports() are async and we can easily lazy import big modules * Angular doesn't seem to support ES import (with .js extension), so we had to correctly organize peertube into a monorepo: * Use yarn workspace feature * Use typescript reference projects for dependencies * Shared projects have been moved into "packages", each one is now a node module (with a dedicated package.json/tsconfig.json) * server/tools have been moved into apps/ and is now a dedicated app bundled and published on NPM so users don't have to build peertube cli tools manually * server/tests have been moved into packages/ so we don't compile them every time we want to run the server * Use isolatedModule option: * Had to move from const enum to const (https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums) * Had to explictely specify "type" imports when used in decorators * Prefer tsx (that uses esbuild under the hood) instead of ts-node to load typescript files (tests with mocha or scripts): * To reduce test complexity as esbuild doesn't support decorator metadata, we only test server files that do not import server models * We still build tests files into js files for a faster CI * Remove unmaintained peertube CLI import script * Removed some barrels to speed up execution (less imports)
Diffstat (limited to 'shared/server-commands/videos/comments-command.ts')
-rw-r--r--shared/server-commands/videos/comments-command.ts159
1 files changed, 0 insertions, 159 deletions
diff --git a/shared/server-commands/videos/comments-command.ts b/shared/server-commands/videos/comments-command.ts
deleted file mode 100644
index 0dab1b66a..000000000
--- a/shared/server-commands/videos/comments-command.ts
+++ /dev/null
@@ -1,159 +0,0 @@
1import { pick } from '@shared/core-utils'
2import { HttpStatusCode, ResultList, VideoComment, VideoCommentThreads, VideoCommentThreadTree } from '@shared/models'
3import { unwrapBody } from '../requests'
4import { AbstractCommand, OverrideCommandOptions } from '../shared'
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}