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