diff options
Diffstat (limited to 'server/controllers/api')
-rw-r--r-- | server/controllers/api/server/index.ts | 2 | ||||
-rw-r--r-- | server/controllers/api/server/server-blocklist.ts | 132 | ||||
-rw-r--r-- | server/controllers/api/users/my-blocklist.ts | 8 |
3 files changed, 138 insertions, 4 deletions
diff --git a/server/controllers/api/server/index.ts b/server/controllers/api/server/index.ts index 43bca2c10..c08192a8c 100644 --- a/server/controllers/api/server/index.ts +++ b/server/controllers/api/server/index.ts | |||
@@ -2,12 +2,14 @@ import * as express from 'express' | |||
2 | import { serverFollowsRouter } from './follows' | 2 | import { serverFollowsRouter } from './follows' |
3 | import { statsRouter } from './stats' | 3 | import { statsRouter } from './stats' |
4 | import { serverRedundancyRouter } from './redundancy' | 4 | import { serverRedundancyRouter } from './redundancy' |
5 | import { serverBlocklistRouter } from './server-blocklist' | ||
5 | 6 | ||
6 | const serverRouter = express.Router() | 7 | const serverRouter = express.Router() |
7 | 8 | ||
8 | serverRouter.use('/', serverFollowsRouter) | 9 | serverRouter.use('/', serverFollowsRouter) |
9 | serverRouter.use('/', serverRedundancyRouter) | 10 | serverRouter.use('/', serverRedundancyRouter) |
10 | serverRouter.use('/', statsRouter) | 11 | serverRouter.use('/', statsRouter) |
12 | serverRouter.use('/', serverBlocklistRouter) | ||
11 | 13 | ||
12 | // --------------------------------------------------------------------------- | 14 | // --------------------------------------------------------------------------- |
13 | 15 | ||
diff --git a/server/controllers/api/server/server-blocklist.ts b/server/controllers/api/server/server-blocklist.ts new file mode 100644 index 000000000..3cb3a96e2 --- /dev/null +++ b/server/controllers/api/server/server-blocklist.ts | |||
@@ -0,0 +1,132 @@ | |||
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 | } | ||
diff --git a/server/controllers/api/users/my-blocklist.ts b/server/controllers/api/users/my-blocklist.ts index 95a4105ec..9575eab46 100644 --- a/server/controllers/api/users/my-blocklist.ts +++ b/server/controllers/api/users/my-blocklist.ts | |||
@@ -12,8 +12,8 @@ import { | |||
12 | } from '../../../middlewares' | 12 | } from '../../../middlewares' |
13 | import { | 13 | import { |
14 | accountsBlocklistSortValidator, | 14 | accountsBlocklistSortValidator, |
15 | blockAccountByAccountValidator, | 15 | blockAccountValidator, |
16 | blockServerByAccountValidator, | 16 | blockServerValidator, |
17 | serversBlocklistSortValidator, | 17 | serversBlocklistSortValidator, |
18 | unblockServerByAccountValidator | 18 | unblockServerByAccountValidator |
19 | } from '../../../middlewares/validators' | 19 | } from '../../../middlewares/validators' |
@@ -37,7 +37,7 @@ myBlocklistRouter.get('/me/blocklist/accounts', | |||
37 | 37 | ||
38 | myBlocklistRouter.post('/me/blocklist/accounts', | 38 | myBlocklistRouter.post('/me/blocklist/accounts', |
39 | authenticate, | 39 | authenticate, |
40 | asyncMiddleware(blockAccountByAccountValidator), | 40 | asyncMiddleware(blockAccountValidator), |
41 | asyncRetryTransactionMiddleware(blockAccount) | 41 | asyncRetryTransactionMiddleware(blockAccount) |
42 | ) | 42 | ) |
43 | 43 | ||
@@ -58,7 +58,7 @@ myBlocklistRouter.get('/me/blocklist/servers', | |||
58 | 58 | ||
59 | myBlocklistRouter.post('/me/blocklist/servers', | 59 | myBlocklistRouter.post('/me/blocklist/servers', |
60 | authenticate, | 60 | authenticate, |
61 | asyncMiddleware(blockServerByAccountValidator), | 61 | asyncMiddleware(blockServerValidator), |
62 | asyncRetryTransactionMiddleware(blockServer) | 62 | asyncRetryTransactionMiddleware(blockServer) |
63 | ) | 63 | ) |
64 | 64 | ||