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