2 import * as express from 'express'
3 import { logger } from '@server/helpers/logger'
4 import { UserNotificationModel } from '@server/models/account/user-notification'
5 import { getServerActor } from '@server/models/application/application'
6 import { UserRight } from '../../../../shared/models/users'
7 import { getFormattedObjects } from '../../../helpers/utils'
8 import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../../../lib/blocklist'
11 asyncRetryTransactionMiddleware,
17 } from '../../../middlewares'
19 accountsBlocklistSortValidator,
20 blockAccountValidator,
22 serversBlocklistSortValidator,
23 unblockAccountByServerValidator,
24 unblockServerByServerValidator
25 } from '../../../middlewares/validators'
26 import { AccountBlocklistModel } from '../../../models/account/account-blocklist'
27 import { ServerBlocklistModel } from '../../../models/server/server-blocklist'
29 const serverBlocklistRouter = express.Router()
31 serverBlocklistRouter.get('/blocklist/accounts',
33 ensureUserHasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST),
35 accountsBlocklistSortValidator,
38 asyncMiddleware(listBlockedAccounts)
41 serverBlocklistRouter.post('/blocklist/accounts',
43 ensureUserHasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST),
44 asyncMiddleware(blockAccountValidator),
45 asyncRetryTransactionMiddleware(blockAccount)
48 serverBlocklistRouter.delete('/blocklist/accounts/:accountName',
50 ensureUserHasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST),
51 asyncMiddleware(unblockAccountByServerValidator),
52 asyncRetryTransactionMiddleware(unblockAccount)
55 serverBlocklistRouter.get('/blocklist/servers',
57 ensureUserHasRight(UserRight.MANAGE_SERVERS_BLOCKLIST),
59 serversBlocklistSortValidator,
62 asyncMiddleware(listBlockedServers)
65 serverBlocklistRouter.post('/blocklist/servers',
67 ensureUserHasRight(UserRight.MANAGE_SERVERS_BLOCKLIST),
68 asyncMiddleware(blockServerValidator),
69 asyncRetryTransactionMiddleware(blockServer)
72 serverBlocklistRouter.delete('/blocklist/servers/:host',
74 ensureUserHasRight(UserRight.MANAGE_SERVERS_BLOCKLIST),
75 asyncMiddleware(unblockServerByServerValidator),
76 asyncRetryTransactionMiddleware(unblockServer)
83 // ---------------------------------------------------------------------------
85 async function listBlockedAccounts (req: express.Request, res: express.Response) {
86 const serverActor = await getServerActor()
88 const resultList = await AccountBlocklistModel.listForApi({
89 start: req.query.start,
90 count: req.query.count,
92 search: req.query.search,
93 accountId: serverActor.Account.id
96 return res.json(getFormattedObjects(resultList.data, resultList.total))
99 async function blockAccount (req: express.Request, res: express.Response) {
100 const serverActor = await getServerActor()
101 const accountToBlock = res.locals.account
103 await addAccountInBlocklist(serverActor.Account.id, accountToBlock.id)
105 UserNotificationModel.removeNotificationsOf({
106 id: accountToBlock.id,
108 forUserId: null // For all users
109 }).catch(err => logger.error('Cannot remove notifications after an account mute.', { err }))
111 return res.status(204).end()
114 async function unblockAccount (req: express.Request, res: express.Response) {
115 const accountBlock = res.locals.accountBlock
117 await removeAccountFromBlocklist(accountBlock)
119 return res.status(204).end()
122 async function listBlockedServers (req: express.Request, res: express.Response) {
123 const serverActor = await getServerActor()
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
133 return res.json(getFormattedObjects(resultList.data, resultList.total))
136 async function blockServer (req: express.Request, res: express.Response) {
137 const serverActor = await getServerActor()
138 const serverToBlock = res.locals.server
140 await addServerInBlocklist(serverActor.Account.id, serverToBlock.id)
142 UserNotificationModel.removeNotificationsOf({
143 id: serverToBlock.id,
145 forUserId: null // For all users
146 }).catch(err => logger.error('Cannot remove notifications after a server mute.', { err }))
148 return res.status(204).end()
151 async function unblockServer (req: express.Request, res: express.Response) {
152 const serverBlock = res.locals.serverBlock
154 await removeServerFromBlocklist(serverBlock)
156 return res.status(204).end()