]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/misc.ts
Federate video views
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / misc.ts
1 import * as magnetUtil from 'magnet-uri'
2 import { VideoTorrentObject } from '../../../../shared'
3 import { VideoChannelObject } from '../../../../shared/models/activitypub/objects/video-channel-object'
4 import { isVideoFileInfoHashValid } from '../../../helpers/custom-validators/videos'
5 import { ACTIVITY_PUB, VIDEO_MIMETYPE_EXT } from '../../../initializers/constants'
6 import { AccountInstance } from '../../../models/account/account-interface'
7 import { VideoChannelInstance } from '../../../models/video/video-channel-interface'
8 import { VideoFileAttributes } from '../../../models/video/video-file-interface'
9 import { VideoAttributes, VideoInstance } from '../../../models/video/video-interface'
10 import { VideoPrivacy } from '../../../../shared/models/videos/video-privacy.enum'
11
12 function videoChannelActivityObjectToDBAttributes (videoChannelObject: VideoChannelObject, account: AccountInstance) {
13 return {
14 name: videoChannelObject.name,
15 description: videoChannelObject.content,
16 uuid: videoChannelObject.uuid,
17 url: videoChannelObject.id,
18 createdAt: new Date(videoChannelObject.published),
19 updatedAt: new Date(videoChannelObject.updated),
20 remote: true,
21 accountId: account.id
22 }
23 }
24
25 async function videoActivityObjectToDBAttributes (
26 videoChannel: VideoChannelInstance,
27 videoObject: VideoTorrentObject,
28 to: string[] = [],
29 cc: string[] = []
30 ) {
31 let privacy = VideoPrivacy.PRIVATE
32 if (to.indexOf(ACTIVITY_PUB.PUBLIC) !== -1) privacy = VideoPrivacy.PUBLIC
33 else if (cc.indexOf(ACTIVITY_PUB.PUBLIC) !== -1) privacy = VideoPrivacy.UNLISTED
34
35 const duration = videoObject.duration.replace(/[^\d]+/, '')
36 let language = null
37 if (videoObject.language) {
38 language = parseInt(videoObject.language.identifier, 10)
39 }
40
41 const videoData: VideoAttributes = {
42 name: videoObject.name,
43 uuid: videoObject.uuid,
44 url: videoObject.id,
45 category: parseInt(videoObject.category.identifier, 10),
46 licence: parseInt(videoObject.licence.identifier, 10),
47 language,
48 nsfw: videoObject.nsfw,
49 description: videoObject.content,
50 channelId: videoChannel.id,
51 duration: parseInt(duration, 10),
52 createdAt: new Date(videoObject.published),
53 // FIXME: updatedAt does not seems to be considered by Sequelize
54 updatedAt: new Date(videoObject.updated),
55 views: videoObject.views,
56 likes: 0,
57 dislikes: 0,
58 remote: true,
59 privacy
60 }
61
62 return videoData
63 }
64
65 function videoFileActivityUrlToDBAttributes (videoCreated: VideoInstance, videoObject: VideoTorrentObject) {
66 const mimeTypes = Object.keys(VIDEO_MIMETYPE_EXT)
67 const fileUrls = videoObject.url.filter(u => {
68 return mimeTypes.indexOf(u.mimeType) !== -1 && u.mimeType.startsWith('video/')
69 })
70
71 if (fileUrls.length === 0) {
72 throw new Error('Cannot find video files for ' + videoCreated.url)
73 }
74
75 const attributes: VideoFileAttributes[] = []
76 for (const fileUrl of fileUrls) {
77 // Fetch associated magnet uri
78 const magnet = videoObject.url.find(u => {
79 return u.mimeType === 'application/x-bittorrent;x-scheme-handler/magnet' && u.width === fileUrl.width
80 })
81
82 if (!magnet) throw new Error('Cannot find associated magnet uri for file ' + fileUrl.url)
83
84 const parsed = magnetUtil.decode(magnet.url)
85 if (!parsed || isVideoFileInfoHashValid(parsed.infoHash) === false) throw new Error('Cannot parse magnet URI ' + magnet.url)
86
87 const attribute = {
88 extname: VIDEO_MIMETYPE_EXT[fileUrl.mimeType],
89 infoHash: parsed.infoHash,
90 resolution: fileUrl.width,
91 size: fileUrl.size,
92 videoId: videoCreated.id
93 }
94 attributes.push(attribute)
95 }
96
97 return attributes
98 }
99
100 // ---------------------------------------------------------------------------
101
102 export {
103 videoFileActivityUrlToDBAttributes,
104 videoActivityObjectToDBAttributes,
105 videoChannelActivityObjectToDBAttributes
106 }