2017-11-10 14:34:45 +01:00
|
|
|
import { VideoTorrentObject } from '../../../shared'
|
|
|
|
import { ActivityAdd } from '../../../shared/models/activitypub/activity'
|
|
|
|
import { generateThumbnailFromUrl, logger, retryTransactionWrapper, getOrCreateAccount } from '../../helpers'
|
|
|
|
import { database as db } from '../../initializers'
|
|
|
|
import { AccountInstance } from '../../models/account/account-interface'
|
|
|
|
import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc'
|
|
|
|
import Bluebird = require('bluebird')
|
2017-11-16 15:22:39 +01:00
|
|
|
import { getOrCreateVideoChannel } from '../../helpers/activitypub'
|
|
|
|
import { VideoChannelInstance } from '../../models/video/video-channel-interface'
|
2017-11-10 14:34:45 +01:00
|
|
|
|
|
|
|
async function processAddActivity (activity: ActivityAdd) {
|
|
|
|
const activityObject = activity.object
|
|
|
|
const activityType = activityObject.type
|
|
|
|
const account = await getOrCreateAccount(activity.actor)
|
|
|
|
|
|
|
|
if (activityType === 'Video') {
|
2017-11-16 15:22:39 +01:00
|
|
|
const videoChannelUrl = activity.target
|
|
|
|
const videoChannel = await getOrCreateVideoChannel(account, videoChannelUrl)
|
|
|
|
|
|
|
|
return processAddVideo(account, videoChannel, activityObject as VideoTorrentObject)
|
2017-11-10 14:34:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
|
|
|
|
return Promise.resolve(undefined)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
processAddActivity
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-11-16 15:22:39 +01:00
|
|
|
function processAddVideo (account: AccountInstance, videoChannel: VideoChannelInstance, video: VideoTorrentObject) {
|
2017-11-10 14:34:45 +01:00
|
|
|
const options = {
|
2017-11-16 15:22:39 +01:00
|
|
|
arguments: [ account, videoChannel, video ],
|
2017-11-10 14:34:45 +01:00
|
|
|
errorMessage: 'Cannot insert the remote video with many retries.'
|
|
|
|
}
|
|
|
|
|
|
|
|
return retryTransactionWrapper(addRemoteVideo, options)
|
|
|
|
}
|
|
|
|
|
2017-11-16 15:22:39 +01:00
|
|
|
function addRemoteVideo (account: AccountInstance, videoChannel: VideoChannelInstance, videoToCreateData: VideoTorrentObject) {
|
2017-11-10 14:34:45 +01:00
|
|
|
logger.debug('Adding remote video %s.', videoToCreateData.url)
|
|
|
|
|
2017-11-15 17:56:21 +01:00
|
|
|
return db.sequelize.transaction(async t => {
|
2017-11-10 14:34:45 +01:00
|
|
|
const sequelizeOptions = {
|
|
|
|
transaction: t
|
|
|
|
}
|
|
|
|
|
|
|
|
if (videoChannel.Account.id !== account.id) throw new Error('Video channel is not owned by this account.')
|
|
|
|
|
|
|
|
const videoData = await videoActivityObjectToDBAttributes(videoChannel, videoToCreateData, t)
|
|
|
|
const video = db.Video.build(videoData)
|
|
|
|
|
|
|
|
// Don't block on request
|
|
|
|
generateThumbnailFromUrl(video, videoToCreateData.icon)
|
2017-11-14 17:31:26 +01:00
|
|
|
.catch(err => logger.warn('Cannot generate thumbnail of %s.', videoToCreateData.id, err))
|
2017-11-10 14:34:45 +01:00
|
|
|
|
|
|
|
const videoCreated = await video.save(sequelizeOptions)
|
|
|
|
|
|
|
|
const videoFileAttributes = await videoFileActivityUrlToDBAttributes(videoCreated, videoToCreateData)
|
2017-11-16 15:22:39 +01:00
|
|
|
if (videoFileAttributes.length === 0) {
|
|
|
|
throw new Error('Cannot find valid files for video %s ' + videoToCreateData.url)
|
|
|
|
}
|
2017-11-10 14:34:45 +01:00
|
|
|
|
2017-11-16 15:22:39 +01:00
|
|
|
const tasks: Bluebird<any>[] = videoFileAttributes.map(f => db.VideoFile.create(f, { transaction: t }))
|
2017-11-10 14:34:45 +01:00
|
|
|
await Promise.all(tasks)
|
|
|
|
|
|
|
|
const tags = videoToCreateData.tag.map(t => t.name)
|
|
|
|
const tagInstances = await db.Tag.findOrCreateTags(tags, t)
|
|
|
|
await videoCreated.setTags(tagInstances, sequelizeOptions)
|
2017-11-15 17:56:21 +01:00
|
|
|
|
|
|
|
logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid)
|
|
|
|
|
|
|
|
return videoCreated
|
2017-11-10 14:34:45 +01:00
|
|
|
})
|
|
|
|
}
|