2 import express from 'express'
3 import { logger } from '@server/helpers/logger'
4 import { getServerActor } from '@server/models/application/application'
5 import { UserNotificationModel } from '@server/models/user/user-notification'
6 import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
7 import { UserRight } from '../../../../shared/models/users'
8 import { getFormattedObjects } from '../../../helpers/utils'
9 import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../../../lib/blocklist'
12 asyncRetryTransactionMiddleware,
18 } from '../../../middlewares'
20 accountsBlocklistSortValidator,
21 blockAccountValidator,
23 serversBlocklistSortValidator,
24 unblockAccountByServerValidator,
25 unblockServerByServerValidator
26 } from '../../../middlewares/validators'
27 import { AccountBlocklistModel } from '../../../models/account/account-blocklist'
28 import { ServerBlocklistModel } from '../../../models/server/server-blocklist'
30 const serverBlocklistRouter = express.Router()
32 serverBlocklistRouter.get('/blocklist/accounts',
34 ensureUserHasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST),
36 accountsBlocklistSortValidator,
39 asyncMiddleware(listBlockedAccounts)
42 serverBlocklistRouter.post('/blocklist/accounts',
44 ensureUserHasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST),
45 asyncMiddleware(blockAccountValidator),
46 asyncRetryTransactionMiddleware(blockAccount)
49 serverBlocklistRouter.delete('/blocklist/accounts/:accountName',
51 ensureUserHasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST),
52 asyncMiddleware(unblockAccountByServerValidator),
53 asyncRetryTransactionMiddleware(unblockAccount)
56 serverBlocklistRouter.get('/blocklist/servers',
58 ensureUserHasRight(UserRight.MANAGE_SERVERS_BLOCKLIST),
60 serversBlocklistSortValidator,
63 asyncMiddleware(listBlockedServers)
66 serverBlocklistRouter.post('/blocklist/servers',
68 ensureUserHasRight(UserRight.MANAGE_SERVERS_BLOCKLIST),
69 asyncMiddleware(blockServerValidator),
70 asyncRetryTransactionMiddleware(blockServer)
73 serverBlocklistRouter.delete('/blocklist/servers/:host',
75 ensureUserHasRight(UserRight.MANAGE_SERVERS_BLOCKLIST),
76 asyncMiddleware(unblockServerByServerValidator),
77 asyncRetryTransactionMiddleware(unblockServer)
84 // ---------------------------------------------------------------------------
86 async function listBlockedAccounts (req: express.Request, res: express.Response) {
87 const serverActor = await getServerActor()
89 const resultList = await AccountBlocklistModel.listForApi({
90 start: req.query.start,
91 count: req.query.count,
93 search: req.query.search,
94 accountId: serverActor.Account.id
97 return res.json(getFormattedObjects(resultList.data, resultList.total))
100 async function blockAccount (req: express.Request, res: express.Response) {
101 const serverActor = await getServerActor()
102 const accountToBlock = res.locals.account
104 await addAccountInBlocklist(serverActor.Account.id, accountToBlock.id)
106 UserNotificationModel.removeNotificationsOf({
107 id: accountToBlock.id,
109 forUserId: null // For all users
110 }).catch(err => logger.error('Cannot remove notifications after an account mute.', { err }))
112 return res.status(HttpStatusCode.NO_CONTENT_204).end()
115 async function unblockAccount (req: express.Request, res: express.Response) {
116 const accountBlock = res.locals.accountBlock
118 await removeAccountFromBlocklist(accountBlock)
120 return res.status(HttpStatusCode.NO_CONTENT_204).end()
123 async function listBlockedServers (req: express.Request, res: express.Response) {
124 const serverActor = await getServerActor()
126 const resultList = await ServerBlocklistModel.listForApi({
127 start: req.query.start,
128 count: req.query.count,
129 sort: req.query.sort,
130 search: req.query.search,
131 accountId: serverActor.Account.id
134 return res.json(getFormattedObjects(resultList.data, resultList.total))
137 async function blockServer (req: express.Request, res: express.Response) {
138 const serverActor = await getServerActor()
139 const serverToBlock = res.locals.server
141 await addServerInBlocklist(serverActor.Account.id, serverToBlock.id)
143 UserNotificationModel.removeNotificationsOf({
144 id: serverToBlock.id,
146 forUserId: null // For all users
147 }).catch(err => logger.error('Cannot remove notifications after a server mute.', { err }))
149 return res.status(HttpStatusCode.NO_CONTENT_204).end()
152 async function unblockServer (req: express.Request, res: express.Response) {
153 const serverBlock = res.locals.serverBlock
155 await removeServerFromBlocklist(serverBlock)
157 return res.status(HttpStatusCode.NO_CONTENT_204).end()