2017-11-10 08:34:45 -05:00
|
|
|
import * as magnetUtil from 'magnet-uri'
|
2017-11-20 03:43:39 -05:00
|
|
|
import { VideoTorrentObject } from '../../../../shared'
|
2017-12-12 11:53:50 -05:00
|
|
|
import { VideoChannelObject } from '../../../../shared/models/activitypub/objects'
|
|
|
|
import { VideoPrivacy } from '../../../../shared/models/videos'
|
|
|
|
import { doRequest } from '../../../helpers'
|
2017-11-20 03:43:39 -05:00
|
|
|
import { isVideoFileInfoHashValid } from '../../../helpers/custom-validators/videos'
|
2017-12-12 11:53:50 -05:00
|
|
|
import { ACTIVITY_PUB, VIDEO_MIMETYPE_EXT } from '../../../initializers'
|
|
|
|
import { AccountModel } from '../../../models/account/account'
|
|
|
|
import { VideoModel } from '../../../models/video/video'
|
|
|
|
import { VideoChannelModel } from '../../../models/video/video-channel'
|
|
|
|
import { VideoChannelShareModel } from '../../../models/video/video-channel-share'
|
|
|
|
import { VideoShareModel } from '../../../models/video/video-share'
|
2017-11-27 08:44:51 -05:00
|
|
|
import { getOrCreateAccountAndServer } from '../account'
|
2017-11-16 09:22:39 -05:00
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
function videoChannelActivityObjectToDBAttributes (videoChannelObject: VideoChannelObject, account: AccountModel) {
|
2017-11-16 09:22:39 -05:00
|
|
|
return {
|
|
|
|
name: videoChannelObject.name,
|
|
|
|
description: videoChannelObject.content,
|
|
|
|
uuid: videoChannelObject.uuid,
|
|
|
|
url: videoChannelObject.id,
|
|
|
|
createdAt: new Date(videoChannelObject.published),
|
|
|
|
updatedAt: new Date(videoChannelObject.updated),
|
|
|
|
remote: true,
|
|
|
|
accountId: account.id
|
|
|
|
}
|
|
|
|
}
|
2017-11-10 08:34:45 -05:00
|
|
|
|
2017-11-10 11:27:49 -05:00
|
|
|
async function videoActivityObjectToDBAttributes (
|
2017-12-12 11:53:50 -05:00
|
|
|
videoChannel: VideoChannelModel,
|
2017-11-17 09:20:42 -05:00
|
|
|
videoObject: VideoTorrentObject,
|
|
|
|
to: string[] = [],
|
|
|
|
cc: string[] = []
|
2017-11-10 11:27:49 -05:00
|
|
|
) {
|
2017-11-17 09:20:42 -05:00
|
|
|
let privacy = VideoPrivacy.PRIVATE
|
|
|
|
if (to.indexOf(ACTIVITY_PUB.PUBLIC) !== -1) privacy = VideoPrivacy.PUBLIC
|
|
|
|
else if (cc.indexOf(ACTIVITY_PUB.PUBLIC) !== -1) privacy = VideoPrivacy.UNLISTED
|
|
|
|
|
2017-11-10 08:34:45 -05:00
|
|
|
const duration = videoObject.duration.replace(/[^\d]+/, '')
|
2017-11-22 10:25:03 -05:00
|
|
|
let language = null
|
|
|
|
if (videoObject.language) {
|
|
|
|
language = parseInt(videoObject.language.identifier, 10)
|
|
|
|
}
|
|
|
|
|
2017-12-08 11:31:21 -05:00
|
|
|
let category = null
|
|
|
|
if (videoObject.category) {
|
|
|
|
category = parseInt(videoObject.category.identifier, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
let licence = null
|
|
|
|
if (videoObject.licence) {
|
|
|
|
licence = parseInt(videoObject.licence.identifier, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
let description = null
|
|
|
|
if (videoObject.content) {
|
|
|
|
description = videoObject.content
|
|
|
|
}
|
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
return {
|
2017-11-10 08:34:45 -05:00
|
|
|
name: videoObject.name,
|
|
|
|
uuid: videoObject.uuid,
|
|
|
|
url: videoObject.id,
|
2017-12-08 11:31:21 -05:00
|
|
|
category,
|
|
|
|
licence,
|
2017-11-22 10:25:03 -05:00
|
|
|
language,
|
2017-12-08 11:31:21 -05:00
|
|
|
description,
|
2017-11-10 08:34:45 -05:00
|
|
|
nsfw: videoObject.nsfw,
|
|
|
|
channelId: videoChannel.id,
|
|
|
|
duration: parseInt(duration, 10),
|
2017-11-16 05:08:25 -05:00
|
|
|
createdAt: new Date(videoObject.published),
|
2017-11-10 08:34:45 -05:00
|
|
|
// FIXME: updatedAt does not seems to be considered by Sequelize
|
2017-11-16 05:08:25 -05:00
|
|
|
updatedAt: new Date(videoObject.updated),
|
2017-11-10 08:34:45 -05:00
|
|
|
views: videoObject.views,
|
|
|
|
likes: 0,
|
|
|
|
dislikes: 0,
|
|
|
|
remote: true,
|
2017-11-17 09:20:42 -05:00
|
|
|
privacy
|
2017-11-10 08:34:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
function videoFileActivityUrlToDBAttributes (videoCreated: VideoModel, videoObject: VideoTorrentObject) {
|
2017-11-16 09:22:39 -05:00
|
|
|
const mimeTypes = Object.keys(VIDEO_MIMETYPE_EXT)
|
|
|
|
const fileUrls = videoObject.url.filter(u => {
|
|
|
|
return mimeTypes.indexOf(u.mimeType) !== -1 && u.mimeType.startsWith('video/')
|
|
|
|
})
|
|
|
|
|
|
|
|
if (fileUrls.length === 0) {
|
|
|
|
throw new Error('Cannot find video files for ' + videoCreated.url)
|
|
|
|
}
|
2017-11-10 08:34:45 -05:00
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
const attributes = []
|
2017-11-16 09:22:39 -05:00
|
|
|
for (const fileUrl of fileUrls) {
|
2017-11-10 08:34:45 -05:00
|
|
|
// Fetch associated magnet uri
|
2017-11-16 09:22:39 -05:00
|
|
|
const magnet = videoObject.url.find(u => {
|
|
|
|
return u.mimeType === 'application/x-bittorrent;x-scheme-handler/magnet' && u.width === fileUrl.width
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!magnet) throw new Error('Cannot find associated magnet uri for file ' + fileUrl.url)
|
2017-11-10 08:34:45 -05:00
|
|
|
|
|
|
|
const parsed = magnetUtil.decode(magnet.url)
|
|
|
|
if (!parsed || isVideoFileInfoHashValid(parsed.infoHash) === false) throw new Error('Cannot parse magnet URI ' + magnet.url)
|
|
|
|
|
|
|
|
const attribute = {
|
2017-11-16 09:22:39 -05:00
|
|
|
extname: VIDEO_MIMETYPE_EXT[fileUrl.mimeType],
|
2017-11-10 08:34:45 -05:00
|
|
|
infoHash: parsed.infoHash,
|
2017-11-16 09:22:39 -05:00
|
|
|
resolution: fileUrl.width,
|
|
|
|
size: fileUrl.size,
|
2017-11-10 08:34:45 -05:00
|
|
|
videoId: videoCreated.id
|
|
|
|
}
|
|
|
|
attributes.push(attribute)
|
|
|
|
}
|
|
|
|
|
|
|
|
return attributes
|
|
|
|
}
|
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
async function addVideoShares (instance: VideoModel, shares: string[]) {
|
2017-11-27 08:44:51 -05:00
|
|
|
for (const share of shares) {
|
|
|
|
// Fetch url
|
|
|
|
const json = await doRequest({
|
|
|
|
uri: share,
|
|
|
|
json: true
|
|
|
|
})
|
|
|
|
const actor = json['actor']
|
|
|
|
if (!actor) continue
|
|
|
|
|
|
|
|
const account = await getOrCreateAccountAndServer(actor)
|
|
|
|
|
|
|
|
const entry = {
|
|
|
|
accountId: account.id,
|
|
|
|
videoId: instance.id
|
|
|
|
}
|
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
await VideoShareModel.findOrCreate({
|
2017-11-27 08:44:51 -05:00
|
|
|
where: entry,
|
|
|
|
defaults: entry
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
async function addVideoChannelShares (instance: VideoChannelModel, shares: string[]) {
|
2017-11-27 08:44:51 -05:00
|
|
|
for (const share of shares) {
|
|
|
|
// Fetch url
|
|
|
|
const json = await doRequest({
|
|
|
|
uri: share,
|
|
|
|
json: true
|
|
|
|
})
|
|
|
|
const actor = json['actor']
|
|
|
|
if (!actor) continue
|
|
|
|
|
|
|
|
const account = await getOrCreateAccountAndServer(actor)
|
|
|
|
|
|
|
|
const entry = {
|
|
|
|
accountId: account.id,
|
|
|
|
videoChannelId: instance.id
|
|
|
|
}
|
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
await VideoChannelShareModel.findOrCreate({
|
2017-11-27 08:44:51 -05:00
|
|
|
where: entry,
|
|
|
|
defaults: entry
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 08:34:45 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
videoFileActivityUrlToDBAttributes,
|
2017-11-16 09:22:39 -05:00
|
|
|
videoActivityObjectToDBAttributes,
|
2017-11-27 08:44:51 -05:00
|
|
|
videoChannelActivityObjectToDBAttributes,
|
|
|
|
addVideoChannelShares,
|
|
|
|
addVideoShares
|
2017-11-10 08:34:45 -05:00
|
|
|
}
|