]>
Commit | Line | Data |
---|---|---|
1 | import 'multer' | |
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' | |
10 | import { | |
11 | asyncMiddleware, | |
12 | asyncRetryTransactionMiddleware, | |
13 | authenticate, | |
14 | ensureUserHasRight, | |
15 | paginationValidator, | |
16 | setDefaultPagination, | |
17 | setDefaultSort | |
18 | } from '../../../middlewares' | |
19 | import { | |
20 | accountsBlocklistSortValidator, | |
21 | blockAccountValidator, | |
22 | blockServerValidator, | |
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' | |
29 | ||
30 | const serverBlocklistRouter = express.Router() | |
31 | ||
32 | serverBlocklistRouter.get('/blocklist/accounts', | |
33 | authenticate, | |
34 | ensureUserHasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST), | |
35 | paginationValidator, | |
36 | accountsBlocklistSortValidator, | |
37 | setDefaultSort, | |
38 | setDefaultPagination, | |
39 | asyncMiddleware(listBlockedAccounts) | |
40 | ) | |
41 | ||
42 | serverBlocklistRouter.post('/blocklist/accounts', | |
43 | authenticate, | |
44 | ensureUserHasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST), | |
45 | asyncMiddleware(blockAccountValidator), | |
46 | asyncRetryTransactionMiddleware(blockAccount) | |
47 | ) | |
48 | ||
49 | serverBlocklistRouter.delete('/blocklist/accounts/:accountName', | |
50 | authenticate, | |
51 | ensureUserHasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST), | |
52 | asyncMiddleware(unblockAccountByServerValidator), | |
53 | asyncRetryTransactionMiddleware(unblockAccount) | |
54 | ) | |
55 | ||
56 | serverBlocklistRouter.get('/blocklist/servers', | |
57 | authenticate, | |
58 | ensureUserHasRight(UserRight.MANAGE_SERVERS_BLOCKLIST), | |
59 | paginationValidator, | |
60 | serversBlocklistSortValidator, | |
61 | setDefaultSort, | |
62 | setDefaultPagination, | |
63 | asyncMiddleware(listBlockedServers) | |
64 | ) | |
65 | ||
66 | serverBlocklistRouter.post('/blocklist/servers', | |
67 | authenticate, | |
68 | ensureUserHasRight(UserRight.MANAGE_SERVERS_BLOCKLIST), | |
69 | asyncMiddleware(blockServerValidator), | |
70 | asyncRetryTransactionMiddleware(blockServer) | |
71 | ) | |
72 | ||
73 | serverBlocklistRouter.delete('/blocklist/servers/:host', | |
74 | authenticate, | |
75 | ensureUserHasRight(UserRight.MANAGE_SERVERS_BLOCKLIST), | |
76 | asyncMiddleware(unblockServerByServerValidator), | |
77 | asyncRetryTransactionMiddleware(unblockServer) | |
78 | ) | |
79 | ||
80 | export { | |
81 | serverBlocklistRouter | |
82 | } | |
83 | ||
84 | // --------------------------------------------------------------------------- | |
85 | ||
86 | async function listBlockedAccounts (req: express.Request, res: express.Response) { | |
87 | const serverActor = await getServerActor() | |
88 | ||
89 | const resultList = await AccountBlocklistModel.listForApi({ | |
90 | start: req.query.start, | |
91 | count: req.query.count, | |
92 | sort: req.query.sort, | |
93 | search: req.query.search, | |
94 | accountId: serverActor.Account.id | |
95 | }) | |
96 | ||
97 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
98 | } | |
99 | ||
100 | async function blockAccount (req: express.Request, res: express.Response) { | |
101 | const serverActor = await getServerActor() | |
102 | const accountToBlock = res.locals.account | |
103 | ||
104 | await addAccountInBlocklist(serverActor.Account.id, accountToBlock.id) | |
105 | ||
106 | UserNotificationModel.removeNotificationsOf({ | |
107 | id: accountToBlock.id, | |
108 | type: 'account', | |
109 | forUserId: null // For all users | |
110 | }).catch(err => logger.error('Cannot remove notifications after an account mute.', { err })) | |
111 | ||
112 | return res.status(HttpStatusCode.NO_CONTENT_204).end() | |
113 | } | |
114 | ||
115 | async function unblockAccount (req: express.Request, res: express.Response) { | |
116 | const accountBlock = res.locals.accountBlock | |
117 | ||
118 | await removeAccountFromBlocklist(accountBlock) | |
119 | ||
120 | return res.status(HttpStatusCode.NO_CONTENT_204).end() | |
121 | } | |
122 | ||
123 | async function listBlockedServers (req: express.Request, res: express.Response) { | |
124 | const serverActor = await getServerActor() | |
125 | ||
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 | |
132 | }) | |
133 | ||
134 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
135 | } | |
136 | ||
137 | async function blockServer (req: express.Request, res: express.Response) { | |
138 | const serverActor = await getServerActor() | |
139 | const serverToBlock = res.locals.server | |
140 | ||
141 | await addServerInBlocklist(serverActor.Account.id, serverToBlock.id) | |
142 | ||
143 | UserNotificationModel.removeNotificationsOf({ | |
144 | id: serverToBlock.id, | |
145 | type: 'server', | |
146 | forUserId: null // For all users | |
147 | }).catch(err => logger.error('Cannot remove notifications after a server mute.', { err })) | |
148 | ||
149 | return res.status(HttpStatusCode.NO_CONTENT_204).end() | |
150 | } | |
151 | ||
152 | async function unblockServer (req: express.Request, res: express.Response) { | |
153 | const serverBlock = res.locals.serverBlock | |
154 | ||
155 | await removeServerFromBlocklist(serverBlock) | |
156 | ||
157 | return res.status(HttpStatusCode.NO_CONTENT_204).end() | |
158 | } |