1 import * as Bluebird from 'bluebird'
2 import { VideoTorrentObject } from '../../../../shared'
3 import { ActivityAdd } from '../../../../shared/models/activitypub/activity'
4 import { retryTransactionWrapper } from '../../../helpers/database-utils'
5 import { logger } from '../../../helpers/logger'
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 { getOrCreateAccountAndServer } from '../account'
10 import { getOrCreateVideoChannel } from '../video-channels'
11 import { generateThumbnailFromUrl } from '../videos'
12 import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc'
14 async function processAddActivity (activity: ActivityAdd) {
15 const activityObject = activity.object
16 const activityType = activityObject.type
17 const account = await getOrCreateAccountAndServer(activity.actor)
19 if (activityType === 'Video') {
20 const videoChannelUrl = activity.target
21 const videoChannel = await getOrCreateVideoChannel(account, videoChannelUrl)
23 return processAddVideo(account, activity, videoChannel, activityObject)
26 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
27 return Promise.resolve(undefined)
30 // ---------------------------------------------------------------------------
36 // ---------------------------------------------------------------------------
38 function processAddVideo (account: AccountInstance, activity: ActivityAdd, videoChannel: VideoChannelInstance, video: VideoTorrentObject) {
40 arguments: [ account, activity, videoChannel, video ],
41 errorMessage: 'Cannot insert the remote video with many retries.'
44 return retryTransactionWrapper(addRemoteVideo, options)
47 function addRemoteVideo (account: AccountInstance,
48 activity: ActivityAdd,
49 videoChannel: VideoChannelInstance,
50 videoToCreateData: VideoTorrentObject) {
51 logger.debug('Adding remote video %s.', videoToCreateData.url)
53 return db.sequelize.transaction(async t => {
54 const sequelizeOptions = {
58 if (videoChannel.Account.id !== account.id) throw new Error('Video channel is not owned by this account.')
60 const videoFromDatabase = await db.Video.loadByUUIDOrURL(videoToCreateData.uuid, videoToCreateData.id, t)
61 if (videoFromDatabase) throw new Error('Video with this UUID/Url already exists.')
63 const videoData = await videoActivityObjectToDBAttributes(videoChannel, videoToCreateData, activity.to, activity.cc)
64 const video = db.Video.build(videoData)
66 // Don't block on request
67 generateThumbnailFromUrl(video, videoToCreateData.icon)
68 .catch(err => logger.warn('Cannot generate thumbnail of %s.', videoToCreateData.id, err))
70 const videoCreated = await video.save(sequelizeOptions)
72 const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoCreated, videoToCreateData)
73 if (videoFileAttributes.length === 0) {
74 throw new Error('Cannot find valid files for video %s ' + videoToCreateData.url)
77 const tasks: Bluebird<any>[] = videoFileAttributes.map(f => db.VideoFile.create(f, { transaction: t }))
78 await Promise.all(tasks)
80 const tags = videoToCreateData.tag.map(t => t.name)
81 const tagInstances = await db.Tag.findOrCreateTags(tags, t)
82 await videoCreated.setTags(tagInstances, sequelizeOptions)
84 logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid)