aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators/remote
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-05-15 22:22:03 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-05-20 09:57:40 +0200
commit65fcc3119c334b75dd13bcfdebf186afdc580a8f (patch)
tree4f2158c61a9b7c3f47cfa233d01413b946ee53c0 /server/helpers/custom-validators/remote
parentd5f345ed4cfac4e1fa84dcb4fce1cda4d32f9c73 (diff)
downloadPeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.gz
PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.zst
PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.zip
First typescript iteration
Diffstat (limited to 'server/helpers/custom-validators/remote')
-rw-r--r--server/helpers/custom-validators/remote/index.js11
-rw-r--r--server/helpers/custom-validators/remote/index.ts1
-rw-r--r--server/helpers/custom-validators/remote/videos.js118
-rw-r--r--server/helpers/custom-validators/remote/videos.ts138
4 files changed, 139 insertions, 129 deletions
diff --git a/server/helpers/custom-validators/remote/index.js b/server/helpers/custom-validators/remote/index.js
deleted file mode 100644
index 1939a95f4..000000000
--- a/server/helpers/custom-validators/remote/index.js
+++ /dev/null
@@ -1,11 +0,0 @@
1'use strict'
2
3const remoteVideosValidators = require('./videos')
4
5const validators = {
6 videos: remoteVideosValidators
7}
8
9// ---------------------------------------------------------------------------
10
11module.exports = validators
diff --git a/server/helpers/custom-validators/remote/index.ts b/server/helpers/custom-validators/remote/index.ts
new file mode 100644
index 000000000..d6f9a7e77
--- /dev/null
+++ b/server/helpers/custom-validators/remote/index.ts
@@ -0,0 +1 @@
export * from './videos';
diff --git a/server/helpers/custom-validators/remote/videos.js b/server/helpers/custom-validators/remote/videos.js
deleted file mode 100644
index 24715b4b3..000000000
--- a/server/helpers/custom-validators/remote/videos.js
+++ /dev/null
@@ -1,118 +0,0 @@
1'use strict'
2
3const has = require('lodash/has')
4const values = require('lodash/values')
5
6const constants = require('../../../initializers/constants')
7const videosValidators = require('../videos')
8const miscValidators = require('../misc')
9
10const ENDPOINT_ACTIONS = constants.REQUEST_ENDPOINT_ACTIONS[constants.REQUEST_ENDPOINTS.VIDEOS]
11
12const remoteVideosValidators = {
13 isEachRemoteRequestVideosValid,
14 isEachRemoteRequestVideosQaduValid,
15 isEachRemoteRequestVideosEventsValid
16}
17
18function isEachRemoteRequestVideosValid (requests) {
19 return miscValidators.isArray(requests) &&
20 requests.every(function (request) {
21 const video = request.data
22
23 if (!video) return false
24
25 return (
26 isRequestTypeAddValid(request.type) &&
27 isCommonVideoAttributesValid(video) &&
28 videosValidators.isVideoAuthorValid(video.author) &&
29 videosValidators.isVideoThumbnailDataValid(video.thumbnailData)
30 ) ||
31 (
32 isRequestTypeUpdateValid(request.type) &&
33 isCommonVideoAttributesValid(video)
34 ) ||
35 (
36 isRequestTypeRemoveValid(request.type) &&
37 videosValidators.isVideoRemoteIdValid(video.remoteId)
38 ) ||
39 (
40 isRequestTypeReportAbuseValid(request.type) &&
41 videosValidators.isVideoRemoteIdValid(request.data.videoRemoteId) &&
42 videosValidators.isVideoAbuseReasonValid(request.data.reportReason) &&
43 videosValidators.isVideoAbuseReporterUsernameValid(request.data.reporterUsername)
44 )
45 })
46}
47
48function isEachRemoteRequestVideosQaduValid (requests) {
49 return miscValidators.isArray(requests) &&
50 requests.every(function (request) {
51 const video = request.data
52
53 if (!video) return false
54
55 return (
56 videosValidators.isVideoRemoteIdValid(video.remoteId) &&
57 (has(video, 'views') === false || videosValidators.isVideoViewsValid) &&
58 (has(video, 'likes') === false || videosValidators.isVideoLikesValid) &&
59 (has(video, 'dislikes') === false || videosValidators.isVideoDislikesValid)
60 )
61 })
62}
63
64function isEachRemoteRequestVideosEventsValid (requests) {
65 return miscValidators.isArray(requests) &&
66 requests.every(function (request) {
67 const eventData = request.data
68
69 if (!eventData) return false
70
71 return (
72 videosValidators.isVideoRemoteIdValid(eventData.remoteId) &&
73 values(constants.REQUEST_VIDEO_EVENT_TYPES).indexOf(eventData.eventType) !== -1 &&
74 videosValidators.isVideoEventCountValid(eventData.count)
75 )
76 })
77}
78
79// ---------------------------------------------------------------------------
80
81module.exports = remoteVideosValidators
82
83// ---------------------------------------------------------------------------
84
85function isCommonVideoAttributesValid (video) {
86 return videosValidators.isVideoDateValid(video.createdAt) &&
87 videosValidators.isVideoDateValid(video.updatedAt) &&
88 videosValidators.isVideoCategoryValid(video.category) &&
89 videosValidators.isVideoLicenceValid(video.licence) &&
90 videosValidators.isVideoLanguageValid(video.language) &&
91 videosValidators.isVideoNSFWValid(video.nsfw) &&
92 videosValidators.isVideoDescriptionValid(video.description) &&
93 videosValidators.isVideoDurationValid(video.duration) &&
94 videosValidators.isVideoInfoHashValid(video.infoHash) &&
95 videosValidators.isVideoNameValid(video.name) &&
96 videosValidators.isVideoTagsValid(video.tags) &&
97 videosValidators.isVideoRemoteIdValid(video.remoteId) &&
98 videosValidators.isVideoExtnameValid(video.extname) &&
99 videosValidators.isVideoViewsValid(video.views) &&
100 videosValidators.isVideoLikesValid(video.likes) &&
101 videosValidators.isVideoDislikesValid(video.dislikes)
102}
103
104function isRequestTypeAddValid (value) {
105 return value === ENDPOINT_ACTIONS.ADD
106}
107
108function isRequestTypeUpdateValid (value) {
109 return value === ENDPOINT_ACTIONS.UPDATE
110}
111
112function isRequestTypeRemoveValid (value) {
113 return value === ENDPOINT_ACTIONS.REMOVE
114}
115
116function isRequestTypeReportAbuseValid (value) {
117 return value === ENDPOINT_ACTIONS.REPORT_ABUSE
118}
diff --git a/server/helpers/custom-validators/remote/videos.ts b/server/helpers/custom-validators/remote/videos.ts
new file mode 100644
index 000000000..4b904d011
--- /dev/null
+++ b/server/helpers/custom-validators/remote/videos.ts
@@ -0,0 +1,138 @@
1import { has, values } from 'lodash'
2
3import {
4 REQUEST_ENDPOINTS,
5 REQUEST_ENDPOINT_ACTIONS,
6 REQUEST_VIDEO_EVENT_TYPES
7} from '../../../initializers'
8import { isArray } from '../misc'
9import {
10 isVideoAuthorValid,
11 isVideoThumbnailDataValid,
12 isVideoRemoteIdValid,
13 isVideoAbuseReasonValid,
14 isVideoAbuseReporterUsernameValid,
15 isVideoViewsValid,
16 isVideoLikesValid,
17 isVideoDislikesValid,
18 isVideoEventCountValid,
19 isVideoDateValid,
20 isVideoCategoryValid,
21 isVideoLicenceValid,
22 isVideoLanguageValid,
23 isVideoNSFWValid,
24 isVideoDescriptionValid,
25 isVideoDurationValid,
26 isVideoInfoHashValid,
27 isVideoNameValid,
28 isVideoTagsValid,
29 isVideoExtnameValid
30} from '../videos'
31
32const ENDPOINT_ACTIONS = REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS]
33
34function isEachRemoteRequestVideosValid (requests) {
35 return isArray(requests) &&
36 requests.every(function (request) {
37 const video = request.data
38
39 if (!video) return false
40
41 return (
42 isRequestTypeAddValid(request.type) &&
43 isCommonVideoAttributesValid(video) &&
44 isVideoAuthorValid(video.author) &&
45 isVideoThumbnailDataValid(video.thumbnailData)
46 ) ||
47 (
48 isRequestTypeUpdateValid(request.type) &&
49 isCommonVideoAttributesValid(video)
50 ) ||
51 (
52 isRequestTypeRemoveValid(request.type) &&
53 isVideoRemoteIdValid(video.remoteId)
54 ) ||
55 (
56 isRequestTypeReportAbuseValid(request.type) &&
57 isVideoRemoteIdValid(request.data.videoRemoteId) &&
58 isVideoAbuseReasonValid(request.data.reportReason) &&
59 isVideoAbuseReporterUsernameValid(request.data.reporterUsername)
60 )
61 })
62}
63
64function isEachRemoteRequestVideosQaduValid (requests) {
65 return isArray(requests) &&
66 requests.every(function (request) {
67 const video = request.data
68
69 if (!video) return false
70
71 return (
72 isVideoRemoteIdValid(video.remoteId) &&
73 (has(video, 'views') === false || isVideoViewsValid) &&
74 (has(video, 'likes') === false || isVideoLikesValid) &&
75 (has(video, 'dislikes') === false || isVideoDislikesValid)
76 )
77 })
78}
79
80function isEachRemoteRequestVideosEventsValid (requests) {
81 return isArray(requests) &&
82 requests.every(function (request) {
83 const eventData = request.data
84
85 if (!eventData) return false
86
87 return (
88 isVideoRemoteIdValid(eventData.remoteId) &&
89 values(REQUEST_VIDEO_EVENT_TYPES).indexOf(eventData.eventType) !== -1 &&
90 isVideoEventCountValid(eventData.count)
91 )
92 })
93}
94
95// ---------------------------------------------------------------------------
96
97export {
98 isEachRemoteRequestVideosValid,
99 isEachRemoteRequestVideosQaduValid,
100 isEachRemoteRequestVideosEventsValid
101}
102
103// ---------------------------------------------------------------------------
104
105function isCommonVideoAttributesValid (video) {
106 return isVideoDateValid(video.createdAt) &&
107 isVideoDateValid(video.updatedAt) &&
108 isVideoCategoryValid(video.category) &&
109 isVideoLicenceValid(video.licence) &&
110 isVideoLanguageValid(video.language) &&
111 isVideoNSFWValid(video.nsfw) &&
112 isVideoDescriptionValid(video.description) &&
113 isVideoDurationValid(video.duration) &&
114 isVideoInfoHashValid(video.infoHash) &&
115 isVideoNameValid(video.name) &&
116 isVideoTagsValid(video.tags) &&
117 isVideoRemoteIdValid(video.remoteId) &&
118 isVideoExtnameValid(video.extname) &&
119 isVideoViewsValid(video.views) &&
120 isVideoLikesValid(video.likes) &&
121 isVideoDislikesValid(video.dislikes)
122}
123
124function isRequestTypeAddValid (value) {
125 return value === ENDPOINT_ACTIONS.ADD
126}
127
128function isRequestTypeUpdateValid (value) {
129 return value === ENDPOINT_ACTIONS.UPDATE
130}
131
132function isRequestTypeRemoveValid (value) {
133 return value === ENDPOINT_ACTIONS.REMOVE
134}
135
136function isRequestTypeReportAbuseValid (value) {
137 return value === ENDPOINT_ACTIONS.REPORT_ABUSE
138}