]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/extra-utils/users/blocklist-command.ts
Refactor requests
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / users / blocklist-command.ts
CommitLineData
5f8bd4cb
C
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
c0e8b12e 3import { HttpStatusCode } from '@shared/models'
5f8bd4cb
C
4import { AccountBlock, ResultList, ServerBlock } from '@shared/models'
5import { AbstractCommand, OverrideCommandOptions } from '../shared'
6
7type ListBlocklistOptions = OverrideCommandOptions & {
8 start: number
9 count: number
10 sort: string // default -createdAt
11}
12
13export 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 },
a1637fa1 59 implicitToken: true,
5f8bd4cb
C
60 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
61 })
62 }
63
64 addToServerBlocklist (options: OverrideCommandOptions & {
65 account?: string
66 server?: string
67 }) {
68 const { account, server } = options
69
70 const path = account
71 ? '/api/v1/server/blocklist/accounts'
72 : '/api/v1/server/blocklist/servers'
73
74 return this.postBodyRequest({
75 ...options,
76
77 path,
78 fields: {
79 accountName: account,
80 host: server
81 },
a1637fa1 82 implicitToken: true,
5f8bd4cb
C
83 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
84 })
85 }
86
87 // ---------------------------------------------------------------------------
88
89 removeFromMyBlocklist (options: OverrideCommandOptions & {
90 account?: string
91 server?: string
92 }) {
93 const { account, server } = options
94
95 const path = account
96 ? '/api/v1/users/me/blocklist/accounts/' + account
97 : '/api/v1/users/me/blocklist/servers/' + server
98
99 return this.deleteRequest({
100 ...options,
101
102 path,
a1637fa1 103 implicitToken: true,
5f8bd4cb
C
104 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
105 })
106 }
107
108 removeFromServerBlocklist (options: OverrideCommandOptions & {
109 account?: string
110 server?: string
111 }) {
112 const { account, server } = options
113
114 const path = account
115 ? '/api/v1/server/blocklist/accounts/' + account
116 : '/api/v1/server/blocklist/servers/' + server
117
118 return this.deleteRequest({
119 ...options,
120
121 path,
a1637fa1 122 implicitToken: true,
5f8bd4cb
C
123 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
124 })
125 }
126
127 private listBlocklist <T> (options: ListBlocklistOptions, path: string) {
128 const { start, count, sort = '-createdAt' } = options
129
130 return this.getRequestBody<ResultList<T>>({
131 ...options,
132
133 path,
134 query: { start, count, sort },
a1637fa1 135 implicitToken: true,
5f8bd4cb
C
136 defaultExpectedStatus: HttpStatusCode.OK_200
137 })
138 }
139
140}