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