1 import * as Bluebird from 'bluebird'
2 import { VideoTorrentObject } from '../../../shared'
3 import { ActivityAdd } from '../../../shared/models/activitypub/activity'
4 import { generateThumbnailFromUrl, getOrCreateAccount, logger, retryTransactionWrapper } from '../../helpers'
5 import { getOrCreateVideoChannel } from '../../helpers/activitypub'
6 import { database as db } from '../../initializers'
7 import { AccountInstance } from '../../models/account/account-interface'
8 import { VideoChannelInstance } from '../../models/video/video-channel-interface'
9 import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc'
11 async function processAddActivity (activity: ActivityAdd) {
12 const activityObject = activity.object
13 const activityType = activityObject.type
14 const account = await getOrCreateAccount(activity.actor)
16 if (activityType === 'Video') {
17 const videoChannelUrl = activity.target
18 const videoChannel = await getOrCreateVideoChannel(account, videoChannelUrl)
20 return processAddVideo(account, videoChannel, activityObject as VideoTorrentObject)
23 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
24 return Promise.resolve(undefined)
27 // ---------------------------------------------------------------------------
33 // ---------------------------------------------------------------------------
35 function processAddVideo (account: AccountInstance, videoChannel: VideoChannelInstance, video: VideoTorrentObject) {
37 arguments: [ account, videoChannel, video ],
38 errorMessage: 'Cannot insert the remote video with many retries.'
41 return retryTransactionWrapper(addRemoteVideo, options)
44 function addRemoteVideo (account: AccountInstance, videoChannel: VideoChannelInstance, videoToCreateData: VideoTorrentObject) {
45 logger.debug('Adding remote video %s.', videoToCreateData.url)
47 return db.sequelize.transaction(async t => {
48 const sequelizeOptions = {
52 if (videoChannel.Account.id !== account.id) throw new Error('Video channel is not owned by this account.')
54 const videoFromDatabase = await db.Video.loadByUUIDOrURL(videoToCreateData.uuid, videoToCreateData.id, t)
55 if (videoFromDatabase) throw new Error('Video with this UUID/Url already exists.')
57 const videoData = await videoActivityObjectToDBAttributes(videoChannel, videoToCreateData)
58 const video = db.Video.build(videoData)
60 // Don't block on request
61 generateThumbnailFromUrl(video, videoToCreateData.icon)
62 .catch(err => logger.warn('Cannot generate thumbnail of %s.', videoToCreateData.id, err))
64 const videoCreated = await video.save(sequelizeOptions)
66 const videoFileAttributes = await videoFileActivityUrlToDBAttributes(videoCreated, videoToCreateData)
67 if (videoFileAttributes.length === 0) {
68 throw new Error('Cannot find valid files for video %s ' + videoToCreateData.url)
71 const tasks: Bluebird<any>[] = videoFileAttributes.map(f => db.VideoFile.create(f, { transaction: t }))
72 await Promise.all(tasks)
74 const tags = videoToCreateData.tag.map(t => t.name)
75 const tagInstances = await db.Tag.findOrCreateTags(tags, t)
76 await videoCreated.setTags(tagInstances, sequelizeOptions)
78 logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid)