diff options
author | Chocobozzz <me@florianbigard.com> | 2018-10-12 15:26:04 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-10-16 16:41:36 +0200 |
commit | 7ad9b9846c44d198a736183fb186c2039f5236b5 (patch) | |
tree | 9c8456882a261c0522efb507f20e323c2741a0f8 /server/controllers | |
parent | dffd5d127f49eb63d2b2b3133aec75ec1d7e4dcb (diff) | |
download | PeerTube-7ad9b9846c44d198a736183fb186c2039f5236b5.tar.gz PeerTube-7ad9b9846c44d198a736183fb186c2039f5236b5.tar.zst PeerTube-7ad9b9846c44d198a736183fb186c2039f5236b5.zip |
Add ability for users to block an account/instance on server side
Diffstat (limited to 'server/controllers')
-rw-r--r-- | server/controllers/api/accounts.ts | 5 | ||||
-rw-r--r-- | server/controllers/api/search.ts | 2 | ||||
-rw-r--r-- | server/controllers/api/users/index.ts | 2 | ||||
-rw-r--r-- | server/controllers/api/users/me.ts | 3 | ||||
-rw-r--r-- | server/controllers/api/users/my-blocklist.ts | 125 | ||||
-rw-r--r-- | server/controllers/api/video-channel.ts | 2 | ||||
-rw-r--r-- | server/controllers/api/videos/comment.ts | 12 | ||||
-rw-r--r-- | server/controllers/api/videos/index.ts | 2 |
8 files changed, 144 insertions, 9 deletions
diff --git a/server/controllers/api/accounts.ts b/server/controllers/api/accounts.ts index 8e3f60010..86ef2aed1 100644 --- a/server/controllers/api/accounts.ts +++ b/server/controllers/api/accounts.ts | |||
@@ -1,7 +1,8 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { getFormattedObjects } from '../../helpers/utils' | 2 | import { getFormattedObjects } from '../../helpers/utils' |
3 | import { | 3 | import { |
4 | asyncMiddleware, commonVideosFiltersValidator, | 4 | asyncMiddleware, |
5 | commonVideosFiltersValidator, | ||
5 | listVideoAccountChannelsValidator, | 6 | listVideoAccountChannelsValidator, |
6 | optionalAuthenticate, | 7 | optionalAuthenticate, |
7 | paginationValidator, | 8 | paginationValidator, |
@@ -90,7 +91,7 @@ async function listAccountVideos (req: express.Request, res: express.Response, n | |||
90 | nsfw: buildNSFWFilter(res, req.query.nsfw), | 91 | nsfw: buildNSFWFilter(res, req.query.nsfw), |
91 | withFiles: false, | 92 | withFiles: false, |
92 | accountId: account.id, | 93 | accountId: account.id, |
93 | userId: res.locals.oauth ? res.locals.oauth.token.User.id : undefined | 94 | user: res.locals.oauth ? res.locals.oauth.token.User : undefined |
94 | }) | 95 | }) |
95 | 96 | ||
96 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | 97 | return res.json(getFormattedObjects(resultList.data, resultList.total)) |
diff --git a/server/controllers/api/search.ts b/server/controllers/api/search.ts index a8a6cfb08..534305ba6 100644 --- a/server/controllers/api/search.ts +++ b/server/controllers/api/search.ts | |||
@@ -119,7 +119,7 @@ async function searchVideosDB (query: VideosSearchQuery, res: express.Response) | |||
119 | includeLocalVideos: true, | 119 | includeLocalVideos: true, |
120 | nsfw: buildNSFWFilter(res, query.nsfw), | 120 | nsfw: buildNSFWFilter(res, query.nsfw), |
121 | filter: query.filter, | 121 | filter: query.filter, |
122 | userId: res.locals.oauth ? res.locals.oauth.token.User.id : undefined | 122 | user: res.locals.oauth ? res.locals.oauth.token.User : undefined |
123 | }) | 123 | }) |
124 | const resultList = await VideoModel.searchAndPopulateAccountAndServer(options) | 124 | const resultList = await VideoModel.searchAndPopulateAccountAndServer(options) |
125 | 125 | ||
diff --git a/server/controllers/api/users/index.ts b/server/controllers/api/users/index.ts index 4f8137c03..9fcb8077f 100644 --- a/server/controllers/api/users/index.ts +++ b/server/controllers/api/users/index.ts | |||
@@ -37,6 +37,7 @@ import { UserModel } from '../../../models/account/user' | |||
37 | import { auditLoggerFactory, getAuditIdFromRes, UserAuditView } from '../../../helpers/audit-logger' | 37 | import { auditLoggerFactory, getAuditIdFromRes, UserAuditView } from '../../../helpers/audit-logger' |
38 | import { meRouter } from './me' | 38 | import { meRouter } from './me' |
39 | import { deleteUserToken } from '../../../lib/oauth-model' | 39 | import { deleteUserToken } from '../../../lib/oauth-model' |
40 | import { myBlocklistRouter } from './my-blocklist' | ||
40 | 41 | ||
41 | const auditLogger = auditLoggerFactory('users') | 42 | const auditLogger = auditLoggerFactory('users') |
42 | 43 | ||
@@ -53,6 +54,7 @@ const askSendEmailLimiter = new RateLimit({ | |||
53 | }) | 54 | }) |
54 | 55 | ||
55 | const usersRouter = express.Router() | 56 | const usersRouter = express.Router() |
57 | usersRouter.use('/', myBlocklistRouter) | ||
56 | usersRouter.use('/', meRouter) | 58 | usersRouter.use('/', meRouter) |
57 | 59 | ||
58 | usersRouter.get('/autocomplete', | 60 | usersRouter.get('/autocomplete', |
diff --git a/server/controllers/api/users/me.ts b/server/controllers/api/users/me.ts index 591ec6b25..ebe668110 100644 --- a/server/controllers/api/users/me.ts +++ b/server/controllers/api/users/me.ts | |||
@@ -238,7 +238,8 @@ async function getUserSubscriptionVideos (req: express.Request, res: express.Res | |||
238 | nsfw: buildNSFWFilter(res, req.query.nsfw), | 238 | nsfw: buildNSFWFilter(res, req.query.nsfw), |
239 | filter: req.query.filter as VideoFilter, | 239 | filter: req.query.filter as VideoFilter, |
240 | withFiles: false, | 240 | withFiles: false, |
241 | actorId: user.Account.Actor.id | 241 | actorId: user.Account.Actor.id, |
242 | user | ||
242 | }) | 243 | }) |
243 | 244 | ||
244 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | 245 | return res.json(getFormattedObjects(resultList.data, resultList.total)) |
diff --git a/server/controllers/api/users/my-blocklist.ts b/server/controllers/api/users/my-blocklist.ts new file mode 100644 index 000000000..e955ffde9 --- /dev/null +++ b/server/controllers/api/users/my-blocklist.ts | |||
@@ -0,0 +1,125 @@ | |||
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, | ||
9 | serverGetValidator, | ||
10 | setDefaultPagination, | ||
11 | setDefaultSort, | ||
12 | unblockAccountByAccountValidator | ||
13 | } from '../../../middlewares' | ||
14 | import { | ||
15 | accountsBlocklistSortValidator, | ||
16 | blockAccountByAccountValidator, | ||
17 | serversBlocklistSortValidator, | ||
18 | unblockServerByAccountValidator | ||
19 | } from '../../../middlewares/validators' | ||
20 | import { UserModel } from '../../../models/account/user' | ||
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 | |||
27 | const myBlocklistRouter = express.Router() | ||
28 | |||
29 | myBlocklistRouter.get('/me/blocklist/accounts', | ||
30 | authenticate, | ||
31 | paginationValidator, | ||
32 | accountsBlocklistSortValidator, | ||
33 | setDefaultSort, | ||
34 | setDefaultPagination, | ||
35 | asyncMiddleware(listBlockedAccounts) | ||
36 | ) | ||
37 | |||
38 | myBlocklistRouter.post('/me/blocklist/accounts', | ||
39 | authenticate, | ||
40 | asyncMiddleware(blockAccountByAccountValidator), | ||
41 | asyncRetryTransactionMiddleware(blockAccount) | ||
42 | ) | ||
43 | |||
44 | myBlocklistRouter.delete('/me/blocklist/accounts/:accountName', | ||
45 | authenticate, | ||
46 | asyncMiddleware(unblockAccountByAccountValidator), | ||
47 | asyncRetryTransactionMiddleware(unblockAccount) | ||
48 | ) | ||
49 | |||
50 | myBlocklistRouter.get('/me/blocklist/servers', | ||
51 | authenticate, | ||
52 | paginationValidator, | ||
53 | serversBlocklistSortValidator, | ||
54 | setDefaultSort, | ||
55 | setDefaultPagination, | ||
56 | asyncMiddleware(listBlockedServers) | ||
57 | ) | ||
58 | |||
59 | myBlocklistRouter.post('/me/blocklist/servers', | ||
60 | authenticate, | ||
61 | asyncMiddleware(serverGetValidator), | ||
62 | asyncRetryTransactionMiddleware(blockServer) | ||
63 | ) | ||
64 | |||
65 | myBlocklistRouter.delete('/me/blocklist/servers/:host', | ||
66 | authenticate, | ||
67 | asyncMiddleware(unblockServerByAccountValidator), | ||
68 | asyncRetryTransactionMiddleware(unblockServer) | ||
69 | ) | ||
70 | |||
71 | export { | ||
72 | myBlocklistRouter | ||
73 | } | ||
74 | |||
75 | // --------------------------------------------------------------------------- | ||
76 | |||
77 | async function listBlockedAccounts (req: express.Request, res: express.Response) { | ||
78 | const user: UserModel = res.locals.oauth.token.User | ||
79 | |||
80 | const resultList = await AccountBlocklistModel.listForApi(user.Account.id, req.query.start, req.query.count, req.query.sort) | ||
81 | |||
82 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | ||
83 | } | ||
84 | |||
85 | async function blockAccount (req: express.Request, res: express.Response) { | ||
86 | const user: UserModel = res.locals.oauth.token.User | ||
87 | const accountToBlock: AccountModel = res.locals.account | ||
88 | |||
89 | await addAccountInBlocklist(user.Account.id, accountToBlock.id) | ||
90 | |||
91 | return res.status(204).end() | ||
92 | } | ||
93 | |||
94 | async function unblockAccount (req: express.Request, res: express.Response) { | ||
95 | const accountBlock: AccountBlocklistModel = res.locals.accountBlock | ||
96 | |||
97 | await removeAccountFromBlocklist(accountBlock) | ||
98 | |||
99 | return res.status(204).end() | ||
100 | } | ||
101 | |||
102 | async function listBlockedServers (req: express.Request, res: express.Response) { | ||
103 | const user: UserModel = res.locals.oauth.token.User | ||
104 | |||
105 | const resultList = await ServerBlocklistModel.listForApi(user.Account.id, req.query.start, req.query.count, req.query.sort) | ||
106 | |||
107 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | ||
108 | } | ||
109 | |||
110 | async function blockServer (req: express.Request, res: express.Response) { | ||
111 | const user: UserModel = res.locals.oauth.token.User | ||
112 | const serverToBlock: ServerModel = res.locals.server | ||
113 | |||
114 | await addServerInBlocklist(user.Account.id, serverToBlock.id) | ||
115 | |||
116 | return res.status(204).end() | ||
117 | } | ||
118 | |||
119 | async function unblockServer (req: express.Request, res: express.Response) { | ||
120 | const serverBlock: ServerBlocklistModel = res.locals.serverBlock | ||
121 | |||
122 | await removeServerFromBlocklist(serverBlock) | ||
123 | |||
124 | return res.status(204).end() | ||
125 | } | ||
diff --git a/server/controllers/api/video-channel.ts b/server/controllers/api/video-channel.ts index c84d1be58..9bf3c5fd8 100644 --- a/server/controllers/api/video-channel.ts +++ b/server/controllers/api/video-channel.ts | |||
@@ -219,7 +219,7 @@ async function listVideoChannelVideos (req: express.Request, res: express.Respon | |||
219 | nsfw: buildNSFWFilter(res, req.query.nsfw), | 219 | nsfw: buildNSFWFilter(res, req.query.nsfw), |
220 | withFiles: false, | 220 | withFiles: false, |
221 | videoChannelId: videoChannelInstance.id, | 221 | videoChannelId: videoChannelInstance.id, |
222 | userId: res.locals.oauth ? res.locals.oauth.token.User.id : undefined | 222 | user: res.locals.oauth ? res.locals.oauth.token.User : undefined |
223 | }) | 223 | }) |
224 | 224 | ||
225 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | 225 | return res.json(getFormattedObjects(resultList.data, resultList.total)) |
diff --git a/server/controllers/api/videos/comment.ts b/server/controllers/api/videos/comment.ts index 4f2b4faee..3875c8f79 100644 --- a/server/controllers/api/videos/comment.ts +++ b/server/controllers/api/videos/comment.ts | |||
@@ -8,7 +8,7 @@ import { buildFormattedCommentTree, createVideoComment } from '../../../lib/vide | |||
8 | import { | 8 | import { |
9 | asyncMiddleware, | 9 | asyncMiddleware, |
10 | asyncRetryTransactionMiddleware, | 10 | asyncRetryTransactionMiddleware, |
11 | authenticate, | 11 | authenticate, optionalAuthenticate, |
12 | paginationValidator, | 12 | paginationValidator, |
13 | setDefaultPagination, | 13 | setDefaultPagination, |
14 | setDefaultSort | 14 | setDefaultSort |
@@ -36,10 +36,12 @@ videoCommentRouter.get('/:videoId/comment-threads', | |||
36 | setDefaultSort, | 36 | setDefaultSort, |
37 | setDefaultPagination, | 37 | setDefaultPagination, |
38 | asyncMiddleware(listVideoCommentThreadsValidator), | 38 | asyncMiddleware(listVideoCommentThreadsValidator), |
39 | optionalAuthenticate, | ||
39 | asyncMiddleware(listVideoThreads) | 40 | asyncMiddleware(listVideoThreads) |
40 | ) | 41 | ) |
41 | videoCommentRouter.get('/:videoId/comment-threads/:threadId', | 42 | videoCommentRouter.get('/:videoId/comment-threads/:threadId', |
42 | asyncMiddleware(listVideoThreadCommentsValidator), | 43 | asyncMiddleware(listVideoThreadCommentsValidator), |
44 | optionalAuthenticate, | ||
43 | asyncMiddleware(listVideoThreadComments) | 45 | asyncMiddleware(listVideoThreadComments) |
44 | ) | 46 | ) |
45 | 47 | ||
@@ -69,10 +71,12 @@ export { | |||
69 | 71 | ||
70 | async function listVideoThreads (req: express.Request, res: express.Response, next: express.NextFunction) { | 72 | async function listVideoThreads (req: express.Request, res: express.Response, next: express.NextFunction) { |
71 | const video = res.locals.video as VideoModel | 73 | const video = res.locals.video as VideoModel |
74 | const user: UserModel = res.locals.oauth ? res.locals.oauth.token.User : undefined | ||
75 | |||
72 | let resultList: ResultList<VideoCommentModel> | 76 | let resultList: ResultList<VideoCommentModel> |
73 | 77 | ||
74 | if (video.commentsEnabled === true) { | 78 | if (video.commentsEnabled === true) { |
75 | resultList = await VideoCommentModel.listThreadsForApi(video.id, req.query.start, req.query.count, req.query.sort) | 79 | resultList = await VideoCommentModel.listThreadsForApi(video.id, req.query.start, req.query.count, req.query.sort, user) |
76 | } else { | 80 | } else { |
77 | resultList = { | 81 | resultList = { |
78 | total: 0, | 82 | total: 0, |
@@ -85,10 +89,12 @@ async function listVideoThreads (req: express.Request, res: express.Response, ne | |||
85 | 89 | ||
86 | async function listVideoThreadComments (req: express.Request, res: express.Response, next: express.NextFunction) { | 90 | async function listVideoThreadComments (req: express.Request, res: express.Response, next: express.NextFunction) { |
87 | const video = res.locals.video as VideoModel | 91 | const video = res.locals.video as VideoModel |
92 | const user: UserModel = res.locals.oauth ? res.locals.oauth.token.User : undefined | ||
93 | |||
88 | let resultList: ResultList<VideoCommentModel> | 94 | let resultList: ResultList<VideoCommentModel> |
89 | 95 | ||
90 | if (video.commentsEnabled === true) { | 96 | if (video.commentsEnabled === true) { |
91 | resultList = await VideoCommentModel.listThreadCommentsForApi(video.id, res.locals.videoCommentThread.id) | 97 | resultList = await VideoCommentModel.listThreadCommentsForApi(video.id, res.locals.videoCommentThread.id, user) |
92 | } else { | 98 | } else { |
93 | resultList = { | 99 | resultList = { |
94 | total: 0, | 100 | total: 0, |
diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts index 6a73e13d0..664154406 100644 --- a/server/controllers/api/videos/index.ts +++ b/server/controllers/api/videos/index.ts | |||
@@ -437,7 +437,7 @@ async function listVideos (req: express.Request, res: express.Response, next: ex | |||
437 | nsfw: buildNSFWFilter(res, req.query.nsfw), | 437 | nsfw: buildNSFWFilter(res, req.query.nsfw), |
438 | filter: req.query.filter as VideoFilter, | 438 | filter: req.query.filter as VideoFilter, |
439 | withFiles: false, | 439 | withFiles: false, |
440 | userId: res.locals.oauth ? res.locals.oauth.token.User.id : undefined | 440 | user: res.locals.oauth ? res.locals.oauth.token.User : undefined |
441 | }) | 441 | }) |
442 | 442 | ||
443 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | 443 | return res.json(getFormattedObjects(resultList.data, resultList.total)) |