2017-11-10 14:34:45 +01:00
|
|
|
import * as validator from 'validator'
|
2018-03-28 11:00:02 +02:00
|
|
|
import { ACTIVITY_PUB, CONSTRAINTS_FIELDS } from '../../../initializers'
|
|
|
|
import { peertubeTruncate } from '../../core-utils'
|
2018-01-03 10:12:36 +01:00
|
|
|
import { exists, isBooleanValid, isDateValid, isUUIDValid } from '../misc'
|
2017-05-15 22:22:03 +02:00
|
|
|
import {
|
2017-11-15 17:56:21 +01:00
|
|
|
isVideoAbuseReasonValid,
|
2017-05-15 22:22:03 +02:00
|
|
|
isVideoDurationValid,
|
|
|
|
isVideoNameValid,
|
2017-11-14 10:57:56 +01:00
|
|
|
isVideoTagValid,
|
2017-11-15 16:28:35 +01:00
|
|
|
isVideoTruncatedDescriptionValid,
|
|
|
|
isVideoViewsValid
|
2017-05-15 22:22:03 +02:00
|
|
|
} from '../videos'
|
2017-12-14 17:38:41 +01:00
|
|
|
import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc'
|
2017-11-10 14:34:45 +01:00
|
|
|
|
2018-05-09 16:16:22 +02:00
|
|
|
function sanitizeAndCheckVideoTorrentCreateActivity (activity: any) {
|
2017-12-14 17:38:41 +01:00
|
|
|
return isBaseActivityValid(activity, 'Create') &&
|
2018-05-09 16:16:22 +02:00
|
|
|
sanitizeAndCheckVideoTorrentObject(activity.object)
|
2017-11-10 14:34:45 +01:00
|
|
|
}
|
|
|
|
|
2018-05-09 16:16:22 +02:00
|
|
|
function sanitizeAndCheckVideoTorrentUpdateActivity (activity: any) {
|
2017-11-10 14:34:45 +01:00
|
|
|
return isBaseActivityValid(activity, 'Update') &&
|
2018-05-09 16:16:22 +02:00
|
|
|
sanitizeAndCheckVideoTorrentObject(activity.object)
|
2017-05-15 22:22:03 +02:00
|
|
|
}
|
|
|
|
|
2017-11-14 17:31:26 +01:00
|
|
|
function isVideoTorrentDeleteActivityValid (activity: any) {
|
|
|
|
return isBaseActivityValid(activity, 'Delete')
|
|
|
|
}
|
|
|
|
|
2017-11-20 09:43:39 +01:00
|
|
|
function isVideoFlagValid (activity: any) {
|
|
|
|
return isBaseActivityValid(activity, 'Create') &&
|
|
|
|
activity.object.type === 'Flag' &&
|
|
|
|
isVideoAbuseReasonValid(activity.object.content) &&
|
|
|
|
isActivityPubUrlValid(activity.object.object)
|
|
|
|
}
|
|
|
|
|
2017-11-15 16:28:35 +01:00
|
|
|
function isActivityPubVideoDurationValid (value: string) {
|
|
|
|
// https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
|
|
|
|
return exists(value) &&
|
|
|
|
typeof value === 'string' &&
|
|
|
|
value.startsWith('PT') &&
|
|
|
|
value.endsWith('S') &&
|
2017-11-16 11:08:25 +01:00
|
|
|
isVideoDurationValid(value.replace(/[^0-9]+/g, ''))
|
2017-11-15 16:28:35 +01:00
|
|
|
}
|
|
|
|
|
2018-05-09 16:16:22 +02:00
|
|
|
function sanitizeAndCheckVideoTorrentObject (video: any) {
|
|
|
|
if (!setValidRemoteTags(video)) return false
|
|
|
|
if (!setValidRemoteVideoUrls(video)) return false
|
|
|
|
if (!setRemoteVideoTruncatedContent(video)) return false
|
|
|
|
if (!setValidAttributedTo(video)) return false
|
|
|
|
|
2017-11-10 14:34:45 +01:00
|
|
|
return video.type === 'Video' &&
|
2017-11-16 15:22:39 +01:00
|
|
|
isActivityPubUrlValid(video.id) &&
|
2017-11-10 14:34:45 +01:00
|
|
|
isVideoNameValid(video.name) &&
|
2017-11-15 16:28:35 +01:00
|
|
|
isActivityPubVideoDurationValid(video.duration) &&
|
2017-11-10 14:34:45 +01:00
|
|
|
isUUIDValid(video.uuid) &&
|
2018-04-23 14:39:52 +02:00
|
|
|
(!video.category || isRemoteNumberIdentifierValid(video.category)) &&
|
|
|
|
(!video.licence || isRemoteNumberIdentifierValid(video.licence)) &&
|
|
|
|
(!video.language || isRemoteStringIdentifierValid(video.language)) &&
|
2017-11-16 11:08:25 +01:00
|
|
|
isVideoViewsValid(video.views) &&
|
2018-01-26 17:25:35 +01:00
|
|
|
isBooleanValid(video.sensitive) &&
|
2018-01-03 10:12:36 +01:00
|
|
|
isBooleanValid(video.commentsEnabled) &&
|
2017-11-10 14:34:45 +01:00
|
|
|
isDateValid(video.published) &&
|
|
|
|
isDateValid(video.updated) &&
|
2017-12-08 17:31:21 +01:00
|
|
|
(!video.content || isRemoteVideoContentValid(video.mediaType, video.content)) &&
|
2017-11-10 14:34:45 +01:00
|
|
|
isRemoteVideoIconValid(video.icon) &&
|
2017-12-14 17:38:41 +01:00
|
|
|
video.url.length !== 0 &&
|
|
|
|
video.attributedTo.length !== 0
|
2017-05-15 22:22:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2018-05-09 16:16:22 +02:00
|
|
|
sanitizeAndCheckVideoTorrentCreateActivity,
|
|
|
|
sanitizeAndCheckVideoTorrentUpdateActivity,
|
2017-11-15 17:56:21 +01:00
|
|
|
isVideoTorrentDeleteActivityValid,
|
2018-04-23 14:39:52 +02:00
|
|
|
isRemoteStringIdentifierValid,
|
2018-01-10 17:18:12 +01:00
|
|
|
isVideoFlagValid,
|
2018-05-09 16:16:22 +02:00
|
|
|
sanitizeAndCheckVideoTorrentObject
|
2017-05-15 22:22:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-11-10 14:34:45 +01:00
|
|
|
function setValidRemoteTags (video: any) {
|
|
|
|
if (Array.isArray(video.tag) === false) return false
|
2017-05-15 22:22:03 +02:00
|
|
|
|
2017-11-27 17:30:46 +01:00
|
|
|
video.tag = video.tag.filter(t => {
|
2017-11-10 14:34:45 +01:00
|
|
|
return t.type === 'Hashtag' &&
|
|
|
|
isVideoTagValid(t.name)
|
|
|
|
})
|
2017-10-24 19:41:09 +02:00
|
|
|
|
2017-11-10 14:34:45 +01:00
|
|
|
return true
|
2017-10-24 19:41:09 +02:00
|
|
|
}
|
|
|
|
|
2018-04-23 14:39:52 +02:00
|
|
|
function isRemoteNumberIdentifierValid (data: any) {
|
2017-11-10 14:34:45 +01:00
|
|
|
return validator.isInt(data.identifier, { min: 0 })
|
2017-10-24 19:41:09 +02:00
|
|
|
}
|
|
|
|
|
2018-04-23 14:39:52 +02:00
|
|
|
function isRemoteStringIdentifierValid (data: any) {
|
|
|
|
return typeof data.identifier === 'string'
|
|
|
|
}
|
|
|
|
|
2017-11-10 14:34:45 +01:00
|
|
|
function isRemoteVideoContentValid (mediaType: string, content: string) {
|
|
|
|
return mediaType === 'text/markdown' && isVideoTruncatedDescriptionValid(content)
|
2017-10-24 19:41:09 +02:00
|
|
|
}
|
|
|
|
|
2017-11-10 14:34:45 +01:00
|
|
|
function isRemoteVideoIconValid (icon: any) {
|
|
|
|
return icon.type === 'Image' &&
|
2017-11-27 17:30:46 +01:00
|
|
|
isActivityPubUrlValid(icon.url) &&
|
2017-11-10 14:34:45 +01:00
|
|
|
icon.mediaType === 'image/jpeg' &&
|
2017-11-16 11:08:25 +01:00
|
|
|
validator.isInt(icon.width + '', { min: 0 }) &&
|
|
|
|
validator.isInt(icon.height + '', { min: 0 })
|
2017-10-24 19:41:09 +02:00
|
|
|
}
|
|
|
|
|
2017-11-10 14:34:45 +01:00
|
|
|
function setValidRemoteVideoUrls (video: any) {
|
|
|
|
if (Array.isArray(video.url) === false) return false
|
2017-05-15 22:22:03 +02:00
|
|
|
|
2017-11-27 17:30:46 +01:00
|
|
|
video.url = video.url.filter(u => isRemoteVideoUrlValid(u))
|
2017-05-15 22:22:03 +02:00
|
|
|
|
2017-11-10 14:34:45 +01:00
|
|
|
return true
|
2017-05-15 22:22:03 +02:00
|
|
|
}
|
|
|
|
|
2018-03-28 13:45:24 +02:00
|
|
|
function setRemoteVideoTruncatedContent (video: any) {
|
2018-03-28 11:00:02 +02:00
|
|
|
if (video.content) {
|
|
|
|
video.content = peertubeTruncate(video.content, CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-11-10 14:34:45 +01:00
|
|
|
function isRemoteVideoUrlValid (url: any) {
|
|
|
|
return url.type === 'Link' &&
|
2017-11-16 15:22:39 +01:00
|
|
|
(
|
|
|
|
ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.indexOf(url.mimeType) !== -1 &&
|
2018-01-12 15:35:30 +01:00
|
|
|
isActivityPubUrlValid(url.href) &&
|
2017-11-16 15:22:39 +01:00
|
|
|
validator.isInt(url.width + '', { min: 0 }) &&
|
|
|
|
validator.isInt(url.size + '', { min: 0 })
|
|
|
|
) ||
|
|
|
|
(
|
|
|
|
ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.indexOf(url.mimeType) !== -1 &&
|
2018-01-12 15:35:30 +01:00
|
|
|
isActivityPubUrlValid(url.href) &&
|
2017-11-16 15:22:39 +01:00
|
|
|
validator.isInt(url.width + '', { min: 0 })
|
|
|
|
) ||
|
|
|
|
(
|
|
|
|
ACTIVITY_PUB.URL_MIME_TYPES.MAGNET.indexOf(url.mimeType) !== -1 &&
|
2018-01-12 15:35:30 +01:00
|
|
|
validator.isLength(url.href, { min: 5 }) &&
|
2017-11-16 15:22:39 +01:00
|
|
|
validator.isInt(url.width + '', { min: 0 })
|
|
|
|
)
|
2017-05-15 22:22:03 +02:00
|
|
|
}
|