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