aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-07-06 12:01:59 +0200
committerChocobozzz <me@florianbigard.com>2021-07-20 15:27:17 +0200
commit0c1a77e9ccf915184c431145a8b326d4ce271b46 (patch)
tree1cea60190ffd246d3b44c0dac457e9ebc99823a6 /shared
parent8ef9457fdee7812b1a8cc3b3bdeff94130819003 (diff)
downloadPeerTube-0c1a77e9ccf915184c431145a8b326d4ce271b46.tar.gz
PeerTube-0c1a77e9ccf915184c431145a8b326d4ce271b46.tar.zst
PeerTube-0c1a77e9ccf915184c431145a8b326d4ce271b46.zip
Introduce abuse command
Diffstat (limited to 'shared')
-rw-r--r--shared/extra-utils/index.ts2
-rw-r--r--shared/extra-utils/moderation/abuses-command.ts205
-rw-r--r--shared/extra-utils/moderation/abuses.ts244
-rw-r--r--shared/extra-utils/moderation/index.ts1
-rw-r--r--shared/extra-utils/server/servers.ts3
-rw-r--r--shared/extra-utils/shared/abstract-command.ts6
6 files changed, 215 insertions, 246 deletions
diff --git a/shared/extra-utils/index.ts b/shared/extra-utils/index.ts
index 7d2887209..bde269052 100644
--- a/shared/extra-utils/index.ts
+++ b/shared/extra-utils/index.ts
@@ -5,8 +5,8 @@ export * from './feeds'
5export * from './logs' 5export * from './logs'
6export * from './miscs' 6export * from './miscs'
7export * from './mock-servers' 7export * from './mock-servers'
8export * from './moderation'
8 9
9export * from './moderation/abuses'
10export * from './plugins/mock-blocklist' 10export * from './plugins/mock-blocklist'
11 11
12export * from './requests/check-api-params' 12export * from './requests/check-api-params'
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 @@
1import { pick } from 'lodash'
2import {
3 AbuseFilter,
4 AbuseMessage,
5 AbusePredefinedReasonsString,
6 AbuseState,
7 AbuseUpdate,
8 AbuseVideoIs,
9 AdminAbuse,
10 ResultList,
11 UserAbuse
12} from '@shared/models'
13import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
14import { AbstractCommand, OverrideCommandOptions } from '../shared'
15import { unwrapBody } from '../requests/requests'
16
17export 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}
diff --git a/shared/extra-utils/moderation/abuses.ts b/shared/extra-utils/moderation/abuses.ts
deleted file mode 100644
index c0fda722f..000000000
--- a/shared/extra-utils/moderation/abuses.ts
+++ /dev/null
@@ -1,244 +0,0 @@
1import { AbuseFilter, AbusePredefinedReasonsString, AbuseState, AbuseUpdate, AbuseVideoIs } from '@shared/models'
2import { makeDeleteRequest, makeGetRequest, makePostBodyRequest, makePutBodyRequest } from '../requests/requests'
3import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
4
5function reportAbuse (options: {
6 url: string
7 token: string
8
9 reason: string
10
11 accountId?: number
12 videoId?: number
13 commentId?: number
14
15 predefinedReasons?: AbusePredefinedReasonsString[]
16
17 startAt?: number
18 endAt?: number
19
20 statusCodeExpected?: number
21}) {
22 const path = '/api/v1/abuses'
23
24 const video = options.videoId
25 ? {
26 id: options.videoId,
27 startAt: options.startAt,
28 endAt: options.endAt
29 }
30 : undefined
31
32 const comment = options.commentId
33 ? { id: options.commentId }
34 : undefined
35
36 const account = options.accountId
37 ? { id: options.accountId }
38 : undefined
39
40 const body = {
41 account,
42 video,
43 comment,
44
45 reason: options.reason,
46 predefinedReasons: options.predefinedReasons
47 }
48
49 return makePostBodyRequest({
50 url: options.url,
51 path,
52 token: options.token,
53
54 fields: body,
55 statusCodeExpected: options.statusCodeExpected || HttpStatusCode.OK_200
56 })
57}
58
59function getAdminAbusesList (options: {
60 url: string
61 token: string
62
63 start?: number
64 count?: number
65 sort?: string
66
67 id?: number
68 predefinedReason?: AbusePredefinedReasonsString
69 search?: string
70 filter?: AbuseFilter
71 state?: AbuseState
72 videoIs?: AbuseVideoIs
73 searchReporter?: string
74 searchReportee?: string
75 searchVideo?: string
76 searchVideoChannel?: string
77}) {
78 const {
79 url,
80 token,
81 start,
82 count,
83 sort,
84 id,
85 predefinedReason,
86 search,
87 filter,
88 state,
89 videoIs,
90 searchReporter,
91 searchReportee,
92 searchVideo,
93 searchVideoChannel
94 } = options
95 const path = '/api/v1/abuses'
96
97 const query = {
98 id,
99 predefinedReason,
100 search,
101 state,
102 filter,
103 videoIs,
104 start,
105 count,
106 sort: sort || 'createdAt',
107 searchReporter,
108 searchReportee,
109 searchVideo,
110 searchVideoChannel
111 }
112
113 return makeGetRequest({
114 url,
115 path,
116 token,
117 query,
118 statusCodeExpected: HttpStatusCode.OK_200
119 })
120}
121
122function getUserAbusesList (options: {
123 url: string
124 token: string
125
126 start?: number
127 count?: number
128 sort?: string
129
130 id?: number
131 search?: string
132 state?: AbuseState
133}) {
134 const {
135 url,
136 token,
137 start,
138 count,
139 sort,
140 id,
141 search,
142 state
143 } = options
144 const path = '/api/v1/users/me/abuses'
145
146 const query = {
147 id,
148 search,
149 state,
150 start,
151 count,
152 sort: sort || 'createdAt'
153 }
154
155 return makeGetRequest({
156 url,
157 path,
158 token,
159 query,
160 statusCodeExpected: HttpStatusCode.OK_200
161 })
162}
163
164function updateAbuse (
165 url: string,
166 token: string,
167 abuseId: number,
168 body: AbuseUpdate,
169 statusCodeExpected = HttpStatusCode.NO_CONTENT_204
170) {
171 const path = '/api/v1/abuses/' + abuseId
172
173 return makePutBodyRequest({
174 url,
175 token,
176 path,
177 fields: body,
178 statusCodeExpected
179 })
180}
181
182function deleteAbuse (url: string, token: string, abuseId: number, statusCodeExpected = HttpStatusCode.NO_CONTENT_204) {
183 const path = '/api/v1/abuses/' + abuseId
184
185 return makeDeleteRequest({
186 url,
187 token,
188 path,
189 statusCodeExpected
190 })
191}
192
193function listAbuseMessages (url: string, token: string, abuseId: number, statusCodeExpected = HttpStatusCode.OK_200) {
194 const path = '/api/v1/abuses/' + abuseId + '/messages'
195
196 return makeGetRequest({
197 url,
198 token,
199 path,
200 statusCodeExpected
201 })
202}
203
204function deleteAbuseMessage (
205 url: string,
206 token: string,
207 abuseId: number,
208 messageId: number,
209 statusCodeExpected = HttpStatusCode.NO_CONTENT_204
210) {
211 const path = '/api/v1/abuses/' + abuseId + '/messages/' + messageId
212
213 return makeDeleteRequest({
214 url,
215 token,
216 path,
217 statusCodeExpected
218 })
219}
220
221function addAbuseMessage (url: string, token: string, abuseId: number, message: string, statusCodeExpected = HttpStatusCode.OK_200) {
222 const path = '/api/v1/abuses/' + abuseId + '/messages'
223
224 return makePostBodyRequest({
225 url,
226 token,
227 path,
228 fields: { message },
229 statusCodeExpected
230 })
231}
232
233// ---------------------------------------------------------------------------
234
235export {
236 reportAbuse,
237 getAdminAbusesList,
238 updateAbuse,
239 deleteAbuse,
240 getUserAbusesList,
241 listAbuseMessages,
242 deleteAbuseMessage,
243 addAbuseMessage
244}
diff --git a/shared/extra-utils/moderation/index.ts b/shared/extra-utils/moderation/index.ts
new file mode 100644
index 000000000..b37643956
--- /dev/null
+++ b/shared/extra-utils/moderation/index.ts
@@ -0,0 +1 @@
export * from './abuses-command'
diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts
index 4343eab93..f05f0dbbe 100644
--- a/shared/extra-utils/server/servers.ts
+++ b/shared/extra-utils/server/servers.ts
@@ -12,6 +12,7 @@ import { CustomPagesCommand } from '../custom-pages'
12import { FeedCommand } from '../feeds' 12import { FeedCommand } from '../feeds'
13import { LogsCommand } from '../logs' 13import { LogsCommand } from '../logs'
14import { buildServerDirectory, getFileSize, isGithubCI, root, wait } from '../miscs/miscs' 14import { buildServerDirectory, getFileSize, isGithubCI, root, wait } from '../miscs/miscs'
15import { AbusesCommand } from '../moderation'
15import { makeGetRequest } from '../requests/requests' 16import { makeGetRequest } from '../requests/requests'
16 17
17interface ServerInfo { 18interface ServerInfo {
@@ -71,6 +72,7 @@ interface ServerInfo {
71 customPageCommand?: CustomPagesCommand 72 customPageCommand?: CustomPagesCommand
72 feedCommand?: FeedCommand 73 feedCommand?: FeedCommand
73 logsCommand?: LogsCommand 74 logsCommand?: LogsCommand
75 abusesCommand?: AbusesCommand
74} 76}
75 77
76function parallelTests () { 78function parallelTests () {
@@ -281,6 +283,7 @@ async function runServer (server: ServerInfo, configOverrideArg?: any, args = []
281 server.customPageCommand = new CustomPagesCommand(server) 283 server.customPageCommand = new CustomPagesCommand(server)
282 server.feedCommand = new FeedCommand(server) 284 server.feedCommand = new FeedCommand(server)
283 server.logsCommand = new LogsCommand(server) 285 server.logsCommand = new LogsCommand(server)
286 server.abusesCommand = new AbusesCommand(server)
284 287
285 res(server) 288 res(server)
286 }) 289 })
diff --git a/shared/extra-utils/shared/abstract-command.ts b/shared/extra-utils/shared/abstract-command.ts
index a57222216..3ee5cd865 100644
--- a/shared/extra-utils/shared/abstract-command.ts
+++ b/shared/extra-utils/shared/abstract-command.ts
@@ -1,5 +1,5 @@
1import { HttpStatusCode } from '@shared/core-utils' 1import { HttpStatusCode } from '@shared/core-utils'
2import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, unwrapBody, unwrapText } from '../requests/requests' 2import { makeDeleteRequest, makeGetRequest, makePostBodyRequest, makePutBodyRequest, unwrapBody, unwrapText } from '../requests/requests'
3import { ServerInfo } from '../server/servers' 3import { ServerInfo } from '../server/servers'
4 4
5export interface OverrideCommandOptions { 5export interface OverrideCommandOptions {
@@ -44,6 +44,10 @@ abstract class AbstractCommand {
44 return unwrapText(this.getRequest(options)) 44 return unwrapText(this.getRequest(options))
45 } 45 }
46 46
47 protected deleteRequest (options: CommonCommandOptions) {
48 return makeDeleteRequest(this.buildCommonRequestOptions(options))
49 }
50
47 protected putBodyRequest (options: CommonCommandOptions & { 51 protected putBodyRequest (options: CommonCommandOptions & {
48 fields?: { [ fieldName: string ]: any } 52 fields?: { [ fieldName: string ]: any }
49 }) { 53 }) {