1 import * as express from 'express'
3 import { getFormattedObjects, getServerActor } from '../../../helpers/utils'
6 asyncRetryTransactionMiddleware,
12 } from '../../../middlewares'
14 accountsBlocklistSortValidator,
15 blockAccountValidator,
17 serversBlocklistSortValidator,
18 unblockAccountByServerValidator,
19 unblockServerByServerValidator
20 } from '../../../middlewares/validators'
21 import { AccountModel } from '../../../models/account/account'
22 import { AccountBlocklistModel } from '../../../models/account/account-blocklist'
23 import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../../../lib/blocklist'
24 import { ServerBlocklistModel } from '../../../models/server/server-blocklist'
25 import { ServerModel } from '../../../models/server/server'
26 import { UserRight } from '../../../../shared/models/users'
28 const serverBlocklistRouter = express.Router()
30 serverBlocklistRouter.get('/blocklist/accounts',
32 ensureUserHasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST),
34 accountsBlocklistSortValidator,
37 asyncMiddleware(listBlockedAccounts)
40 serverBlocklistRouter.post('/blocklist/accounts',
42 ensureUserHasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST),
43 asyncMiddleware(blockAccountValidator),
44 asyncRetryTransactionMiddleware(blockAccount)
47 serverBlocklistRouter.delete('/blocklist/accounts/:accountName',
49 ensureUserHasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST),
50 asyncMiddleware(unblockAccountByServerValidator),
51 asyncRetryTransactionMiddleware(unblockAccount)
54 serverBlocklistRouter.get('/blocklist/servers',
56 ensureUserHasRight(UserRight.MANAGE_SERVERS_BLOCKLIST),
58 serversBlocklistSortValidator,
61 asyncMiddleware(listBlockedServers)
64 serverBlocklistRouter.post('/blocklist/servers',
66 ensureUserHasRight(UserRight.MANAGE_SERVERS_BLOCKLIST),
67 asyncMiddleware(blockServerValidator),
68 asyncRetryTransactionMiddleware(blockServer)
71 serverBlocklistRouter.delete('/blocklist/servers/:host',
73 ensureUserHasRight(UserRight.MANAGE_SERVERS_BLOCKLIST),
74 asyncMiddleware(unblockServerByServerValidator),
75 asyncRetryTransactionMiddleware(unblockServer)
82 // ---------------------------------------------------------------------------
84 async function listBlockedAccounts (req: express.Request, res: express.Response) {
85 const serverActor = await getServerActor()
87 const resultList = await AccountBlocklistModel.listForApi(serverActor.Account.id, req.query.start, req.query.count, req.query.sort)
89 return res.json(getFormattedObjects(resultList.data, resultList.total))
92 async function blockAccount (req: express.Request, res: express.Response) {
93 const serverActor = await getServerActor()
94 const accountToBlock: AccountModel = res.locals.account
96 await addAccountInBlocklist(serverActor.Account.id, accountToBlock.id)
98 return res.status(204).end()
101 async function unblockAccount (req: express.Request, res: express.Response) {
102 const accountBlock: AccountBlocklistModel = res.locals.accountBlock
104 await removeAccountFromBlocklist(accountBlock)
106 return res.status(204).end()
109 async function listBlockedServers (req: express.Request, res: express.Response) {
110 const serverActor = await getServerActor()
112 const resultList = await ServerBlocklistModel.listForApi(serverActor.Account.id, req.query.start, req.query.count, req.query.sort)
114 return res.json(getFormattedObjects(resultList.data, resultList.total))
117 async function blockServer (req: express.Request, res: express.Response) {
118 const serverActor = await getServerActor()
119 const serverToBlock: ServerModel = res.locals.server
121 await addServerInBlocklist(serverActor.Account.id, serverToBlock.id)
123 return res.status(204).end()
126 async function unblockServer (req: express.Request, res: express.Response) {
127 const serverBlock: ServerBlocklistModel = res.locals.serverBlock
129 await removeServerFromBlocklist(serverBlock)
131 return res.status(204).end()