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