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