1 import * as Bluebird from 'bluebird'
2 import { VideoTorrentObject } from '../../../../shared'
3 import { ActivityAdd } from '../../../../shared/models/activitypub/activity'
4 import { VideoRateType } from '../../../../shared/models/videos/video-rate.type'
5 import { retryTransactionWrapper } from '../../../helpers/database-utils'
6 import { logger } from '../../../helpers/logger'
7 import { database as db } from '../../../initializers'
8 import { AccountInstance } from '../../../models/account/account-interface'
9 import { VideoChannelInstance } from '../../../models/video/video-channel-interface'
10 import { VideoInstance } from '../../../models/video/video-interface'
11 import { getOrCreateAccountAndServer } from '../account'
12 import { getOrCreateVideoChannel } from '../video-channels'
13 import { generateThumbnailFromUrl } from '../videos'
14 import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc'
16 async function processAddActivity (activity: ActivityAdd) {
17 const activityObject = activity.object
18 const activityType = activityObject.type
19 const account = await getOrCreateAccountAndServer(activity.actor)
21 if (activityType === 'Video') {
22 const videoChannelUrl = activity.target
23 const videoChannel = await getOrCreateVideoChannel(account, videoChannelUrl)
25 return processAddVideo(account, activity, videoChannel, activityObject)
28 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
29 return Promise.resolve(undefined)
32 // ---------------------------------------------------------------------------
38 // ---------------------------------------------------------------------------
40 async function processAddVideo (
41 account: AccountInstance,
42 activity: ActivityAdd,
43 videoChannel: VideoChannelInstance,
44 videoToCreateData: VideoTorrentObject
47 arguments: [ account, activity, videoChannel, videoToCreateData ],
48 errorMessage: 'Cannot insert the remote video with many retries.'
51 const video = await retryTransactionWrapper(addRemoteVideo, options)
53 // Process outside the transaction because we could fetch remote data
54 if (videoToCreateData.likes && Array.isArray(videoToCreateData.likes.orderedItems)) {
55 await createRates(videoToCreateData.likes.orderedItems, video, 'like')
58 if (videoToCreateData.dislikes && Array.isArray(videoToCreateData.dislikes.orderedItems)) {
59 await createRates(videoToCreateData.dislikes.orderedItems, video, 'dislike')
65 function addRemoteVideo (account: AccountInstance,
66 activity: ActivityAdd,
67 videoChannel: VideoChannelInstance,
68 videoToCreateData: VideoTorrentObject) {
69 logger.debug('Adding remote video %s.', videoToCreateData.id)
71 return db.sequelize.transaction(async t => {
72 const sequelizeOptions = {
76 if (videoChannel.Account.id !== account.id) throw new Error('Video channel is not owned by this account.')
78 const videoFromDatabase = await db.Video.loadByUUIDOrURL(videoToCreateData.uuid, videoToCreateData.id, t)
79 if (videoFromDatabase) throw new Error('Video with this UUID/Url already exists.')
81 const videoData = await videoActivityObjectToDBAttributes(videoChannel, videoToCreateData, activity.to, activity.cc)
82 const video = db.Video.build(videoData)
84 // Don't block on request
85 generateThumbnailFromUrl(video, videoToCreateData.icon)
86 .catch(err => logger.warn('Cannot generate thumbnail of %s.', videoToCreateData.id, err))
88 const videoCreated = await video.save(sequelizeOptions)
90 const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoCreated, videoToCreateData)
91 if (videoFileAttributes.length === 0) {
92 throw new Error('Cannot find valid files for video %s ' + videoToCreateData.url)
95 const tasks: Bluebird<any>[] = videoFileAttributes.map(f => db.VideoFile.create(f, { transaction: t }))
96 await Promise.all(tasks)
98 const tags = videoToCreateData.tag.map(t => t.name)
99 const tagInstances = await db.Tag.findOrCreateTags(tags, t)
100 await videoCreated.setTags(tagInstances, sequelizeOptions)
102 logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid)
108 async function createRates (accountUrls: string[], video: VideoInstance, rate: VideoRateType) {
110 const tasks: Bluebird<any>[] = []
112 for (const accountUrl of accountUrls) {
113 const account = await getOrCreateAccountAndServer(accountUrl)
114 const p = db.AccountVideoRate
117 accountId: account.id,
120 .then(() => rateCounts += 1)
125 await Promise.all(tasks)
127 logger.info('Adding %d %s to video %s.', rateCounts, rate, video.uuid)
129 // This is "likes" and "dislikes"
130 await video.increment(rate + 's', { by: rateCounts })