aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-08-16 15:25:20 +0200
committerChocobozzz <me@florianbigard.com>2018-08-27 09:41:54 +0200
commit06a05d5f4784a7cbb27aa1188385b5679845dad8 (patch)
treeac197f3ed0768529456225ad76c912f22bc55e29 /server/middlewares
parent4bda2e47bbc937c401ddcf14c1be53c70481a294 (diff)
downloadPeerTube-06a05d5f4784a7cbb27aa1188385b5679845dad8.tar.gz
PeerTube-06a05d5f4784a7cbb27aa1188385b5679845dad8.tar.zst
PeerTube-06a05d5f4784a7cbb27aa1188385b5679845dad8.zip
Add subscriptions endpoints to REST API
Diffstat (limited to 'server/middlewares')
-rw-r--r--server/middlewares/validators/follows.ts4
-rw-r--r--server/middlewares/validators/index.ts1
-rw-r--r--server/middlewares/validators/sort.ts5
-rw-r--r--server/middlewares/validators/user-subscriptions.ts58
-rw-r--r--server/middlewares/validators/video-channels.ts17
-rw-r--r--server/middlewares/validators/webfinger.ts4
6 files changed, 83 insertions, 6 deletions
diff --git a/server/middlewares/validators/follows.ts b/server/middlewares/validators/follows.ts
index 040ee1f21..faefc1179 100644
--- a/server/middlewares/validators/follows.ts
+++ b/server/middlewares/validators/follows.ts
@@ -4,7 +4,7 @@ import { isTestInstance } from '../../helpers/core-utils'
4import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers' 4import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers'
5import { logger } from '../../helpers/logger' 5import { logger } from '../../helpers/logger'
6import { getServerActor } from '../../helpers/utils' 6import { getServerActor } from '../../helpers/utils'
7import { CONFIG } from '../../initializers' 7import { CONFIG, SERVER_ACTOR_NAME } from '../../initializers'
8import { ActorFollowModel } from '../../models/activitypub/actor-follow' 8import { ActorFollowModel } from '../../models/activitypub/actor-follow'
9import { areValidationErrors } from './utils' 9import { areValidationErrors } from './utils'
10 10
@@ -38,7 +38,7 @@ const removeFollowingValidator = [
38 if (areValidationErrors(req, res)) return 38 if (areValidationErrors(req, res)) return
39 39
40 const serverActor = await getServerActor() 40 const serverActor = await getServerActor()
41 const follow = await ActorFollowModel.loadByActorAndTargetHost(serverActor.id, req.params.host) 41 const follow = await ActorFollowModel.loadByActorAndTargetNameAndHost(serverActor.id, SERVER_ACTOR_NAME, req.params.host)
42 42
43 if (!follow) { 43 if (!follow) {
44 return res 44 return res
diff --git a/server/middlewares/validators/index.ts b/server/middlewares/validators/index.ts
index ccbedd57d..940547a3e 100644
--- a/server/middlewares/validators/index.ts
+++ b/server/middlewares/validators/index.ts
@@ -6,6 +6,7 @@ export * from './follows'
6export * from './feeds' 6export * from './feeds'
7export * from './sort' 7export * from './sort'
8export * from './users' 8export * from './users'
9export * from './user-subscriptions'
9export * from './videos' 10export * from './videos'
10export * from './video-abuses' 11export * from './video-abuses'
11export * from './video-blacklist' 12export * from './video-blacklist'
diff --git a/server/middlewares/validators/sort.ts b/server/middlewares/validators/sort.ts
index d85611773..b30e97e61 100644
--- a/server/middlewares/validators/sort.ts
+++ b/server/middlewares/validators/sort.ts
@@ -14,6 +14,7 @@ const SORTABLE_BLACKLISTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.BLACK
14const SORTABLE_VIDEO_CHANNELS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_CHANNELS) 14const SORTABLE_VIDEO_CHANNELS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_CHANNELS)
15const SORTABLE_FOLLOWERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.FOLLOWERS) 15const SORTABLE_FOLLOWERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.FOLLOWERS)
16const SORTABLE_FOLLOWING_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.FOLLOWING) 16const SORTABLE_FOLLOWING_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.FOLLOWING)
17const SORTABLE_USER_SUBSCRIPTIONS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USER_SUBSCRIPTIONS)
17 18
18const usersSortValidator = checkSort(SORTABLE_USERS_COLUMNS) 19const usersSortValidator = checkSort(SORTABLE_USERS_COLUMNS)
19const accountsSortValidator = checkSort(SORTABLE_ACCOUNTS_COLUMNS) 20const accountsSortValidator = checkSort(SORTABLE_ACCOUNTS_COLUMNS)
@@ -27,6 +28,7 @@ const blacklistSortValidator = checkSort(SORTABLE_BLACKLISTS_COLUMNS)
27const videoChannelsSortValidator = checkSort(SORTABLE_VIDEO_CHANNELS_COLUMNS) 28const videoChannelsSortValidator = checkSort(SORTABLE_VIDEO_CHANNELS_COLUMNS)
28const followersSortValidator = checkSort(SORTABLE_FOLLOWERS_COLUMNS) 29const followersSortValidator = checkSort(SORTABLE_FOLLOWERS_COLUMNS)
29const followingSortValidator = checkSort(SORTABLE_FOLLOWING_COLUMNS) 30const followingSortValidator = checkSort(SORTABLE_FOLLOWING_COLUMNS)
31const userSubscriptionsSortValidator = checkSort(SORTABLE_USER_SUBSCRIPTIONS_COLUMNS)
30 32
31// --------------------------------------------------------------------------- 33// ---------------------------------------------------------------------------
32 34
@@ -42,5 +44,6 @@ export {
42 followersSortValidator, 44 followersSortValidator,
43 followingSortValidator, 45 followingSortValidator,
44 jobsSortValidator, 46 jobsSortValidator,
45 videoCommentThreadsSortValidator 47 videoCommentThreadsSortValidator,
48 userSubscriptionsSortValidator
46} 49}
diff --git a/server/middlewares/validators/user-subscriptions.ts b/server/middlewares/validators/user-subscriptions.ts
new file mode 100644
index 000000000..f331b6c34
--- /dev/null
+++ b/server/middlewares/validators/user-subscriptions.ts
@@ -0,0 +1,58 @@
1import * as express from 'express'
2import 'express-validator'
3import { body, param } from 'express-validator/check'
4import { logger } from '../../helpers/logger'
5import { areValidationErrors } from './utils'
6import { ActorFollowModel } from '../../models/activitypub/actor-follow'
7import { isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor'
8import { UserModel } from '../../models/account/user'
9import { CONFIG } from '../../initializers'
10
11const userSubscriptionAddValidator = [
12 body('uri').custom(isValidActorHandle).withMessage('Should have a valid URI to follow (username@domain)'),
13
14 (req: express.Request, res: express.Response, next: express.NextFunction) => {
15 logger.debug('Checking userSubscriptionAddValidator parameters', { parameters: req.body })
16
17 if (areValidationErrors(req, res)) return
18
19 return next()
20 }
21]
22
23const userSubscriptionRemoveValidator = [
24 param('uri').custom(isValidActorHandle).withMessage('Should have a valid URI to unfollow'),
25
26 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
27 logger.debug('Checking unfollow parameters', { parameters: req.params })
28
29 if (areValidationErrors(req, res)) return
30
31 let [ name, host ] = req.params.uri.split('@')
32 if (host === CONFIG.WEBSERVER.HOST) host = null
33
34 const user: UserModel = res.locals.oauth.token.User
35 const subscription = await ActorFollowModel.loadByActorAndTargetNameAndHost(user.Account.Actor.id, name, host)
36
37 if (!subscription) {
38 return res
39 .status(404)
40 .json({
41 error: `Subscription ${req.params.uri} not found.`
42 })
43 .end()
44 }
45
46 res.locals.subscription = subscription
47 return next()
48 }
49]
50
51// ---------------------------------------------------------------------------
52
53export {
54 userSubscriptionAddValidator,
55 userSubscriptionRemoveValidator
56}
57
58// ---------------------------------------------------------------------------
diff --git a/server/middlewares/validators/video-channels.ts b/server/middlewares/validators/video-channels.ts
index 143ce9582..d354c7e05 100644
--- a/server/middlewares/validators/video-channels.ts
+++ b/server/middlewares/validators/video-channels.ts
@@ -4,6 +4,7 @@ import { UserRight } from '../../../shared'
4import { isAccountNameWithHostExist } from '../../helpers/custom-validators/accounts' 4import { isAccountNameWithHostExist } from '../../helpers/custom-validators/accounts'
5import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' 5import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
6import { 6import {
7 isLocalVideoChannelNameExist,
7 isVideoChannelDescriptionValid, 8 isVideoChannelDescriptionValid,
8 isVideoChannelExist, 9 isVideoChannelExist,
9 isVideoChannelNameValid, 10 isVideoChannelNameValid,
@@ -100,6 +101,19 @@ const videoChannelsGetValidator = [
100 } 101 }
101] 102]
102 103
104const localVideoChannelValidator = [
105 param('name').custom(isVideoChannelNameValid).withMessage('Should have a valid video channel name'),
106
107 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
108 logger.debug('Checking localVideoChannelValidator parameters', { parameters: req.params })
109
110 if (areValidationErrors(req, res)) return
111 if (!await isLocalVideoChannelNameExist(req.params.name, res)) return
112
113 return next()
114 }
115]
116
103// --------------------------------------------------------------------------- 117// ---------------------------------------------------------------------------
104 118
105export { 119export {
@@ -107,7 +121,8 @@ export {
107 videoChannelsAddValidator, 121 videoChannelsAddValidator,
108 videoChannelsUpdateValidator, 122 videoChannelsUpdateValidator,
109 videoChannelsRemoveValidator, 123 videoChannelsRemoveValidator,
110 videoChannelsGetValidator 124 videoChannelsGetValidator,
125 localVideoChannelValidator
111} 126}
112 127
113// --------------------------------------------------------------------------- 128// ---------------------------------------------------------------------------
diff --git a/server/middlewares/validators/webfinger.ts b/server/middlewares/validators/webfinger.ts
index 3b9645048..63a1678ec 100644
--- a/server/middlewares/validators/webfinger.ts
+++ b/server/middlewares/validators/webfinger.ts
@@ -1,13 +1,13 @@
1import * as express from 'express' 1import * as express from 'express'
2import { query } from 'express-validator/check' 2import { query } from 'express-validator/check'
3import { isWebfingerResourceValid } from '../../helpers/custom-validators/webfinger' 3import { isWebfingerLocalResourceValid } from '../../helpers/custom-validators/webfinger'
4import { logger } from '../../helpers/logger' 4import { logger } from '../../helpers/logger'
5import { ActorModel } from '../../models/activitypub/actor' 5import { ActorModel } from '../../models/activitypub/actor'
6import { areValidationErrors } from './utils' 6import { areValidationErrors } from './utils'
7import { getHostWithPort } from '../../helpers/express-utils' 7import { getHostWithPort } from '../../helpers/express-utils'
8 8
9const webfingerValidator = [ 9const webfingerValidator = [
10 query('resource').custom(isWebfingerResourceValid).withMessage('Should have a valid webfinger resource'), 10 query('resource').custom(isWebfingerLocalResourceValid).withMessage('Should have a valid webfinger resource'),
11 11
12 async (req: express.Request, res: express.Response, next: express.NextFunction) => { 12 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
13 logger.debug('Checking webfinger parameters', { parameters: req.query }) 13 logger.debug('Checking webfinger parameters', { parameters: req.query })