diff options
author | Chocobozzz <me@florianbigard.com> | 2021-07-07 16:02:46 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-07-20 15:27:17 +0200 |
commit | 5f8bd4cbb178290da7d8f81e996f19f0eccc8e4c (patch) | |
tree | 02d056121540652b0867fc2ef56699138afe6271 /shared | |
parent | 9fff08cf83f34339df7ed4ac770e1dee536adf9d (diff) | |
download | PeerTube-5f8bd4cbb178290da7d8f81e996f19f0eccc8e4c.tar.gz PeerTube-5f8bd4cbb178290da7d8f81e996f19f0eccc8e4c.tar.zst PeerTube-5f8bd4cbb178290da7d8f81e996f19f0eccc8e4c.zip |
Introduce blocklist command
Diffstat (limited to 'shared')
-rw-r--r-- | shared/extra-utils/server/servers.ts | 4 | ||||
-rw-r--r-- | shared/extra-utils/socket/socket-io-command.ts | 2 | ||||
-rw-r--r-- | shared/extra-utils/users/blocklist-command.ts | 135 | ||||
-rw-r--r-- | shared/extra-utils/users/blocklist.ts | 238 | ||||
-rw-r--r-- | shared/extra-utils/users/index.ts | 4 |
5 files changed, 141 insertions, 242 deletions
diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts index c2cab9818..3c709666d 100644 --- a/shared/extra-utils/server/servers.ts +++ b/shared/extra-utils/server/servers.ts | |||
@@ -17,7 +17,7 @@ import { OverviewsCommand } from '../overviews' | |||
17 | import { makeGetRequest } from '../requests/requests' | 17 | import { makeGetRequest } from '../requests/requests' |
18 | import { SearchCommand } from '../search' | 18 | import { SearchCommand } from '../search' |
19 | import { SocketIOCommand } from '../socket' | 19 | import { SocketIOCommand } from '../socket' |
20 | import { AccountsCommand } from '../users' | 20 | import { AccountsCommand, BlocklistCommand } from '../users' |
21 | import { ConfigCommand } from './config-command' | 21 | import { ConfigCommand } from './config-command' |
22 | import { ContactFormCommand } from './contact-form-command' | 22 | import { ContactFormCommand } from './contact-form-command' |
23 | import { DebugCommand } from './debug-command' | 23 | import { DebugCommand } from './debug-command' |
@@ -97,6 +97,7 @@ interface ServerInfo { | |||
97 | configCommand?: ConfigCommand | 97 | configCommand?: ConfigCommand |
98 | socketIOCommand?: SocketIOCommand | 98 | socketIOCommand?: SocketIOCommand |
99 | accountsCommand?: AccountsCommand | 99 | accountsCommand?: AccountsCommand |
100 | blocklistCommand?: BlocklistCommand | ||
100 | } | 101 | } |
101 | 102 | ||
102 | function parallelTests () { | 103 | function parallelTests () { |
@@ -320,6 +321,7 @@ async function runServer (server: ServerInfo, configOverrideArg?: any, args = [] | |||
320 | server.configCommand = new ConfigCommand(server) | 321 | server.configCommand = new ConfigCommand(server) |
321 | server.socketIOCommand = new SocketIOCommand(server) | 322 | server.socketIOCommand = new SocketIOCommand(server) |
322 | server.accountsCommand = new AccountsCommand(server) | 323 | server.accountsCommand = new AccountsCommand(server) |
324 | server.blocklistCommand = new BlocklistCommand(server) | ||
323 | 325 | ||
324 | res(server) | 326 | res(server) |
325 | }) | 327 | }) |
diff --git a/shared/extra-utils/socket/socket-io-command.ts b/shared/extra-utils/socket/socket-io-command.ts index 545561bed..c277ead28 100644 --- a/shared/extra-utils/socket/socket-io-command.ts +++ b/shared/extra-utils/socket/socket-io-command.ts | |||
@@ -5,7 +5,7 @@ export class SocketIOCommand extends AbstractCommand { | |||
5 | 5 | ||
6 | getUserNotificationSocket (options: OverrideCommandOptions = {}) { | 6 | getUserNotificationSocket (options: OverrideCommandOptions = {}) { |
7 | return io(this.server.url + '/user-notifications', { | 7 | return io(this.server.url + '/user-notifications', { |
8 | query: { accessToken: this.server.accessToken } | 8 | query: { accessToken: options.token ?? this.server.accessToken } |
9 | }) | 9 | }) |
10 | } | 10 | } |
11 | 11 | ||
diff --git a/shared/extra-utils/users/blocklist-command.ts b/shared/extra-utils/users/blocklist-command.ts new file mode 100644 index 000000000..96afdc3fd --- /dev/null +++ b/shared/extra-utils/users/blocklist-command.ts | |||
@@ -0,0 +1,135 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | ||
2 | |||
3 | import { HttpStatusCode } from '@shared/core-utils' | ||
4 | import { AccountBlock, ResultList, ServerBlock } from '@shared/models' | ||
5 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
6 | |||
7 | type ListBlocklistOptions = OverrideCommandOptions & { | ||
8 | start: number | ||
9 | count: number | ||
10 | sort: string // default -createdAt | ||
11 | } | ||
12 | |||
13 | export class BlocklistCommand extends AbstractCommand { | ||
14 | |||
15 | listMyAccountBlocklist (options: ListBlocklistOptions) { | ||
16 | const path = '/api/v1/users/me/blocklist/accounts' | ||
17 | |||
18 | return this.listBlocklist<AccountBlock>(options, path) | ||
19 | } | ||
20 | |||
21 | listMyServerBlocklist (options: ListBlocklistOptions) { | ||
22 | const path = '/api/v1/users/me/blocklist/servers' | ||
23 | |||
24 | return this.listBlocklist<ServerBlock>(options, path) | ||
25 | } | ||
26 | |||
27 | listServerAccountBlocklist (options: ListBlocklistOptions) { | ||
28 | const path = '/api/v1/server/blocklist/accounts' | ||
29 | |||
30 | return this.listBlocklist<AccountBlock>(options, path) | ||
31 | } | ||
32 | |||
33 | listServerServerBlocklist (options: ListBlocklistOptions) { | ||
34 | const path = '/api/v1/server/blocklist/servers' | ||
35 | |||
36 | return this.listBlocklist<ServerBlock>(options, path) | ||
37 | } | ||
38 | |||
39 | // --------------------------------------------------------------------------- | ||
40 | |||
41 | addToMyBlocklist (options: OverrideCommandOptions & { | ||
42 | account?: string | ||
43 | server?: string | ||
44 | }) { | ||
45 | const { account, server } = options | ||
46 | |||
47 | const path = account | ||
48 | ? '/api/v1/users/me/blocklist/accounts' | ||
49 | : '/api/v1/users/me/blocklist/servers' | ||
50 | |||
51 | return this.postBodyRequest({ | ||
52 | ...options, | ||
53 | |||
54 | path, | ||
55 | fields: { | ||
56 | accountName: account, | ||
57 | host: server | ||
58 | }, | ||
59 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
60 | }) | ||
61 | } | ||
62 | |||
63 | addToServerBlocklist (options: OverrideCommandOptions & { | ||
64 | account?: string | ||
65 | server?: string | ||
66 | }) { | ||
67 | const { account, server } = options | ||
68 | |||
69 | const path = account | ||
70 | ? '/api/v1/server/blocklist/accounts' | ||
71 | : '/api/v1/server/blocklist/servers' | ||
72 | |||
73 | return this.postBodyRequest({ | ||
74 | ...options, | ||
75 | |||
76 | path, | ||
77 | fields: { | ||
78 | accountName: account, | ||
79 | host: server | ||
80 | }, | ||
81 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
82 | }) | ||
83 | } | ||
84 | |||
85 | // --------------------------------------------------------------------------- | ||
86 | |||
87 | removeFromMyBlocklist (options: OverrideCommandOptions & { | ||
88 | account?: string | ||
89 | server?: string | ||
90 | }) { | ||
91 | const { account, server } = options | ||
92 | |||
93 | const path = account | ||
94 | ? '/api/v1/users/me/blocklist/accounts/' + account | ||
95 | : '/api/v1/users/me/blocklist/servers/' + server | ||
96 | |||
97 | return this.deleteRequest({ | ||
98 | ...options, | ||
99 | |||
100 | path, | ||
101 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
102 | }) | ||
103 | } | ||
104 | |||
105 | removeFromServerBlocklist (options: OverrideCommandOptions & { | ||
106 | account?: string | ||
107 | server?: string | ||
108 | }) { | ||
109 | const { account, server } = options | ||
110 | |||
111 | const path = account | ||
112 | ? '/api/v1/server/blocklist/accounts/' + account | ||
113 | : '/api/v1/server/blocklist/servers/' + server | ||
114 | |||
115 | return this.deleteRequest({ | ||
116 | ...options, | ||
117 | |||
118 | path, | ||
119 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
120 | }) | ||
121 | } | ||
122 | |||
123 | private listBlocklist <T> (options: ListBlocklistOptions, path: string) { | ||
124 | const { start, count, sort = '-createdAt' } = options | ||
125 | |||
126 | return this.getRequestBody<ResultList<T>>({ | ||
127 | ...options, | ||
128 | |||
129 | path, | ||
130 | query: { start, count, sort }, | ||
131 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
132 | }) | ||
133 | } | ||
134 | |||
135 | } | ||
diff --git a/shared/extra-utils/users/blocklist.ts b/shared/extra-utils/users/blocklist.ts deleted file mode 100644 index bdf7ee58a..000000000 --- a/shared/extra-utils/users/blocklist.ts +++ /dev/null | |||
@@ -1,238 +0,0 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | ||
2 | |||
3 | import { makeGetRequest, makeDeleteRequest, makePostBodyRequest } from '../requests/requests' | ||
4 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
5 | |||
6 | function getAccountBlocklistByAccount ( | ||
7 | url: string, | ||
8 | token: string, | ||
9 | start: number, | ||
10 | count: number, | ||
11 | sort = '-createdAt', | ||
12 | statusCodeExpected = HttpStatusCode.OK_200 | ||
13 | ) { | ||
14 | const path = '/api/v1/users/me/blocklist/accounts' | ||
15 | |||
16 | return makeGetRequest({ | ||
17 | url, | ||
18 | token, | ||
19 | query: { start, count, sort }, | ||
20 | path, | ||
21 | statusCodeExpected | ||
22 | }) | ||
23 | } | ||
24 | |||
25 | function addAccountToAccountBlocklist ( | ||
26 | url: string, | ||
27 | token: string, | ||
28 | accountToBlock: string, | ||
29 | statusCodeExpected = HttpStatusCode.NO_CONTENT_204 | ||
30 | ) { | ||
31 | const path = '/api/v1/users/me/blocklist/accounts' | ||
32 | |||
33 | return makePostBodyRequest({ | ||
34 | url, | ||
35 | path, | ||
36 | token, | ||
37 | fields: { | ||
38 | accountName: accountToBlock | ||
39 | }, | ||
40 | statusCodeExpected | ||
41 | }) | ||
42 | } | ||
43 | |||
44 | function removeAccountFromAccountBlocklist ( | ||
45 | url: string, | ||
46 | token: string, | ||
47 | accountToUnblock: string, | ||
48 | statusCodeExpected = HttpStatusCode.NO_CONTENT_204 | ||
49 | ) { | ||
50 | const path = '/api/v1/users/me/blocklist/accounts/' + accountToUnblock | ||
51 | |||
52 | return makeDeleteRequest({ | ||
53 | url, | ||
54 | path, | ||
55 | token, | ||
56 | statusCodeExpected | ||
57 | }) | ||
58 | } | ||
59 | |||
60 | function getServerBlocklistByAccount ( | ||
61 | url: string, | ||
62 | token: string, | ||
63 | start: number, | ||
64 | count: number, | ||
65 | sort = '-createdAt', | ||
66 | statusCodeExpected = HttpStatusCode.OK_200 | ||
67 | ) { | ||
68 | const path = '/api/v1/users/me/blocklist/servers' | ||
69 | |||
70 | return makeGetRequest({ | ||
71 | url, | ||
72 | token, | ||
73 | query: { start, count, sort }, | ||
74 | path, | ||
75 | statusCodeExpected | ||
76 | }) | ||
77 | } | ||
78 | |||
79 | function addServerToAccountBlocklist ( | ||
80 | url: string, | ||
81 | token: string, | ||
82 | serverToBlock: string, | ||
83 | statusCodeExpected = HttpStatusCode.NO_CONTENT_204 | ||
84 | ) { | ||
85 | const path = '/api/v1/users/me/blocklist/servers' | ||
86 | |||
87 | return makePostBodyRequest({ | ||
88 | url, | ||
89 | path, | ||
90 | token, | ||
91 | fields: { | ||
92 | host: serverToBlock | ||
93 | }, | ||
94 | statusCodeExpected | ||
95 | }) | ||
96 | } | ||
97 | |||
98 | function removeServerFromAccountBlocklist ( | ||
99 | url: string, | ||
100 | token: string, | ||
101 | serverToBlock: string, | ||
102 | statusCodeExpected = HttpStatusCode.NO_CONTENT_204 | ||
103 | ) { | ||
104 | const path = '/api/v1/users/me/blocklist/servers/' + serverToBlock | ||
105 | |||
106 | return makeDeleteRequest({ | ||
107 | url, | ||
108 | path, | ||
109 | token, | ||
110 | statusCodeExpected | ||
111 | }) | ||
112 | } | ||
113 | |||
114 | function getAccountBlocklistByServer ( | ||
115 | url: string, | ||
116 | token: string, | ||
117 | start: number, | ||
118 | count: number, | ||
119 | sort = '-createdAt', | ||
120 | statusCodeExpected = HttpStatusCode.OK_200 | ||
121 | ) { | ||
122 | const path = '/api/v1/server/blocklist/accounts' | ||
123 | |||
124 | return makeGetRequest({ | ||
125 | url, | ||
126 | token, | ||
127 | query: { start, count, sort }, | ||
128 | path, | ||
129 | statusCodeExpected | ||
130 | }) | ||
131 | } | ||
132 | |||
133 | function addAccountToServerBlocklist ( | ||
134 | url: string, | ||
135 | token: string, | ||
136 | accountToBlock: string, | ||
137 | statusCodeExpected = HttpStatusCode.NO_CONTENT_204 | ||
138 | ) { | ||
139 | const path = '/api/v1/server/blocklist/accounts' | ||
140 | |||
141 | return makePostBodyRequest({ | ||
142 | url, | ||
143 | path, | ||
144 | token, | ||
145 | fields: { | ||
146 | accountName: accountToBlock | ||
147 | }, | ||
148 | statusCodeExpected | ||
149 | }) | ||
150 | } | ||
151 | |||
152 | function removeAccountFromServerBlocklist ( | ||
153 | url: string, | ||
154 | token: string, | ||
155 | accountToUnblock: string, | ||
156 | statusCodeExpected = HttpStatusCode.NO_CONTENT_204 | ||
157 | ) { | ||
158 | const path = '/api/v1/server/blocklist/accounts/' + accountToUnblock | ||
159 | |||
160 | return makeDeleteRequest({ | ||
161 | url, | ||
162 | path, | ||
163 | token, | ||
164 | statusCodeExpected | ||
165 | }) | ||
166 | } | ||
167 | |||
168 | function getServerBlocklistByServer ( | ||
169 | url: string, | ||
170 | token: string, | ||
171 | start: number, | ||
172 | count: number, | ||
173 | sort = '-createdAt', | ||
174 | statusCodeExpected = HttpStatusCode.OK_200 | ||
175 | ) { | ||
176 | const path = '/api/v1/server/blocklist/servers' | ||
177 | |||
178 | return makeGetRequest({ | ||
179 | url, | ||
180 | token, | ||
181 | query: { start, count, sort }, | ||
182 | path, | ||
183 | statusCodeExpected | ||
184 | }) | ||
185 | } | ||
186 | |||
187 | function addServerToServerBlocklist ( | ||
188 | url: string, | ||
189 | token: string, | ||
190 | serverToBlock: string, | ||
191 | statusCodeExpected = HttpStatusCode.NO_CONTENT_204 | ||
192 | ) { | ||
193 | const path = '/api/v1/server/blocklist/servers' | ||
194 | |||
195 | return makePostBodyRequest({ | ||
196 | url, | ||
197 | path, | ||
198 | token, | ||
199 | fields: { | ||
200 | host: serverToBlock | ||
201 | }, | ||
202 | statusCodeExpected | ||
203 | }) | ||
204 | } | ||
205 | |||
206 | function removeServerFromServerBlocklist ( | ||
207 | url: string, | ||
208 | token: string, | ||
209 | serverToBlock: string, | ||
210 | statusCodeExpected = HttpStatusCode.NO_CONTENT_204 | ||
211 | ) { | ||
212 | const path = '/api/v1/server/blocklist/servers/' + serverToBlock | ||
213 | |||
214 | return makeDeleteRequest({ | ||
215 | url, | ||
216 | path, | ||
217 | token, | ||
218 | statusCodeExpected | ||
219 | }) | ||
220 | } | ||
221 | |||
222 | // --------------------------------------------------------------------------- | ||
223 | |||
224 | export { | ||
225 | getAccountBlocklistByAccount, | ||
226 | addAccountToAccountBlocklist, | ||
227 | removeAccountFromAccountBlocklist, | ||
228 | getServerBlocklistByAccount, | ||
229 | addServerToAccountBlocklist, | ||
230 | removeServerFromAccountBlocklist, | ||
231 | |||
232 | getAccountBlocklistByServer, | ||
233 | addAccountToServerBlocklist, | ||
234 | removeAccountFromServerBlocklist, | ||
235 | getServerBlocklistByServer, | ||
236 | addServerToServerBlocklist, | ||
237 | removeServerFromServerBlocklist | ||
238 | } | ||
diff --git a/shared/extra-utils/users/index.ts b/shared/extra-utils/users/index.ts index b3387ed8a..ea5dbbf14 100644 --- a/shared/extra-utils/users/index.ts +++ b/shared/extra-utils/users/index.ts | |||
@@ -1,7 +1,7 @@ | |||
1 | export * from './accounts' | ||
2 | export * from './accounts-command' | 1 | export * from './accounts-command' |
2 | export * from './accounts' | ||
3 | export * from './blocklist-command' | ||
3 | 4 | ||
4 | export * from './blocklist' | ||
5 | export * from './login' | 5 | export * from './login' |
6 | export * from './user-notifications' | 6 | export * from './user-notifications' |
7 | export * from './user-subscriptions' | 7 | export * from './user-subscriptions' |