1 import * as Bluebird from 'bluebird'
2 import { VideoTorrentObject } from '../../../../shared'
3 import { ActivityAdd } from '../../../../shared/models/activitypub'
4 import { VideoRateType } from '../../../../shared/models/videos'
5 import { logger, retryTransactionWrapper } from '../../../helpers'
6 import { sequelizeTypescript } from '../../../initializers'
7 import { AccountModel } from '../../../models/account/account'
8 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
9 import { TagModel } from '../../../models/video/tag'
10 import { VideoModel } from '../../../models/video/video'
11 import { VideoChannelModel } from '../../../models/video/video-channel'
12 import { VideoFileModel } from '../../../models/video/video-file'
13 import { getOrCreateAccountAndServer } from '../account'
14 import { getOrCreateVideoChannel } from '../video-channels'
15 import { generateThumbnailFromUrl } from '../videos'
16 import { addVideoShares, videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc'
18 async function processAddActivity (activity: ActivityAdd) {
19 const activityObject = activity.object
20 const activityType = activityObject.type
21 const account = await getOrCreateAccountAndServer(activity.actor)
23 if (activityType === 'Video') {
24 const videoChannelUrl = activity.target
25 const videoChannel = await getOrCreateVideoChannel(account, videoChannelUrl)
27 return processAddVideo(account, activity, videoChannel, activityObject)
30 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
31 return Promise.resolve(undefined)
34 // ---------------------------------------------------------------------------
40 // ---------------------------------------------------------------------------
42 async function processAddVideo (account: AccountModel,
43 activity: ActivityAdd,
44 videoChannel: VideoChannelModel,
45 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')
62 if (videoToCreateData.shares && Array.isArray(videoToCreateData.shares.orderedItems)) {
63 await addVideoShares(video, videoToCreateData.shares.orderedItems)
69 function addRemoteVideo (account: AccountModel,
70 activity: ActivityAdd,
71 videoChannel: VideoChannelModel,
72 videoToCreateData: VideoTorrentObject) {
73 logger.debug('Adding remote video %s.', videoToCreateData.id)
75 return sequelizeTypescript.transaction(async t => {
76 const sequelizeOptions = {
80 if (videoChannel.Account.id !== account.id) throw new Error('Video channel is not owned by this account.')
82 const videoFromDatabase = await VideoModel.loadByUUIDOrURL(videoToCreateData.uuid, videoToCreateData.id, t)
83 if (videoFromDatabase) return videoFromDatabase
85 const videoData = await videoActivityObjectToDBAttributes(videoChannel, videoToCreateData, activity.to, activity.cc)
86 const video = VideoModel.build(videoData)
88 // Don't block on request
89 generateThumbnailFromUrl(video, videoToCreateData.icon)
90 .catch(err => logger.warn('Cannot generate thumbnail of %s.', videoToCreateData.id, err))
92 const videoCreated = await video.save(sequelizeOptions)
94 const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoCreated, videoToCreateData)
95 if (videoFileAttributes.length === 0) {
96 throw new Error('Cannot find valid files for video %s ' + videoToCreateData.url)
99 const tasks: Bluebird<any>[] = videoFileAttributes.map(f => VideoFileModel.create(f, { transaction: t }))
100 await Promise.all(tasks)
102 const tags = videoToCreateData.tag.map(t => t.name)
103 const tagInstances = await TagModel.findOrCreateTags(tags, t)
104 await videoCreated.$set('Tags', tagInstances, sequelizeOptions)
106 logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid)
112 async function createRates (accountUrls: string[], video: VideoModel, rate: VideoRateType) {
114 const tasks: Bluebird<any>[] = []
116 for (const accountUrl of accountUrls) {
117 const account = await getOrCreateAccountAndServer(accountUrl)
118 const p = AccountVideoRateModel
121 accountId: account.id,
124 .then(() => rateCounts += 1)
129 await Promise.all(tasks)
131 logger.info('Adding %d %s to video %s.', rateCounts, rate, video.uuid)
133 // This is "likes" and "dislikes"
134 await video.increment(rate + 's', { by: rateCounts })