]>
Commit | Line | Data |
---|---|---|
b44164bb C |
1 | import * as express from 'express' |
2 | import 'multer' | |
3 | import { getFormattedObjects, getServerActor } from '../../../helpers/utils' | |
4 | import { | |
5 | asyncMiddleware, | |
6 | asyncRetryTransactionMiddleware, | |
7 | authenticate, | |
8 | ensureUserHasRight, | |
9 | paginationValidator, | |
10 | setDefaultPagination, | |
11 | setDefaultSort | |
12 | } from '../../../middlewares' | |
13 | import { | |
14 | accountsBlocklistSortValidator, | |
15 | blockAccountValidator, | |
16 | blockServerValidator, | |
17 | serversBlocklistSortValidator, | |
18 | unblockAccountByServerValidator, | |
19 | unblockServerByServerValidator | |
20 | } from '../../../middlewares/validators' | |
b44164bb C |
21 | import { AccountBlocklistModel } from '../../../models/account/account-blocklist' |
22 | import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../../../lib/blocklist' | |
23 | import { ServerBlocklistModel } from '../../../models/server/server-blocklist' | |
b44164bb C |
24 | import { UserRight } from '../../../../shared/models/users' |
25 | ||
26 | const serverBlocklistRouter = express.Router() | |
27 | ||
28 | serverBlocklistRouter.get('/blocklist/accounts', | |
29 | authenticate, | |
30 | ensureUserHasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST), | |
31 | paginationValidator, | |
32 | accountsBlocklistSortValidator, | |
33 | setDefaultSort, | |
34 | setDefaultPagination, | |
35 | asyncMiddleware(listBlockedAccounts) | |
36 | ) | |
37 | ||
38 | serverBlocklistRouter.post('/blocklist/accounts', | |
39 | authenticate, | |
40 | ensureUserHasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST), | |
41 | asyncMiddleware(blockAccountValidator), | |
42 | asyncRetryTransactionMiddleware(blockAccount) | |
43 | ) | |
44 | ||
45 | serverBlocklistRouter.delete('/blocklist/accounts/:accountName', | |
46 | authenticate, | |
47 | ensureUserHasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST), | |
48 | asyncMiddleware(unblockAccountByServerValidator), | |
49 | asyncRetryTransactionMiddleware(unblockAccount) | |
50 | ) | |
51 | ||
52 | serverBlocklistRouter.get('/blocklist/servers', | |
53 | authenticate, | |
54 | ensureUserHasRight(UserRight.MANAGE_SERVERS_BLOCKLIST), | |
55 | paginationValidator, | |
56 | serversBlocklistSortValidator, | |
57 | setDefaultSort, | |
58 | setDefaultPagination, | |
59 | asyncMiddleware(listBlockedServers) | |
60 | ) | |
61 | ||
62 | serverBlocklistRouter.post('/blocklist/servers', | |
63 | authenticate, | |
64 | ensureUserHasRight(UserRight.MANAGE_SERVERS_BLOCKLIST), | |
65 | asyncMiddleware(blockServerValidator), | |
66 | asyncRetryTransactionMiddleware(blockServer) | |
67 | ) | |
68 | ||
69 | serverBlocklistRouter.delete('/blocklist/servers/:host', | |
70 | authenticate, | |
71 | ensureUserHasRight(UserRight.MANAGE_SERVERS_BLOCKLIST), | |
72 | asyncMiddleware(unblockServerByServerValidator), | |
73 | asyncRetryTransactionMiddleware(unblockServer) | |
74 | ) | |
75 | ||
76 | export { | |
77 | serverBlocklistRouter | |
78 | } | |
79 | ||
80 | // --------------------------------------------------------------------------- | |
81 | ||
82 | async function listBlockedAccounts (req: express.Request, res: express.Response) { | |
83 | const serverActor = await getServerActor() | |
84 | ||
85 | const resultList = await AccountBlocklistModel.listForApi(serverActor.Account.id, req.query.start, req.query.count, req.query.sort) | |
86 | ||
87 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
88 | } | |
89 | ||
90 | async function blockAccount (req: express.Request, res: express.Response) { | |
91 | const serverActor = await getServerActor() | |
dae86118 | 92 | const accountToBlock = res.locals.account |
b44164bb C |
93 | |
94 | await addAccountInBlocklist(serverActor.Account.id, accountToBlock.id) | |
95 | ||
96 | return res.status(204).end() | |
97 | } | |
98 | ||
99 | async function unblockAccount (req: express.Request, res: express.Response) { | |
dae86118 | 100 | const accountBlock = res.locals.accountBlock |
b44164bb C |
101 | |
102 | await removeAccountFromBlocklist(accountBlock) | |
103 | ||
104 | return res.status(204).end() | |
105 | } | |
106 | ||
107 | async function listBlockedServers (req: express.Request, res: express.Response) { | |
108 | const serverActor = await getServerActor() | |
109 | ||
110 | const resultList = await ServerBlocklistModel.listForApi(serverActor.Account.id, req.query.start, req.query.count, req.query.sort) | |
111 | ||
112 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
113 | } | |
114 | ||
115 | async function blockServer (req: express.Request, res: express.Response) { | |
116 | const serverActor = await getServerActor() | |
dae86118 | 117 | const serverToBlock = res.locals.server |
b44164bb C |
118 | |
119 | await addServerInBlocklist(serverActor.Account.id, serverToBlock.id) | |
120 | ||
121 | return res.status(204).end() | |
122 | } | |
123 | ||
124 | async function unblockServer (req: express.Request, res: express.Response) { | |
dae86118 | 125 | const serverBlock = res.locals.serverBlock |
b44164bb C |
126 | |
127 | await removeServerFromBlocklist(serverBlock) | |
128 | ||
129 | return res.status(204).end() | |
130 | } |