diff options
author | Chocobozzz <me@florianbigard.com> | 2019-02-26 10:55:40 +0100 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2019-03-18 11:17:59 +0100 |
commit | 418d092afa81e2c8fe8ac6838fc4b5eb0af6a782 (patch) | |
tree | 5e9bc5604fd5d66a006cfebb7acdbdd5486e5d1e /server/helpers/custom-validators | |
parent | b427febb4d5cebf03b815bca2c59af6e82491569 (diff) | |
download | PeerTube-418d092afa81e2c8fe8ac6838fc4b5eb0af6a782.tar.gz PeerTube-418d092afa81e2c8fe8ac6838fc4b5eb0af6a782.tar.zst PeerTube-418d092afa81e2c8fe8ac6838fc4b5eb0af6a782.zip |
Playlist server API
Diffstat (limited to 'server/helpers/custom-validators')
4 files changed, 73 insertions, 1 deletions
diff --git a/server/helpers/custom-validators/activitypub/activity.ts b/server/helpers/custom-validators/activitypub/activity.ts index b24590d9d..e0d170d9d 100644 --- a/server/helpers/custom-validators/activitypub/activity.ts +++ b/server/helpers/custom-validators/activitypub/activity.ts | |||
@@ -9,6 +9,7 @@ import { isViewActivityValid } from './view' | |||
9 | import { exists } from '../misc' | 9 | import { exists } from '../misc' |
10 | import { isCacheFileObjectValid } from './cache-file' | 10 | import { isCacheFileObjectValid } from './cache-file' |
11 | import { isFlagActivityValid } from './flag' | 11 | import { isFlagActivityValid } from './flag' |
12 | import { isPlaylistObjectValid } from './playlist' | ||
12 | 13 | ||
13 | function isRootActivityValid (activity: any) { | 14 | function isRootActivityValid (activity: any) { |
14 | return Array.isArray(activity['@context']) && ( | 15 | return Array.isArray(activity['@context']) && ( |
@@ -78,6 +79,7 @@ function checkCreateActivity (activity: any) { | |||
78 | isViewActivityValid(activity.object) || | 79 | isViewActivityValid(activity.object) || |
79 | isDislikeActivityValid(activity.object) || | 80 | isDislikeActivityValid(activity.object) || |
80 | isFlagActivityValid(activity.object) || | 81 | isFlagActivityValid(activity.object) || |
82 | isPlaylistObjectValid(activity.object) || | ||
81 | 83 | ||
82 | isCacheFileObjectValid(activity.object) || | 84 | isCacheFileObjectValid(activity.object) || |
83 | sanitizeAndCheckVideoCommentObject(activity.object) || | 85 | sanitizeAndCheckVideoCommentObject(activity.object) || |
@@ -89,6 +91,7 @@ function checkUpdateActivity (activity: any) { | |||
89 | return isBaseActivityValid(activity, 'Update') && | 91 | return isBaseActivityValid(activity, 'Update') && |
90 | ( | 92 | ( |
91 | isCacheFileObjectValid(activity.object) || | 93 | isCacheFileObjectValid(activity.object) || |
94 | isPlaylistObjectValid(activity.object) || | ||
92 | sanitizeAndCheckVideoTorrentObject(activity.object) || | 95 | sanitizeAndCheckVideoTorrentObject(activity.object) || |
93 | sanitizeAndCheckActorObject(activity.object) | 96 | sanitizeAndCheckActorObject(activity.object) |
94 | ) | 97 | ) |
diff --git a/server/helpers/custom-validators/activitypub/playlist.ts b/server/helpers/custom-validators/activitypub/playlist.ts new file mode 100644 index 000000000..ecdc7975e --- /dev/null +++ b/server/helpers/custom-validators/activitypub/playlist.ts | |||
@@ -0,0 +1,25 @@ | |||
1 | import { exists } from '../misc' | ||
2 | import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object' | ||
3 | import * as validator from 'validator' | ||
4 | import { PlaylistElementObject } from '../../../../shared/models/activitypub/objects/playlist-element-object' | ||
5 | import { isActivityPubUrlValid } from './misc' | ||
6 | |||
7 | function isPlaylistObjectValid (object: PlaylistObject) { | ||
8 | return exists(object) && | ||
9 | object.type === 'Playlist' && | ||
10 | validator.isInt(object.totalItems + '') | ||
11 | } | ||
12 | |||
13 | function isPlaylistElementObjectValid (object: PlaylistElementObject) { | ||
14 | return exists(object) && | ||
15 | object.type === 'PlaylistElement' && | ||
16 | validator.isInt(object.position + '') && | ||
17 | isActivityPubUrlValid(object.url) | ||
18 | } | ||
19 | |||
20 | // --------------------------------------------------------------------------- | ||
21 | |||
22 | export { | ||
23 | isPlaylistObjectValid, | ||
24 | isPlaylistElementObjectValid | ||
25 | } | ||
diff --git a/server/helpers/custom-validators/video-playlists.ts b/server/helpers/custom-validators/video-playlists.ts new file mode 100644 index 000000000..0f5af4ec0 --- /dev/null +++ b/server/helpers/custom-validators/video-playlists.ts | |||
@@ -0,0 +1,44 @@ | |||
1 | import { exists } from './misc' | ||
2 | import * as validator from 'validator' | ||
3 | import { CONSTRAINTS_FIELDS, VIDEO_PLAYLIST_PRIVACIES } from '../../initializers' | ||
4 | import * as express from 'express' | ||
5 | import { VideoPlaylistModel } from '../../models/video/video-playlist' | ||
6 | import { VideoPlaylistElementModel } from '../../models/video/video-playlist-element' | ||
7 | |||
8 | const PLAYLISTS_CONSTRAINT_FIELDS = CONSTRAINTS_FIELDS.VIDEO_PLAYLISTS | ||
9 | |||
10 | function isVideoPlaylistNameValid (value: any) { | ||
11 | return exists(value) && validator.isLength(value, PLAYLISTS_CONSTRAINT_FIELDS.NAME) | ||
12 | } | ||
13 | |||
14 | function isVideoPlaylistDescriptionValid (value: any) { | ||
15 | return value === null || (exists(value) && validator.isLength(value, PLAYLISTS_CONSTRAINT_FIELDS.DESCRIPTION)) | ||
16 | } | ||
17 | |||
18 | function isVideoPlaylistPrivacyValid (value: number) { | ||
19 | return validator.isInt(value + '') && VIDEO_PLAYLIST_PRIVACIES[ value ] !== undefined | ||
20 | } | ||
21 | |||
22 | async function isVideoPlaylistExist (id: number | string, res: express.Response) { | ||
23 | const videoPlaylist = await VideoPlaylistModel.load(id, undefined) | ||
24 | |||
25 | if (!videoPlaylist) { | ||
26 | res.status(404) | ||
27 | .json({ error: 'Video playlist not found' }) | ||
28 | .end() | ||
29 | |||
30 | return false | ||
31 | } | ||
32 | |||
33 | res.locals.videoPlaylist = videoPlaylist | ||
34 | return true | ||
35 | } | ||
36 | |||
37 | // --------------------------------------------------------------------------- | ||
38 | |||
39 | export { | ||
40 | isVideoPlaylistExist, | ||
41 | isVideoPlaylistNameValid, | ||
42 | isVideoPlaylistDescriptionValid, | ||
43 | isVideoPlaylistPrivacyValid | ||
44 | } | ||
diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts index dd04aa5f6..d00d24c4c 100644 --- a/server/helpers/custom-validators/videos.ts +++ b/server/helpers/custom-validators/videos.ts | |||
@@ -165,7 +165,7 @@ function checkUserCanManageVideo (user: UserModel, video: VideoModel, right: Use | |||
165 | return true | 165 | return true |
166 | } | 166 | } |
167 | 167 | ||
168 | async function isVideoExist (id: string, res: Response, fetchType: VideoFetchType = 'all') { | 168 | async function isVideoExist (id: number | string, res: Response, fetchType: VideoFetchType = 'all') { |
169 | const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined | 169 | const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined |
170 | 170 | ||
171 | const video = await fetchVideo(id, fetchType, userId) | 171 | const video = await fetchVideo(id, fetchType, userId) |