diff options
Diffstat (limited to 'shared/extra-utils/moderation/abuses-command.ts')
-rw-r--r-- | shared/extra-utils/moderation/abuses-command.ts | 205 |
1 files changed, 205 insertions, 0 deletions
diff --git a/shared/extra-utils/moderation/abuses-command.ts b/shared/extra-utils/moderation/abuses-command.ts new file mode 100644 index 000000000..59126d0a9 --- /dev/null +++ b/shared/extra-utils/moderation/abuses-command.ts | |||
@@ -0,0 +1,205 @@ | |||
1 | import { pick } from 'lodash' | ||
2 | import { | ||
3 | AbuseFilter, | ||
4 | AbuseMessage, | ||
5 | AbusePredefinedReasonsString, | ||
6 | AbuseState, | ||
7 | AbuseUpdate, | ||
8 | AbuseVideoIs, | ||
9 | AdminAbuse, | ||
10 | ResultList, | ||
11 | UserAbuse | ||
12 | } from '@shared/models' | ||
13 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
14 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
15 | import { unwrapBody } from '../requests/requests' | ||
16 | |||
17 | export class AbusesCommand extends AbstractCommand { | ||
18 | |||
19 | report (options: OverrideCommandOptions & { | ||
20 | reason: string | ||
21 | |||
22 | accountId?: number | ||
23 | videoId?: number | ||
24 | commentId?: number | ||
25 | |||
26 | predefinedReasons?: AbusePredefinedReasonsString[] | ||
27 | |||
28 | startAt?: number | ||
29 | endAt?: number | ||
30 | }) { | ||
31 | const path = '/api/v1/abuses' | ||
32 | |||
33 | const video = options.videoId | ||
34 | ? { | ||
35 | id: options.videoId, | ||
36 | startAt: options.startAt, | ||
37 | endAt: options.endAt | ||
38 | } | ||
39 | : undefined | ||
40 | |||
41 | const comment = options.commentId | ||
42 | ? { id: options.commentId } | ||
43 | : undefined | ||
44 | |||
45 | const account = options.accountId | ||
46 | ? { id: options.accountId } | ||
47 | : undefined | ||
48 | |||
49 | const body = { | ||
50 | account, | ||
51 | video, | ||
52 | comment, | ||
53 | |||
54 | reason: options.reason, | ||
55 | predefinedReasons: options.predefinedReasons | ||
56 | } | ||
57 | |||
58 | return unwrapBody<{ abuse: { id: number } }>(this.postBodyRequest({ | ||
59 | ...options, | ||
60 | |||
61 | path, | ||
62 | fields: body, | ||
63 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
64 | })) | ||
65 | } | ||
66 | |||
67 | getAdminList (options: OverrideCommandOptions & { | ||
68 | start?: number | ||
69 | count?: number | ||
70 | sort?: string | ||
71 | |||
72 | id?: number | ||
73 | predefinedReason?: AbusePredefinedReasonsString | ||
74 | search?: string | ||
75 | filter?: AbuseFilter | ||
76 | state?: AbuseState | ||
77 | videoIs?: AbuseVideoIs | ||
78 | searchReporter?: string | ||
79 | searchReportee?: string | ||
80 | searchVideo?: string | ||
81 | searchVideoChannel?: string | ||
82 | } = {}) { | ||
83 | const toPick = [ | ||
84 | 'count', | ||
85 | 'filter', | ||
86 | 'id', | ||
87 | 'predefinedReason', | ||
88 | 'search', | ||
89 | 'searchReportee', | ||
90 | 'searchReporter', | ||
91 | 'searchVideo', | ||
92 | 'searchVideoChannel', | ||
93 | 'sort', | ||
94 | 'start', | ||
95 | 'state', | ||
96 | 'videoIs' | ||
97 | ] | ||
98 | |||
99 | const path = '/api/v1/abuses' | ||
100 | |||
101 | const defaultQuery = { sort: 'createdAt' } | ||
102 | const query = { ...defaultQuery, ...pick(options, toPick) } | ||
103 | |||
104 | return this.getRequestBody<ResultList<AdminAbuse>>({ | ||
105 | ...options, | ||
106 | |||
107 | path, | ||
108 | query, | ||
109 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
110 | }) | ||
111 | } | ||
112 | |||
113 | getUserList (options: OverrideCommandOptions & { | ||
114 | start?: number | ||
115 | count?: number | ||
116 | sort?: string | ||
117 | |||
118 | id?: number | ||
119 | search?: string | ||
120 | state?: AbuseState | ||
121 | }) { | ||
122 | const toPick = [ | ||
123 | 'id', | ||
124 | 'search', | ||
125 | 'state', | ||
126 | 'start', | ||
127 | 'count', | ||
128 | 'sort' | ||
129 | ] | ||
130 | |||
131 | const path = '/api/v1/users/me/abuses' | ||
132 | |||
133 | const defaultQuery = { sort: 'createdAt' } | ||
134 | const query = { ...defaultQuery, ...pick(options, toPick) } | ||
135 | |||
136 | return this.getRequestBody<ResultList<UserAbuse>>({ | ||
137 | ...options, | ||
138 | |||
139 | path, | ||
140 | query, | ||
141 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
142 | }) | ||
143 | } | ||
144 | |||
145 | update (options: OverrideCommandOptions & { | ||
146 | abuseId: number | ||
147 | body: AbuseUpdate | ||
148 | }) { | ||
149 | const { abuseId, body } = options | ||
150 | const path = '/api/v1/abuses/' + abuseId | ||
151 | |||
152 | return this.putBodyRequest({ | ||
153 | ...options, | ||
154 | |||
155 | path, | ||
156 | fields: body, | ||
157 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
158 | }) | ||
159 | } | ||
160 | |||
161 | delete (options: OverrideCommandOptions & { | ||
162 | abuseId: number | ||
163 | }) { | ||
164 | const { abuseId } = options | ||
165 | const path = '/api/v1/abuses/' + abuseId | ||
166 | |||
167 | return this.deleteRequest({ ...options, path, defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 }) | ||
168 | } | ||
169 | |||
170 | listMessages (options: OverrideCommandOptions & { | ||
171 | abuseId: number | ||
172 | }) { | ||
173 | const { abuseId } = options | ||
174 | const path = '/api/v1/abuses/' + abuseId + '/messages' | ||
175 | |||
176 | return this.getRequestBody<ResultList<AbuseMessage>>({ ...options, path, defaultExpectedStatus: HttpStatusCode.OK_200 }) | ||
177 | } | ||
178 | |||
179 | deleteMessage (options: OverrideCommandOptions & { | ||
180 | abuseId: number | ||
181 | messageId: number | ||
182 | }) { | ||
183 | const { abuseId, messageId } = options | ||
184 | const path = '/api/v1/abuses/' + abuseId + '/messages/' + messageId | ||
185 | |||
186 | return this.deleteRequest({ ...options, path, defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 }) | ||
187 | } | ||
188 | |||
189 | addMessage (options: OverrideCommandOptions & { | ||
190 | abuseId: number | ||
191 | message: string | ||
192 | }) { | ||
193 | const { abuseId, message } = options | ||
194 | const path = '/api/v1/abuses/' + abuseId + '/messages' | ||
195 | |||
196 | return this.postBodyRequest({ | ||
197 | ...options, | ||
198 | |||
199 | path, | ||
200 | fields: { message }, | ||
201 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
202 | }) | ||
203 | } | ||
204 | |||
205 | } | ||