]>
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 | |
d3976db2 C |
9 | |
10 | sort?: string // default -createdAt | |
11 | ||
12 | search?: string | |
5f8bd4cb C |
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 | ||
80badf49 C |
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 | ||
5f8bd4cb C |
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 | }, | |
a1637fa1 | 84 | implicitToken: true, |
5f8bd4cb C |
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 | }, | |
a1637fa1 | 107 | implicitToken: true, |
5f8bd4cb C |
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, | |
a1637fa1 | 128 | implicitToken: true, |
5f8bd4cb C |
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, | |
a1637fa1 | 147 | implicitToken: true, |
5f8bd4cb C |
148 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 |
149 | }) | |
150 | } | |
151 | ||
152 | private listBlocklist <T> (options: ListBlocklistOptions, path: string) { | |
d3976db2 | 153 | const { start, count, search, sort = '-createdAt' } = options |
5f8bd4cb C |
154 | |
155 | return this.getRequestBody<ResultList<T>>({ | |
156 | ...options, | |
157 | ||
158 | path, | |
d3976db2 | 159 | query: { start, count, sort, search }, |
a1637fa1 | 160 | implicitToken: true, |
5f8bd4cb C |
161 | defaultExpectedStatus: HttpStatusCode.OK_200 |
162 | }) | |
163 | } | |
164 | ||
165 | } |