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