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