diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-05-15 22:22:03 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-05-20 09:57:40 +0200 |
commit | 65fcc3119c334b75dd13bcfdebf186afdc580a8f (patch) | |
tree | 4f2158c61a9b7c3f47cfa233d01413b946ee53c0 /server/helpers/custom-validators/remote | |
parent | d5f345ed4cfac4e1fa84dcb4fce1cda4d32f9c73 (diff) | |
download | PeerTube-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.js | 11 | ||||
-rw-r--r-- | server/helpers/custom-validators/remote/index.ts | 1 | ||||
-rw-r--r-- | server/helpers/custom-validators/remote/videos.js | 118 | ||||
-rw-r--r-- | server/helpers/custom-validators/remote/videos.ts | 138 |
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 | |||
3 | const remoteVideosValidators = require('./videos') | ||
4 | |||
5 | const validators = { | ||
6 | videos: remoteVideosValidators | ||
7 | } | ||
8 | |||
9 | // --------------------------------------------------------------------------- | ||
10 | |||
11 | module.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 | |||
3 | const has = require('lodash/has') | ||
4 | const values = require('lodash/values') | ||
5 | |||
6 | const constants = require('../../../initializers/constants') | ||
7 | const videosValidators = require('../videos') | ||
8 | const miscValidators = require('../misc') | ||
9 | |||
10 | const ENDPOINT_ACTIONS = constants.REQUEST_ENDPOINT_ACTIONS[constants.REQUEST_ENDPOINTS.VIDEOS] | ||
11 | |||
12 | const remoteVideosValidators = { | ||
13 | isEachRemoteRequestVideosValid, | ||
14 | isEachRemoteRequestVideosQaduValid, | ||
15 | isEachRemoteRequestVideosEventsValid | ||
16 | } | ||
17 | |||
18 | function 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 | |||
48 | function 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 | |||
64 | function 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 | |||
81 | module.exports = remoteVideosValidators | ||
82 | |||
83 | // --------------------------------------------------------------------------- | ||
84 | |||
85 | function 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 | |||
104 | function isRequestTypeAddValid (value) { | ||
105 | return value === ENDPOINT_ACTIONS.ADD | ||
106 | } | ||
107 | |||
108 | function isRequestTypeUpdateValid (value) { | ||
109 | return value === ENDPOINT_ACTIONS.UPDATE | ||
110 | } | ||
111 | |||
112 | function isRequestTypeRemoveValid (value) { | ||
113 | return value === ENDPOINT_ACTIONS.REMOVE | ||
114 | } | ||
115 | |||
116 | function 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 @@ | |||
1 | import { has, values } from 'lodash' | ||
2 | |||
3 | import { | ||
4 | REQUEST_ENDPOINTS, | ||
5 | REQUEST_ENDPOINT_ACTIONS, | ||
6 | REQUEST_VIDEO_EVENT_TYPES | ||
7 | } from '../../../initializers' | ||
8 | import { isArray } from '../misc' | ||
9 | import { | ||
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 | |||
32 | const ENDPOINT_ACTIONS = REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS] | ||
33 | |||
34 | function 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 | |||
64 | function 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 | |||
80 | function 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 | |||
97 | export { | ||
98 | isEachRemoteRequestVideosValid, | ||
99 | isEachRemoteRequestVideosQaduValid, | ||
100 | isEachRemoteRequestVideosEventsValid | ||
101 | } | ||
102 | |||
103 | // --------------------------------------------------------------------------- | ||
104 | |||
105 | function 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 | |||
124 | function isRequestTypeAddValid (value) { | ||
125 | return value === ENDPOINT_ACTIONS.ADD | ||
126 | } | ||
127 | |||
128 | function isRequestTypeUpdateValid (value) { | ||
129 | return value === ENDPOINT_ACTIONS.UPDATE | ||
130 | } | ||
131 | |||
132 | function isRequestTypeRemoveValid (value) { | ||
133 | return value === ENDPOINT_ACTIONS.REMOVE | ||
134 | } | ||
135 | |||
136 | function isRequestTypeReportAbuseValid (value) { | ||
137 | return value === ENDPOINT_ACTIONS.REPORT_ABUSE | ||
138 | } | ||