aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-10-15 13:03:04 +0200
committerChocobozzz <me@florianbigard.com>2018-10-16 16:41:36 +0200
commitb44164bb567fe7c9f65f1ac2908d44990a8ccc8e (patch)
tree37baa0346293e1ed30f8a43270dfd54a4791c6f0 /server/controllers/api
parentaf5767ffae41b2d5604e41ba9a7225c623dd6735 (diff)
downloadPeerTube-b44164bb567fe7c9f65f1ac2908d44990a8ccc8e.tar.gz
PeerTube-b44164bb567fe7c9f65f1ac2908d44990a8ccc8e.tar.zst
PeerTube-b44164bb567fe7c9f65f1ac2908d44990a8ccc8e.zip
Add ability to mute a user/instance by server in server api
Diffstat (limited to 'server/controllers/api')
-rw-r--r--server/controllers/api/server/index.ts2
-rw-r--r--server/controllers/api/server/server-blocklist.ts132
-rw-r--r--server/controllers/api/users/my-blocklist.ts8
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'
2import { serverFollowsRouter } from './follows' 2import { serverFollowsRouter } from './follows'
3import { statsRouter } from './stats' 3import { statsRouter } from './stats'
4import { serverRedundancyRouter } from './redundancy' 4import { serverRedundancyRouter } from './redundancy'
5import { serverBlocklistRouter } from './server-blocklist'
5 6
6const serverRouter = express.Router() 7const serverRouter = express.Router()
7 8
8serverRouter.use('/', serverFollowsRouter) 9serverRouter.use('/', serverFollowsRouter)
9serverRouter.use('/', serverRedundancyRouter) 10serverRouter.use('/', serverRedundancyRouter)
10serverRouter.use('/', statsRouter) 11serverRouter.use('/', statsRouter)
12serverRouter.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 @@
1import * as express from 'express'
2import 'multer'
3import { getFormattedObjects, getServerActor } from '../../../helpers/utils'
4import {
5 asyncMiddleware,
6 asyncRetryTransactionMiddleware,
7 authenticate,
8 ensureUserHasRight,
9 paginationValidator,
10 setDefaultPagination,
11 setDefaultSort
12} from '../../../middlewares'
13import {
14 accountsBlocklistSortValidator,
15 blockAccountValidator,
16 blockServerValidator,
17 serversBlocklistSortValidator,
18 unblockAccountByServerValidator,
19 unblockServerByServerValidator
20} from '../../../middlewares/validators'
21import { AccountModel } from '../../../models/account/account'
22import { AccountBlocklistModel } from '../../../models/account/account-blocklist'
23import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../../../lib/blocklist'
24import { ServerBlocklistModel } from '../../../models/server/server-blocklist'
25import { ServerModel } from '../../../models/server/server'
26import { UserRight } from '../../../../shared/models/users'
27
28const serverBlocklistRouter = express.Router()
29
30serverBlocklistRouter.get('/blocklist/accounts',
31 authenticate,
32 ensureUserHasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST),
33 paginationValidator,
34 accountsBlocklistSortValidator,
35 setDefaultSort,
36 setDefaultPagination,
37 asyncMiddleware(listBlockedAccounts)
38)
39
40serverBlocklistRouter.post('/blocklist/accounts',
41 authenticate,
42 ensureUserHasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST),
43 asyncMiddleware(blockAccountValidator),
44 asyncRetryTransactionMiddleware(blockAccount)
45)
46
47serverBlocklistRouter.delete('/blocklist/accounts/:accountName',
48 authenticate,
49 ensureUserHasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST),
50 asyncMiddleware(unblockAccountByServerValidator),
51 asyncRetryTransactionMiddleware(unblockAccount)
52)
53
54serverBlocklistRouter.get('/blocklist/servers',
55 authenticate,
56 ensureUserHasRight(UserRight.MANAGE_SERVERS_BLOCKLIST),
57 paginationValidator,
58 serversBlocklistSortValidator,
59 setDefaultSort,
60 setDefaultPagination,
61 asyncMiddleware(listBlockedServers)
62)
63
64serverBlocklistRouter.post('/blocklist/servers',
65 authenticate,
66 ensureUserHasRight(UserRight.MANAGE_SERVERS_BLOCKLIST),
67 asyncMiddleware(blockServerValidator),
68 asyncRetryTransactionMiddleware(blockServer)
69)
70
71serverBlocklistRouter.delete('/blocklist/servers/:host',
72 authenticate,
73 ensureUserHasRight(UserRight.MANAGE_SERVERS_BLOCKLIST),
74 asyncMiddleware(unblockServerByServerValidator),
75 asyncRetryTransactionMiddleware(unblockServer)
76)
77
78export {
79 serverBlocklistRouter
80}
81
82// ---------------------------------------------------------------------------
83
84async 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
92async 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
101async 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
109async 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
117async 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
126async 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'
13import { 13import {
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
38myBlocklistRouter.post('/me/blocklist/accounts', 38myBlocklistRouter.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
59myBlocklistRouter.post('/me/blocklist/servers', 59myBlocklistRouter.post('/me/blocklist/servers',
60 authenticate, 60 authenticate,
61 asyncMiddleware(blockServerByAccountValidator), 61 asyncMiddleware(blockServerValidator),
62 asyncRetryTransactionMiddleware(blockServer) 62 asyncRetryTransactionMiddleware(blockServer)
63) 63)
64 64