]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/videos/shared/object-to-model-attributes.ts
Don't clean mastodon rates
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / videos / shared / object-to-model-attributes.ts
CommitLineData
69290ab3 1import { maxBy, minBy } from 'lodash'
41fb13c3 2import magnetUtil from 'magnet-uri'
69290ab3
C
3import { basename } from 'path'
4import { isAPVideoFileUrlMetadataObject } from '@server/helpers/custom-validators/activitypub/videos'
5import { isVideoFileInfoHashValid } from '@server/helpers/custom-validators/videos'
6import { logger } from '@server/helpers/logger'
7import { getExtFromMimetype } from '@server/helpers/video'
8import { ACTIVITY_PUB, MIMETYPES, P2P_MEDIA_LOADER_PEER_VERSION, PREVIEWS_SIZE, THUMBNAILS_SIZE } from '@server/initializers/constants'
0305db28 9import { generateTorrentFileName } from '@server/lib/paths'
764b1a14 10import { VideoCaptionModel } from '@server/models/video/video-caption'
69290ab3
C
11import { VideoFileModel } from '@server/models/video/video-file'
12import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
13import { FilteredModelAttributes } from '@server/types'
1e2fe802 14import { isStreamingPlaylist, MChannelId, MStreamingPlaylistVideo, MVideo, MVideoId } from '@server/types/models'
69290ab3
C
15import {
16 ActivityHashTagObject,
17 ActivityMagnetUrlObject,
18 ActivityPlaylistSegmentHashesObject,
19 ActivityPlaylistUrlObject,
20 ActivityTagObject,
21 ActivityUrlObject,
22 ActivityVideoUrlObject,
23 VideoObject,
24 VideoPrivacy,
25 VideoStreamingPlaylistType
26} from '@shared/models'
27
28function getThumbnailFromIcons (videoObject: VideoObject) {
29 let validIcons = videoObject.icon.filter(i => i.width > THUMBNAILS_SIZE.minWidth)
30 // Fallback if there are not valid icons
31 if (validIcons.length === 0) validIcons = videoObject.icon
32
33 return minBy(validIcons, 'width')
34}
35
36function getPreviewFromIcons (videoObject: VideoObject) {
37 const validIcons = videoObject.icon.filter(i => i.width > PREVIEWS_SIZE.minWidth)
38
39 return maxBy(validIcons, 'width')
40}
41
42function getTagsFromObject (videoObject: VideoObject) {
43 return videoObject.tag
44 .filter(isAPHashTagObject)
45 .map(t => t.name)
46}
47
08a47c75 48function getFileAttributesFromUrl (
69290ab3
C
49 videoOrPlaylist: MVideo | MStreamingPlaylistVideo,
50 urls: (ActivityTagObject | ActivityUrlObject)[]
51) {
52 const fileUrls = urls.filter(u => isAPVideoUrlObject(u)) as ActivityVideoUrlObject[]
53
54 if (fileUrls.length === 0) return []
55
56 const attributes: FilteredModelAttributes<VideoFileModel>[] = []
57 for (const fileUrl of fileUrls) {
58 // Fetch associated magnet uri
59 const magnet = urls.filter(isAPMagnetUrlObject)
60 .find(u => u.height === fileUrl.height)
61
62 if (!magnet) throw new Error('Cannot find associated magnet uri for file ' + fileUrl.href)
63
64 const parsed = magnetUtil.decode(magnet.href)
65 if (!parsed || isVideoFileInfoHashValid(parsed.infoHash) === false) {
66 throw new Error('Cannot parse magnet URI ' + magnet.href)
67 }
68
69 const torrentUrl = Array.isArray(parsed.xs)
70 ? parsed.xs[0]
71 : parsed.xs
72
73 // Fetch associated metadata url, if any
74 const metadata = urls.filter(isAPVideoFileUrlMetadataObject)
75 .find(u => {
76 return u.height === fileUrl.height &&
77 u.fps === fileUrl.fps &&
78 u.rel.includes(fileUrl.mediaType)
79 })
80
81 const extname = getExtFromMimetype(MIMETYPES.VIDEO.MIMETYPE_EXT, fileUrl.mediaType)
82 const resolution = fileUrl.height
764b1a14
C
83 const videoId = isStreamingPlaylist(videoOrPlaylist) ? null : videoOrPlaylist.id
84 const videoStreamingPlaylistId = isStreamingPlaylist(videoOrPlaylist) ? videoOrPlaylist.id : null
69290ab3
C
85
86 const attribute = {
87 extname,
88 infoHash: parsed.infoHash,
89 resolution,
90 size: fileUrl.size,
91 fps: fileUrl.fps || -1,
92 metadataUrl: metadata?.href,
93
94 // Use the name of the remote file because we don't proxify video file requests
95 filename: basename(fileUrl.href),
96 fileUrl: fileUrl.href,
97
98 torrentUrl,
99 // Use our own torrent name since we proxify torrent requests
100 torrentFilename: generateTorrentFileName(videoOrPlaylist, resolution),
101
102 // This is a video file owned by a video or by a streaming playlist
103 videoId,
104 videoStreamingPlaylistId
105 }
106
107 attributes.push(attribute)
108 }
109
110 return attributes
111}
112
1e2fe802 113function getStreamingPlaylistAttributesFromObject (video: MVideoId, videoObject: VideoObject) {
69290ab3
C
114 const playlistUrls = videoObject.url.filter(u => isAPStreamingPlaylistUrlObject(u)) as ActivityPlaylistUrlObject[]
115 if (playlistUrls.length === 0) return []
116
117 const attributes: (FilteredModelAttributes<VideoStreamingPlaylistModel> & { tagAPObject?: ActivityTagObject[] })[] = []
118 for (const playlistUrlObject of playlistUrls) {
119 const segmentsSha256UrlObject = playlistUrlObject.tag.find(isAPPlaylistSegmentHashesUrlObject)
120
1e2fe802 121 const files: unknown[] = playlistUrlObject.tag.filter(u => isAPVideoUrlObject(u)) as ActivityVideoUrlObject[]
69290ab3 122
69290ab3
C
123 if (!segmentsSha256UrlObject) {
124 logger.warn('No segment sha256 URL found in AP playlist object.', { playlistUrl: playlistUrlObject })
125 continue
126 }
127
128 const attribute = {
129 type: VideoStreamingPlaylistType.HLS,
764b1a14
C
130
131 playlistFilename: basename(playlistUrlObject.href),
69290ab3 132 playlistUrl: playlistUrlObject.href,
764b1a14
C
133
134 segmentsSha256Filename: basename(segmentsSha256UrlObject.href),
69290ab3 135 segmentsSha256Url: segmentsSha256UrlObject.href,
764b1a14 136
69290ab3
C
137 p2pMediaLoaderInfohashes: VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(playlistUrlObject.href, files),
138 p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION,
139 videoId: video.id,
08a47c75 140
69290ab3
C
141 tagAPObject: playlistUrlObject.tag
142 }
143
144 attributes.push(attribute)
145 }
146
147 return attributes
148}
149
08a47c75
C
150function getLiveAttributesFromObject (video: MVideoId, videoObject: VideoObject) {
151 return {
152 saveReplay: videoObject.liveSaveReplay,
153 permanentLive: videoObject.permanentLive,
f443a746 154 latencyMode: videoObject.latencyMode,
08a47c75
C
155 videoId: video.id
156 }
157}
158
159function getCaptionAttributesFromObject (video: MVideoId, videoObject: VideoObject) {
160 return videoObject.subtitleLanguage.map(c => ({
161 videoId: video.id,
162 filename: VideoCaptionModel.generateCaptionName(c.identifier),
163 language: c.identifier,
164 fileUrl: c.url
165 }))
166}
167
168function getVideoAttributesFromObject (videoChannel: MChannelId, videoObject: VideoObject, to: string[] = []) {
69290ab3
C
169 const privacy = to.includes(ACTIVITY_PUB.PUBLIC)
170 ? VideoPrivacy.PUBLIC
171 : VideoPrivacy.UNLISTED
172
173 const duration = videoObject.duration.replace(/[^\d]+/, '')
174 const language = videoObject.language?.identifier
175
176 const category = videoObject.category
177 ? parseInt(videoObject.category.identifier, 10)
178 : undefined
179
180 const licence = videoObject.licence
181 ? parseInt(videoObject.licence.identifier, 10)
182 : undefined
183
184 const description = videoObject.content || null
185 const support = videoObject.support || null
186
187 return {
188 name: videoObject.name,
189 uuid: videoObject.uuid,
190 url: videoObject.id,
191 category,
192 licence,
193 language,
194 description,
195 support,
196 nsfw: videoObject.sensitive,
197 commentsEnabled: videoObject.commentsEnabled,
198 downloadEnabled: videoObject.downloadEnabled,
199 waitTranscoding: videoObject.waitTranscoding,
200 isLive: videoObject.isLiveBroadcast,
201 state: videoObject.state,
202 channelId: videoChannel.id,
203 duration: parseInt(duration, 10),
204 createdAt: new Date(videoObject.published),
205 publishedAt: new Date(videoObject.published),
206
207 originallyPublishedAt: videoObject.originallyPublishedAt
208 ? new Date(videoObject.originallyPublishedAt)
209 : null,
210
211 updatedAt: new Date(videoObject.updated),
212 views: videoObject.views,
213 likes: 0,
214 dislikes: 0,
215 remote: true,
216 privacy
217 }
218}
219
220// ---------------------------------------------------------------------------
221
222export {
223 getThumbnailFromIcons,
224 getPreviewFromIcons,
225
226 getTagsFromObject,
227
08a47c75
C
228 getFileAttributesFromUrl,
229 getStreamingPlaylistAttributesFromObject,
230
231 getLiveAttributesFromObject,
232 getCaptionAttributesFromObject,
69290ab3 233
08a47c75 234 getVideoAttributesFromObject
69290ab3
C
235}
236
237// ---------------------------------------------------------------------------
238
239function isAPVideoUrlObject (url: any): url is ActivityVideoUrlObject {
240 const urlMediaType = url.mediaType
241
242 return MIMETYPES.VIDEO.MIMETYPE_EXT[urlMediaType] && urlMediaType.startsWith('video/')
243}
244
245function isAPStreamingPlaylistUrlObject (url: any): url is ActivityPlaylistUrlObject {
246 return url && url.mediaType === 'application/x-mpegURL'
247}
248
249function isAPPlaylistSegmentHashesUrlObject (tag: any): tag is ActivityPlaylistSegmentHashesObject {
250 return tag && tag.name === 'sha256' && tag.type === 'Link' && tag.mediaType === 'application/json'
251}
252
253function isAPMagnetUrlObject (url: any): url is ActivityMagnetUrlObject {
254 return url && url.mediaType === 'application/x-bittorrent;x-scheme-handler/magnet'
255}
256
257function isAPHashTagObject (url: any): url is ActivityHashTagObject {
258 return url && url.type === 'Hashtag'
259}