aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/misc.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/activitypub/misc.ts')
-rw-r--r--server/lib/activitypub/misc.ts46
1 files changed, 34 insertions, 12 deletions
diff --git a/server/lib/activitypub/misc.ts b/server/lib/activitypub/misc.ts
index 43d26c328..13838fc4c 100644
--- a/server/lib/activitypub/misc.ts
+++ b/server/lib/activitypub/misc.ts
@@ -7,6 +7,21 @@ import { VIDEO_MIMETYPE_EXT } from '../../initializers/constants'
7import { VideoChannelInstance } from '../../models/video/video-channel-interface' 7import { VideoChannelInstance } from '../../models/video/video-channel-interface'
8import { VideoFileAttributes } from '../../models/video/video-file-interface' 8import { VideoFileAttributes } from '../../models/video/video-file-interface'
9import { VideoAttributes, VideoInstance } from '../../models/video/video-interface' 9import { VideoAttributes, VideoInstance } from '../../models/video/video-interface'
10import { VideoChannelObject } from '../../../shared/models/activitypub/objects/video-channel-object'
11import { AccountInstance } from '../../models/account/account-interface'
12
13function videoChannelActivityObjectToDBAttributes (videoChannelObject: VideoChannelObject, account: AccountInstance) {
14 return {
15 name: videoChannelObject.name,
16 description: videoChannelObject.content,
17 uuid: videoChannelObject.uuid,
18 url: videoChannelObject.id,
19 createdAt: new Date(videoChannelObject.published),
20 updatedAt: new Date(videoChannelObject.updated),
21 remote: true,
22 accountId: account.id
23 }
24}
10 25
11async function videoActivityObjectToDBAttributes ( 26async function videoActivityObjectToDBAttributes (
12 videoChannel: VideoChannelInstance, 27 videoChannel: VideoChannelInstance,
@@ -45,26 +60,32 @@ async function videoActivityObjectToDBAttributes (
45} 60}
46 61
47function videoFileActivityUrlToDBAttributes (videoCreated: VideoInstance, videoObject: VideoTorrentObject) { 62function videoFileActivityUrlToDBAttributes (videoCreated: VideoInstance, videoObject: VideoTorrentObject) {
48 const fileUrls = videoObject.url 63 const mimeTypes = Object.keys(VIDEO_MIMETYPE_EXT)
49 .filter(u => Object.keys(VIDEO_MIMETYPE_EXT).indexOf(u.mimeType) !== -1 && u.url.startsWith('video/')) 64 const fileUrls = videoObject.url.filter(u => {
65 return mimeTypes.indexOf(u.mimeType) !== -1 && u.mimeType.startsWith('video/')
66 })
67
68 if (fileUrls.length === 0) {
69 throw new Error('Cannot find video files for ' + videoCreated.url)
70 }
50 71
51 const attributes: VideoFileAttributes[] = [] 72 const attributes: VideoFileAttributes[] = []
52 for (const url of fileUrls) { 73 for (const fileUrl of fileUrls) {
53 // Fetch associated magnet uri 74 // Fetch associated magnet uri
54 const magnet = videoObject.url 75 const magnet = videoObject.url.find(u => {
55 .find(u => { 76 return u.mimeType === 'application/x-bittorrent;x-scheme-handler/magnet' && u.width === fileUrl.width
56 return u.mimeType === 'application/x-bittorrent;x-scheme-handler/magnet' && u.width === url.width 77 })
57 }) 78
58 if (!magnet) throw new Error('Cannot find associated magnet uri for file ' + url.url) 79 if (!magnet) throw new Error('Cannot find associated magnet uri for file ' + fileUrl.url)
59 80
60 const parsed = magnetUtil.decode(magnet.url) 81 const parsed = magnetUtil.decode(magnet.url)
61 if (!parsed || isVideoFileInfoHashValid(parsed.infoHash) === false) throw new Error('Cannot parse magnet URI ' + magnet.url) 82 if (!parsed || isVideoFileInfoHashValid(parsed.infoHash) === false) throw new Error('Cannot parse magnet URI ' + magnet.url)
62 83
63 const attribute = { 84 const attribute = {
64 extname: VIDEO_MIMETYPE_EXT[url.mimeType], 85 extname: VIDEO_MIMETYPE_EXT[fileUrl.mimeType],
65 infoHash: parsed.infoHash, 86 infoHash: parsed.infoHash,
66 resolution: url.width, 87 resolution: fileUrl.width,
67 size: url.size, 88 size: fileUrl.size,
68 videoId: videoCreated.id 89 videoId: videoCreated.id
69 } 90 }
70 attributes.push(attribute) 91 attributes.push(attribute)
@@ -77,5 +98,6 @@ function videoFileActivityUrlToDBAttributes (videoCreated: VideoInstance, videoO
77 98
78export { 99export {
79 videoFileActivityUrlToDBAttributes, 100 videoFileActivityUrlToDBAttributes,
80 videoActivityObjectToDBAttributes 101 videoActivityObjectToDBAttributes,
102 videoChannelActivityObjectToDBAttributes
81} 103}