aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/misc.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-11-10 14:34:45 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-11-27 19:40:51 +0100
commit0d0e8dd0904b380b70e19ebcb4763d65601c4632 (patch)
treeacb625d7c88fbe863fa14bf6783fafe4a8e35137 /server/lib/activitypub/misc.ts
parente4f97babf701481b55cc10fb3448feab5f97c867 (diff)
downloadPeerTube-0d0e8dd0904b380b70e19ebcb4763d65601c4632.tar.gz
PeerTube-0d0e8dd0904b380b70e19ebcb4763d65601c4632.tar.zst
PeerTube-0d0e8dd0904b380b70e19ebcb4763d65601c4632.zip
Continue activitypub
Diffstat (limited to 'server/lib/activitypub/misc.ts')
-rw-r--r--server/lib/activitypub/misc.ts77
1 files changed, 77 insertions, 0 deletions
diff --git a/server/lib/activitypub/misc.ts b/server/lib/activitypub/misc.ts
new file mode 100644
index 000000000..05e77ebc3
--- /dev/null
+++ b/server/lib/activitypub/misc.ts
@@ -0,0 +1,77 @@
1import * as magnetUtil from 'magnet-uri'
2import * as Sequelize from 'sequelize'
3import { VideoTorrentObject } from '../../../shared'
4import { isVideoFileInfoHashValid } from '../../helpers/custom-validators/videos'
5import { database as db } from '../../initializers'
6import { VIDEO_MIMETYPE_EXT } from '../../initializers/constants'
7import { VideoChannelInstance } from '../../models/video/video-channel-interface'
8import { VideoFileAttributes } from '../../models/video/video-file-interface'
9import { VideoAttributes, VideoInstance } from '../../models/video/video-interface'
10
11async function videoActivityObjectToDBAttributes (videoChannel: VideoChannelInstance, videoObject: VideoTorrentObject, t: Sequelize.Transaction) {
12 const videoFromDatabase = await db.Video.loadByUUIDOrURL(videoObject.uuid, videoObject.id, t)
13 if (videoFromDatabase) throw new Error('Video with this UUID/Url already exists.')
14
15 const duration = videoObject.duration.replace(/[^\d]+/, '')
16 const videoData: VideoAttributes = {
17 name: videoObject.name,
18 uuid: videoObject.uuid,
19 url: videoObject.id,
20 category: parseInt(videoObject.category.identifier, 10),
21 licence: parseInt(videoObject.licence.identifier, 10),
22 language: parseInt(videoObject.language.identifier, 10),
23 nsfw: videoObject.nsfw,
24 description: videoObject.content,
25 channelId: videoChannel.id,
26 duration: parseInt(duration, 10),
27 createdAt: videoObject.published,
28 // FIXME: updatedAt does not seems to be considered by Sequelize
29 updatedAt: videoObject.updated,
30 views: videoObject.views,
31 likes: 0,
32 dislikes: 0,
33 // likes: videoToCreateData.likes,
34 // dislikes: videoToCreateData.dislikes,
35 remote: true,
36 privacy: 1
37 // privacy: videoToCreateData.privacy
38 }
39
40 return videoData
41}
42
43function videoFileActivityUrlToDBAttributes (videoCreated: VideoInstance, videoObject: VideoTorrentObject) {
44 const fileUrls = videoObject.url
45 .filter(u => Object.keys(VIDEO_MIMETYPE_EXT).indexOf(u.mimeType) !== -1)
46
47 const attributes: VideoFileAttributes[] = []
48 for (const url of fileUrls) {
49 // Fetch associated magnet uri
50 const magnet = videoObject.url
51 .find(u => {
52 return u.mimeType === 'application/x-bittorrent;x-scheme-handler/magnet' && u.width === url.width
53 })
54 if (!magnet) throw new Error('Cannot find associated magnet uri for file ' + url.url)
55
56 const parsed = magnetUtil.decode(magnet.url)
57 if (!parsed || isVideoFileInfoHashValid(parsed.infoHash) === false) throw new Error('Cannot parse magnet URI ' + magnet.url)
58
59 const attribute = {
60 extname: VIDEO_MIMETYPE_EXT[url.mimeType],
61 infoHash: parsed.infoHash,
62 resolution: url.width,
63 size: url.size,
64 videoId: videoCreated.id
65 }
66 attributes.push(attribute)
67 }
68
69 return attributes
70}
71
72// ---------------------------------------------------------------------------
73
74export {
75 videoFileActivityUrlToDBAttributes,
76 videoActivityObjectToDBAttributes
77}