]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/server/server-blocklist.ts
Add action hooks to user routes
[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
85 const resultList = await AccountBlocklistModel.listForApi(serverActor.Account.id, req.query.start, req.query.count, req.query.sort)
86
87 return res.json(getFormattedObjects(resultList.data, resultList.total))
88}
89
90async function blockAccount (req: express.Request, res: express.Response) {
91 const serverActor = await getServerActor()
dae86118 92 const accountToBlock = res.locals.account
b44164bb
C
93
94 await addAccountInBlocklist(serverActor.Account.id, accountToBlock.id)
95
96 return res.status(204).end()
97}
98
99async function unblockAccount (req: express.Request, res: express.Response) {
dae86118 100 const accountBlock = res.locals.accountBlock
b44164bb
C
101
102 await removeAccountFromBlocklist(accountBlock)
103
104 return res.status(204).end()
105}
106
107async function listBlockedServers (req: express.Request, res: express.Response) {
108 const serverActor = await getServerActor()
109
110 const resultList = await ServerBlocklistModel.listForApi(serverActor.Account.id, req.query.start, req.query.count, req.query.sort)
111
112 return res.json(getFormattedObjects(resultList.data, resultList.total))
113}
114
115async function blockServer (req: express.Request, res: express.Response) {
116 const serverActor = await getServerActor()
dae86118 117 const serverToBlock = res.locals.server
b44164bb
C
118
119 await addServerInBlocklist(serverActor.Account.id, serverToBlock.id)
120
121 return res.status(204).end()
122}
123
124async function unblockServer (req: express.Request, res: express.Response) {
dae86118 125 const serverBlock = res.locals.serverBlock
b44164bb
C
126
127 await removeServerFromBlocklist(serverBlock)
128
129 return res.status(204).end()
130}