diff options
author | Chocobozzz <me@florianbigard.com> | 2018-08-16 15:25:20 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-08-27 09:41:54 +0200 |
commit | 06a05d5f4784a7cbb27aa1188385b5679845dad8 (patch) | |
tree | ac197f3ed0768529456225ad76c912f22bc55e29 /server/helpers | |
parent | 4bda2e47bbc937c401ddcf14c1be53c70481a294 (diff) | |
download | PeerTube-06a05d5f4784a7cbb27aa1188385b5679845dad8.tar.gz PeerTube-06a05d5f4784a7cbb27aa1188385b5679845dad8.tar.zst PeerTube-06a05d5f4784a7cbb27aa1188385b5679845dad8.zip |
Add subscriptions endpoints to REST API
Diffstat (limited to 'server/helpers')
-rw-r--r-- | server/helpers/custom-validators/activitypub/actor.ts | 13 | ||||
-rw-r--r-- | server/helpers/custom-validators/video-channels.ts | 32 | ||||
-rw-r--r-- | server/helpers/custom-validators/webfinger.ts | 4 | ||||
-rw-r--r-- | server/helpers/webfinger.ts | 10 |
4 files changed, 42 insertions, 17 deletions
diff --git a/server/helpers/custom-validators/activitypub/actor.ts b/server/helpers/custom-validators/activitypub/actor.ts index c7a64e24d..ae5014f8f 100644 --- a/server/helpers/custom-validators/activitypub/actor.ts +++ b/server/helpers/custom-validators/activitypub/actor.ts | |||
@@ -3,6 +3,7 @@ import { CONSTRAINTS_FIELDS } from '../../../initializers' | |||
3 | import { exists } from '../misc' | 3 | import { exists } from '../misc' |
4 | import { truncate } from 'lodash' | 4 | import { truncate } from 'lodash' |
5 | import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc' | 5 | import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc' |
6 | import { isHostValid } from '../servers' | ||
6 | 7 | ||
7 | function isActorEndpointsObjectValid (endpointObject: any) { | 8 | function isActorEndpointsObjectValid (endpointObject: any) { |
8 | return isActivityPubUrlValid(endpointObject.sharedInbox) | 9 | return isActivityPubUrlValid(endpointObject.sharedInbox) |
@@ -109,6 +110,15 @@ function normalizeActor (actor: any) { | |||
109 | return | 110 | return |
110 | } | 111 | } |
111 | 112 | ||
113 | function isValidActorHandle (handle: string) { | ||
114 | if (!exists(handle)) return false | ||
115 | |||
116 | const parts = handle.split('@') | ||
117 | if (parts.length !== 2) return false | ||
118 | |||
119 | return isHostValid(parts[1]) | ||
120 | } | ||
121 | |||
112 | // --------------------------------------------------------------------------- | 122 | // --------------------------------------------------------------------------- |
113 | 123 | ||
114 | export { | 124 | export { |
@@ -126,5 +136,6 @@ export { | |||
126 | isActorAcceptActivityValid, | 136 | isActorAcceptActivityValid, |
127 | isActorRejectActivityValid, | 137 | isActorRejectActivityValid, |
128 | isActorDeleteActivityValid, | 138 | isActorDeleteActivityValid, |
129 | isActorUpdateActivityValid | 139 | isActorUpdateActivityValid, |
140 | isValidActorHandle | ||
130 | } | 141 | } |
diff --git a/server/helpers/custom-validators/video-channels.ts b/server/helpers/custom-validators/video-channels.ts index 2a6f56840..32faf36f7 100644 --- a/server/helpers/custom-validators/video-channels.ts +++ b/server/helpers/custom-validators/video-channels.ts | |||
@@ -5,6 +5,7 @@ import * as validator from 'validator' | |||
5 | import { CONSTRAINTS_FIELDS } from '../../initializers' | 5 | import { CONSTRAINTS_FIELDS } from '../../initializers' |
6 | import { VideoChannelModel } from '../../models/video/video-channel' | 6 | import { VideoChannelModel } from '../../models/video/video-channel' |
7 | import { exists } from './misc' | 7 | import { exists } from './misc' |
8 | import { Response } from 'express' | ||
8 | 9 | ||
9 | const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS | 10 | const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS |
10 | 11 | ||
@@ -20,6 +21,12 @@ function isVideoChannelSupportValid (value: string) { | |||
20 | return value === null || (exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.SUPPORT)) | 21 | return value === null || (exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.SUPPORT)) |
21 | } | 22 | } |
22 | 23 | ||
24 | async function isLocalVideoChannelNameExist (name: string, res: Response) { | ||
25 | const videoChannel = await VideoChannelModel.loadLocalByName(name) | ||
26 | |||
27 | return processVideoChannelExist(videoChannel, res) | ||
28 | } | ||
29 | |||
23 | async function isVideoChannelExist (id: string, res: express.Response) { | 30 | async function isVideoChannelExist (id: string, res: express.Response) { |
24 | let videoChannel: VideoChannelModel | 31 | let videoChannel: VideoChannelModel |
25 | if (validator.isInt(id)) { | 32 | if (validator.isInt(id)) { |
@@ -28,23 +35,28 @@ async function isVideoChannelExist (id: string, res: express.Response) { | |||
28 | videoChannel = await VideoChannelModel.loadByUUIDAndPopulateAccount(id) | 35 | videoChannel = await VideoChannelModel.loadByUUIDAndPopulateAccount(id) |
29 | } | 36 | } |
30 | 37 | ||
31 | if (!videoChannel) { | 38 | return processVideoChannelExist(videoChannel, res) |
32 | res.status(404) | ||
33 | .json({ error: 'Video channel not found' }) | ||
34 | .end() | ||
35 | |||
36 | return false | ||
37 | } | ||
38 | |||
39 | res.locals.videoChannel = videoChannel | ||
40 | return true | ||
41 | } | 39 | } |
42 | 40 | ||
43 | // --------------------------------------------------------------------------- | 41 | // --------------------------------------------------------------------------- |
44 | 42 | ||
45 | export { | 43 | export { |
44 | isLocalVideoChannelNameExist, | ||
46 | isVideoChannelDescriptionValid, | 45 | isVideoChannelDescriptionValid, |
47 | isVideoChannelNameValid, | 46 | isVideoChannelNameValid, |
48 | isVideoChannelSupportValid, | 47 | isVideoChannelSupportValid, |
49 | isVideoChannelExist | 48 | isVideoChannelExist |
50 | } | 49 | } |
50 | |||
51 | function processVideoChannelExist (videoChannel: VideoChannelModel, res: express.Response) { | ||
52 | if (!videoChannel) { | ||
53 | res.status(404) | ||
54 | .json({ error: 'Video channel not found' }) | ||
55 | .end() | ||
56 | |||
57 | return false | ||
58 | } | ||
59 | |||
60 | res.locals.videoChannel = videoChannel | ||
61 | return true | ||
62 | } | ||
diff --git a/server/helpers/custom-validators/webfinger.ts b/server/helpers/custom-validators/webfinger.ts index d8c1232ce..80a7e4a9d 100644 --- a/server/helpers/custom-validators/webfinger.ts +++ b/server/helpers/custom-validators/webfinger.ts | |||
@@ -2,7 +2,7 @@ import { CONFIG, REMOTE_SCHEME } from '../../initializers' | |||
2 | import { sanitizeHost } from '../core-utils' | 2 | import { sanitizeHost } from '../core-utils' |
3 | import { exists } from './misc' | 3 | import { exists } from './misc' |
4 | 4 | ||
5 | function isWebfingerResourceValid (value: string) { | 5 | function isWebfingerLocalResourceValid (value: string) { |
6 | if (!exists(value)) return false | 6 | if (!exists(value)) return false |
7 | if (value.startsWith('acct:') === false) return false | 7 | if (value.startsWith('acct:') === false) return false |
8 | 8 | ||
@@ -17,5 +17,5 @@ function isWebfingerResourceValid (value: string) { | |||
17 | // --------------------------------------------------------------------------- | 17 | // --------------------------------------------------------------------------- |
18 | 18 | ||
19 | export { | 19 | export { |
20 | isWebfingerResourceValid | 20 | isWebfingerLocalResourceValid |
21 | } | 21 | } |
diff --git a/server/helpers/webfinger.ts b/server/helpers/webfinger.ts index 688bf2bab..5c60de10c 100644 --- a/server/helpers/webfinger.ts +++ b/server/helpers/webfinger.ts | |||
@@ -11,15 +11,17 @@ const webfinger = new WebFinger({ | |||
11 | request_timeout: 3000 | 11 | request_timeout: 3000 |
12 | }) | 12 | }) |
13 | 13 | ||
14 | async function loadActorUrlOrGetFromWebfinger (name: string, host: string) { | 14 | async function loadActorUrlOrGetFromWebfinger (uri: string) { |
15 | const [ name, host ] = uri.split('@') | ||
16 | |||
15 | const actor = await ActorModel.loadByNameAndHost(name, host) | 17 | const actor = await ActorModel.loadByNameAndHost(name, host) |
16 | if (actor) return actor.url | 18 | if (actor) return actor.url |
17 | 19 | ||
18 | return getUrlFromWebfinger(name, host) | 20 | return getUrlFromWebfinger(uri) |
19 | } | 21 | } |
20 | 22 | ||
21 | async function getUrlFromWebfinger (name: string, host: string) { | 23 | async function getUrlFromWebfinger (uri: string) { |
22 | const webfingerData: WebFingerData = await webfingerLookup(name + '@' + host) | 24 | const webfingerData: WebFingerData = await webfingerLookup(uri) |
23 | return getLinkOrThrow(webfingerData) | 25 | return getLinkOrThrow(webfingerData) |
24 | } | 26 | } |
25 | 27 | ||