-function createRemoteVideo (
- account: ActorModel,
- activity: ActivityCreate,
- videoToCreateData: VideoTorrentObject,
- channelActor: ActorModel
-) {
- logger.debug('Adding remote video %s.', videoToCreateData.id)
-
- return sequelizeTypescript.transaction(async t => {
- const sequelizeOptions = {
- transaction: t
- }
- const videoFromDatabase = await VideoModel.loadByUUIDOrURL(videoToCreateData.uuid, videoToCreateData.id, t)
- if (videoFromDatabase) return videoFromDatabase
-
- const videoData = await videoActivityObjectToDBAttributes(channelActor.VideoChannel, videoToCreateData, activity.to, activity.cc)
- const video = VideoModel.build(videoData)
-
- // Don't block on request
- generateThumbnailFromUrl(video, videoToCreateData.icon)
- .catch(err => logger.warn('Cannot generate thumbnail of %s.', videoToCreateData.id, err))
-
- const videoCreated = await video.save(sequelizeOptions)
-
- const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoCreated, videoToCreateData)
- if (videoFileAttributes.length === 0) {
- throw new Error('Cannot find valid files for video %s ' + videoToCreateData.url)
- }
-
- const tasks: Bluebird<any>[] = videoFileAttributes.map(f => VideoFileModel.create(f, { transaction: t }))
- await Promise.all(tasks)
-
- const tags = videoToCreateData.tag.map(t => t.name)
- const tagInstances = await TagModel.findOrCreateTags(tags, t)
- await videoCreated.$set('Tags', tagInstances, sequelizeOptions)
-
- logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid)
-
- return videoCreated
- })
-}
-
-async function createRates (actorUrls: string[], video: VideoModel, rate: VideoRateType) {
- let rateCounts = 0
- const tasks: Bluebird<any>[] = []
-
- for (const actorUrl of actorUrls) {
- const actor = await getOrCreateActorAndServerAndModel(actorUrl)
- const p = AccountVideoRateModel
- .create({
- videoId: video.id,
- accountId: actor.Account.id,
- type: rate
- })
- .then(() => rateCounts += 1)
-
- tasks.push(p)
- }
-
- await Promise.all(tasks)
-
- logger.info('Adding %d %s to video %s.', rateCounts, rate, video.uuid)
-
- // This is "likes" and "dislikes"
- await video.increment(rate + 's', { by: rateCounts })
-
- return
-}
-
-async function processCreateDislike (byActor: ActorModel, activity: ActivityCreate) {
- const options = {
- arguments: [ byActor, activity ],
- errorMessage: 'Cannot dislike the video with many retries.'
- }
-
- return retryTransactionWrapper(createVideoDislike, options)
-}
-
-function createVideoDislike (byActor: ActorModel, activity: ActivityCreate) {
- const dislike = activity.object as DislikeObject
- const byAccount = byActor.Account
-
- if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url)