1 import * as express from 'express'
3 import { getFormattedObjects } from '../../../helpers/utils'
6 asyncRetryTransactionMiddleware,
11 unblockAccountByAccountValidator
12 } from '../../../middlewares'
14 accountsBlocklistSortValidator,
15 blockAccountValidator,
17 serversBlocklistSortValidator,
18 unblockServerByAccountValidator
19 } from '../../../middlewares/validators'
20 import { AccountBlocklistModel } from '../../../models/account/account-blocklist'
21 import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../../../lib/blocklist'
22 import { ServerBlocklistModel } from '../../../models/server/server-blocklist'
23 import { UserNotificationModel } from '@server/models/account/user-notification'
24 import { logger } from '@server/helpers/logger'
25 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
27 const myBlocklistRouter = express.Router()
29 myBlocklistRouter.get('/me/blocklist/accounts',
32 accountsBlocklistSortValidator,
35 asyncMiddleware(listBlockedAccounts)
38 myBlocklistRouter.post('/me/blocklist/accounts',
40 asyncMiddleware(blockAccountValidator),
41 asyncRetryTransactionMiddleware(blockAccount)
44 myBlocklistRouter.delete('/me/blocklist/accounts/:accountName',
46 asyncMiddleware(unblockAccountByAccountValidator),
47 asyncRetryTransactionMiddleware(unblockAccount)
50 myBlocklistRouter.get('/me/blocklist/servers',
53 serversBlocklistSortValidator,
56 asyncMiddleware(listBlockedServers)
59 myBlocklistRouter.post('/me/blocklist/servers',
61 asyncMiddleware(blockServerValidator),
62 asyncRetryTransactionMiddleware(blockServer)
65 myBlocklistRouter.delete('/me/blocklist/servers/:host',
67 asyncMiddleware(unblockServerByAccountValidator),
68 asyncRetryTransactionMiddleware(unblockServer)
75 // ---------------------------------------------------------------------------
77 async function listBlockedAccounts (req: express.Request, res: express.Response) {
78 const user = res.locals.oauth.token.User
80 const resultList = await AccountBlocklistModel.listForApi({
81 start: req.query.start,
82 count: req.query.count,
84 search: req.query.search,
85 accountId: user.Account.id
88 return res.json(getFormattedObjects(resultList.data, resultList.total))
91 async function blockAccount (req: express.Request, res: express.Response) {
92 const user = res.locals.oauth.token.User
93 const accountToBlock = res.locals.account
95 await addAccountInBlocklist(user.Account.id, accountToBlock.id)
97 UserNotificationModel.removeNotificationsOf({
98 id: accountToBlock.id,
101 }).catch(err => logger.error('Cannot remove notifications after an account mute.', { err }))
103 return res.status(HttpStatusCode.NO_CONTENT_204).end()
106 async function unblockAccount (req: express.Request, res: express.Response) {
107 const accountBlock = res.locals.accountBlock
109 await removeAccountFromBlocklist(accountBlock)
111 return res.status(HttpStatusCode.NO_CONTENT_204).end()
114 async function listBlockedServers (req: express.Request, res: express.Response) {
115 const user = res.locals.oauth.token.User
117 const resultList = await ServerBlocklistModel.listForApi({
118 start: req.query.start,
119 count: req.query.count,
120 sort: req.query.sort,
121 search: req.query.search,
122 accountId: user.Account.id
125 return res.json(getFormattedObjects(resultList.data, resultList.total))
128 async function blockServer (req: express.Request, res: express.Response) {
129 const user = res.locals.oauth.token.User
130 const serverToBlock = res.locals.server
132 await addServerInBlocklist(user.Account.id, serverToBlock.id)
134 UserNotificationModel.removeNotificationsOf({
135 id: serverToBlock.id,
138 }).catch(err => logger.error('Cannot remove notifications after a server mute.', { err }))
140 return res.status(HttpStatusCode.NO_CONTENT_204).end()
143 async function unblockServer (req: express.Request, res: express.Response) {
144 const serverBlock = res.locals.serverBlock
146 await removeServerFromBlocklist(serverBlock)
148 return res.status(HttpStatusCode.NO_CONTENT_204).end()