diff options
Diffstat (limited to 'packages/server-commands/src/users/blocklist-command.ts')
-rw-r--r-- | packages/server-commands/src/users/blocklist-command.ts | 165 |
1 files changed, 165 insertions, 0 deletions
diff --git a/packages/server-commands/src/users/blocklist-command.ts b/packages/server-commands/src/users/blocklist-command.ts new file mode 100644 index 000000000..c77c56131 --- /dev/null +++ b/packages/server-commands/src/users/blocklist-command.ts | |||
@@ -0,0 +1,165 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | ||
2 | |||
3 | import { AccountBlock, BlockStatus, HttpStatusCode, ResultList, ServerBlock } from '@peertube/peertube-models' | ||
4 | import { AbstractCommand, OverrideCommandOptions } from '../shared/index.js' | ||
5 | |||
6 | type ListBlocklistOptions = OverrideCommandOptions & { | ||
7 | start: number | ||
8 | count: number | ||
9 | |||
10 | sort?: string // default -createdAt | ||
11 | |||
12 | search?: string | ||
13 | } | ||
14 | |||
15 | export class BlocklistCommand extends AbstractCommand { | ||
16 | |||
17 | listMyAccountBlocklist (options: ListBlocklistOptions) { | ||
18 | const path = '/api/v1/users/me/blocklist/accounts' | ||
19 | |||
20 | return this.listBlocklist<AccountBlock>(options, path) | ||
21 | } | ||
22 | |||
23 | listMyServerBlocklist (options: ListBlocklistOptions) { | ||
24 | const path = '/api/v1/users/me/blocklist/servers' | ||
25 | |||
26 | return this.listBlocklist<ServerBlock>(options, path) | ||
27 | } | ||
28 | |||
29 | listServerAccountBlocklist (options: ListBlocklistOptions) { | ||
30 | const path = '/api/v1/server/blocklist/accounts' | ||
31 | |||
32 | return this.listBlocklist<AccountBlock>(options, path) | ||
33 | } | ||
34 | |||
35 | listServerServerBlocklist (options: ListBlocklistOptions) { | ||
36 | const path = '/api/v1/server/blocklist/servers' | ||
37 | |||
38 | return this.listBlocklist<ServerBlock>(options, path) | ||
39 | } | ||
40 | |||
41 | // --------------------------------------------------------------------------- | ||
42 | |||
43 | getStatus (options: OverrideCommandOptions & { | ||
44 | accounts?: string[] | ||
45 | hosts?: string[] | ||
46 | }) { | ||
47 | const { accounts, hosts } = options | ||
48 | |||
49 | const path = '/api/v1/blocklist/status' | ||
50 | |||
51 | return this.getRequestBody<BlockStatus>({ | ||
52 | ...options, | ||
53 | |||
54 | path, | ||
55 | query: { | ||
56 | accounts, | ||
57 | hosts | ||
58 | }, | ||
59 | implicitToken: false, | ||
60 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
61 | }) | ||
62 | } | ||
63 | |||
64 | // --------------------------------------------------------------------------- | ||
65 | |||
66 | addToMyBlocklist (options: OverrideCommandOptions & { | ||
67 | account?: string | ||
68 | server?: string | ||
69 | }) { | ||
70 | const { account, server } = options | ||
71 | |||
72 | const path = account | ||
73 | ? '/api/v1/users/me/blocklist/accounts' | ||
74 | : '/api/v1/users/me/blocklist/servers' | ||
75 | |||
76 | return this.postBodyRequest({ | ||
77 | ...options, | ||
78 | |||
79 | path, | ||
80 | fields: { | ||
81 | accountName: account, | ||
82 | host: server | ||
83 | }, | ||
84 | implicitToken: true, | ||
85 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
86 | }) | ||
87 | } | ||
88 | |||
89 | addToServerBlocklist (options: OverrideCommandOptions & { | ||
90 | account?: string | ||
91 | server?: string | ||
92 | }) { | ||
93 | const { account, server } = options | ||
94 | |||
95 | const path = account | ||
96 | ? '/api/v1/server/blocklist/accounts' | ||
97 | : '/api/v1/server/blocklist/servers' | ||
98 | |||
99 | return this.postBodyRequest({ | ||
100 | ...options, | ||
101 | |||
102 | path, | ||
103 | fields: { | ||
104 | accountName: account, | ||
105 | host: server | ||
106 | }, | ||
107 | implicitToken: true, | ||
108 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
109 | }) | ||
110 | } | ||
111 | |||
112 | // --------------------------------------------------------------------------- | ||
113 | |||
114 | removeFromMyBlocklist (options: OverrideCommandOptions & { | ||
115 | account?: string | ||
116 | server?: string | ||
117 | }) { | ||
118 | const { account, server } = options | ||
119 | |||
120 | const path = account | ||
121 | ? '/api/v1/users/me/blocklist/accounts/' + account | ||
122 | : '/api/v1/users/me/blocklist/servers/' + server | ||
123 | |||
124 | return this.deleteRequest({ | ||
125 | ...options, | ||
126 | |||
127 | path, | ||
128 | implicitToken: true, | ||
129 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
130 | }) | ||
131 | } | ||
132 | |||
133 | removeFromServerBlocklist (options: OverrideCommandOptions & { | ||
134 | account?: string | ||
135 | server?: string | ||
136 | }) { | ||
137 | const { account, server } = options | ||
138 | |||
139 | const path = account | ||
140 | ? '/api/v1/server/blocklist/accounts/' + account | ||
141 | : '/api/v1/server/blocklist/servers/' + server | ||
142 | |||
143 | return this.deleteRequest({ | ||
144 | ...options, | ||
145 | |||
146 | path, | ||
147 | implicitToken: true, | ||
148 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
149 | }) | ||
150 | } | ||
151 | |||
152 | private listBlocklist <T> (options: ListBlocklistOptions, path: string) { | ||
153 | const { start, count, search, sort = '-createdAt' } = options | ||
154 | |||
155 | return this.getRequestBody<ResultList<T>>({ | ||
156 | ...options, | ||
157 | |||
158 | path, | ||
159 | query: { start, count, sort, search }, | ||
160 | implicitToken: true, | ||
161 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
162 | }) | ||
163 | } | ||
164 | |||
165 | } | ||