diff options
Diffstat (limited to 'shared/extra-utils/users/blocklist-command.ts')
-rw-r--r-- | shared/extra-utils/users/blocklist-command.ts | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/shared/extra-utils/users/blocklist-command.ts b/shared/extra-utils/users/blocklist-command.ts new file mode 100644 index 000000000..14491a1ae --- /dev/null +++ b/shared/extra-utils/users/blocklist-command.ts | |||
@@ -0,0 +1,139 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | ||
2 | |||
3 | import { AccountBlock, HttpStatusCode, ResultList, ServerBlock } from '@shared/models' | ||
4 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
5 | |||
6 | type ListBlocklistOptions = OverrideCommandOptions & { | ||
7 | start: number | ||
8 | count: number | ||
9 | sort: string // default -createdAt | ||
10 | } | ||
11 | |||
12 | export class BlocklistCommand extends AbstractCommand { | ||
13 | |||
14 | listMyAccountBlocklist (options: ListBlocklistOptions) { | ||
15 | const path = '/api/v1/users/me/blocklist/accounts' | ||
16 | |||
17 | return this.listBlocklist<AccountBlock>(options, path) | ||
18 | } | ||
19 | |||
20 | listMyServerBlocklist (options: ListBlocklistOptions) { | ||
21 | const path = '/api/v1/users/me/blocklist/servers' | ||
22 | |||
23 | return this.listBlocklist<ServerBlock>(options, path) | ||
24 | } | ||
25 | |||
26 | listServerAccountBlocklist (options: ListBlocklistOptions) { | ||
27 | const path = '/api/v1/server/blocklist/accounts' | ||
28 | |||
29 | return this.listBlocklist<AccountBlock>(options, path) | ||
30 | } | ||
31 | |||
32 | listServerServerBlocklist (options: ListBlocklistOptions) { | ||
33 | const path = '/api/v1/server/blocklist/servers' | ||
34 | |||
35 | return this.listBlocklist<ServerBlock>(options, path) | ||
36 | } | ||
37 | |||
38 | // --------------------------------------------------------------------------- | ||
39 | |||
40 | addToMyBlocklist (options: OverrideCommandOptions & { | ||
41 | account?: string | ||
42 | server?: string | ||
43 | }) { | ||
44 | const { account, server } = options | ||
45 | |||
46 | const path = account | ||
47 | ? '/api/v1/users/me/blocklist/accounts' | ||
48 | : '/api/v1/users/me/blocklist/servers' | ||
49 | |||
50 | return this.postBodyRequest({ | ||
51 | ...options, | ||
52 | |||
53 | path, | ||
54 | fields: { | ||
55 | accountName: account, | ||
56 | host: server | ||
57 | }, | ||
58 | implicitToken: true, | ||
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 | implicitToken: true, | ||
82 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
83 | }) | ||
84 | } | ||
85 | |||
86 | // --------------------------------------------------------------------------- | ||
87 | |||
88 | removeFromMyBlocklist (options: OverrideCommandOptions & { | ||
89 | account?: string | ||
90 | server?: string | ||
91 | }) { | ||
92 | const { account, server } = options | ||
93 | |||
94 | const path = account | ||
95 | ? '/api/v1/users/me/blocklist/accounts/' + account | ||
96 | : '/api/v1/users/me/blocklist/servers/' + server | ||
97 | |||
98 | return this.deleteRequest({ | ||
99 | ...options, | ||
100 | |||
101 | path, | ||
102 | implicitToken: true, | ||
103 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
104 | }) | ||
105 | } | ||
106 | |||
107 | removeFromServerBlocklist (options: OverrideCommandOptions & { | ||
108 | account?: string | ||
109 | server?: string | ||
110 | }) { | ||
111 | const { account, server } = options | ||
112 | |||
113 | const path = account | ||
114 | ? '/api/v1/server/blocklist/accounts/' + account | ||
115 | : '/api/v1/server/blocklist/servers/' + server | ||
116 | |||
117 | return this.deleteRequest({ | ||
118 | ...options, | ||
119 | |||
120 | path, | ||
121 | implicitToken: true, | ||
122 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
123 | }) | ||
124 | } | ||
125 | |||
126 | private listBlocklist <T> (options: ListBlocklistOptions, path: string) { | ||
127 | const { start, count, sort = '-createdAt' } = options | ||
128 | |||
129 | return this.getRequestBody<ResultList<T>>({ | ||
130 | ...options, | ||
131 | |||
132 | path, | ||
133 | query: { start, count, sort }, | ||
134 | implicitToken: true, | ||
135 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
136 | }) | ||
137 | } | ||
138 | |||
139 | } | ||