aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators/activitypub/videos.ts
blob: a46757397e74e7bf80c6bab35a43a2c883b85386 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import * as validator from 'validator'
import { ACTIVITY_PUB } from '../../../initializers'
import { exists, isDateValid, isUUIDValid } from '../misc'
import { isVideoChannelDescriptionValid, isVideoChannelNameValid } from '../video-channels'
import {
  isVideoDurationValid,
  isVideoNameValid,
  isVideoNSFWValid,
  isVideoTagValid,
  isVideoTruncatedDescriptionValid,
  isVideoUrlValid,
  isVideoViewsValid
} from '../videos'
import { isBaseActivityValid } from './misc'

function isVideoTorrentAddActivityValid (activity: any) {
  return isBaseActivityValid(activity, 'Add') &&
    isVideoTorrentObjectValid(activity.object)
}

function isVideoTorrentUpdateActivityValid (activity: any) {
  return isBaseActivityValid(activity, 'Update') &&
    isVideoTorrentObjectValid(activity.object)
}

function isVideoTorrentDeleteActivityValid (activity: any) {
  return isBaseActivityValid(activity, 'Delete')
}

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') &&
    isVideoDurationValid(value.replace(/[^0-9]+/, ''))
}

function isVideoTorrentObjectValid (video: any) {
  return video.type === 'Video' &&
    isVideoNameValid(video.name) &&
    isActivityPubVideoDurationValid(video.duration) &&
    isUUIDValid(video.uuid) &&
    setValidRemoteTags(video) &&
    isRemoteIdentifierValid(video.category) &&
    isRemoteIdentifierValid(video.licence) &&
    isRemoteIdentifierValid(video.language) &&
    isVideoViewsValid(video.video) &&
    isVideoNSFWValid(video.nsfw) &&
    isDateValid(video.published) &&
    isDateValid(video.updated) &&
    isRemoteVideoContentValid(video.mediaType, video.content) &&
    isRemoteVideoIconValid(video.icon) &&
    setValidRemoteVideoUrls(video.url)
}

function isVideoChannelCreateActivityValid (activity: any) {
  return isBaseActivityValid(activity, 'Create') &&
    isVideoChannelObjectValid(activity.object)
}

function isVideoChannelUpdateActivityValid (activity: any) {
  return isBaseActivityValid(activity, 'Update') &&
    isVideoChannelObjectValid(activity.object)
}

function isVideoChannelDeleteActivityValid (activity: any) {
  return isBaseActivityValid(activity, 'Delete')
}

function isVideoChannelObjectValid (videoChannel: any) {
  return videoChannel.type === 'VideoChannel' &&
    isVideoChannelNameValid(videoChannel.name) &&
    isVideoChannelDescriptionValid(videoChannel.description) &&
    isUUIDValid(videoChannel.uuid)
}

// ---------------------------------------------------------------------------

export {
  isVideoTorrentAddActivityValid,
  isVideoChannelCreateActivityValid,
  isVideoTorrentUpdateActivityValid,
  isVideoChannelUpdateActivityValid,
  isVideoChannelDeleteActivityValid,
  isVideoTorrentDeleteActivityValid
}

// ---------------------------------------------------------------------------

function setValidRemoteTags (video: any) {
  if (Array.isArray(video.tag) === false) return false

  const newTag = video.tag.filter(t => {
    return t.type === 'Hashtag' &&
      isVideoTagValid(t.name)
  })

  video.tag = newTag
  return true
}

function isRemoteIdentifierValid (data: any) {
  return validator.isInt(data.identifier, { min: 0 })
}

function isRemoteVideoContentValid (mediaType: string, content: string) {
  return mediaType === 'text/markdown' && isVideoTruncatedDescriptionValid(content)
}

function isRemoteVideoIconValid (icon: any) {
  return icon.type === 'Image' &&
    isVideoUrlValid(icon.url) &&
    icon.mediaType === 'image/jpeg' &&
    validator.isInt(icon.width, { min: 0 }) &&
    validator.isInt(icon.height, { min: 0 })
}

function setValidRemoteVideoUrls (video: any) {
  if (Array.isArray(video.url) === false) return false

  const newUrl = video.url.filter(u => isRemoteVideoUrlValid(u))
  video.url = newUrl

  return true
}

function isRemoteVideoUrlValid (url: any) {
  return url.type === 'Link' &&
    ACTIVITY_PUB.VIDEO_URL_MIME_TYPES.indexOf(url.mimeType) !== -1 &&
    isVideoUrlValid(url.url) &&
    validator.isInt(url.width, { min: 0 }) &&
    validator.isInt(url.size, { min: 0 })
}