]>
Commit | Line | Data |
---|---|---|
5f8bd4cb C |
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
2 | ||
80badf49 | 3 | import { AccountBlock, BlockStatus, HttpStatusCode, ResultList, ServerBlock } from '@shared/models' |
5f8bd4cb C |
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 | ||
80badf49 C |
40 | getStatus (options: OverrideCommandOptions & { |
41 | accounts?: string[] | |
42 | hosts?: string[] | |
43 | }) { | |
44 | const { accounts, hosts } = options | |
45 | ||
46 | const path = '/api/v1/blocklist/status' | |
47 | ||
48 | return this.getRequestBody<BlockStatus>({ | |
49 | ...options, | |
50 | ||
51 | path, | |
52 | query: { | |
53 | accounts, | |
54 | hosts | |
55 | }, | |
56 | implicitToken: false, | |
57 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
58 | }) | |
59 | } | |
60 | ||
61 | // --------------------------------------------------------------------------- | |
62 | ||
5f8bd4cb C |
63 | addToMyBlocklist (options: OverrideCommandOptions & { |
64 | account?: string | |
65 | server?: string | |
66 | }) { | |
67 | const { account, server } = options | |
68 | ||
69 | const path = account | |
70 | ? '/api/v1/users/me/blocklist/accounts' | |
71 | : '/api/v1/users/me/blocklist/servers' | |
72 | ||
73 | return this.postBodyRequest({ | |
74 | ...options, | |
75 | ||
76 | path, | |
77 | fields: { | |
78 | accountName: account, | |
79 | host: server | |
80 | }, | |
a1637fa1 | 81 | implicitToken: true, |
5f8bd4cb C |
82 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 |
83 | }) | |
84 | } | |
85 | ||
86 | addToServerBlocklist (options: OverrideCommandOptions & { | |
87 | account?: string | |
88 | server?: string | |
89 | }) { | |
90 | const { account, server } = options | |
91 | ||
92 | const path = account | |
93 | ? '/api/v1/server/blocklist/accounts' | |
94 | : '/api/v1/server/blocklist/servers' | |
95 | ||
96 | return this.postBodyRequest({ | |
97 | ...options, | |
98 | ||
99 | path, | |
100 | fields: { | |
101 | accountName: account, | |
102 | host: server | |
103 | }, | |
a1637fa1 | 104 | implicitToken: true, |
5f8bd4cb C |
105 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 |
106 | }) | |
107 | } | |
108 | ||
109 | // --------------------------------------------------------------------------- | |
110 | ||
111 | removeFromMyBlocklist (options: OverrideCommandOptions & { | |
112 | account?: string | |
113 | server?: string | |
114 | }) { | |
115 | const { account, server } = options | |
116 | ||
117 | const path = account | |
118 | ? '/api/v1/users/me/blocklist/accounts/' + account | |
119 | : '/api/v1/users/me/blocklist/servers/' + server | |
120 | ||
121 | return this.deleteRequest({ | |
122 | ...options, | |
123 | ||
124 | path, | |
a1637fa1 | 125 | implicitToken: true, |
5f8bd4cb C |
126 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 |
127 | }) | |
128 | } | |
129 | ||
130 | removeFromServerBlocklist (options: OverrideCommandOptions & { | |
131 | account?: string | |
132 | server?: string | |
133 | }) { | |
134 | const { account, server } = options | |
135 | ||
136 | const path = account | |
137 | ? '/api/v1/server/blocklist/accounts/' + account | |
138 | : '/api/v1/server/blocklist/servers/' + server | |
139 | ||
140 | return this.deleteRequest({ | |
141 | ...options, | |
142 | ||
143 | path, | |
a1637fa1 | 144 | implicitToken: true, |
5f8bd4cb C |
145 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 |
146 | }) | |
147 | } | |
148 | ||
149 | private listBlocklist <T> (options: ListBlocklistOptions, path: string) { | |
150 | const { start, count, sort = '-createdAt' } = options | |
151 | ||
152 | return this.getRequestBody<ResultList<T>>({ | |
153 | ...options, | |
154 | ||
155 | path, | |
156 | query: { start, count, sort }, | |
a1637fa1 | 157 | implicitToken: true, |
5f8bd4cb C |
158 | defaultExpectedStatus: HttpStatusCode.OK_200 |
159 | }) | |
160 | } | |
161 | ||
162 | } |