]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/server/server-blocklist.ts
Begin auth plugin support
[github/Chocobozzz/PeerTube.git] / server / controllers / api / server / server-blocklist.ts
CommitLineData
b44164bb
C
1import * as express from 'express'
2import 'multer'
3import { getFormattedObjects, getServerActor } from '../../../helpers/utils'
4import {
5 asyncMiddleware,
6 asyncRetryTransactionMiddleware,
7 authenticate,
8 ensureUserHasRight,
9 paginationValidator,
10 setDefaultPagination,
11 setDefaultSort
12} from '../../../middlewares'
13import {
14 accountsBlocklistSortValidator,
15 blockAccountValidator,
16 blockServerValidator,
17 serversBlocklistSortValidator,
18 unblockAccountByServerValidator,
19 unblockServerByServerValidator
20} from '../../../middlewares/validators'
b44164bb
C
21import { AccountBlocklistModel } from '../../../models/account/account-blocklist'
22import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../../../lib/blocklist'
23import { ServerBlocklistModel } from '../../../models/server/server-blocklist'
b44164bb
C
24import { UserRight } from '../../../../shared/models/users'
25
26const serverBlocklistRouter = express.Router()
27
28serverBlocklistRouter.get('/blocklist/accounts',
29 authenticate,
30 ensureUserHasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST),
31 paginationValidator,
32 accountsBlocklistSortValidator,
33 setDefaultSort,
34 setDefaultPagination,
35 asyncMiddleware(listBlockedAccounts)
36)
37
38serverBlocklistRouter.post('/blocklist/accounts',
39 authenticate,
40 ensureUserHasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST),
41 asyncMiddleware(blockAccountValidator),
42 asyncRetryTransactionMiddleware(blockAccount)
43)
44
45serverBlocklistRouter.delete('/blocklist/accounts/:accountName',
46 authenticate,
47 ensureUserHasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST),
48 asyncMiddleware(unblockAccountByServerValidator),
49 asyncRetryTransactionMiddleware(unblockAccount)
50)
51
52serverBlocklistRouter.get('/blocklist/servers',
53 authenticate,
54 ensureUserHasRight(UserRight.MANAGE_SERVERS_BLOCKLIST),
55 paginationValidator,
56 serversBlocklistSortValidator,
57 setDefaultSort,
58 setDefaultPagination,
59 asyncMiddleware(listBlockedServers)
60)
61
62serverBlocklistRouter.post('/blocklist/servers',
63 authenticate,
64 ensureUserHasRight(UserRight.MANAGE_SERVERS_BLOCKLIST),
65 asyncMiddleware(blockServerValidator),
66 asyncRetryTransactionMiddleware(blockServer)
67)
68
69serverBlocklistRouter.delete('/blocklist/servers/:host',
70 authenticate,
71 ensureUserHasRight(UserRight.MANAGE_SERVERS_BLOCKLIST),
72 asyncMiddleware(unblockServerByServerValidator),
73 asyncRetryTransactionMiddleware(unblockServer)
74)
75
76export {
77 serverBlocklistRouter
78}
79
80// ---------------------------------------------------------------------------
81
82async function listBlockedAccounts (req: express.Request, res: express.Response) {
83 const serverActor = await getServerActor()
84
e0a92917
RK
85 const resultList = await AccountBlocklistModel.listForApi({
86 start: req.query.start,
87 count: req.query.count,
88 sort: req.query.sort,
89 search: req.query.search,
90 accountId: serverActor.Account.id
91 })
b44164bb
C
92
93 return res.json(getFormattedObjects(resultList.data, resultList.total))
94}
95
96async function blockAccount (req: express.Request, res: express.Response) {
97 const serverActor = await getServerActor()
dae86118 98 const accountToBlock = res.locals.account
b44164bb
C
99
100 await addAccountInBlocklist(serverActor.Account.id, accountToBlock.id)
101
102 return res.status(204).end()
103}
104
105async function unblockAccount (req: express.Request, res: express.Response) {
dae86118 106 const accountBlock = res.locals.accountBlock
b44164bb
C
107
108 await removeAccountFromBlocklist(accountBlock)
109
110 return res.status(204).end()
111}
112
113async function listBlockedServers (req: express.Request, res: express.Response) {
114 const serverActor = await getServerActor()
115
e0a92917
RK
116 const resultList = await ServerBlocklistModel.listForApi({
117 start: req.query.start,
118 count: req.query.count,
119 sort: req.query.sort,
120 search: req.query.search,
121 accountId: serverActor.Account.id
122 })
b44164bb
C
123
124 return res.json(getFormattedObjects(resultList.data, resultList.total))
125}
126
127async function blockServer (req: express.Request, res: express.Response) {
128 const serverActor = await getServerActor()
dae86118 129 const serverToBlock = res.locals.server
b44164bb
C
130
131 await addServerInBlocklist(serverActor.Account.id, serverToBlock.id)
132
133 return res.status(204).end()
134}
135
136async function unblockServer (req: express.Request, res: express.Response) {
dae86118 137 const serverBlock = res.locals.serverBlock
b44164bb
C
138
139 await removeServerFromBlocklist(serverBlock)
140
141 return res.status(204).end()
142}