2017-11-16 08:55:01 -06:00
|
|
|
import * as Bluebird from 'bluebird'
|
2017-11-20 02:43:39 -06:00
|
|
|
import { VideoTorrentObject } from '../../../../shared'
|
|
|
|
import { ActivityAdd } from '../../../../shared/models/activitypub/activity'
|
2017-11-23 09:55:13 -06:00
|
|
|
import { VideoRateType } from '../../../../shared/models/videos/video-rate.type'
|
2017-11-20 03:24:29 -06:00
|
|
|
import { retryTransactionWrapper } from '../../../helpers/database-utils'
|
|
|
|
import { logger } from '../../../helpers/logger'
|
2017-11-20 02:43:39 -06:00
|
|
|
import { database as db } from '../../../initializers'
|
|
|
|
import { AccountInstance } from '../../../models/account/account-interface'
|
|
|
|
import { VideoChannelInstance } from '../../../models/video/video-channel-interface'
|
2017-11-23 09:55:13 -06:00
|
|
|
import { VideoInstance } from '../../../models/video/video-interface'
|
2017-11-21 06:43:29 -06:00
|
|
|
import { getOrCreateAccountAndServer } from '../account'
|
2017-11-20 03:24:29 -06:00
|
|
|
import { getOrCreateVideoChannel } from '../video-channels'
|
|
|
|
import { generateThumbnailFromUrl } from '../videos'
|
2017-11-27 07:44:51 -06:00
|
|
|
import { addVideoShares, videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc'
|
2017-11-10 07:34:45 -06:00
|
|
|
|
|
|
|
async function processAddActivity (activity: ActivityAdd) {
|
|
|
|
const activityObject = activity.object
|
|
|
|
const activityType = activityObject.type
|
2017-11-21 06:43:29 -06:00
|
|
|
const account = await getOrCreateAccountAndServer(activity.actor)
|
2017-11-10 07:34:45 -06:00
|
|
|
|
|
|
|
if (activityType === 'Video') {
|
2017-11-16 08:22:39 -06:00
|
|
|
const videoChannelUrl = activity.target
|
|
|
|
const videoChannel = await getOrCreateVideoChannel(account, videoChannelUrl)
|
|
|
|
|
2017-11-17 08:52:26 -06:00
|
|
|
return processAddVideo(account, activity, videoChannel, activityObject)
|
2017-11-10 07:34:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
|
|
|
|
return Promise.resolve(undefined)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
processAddActivity
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-11-27 07:44:51 -06:00
|
|
|
async function processAddVideo (account: AccountInstance,
|
|
|
|
activity: ActivityAdd,
|
|
|
|
videoChannel: VideoChannelInstance,
|
|
|
|
videoToCreateData: VideoTorrentObject) {
|
2017-11-10 07:34:45 -06:00
|
|
|
const options = {
|
2017-11-23 09:55:13 -06:00
|
|
|
arguments: [ account, activity, videoChannel, videoToCreateData ],
|
2017-11-10 07:34:45 -06:00
|
|
|
errorMessage: 'Cannot insert the remote video with many retries.'
|
|
|
|
}
|
|
|
|
|
2017-11-23 09:55:13 -06:00
|
|
|
const video = await retryTransactionWrapper(addRemoteVideo, options)
|
|
|
|
|
|
|
|
// Process outside the transaction because we could fetch remote data
|
|
|
|
if (videoToCreateData.likes && Array.isArray(videoToCreateData.likes.orderedItems)) {
|
|
|
|
await createRates(videoToCreateData.likes.orderedItems, video, 'like')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (videoToCreateData.dislikes && Array.isArray(videoToCreateData.dislikes.orderedItems)) {
|
|
|
|
await createRates(videoToCreateData.dislikes.orderedItems, video, 'dislike')
|
|
|
|
}
|
|
|
|
|
2017-11-27 07:44:51 -06:00
|
|
|
if (videoToCreateData.shares && Array.isArray(videoToCreateData.shares.orderedItems)) {
|
|
|
|
await addVideoShares(video, videoToCreateData.shares.orderedItems)
|
|
|
|
}
|
|
|
|
|
2017-11-23 09:55:13 -06:00
|
|
|
return video
|
2017-11-10 07:34:45 -06:00
|
|
|
}
|
|
|
|
|
2017-11-20 03:24:29 -06:00
|
|
|
function addRemoteVideo (account: AccountInstance,
|
|
|
|
activity: ActivityAdd,
|
|
|
|
videoChannel: VideoChannelInstance,
|
|
|
|
videoToCreateData: VideoTorrentObject) {
|
2017-11-22 04:27:40 -06:00
|
|
|
logger.debug('Adding remote video %s.', videoToCreateData.id)
|
2017-11-10 07:34:45 -06:00
|
|
|
|
2017-11-15 10:56:21 -06:00
|
|
|
return db.sequelize.transaction(async t => {
|
2017-11-10 07:34:45 -06:00
|
|
|
const sequelizeOptions = {
|
|
|
|
transaction: t
|
|
|
|
}
|
|
|
|
|
|
|
|
if (videoChannel.Account.id !== account.id) throw new Error('Video channel is not owned by this account.')
|
|
|
|
|
2017-11-16 08:55:01 -06:00
|
|
|
const videoFromDatabase = await db.Video.loadByUUIDOrURL(videoToCreateData.uuid, videoToCreateData.id, t)
|
2017-11-24 06:41:10 -06:00
|
|
|
if (videoFromDatabase) return videoFromDatabase
|
2017-11-16 08:55:01 -06:00
|
|
|
|
2017-11-17 08:20:42 -06:00
|
|
|
const videoData = await videoActivityObjectToDBAttributes(videoChannel, videoToCreateData, activity.to, activity.cc)
|
2017-11-10 07:34:45 -06:00
|
|
|
const video = db.Video.build(videoData)
|
|
|
|
|
|
|
|
// Don't block on request
|
|
|
|
generateThumbnailFromUrl(video, videoToCreateData.icon)
|
2017-11-14 10:31:26 -06:00
|
|
|
.catch(err => logger.warn('Cannot generate thumbnail of %s.', videoToCreateData.id, err))
|
2017-11-10 07:34:45 -06:00
|
|
|
|
|
|
|
const videoCreated = await video.save(sequelizeOptions)
|
|
|
|
|
2017-11-17 08:52:26 -06:00
|
|
|
const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoCreated, videoToCreateData)
|
2017-11-16 08:22:39 -06:00
|
|
|
if (videoFileAttributes.length === 0) {
|
|
|
|
throw new Error('Cannot find valid files for video %s ' + videoToCreateData.url)
|
|
|
|
}
|
2017-11-10 07:34:45 -06:00
|
|
|
|
2017-11-16 08:22:39 -06:00
|
|
|
const tasks: Bluebird<any>[] = videoFileAttributes.map(f => db.VideoFile.create(f, { transaction: t }))
|
2017-11-10 07:34:45 -06:00
|
|
|
await Promise.all(tasks)
|
|
|
|
|
|
|
|
const tags = videoToCreateData.tag.map(t => t.name)
|
|
|
|
const tagInstances = await db.Tag.findOrCreateTags(tags, t)
|
|
|
|
await videoCreated.setTags(tagInstances, sequelizeOptions)
|
2017-11-15 10:56:21 -06:00
|
|
|
|
|
|
|
logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid)
|
|
|
|
|
|
|
|
return videoCreated
|
2017-11-10 07:34:45 -06:00
|
|
|
})
|
|
|
|
}
|
2017-11-23 09:55:13 -06:00
|
|
|
|
|
|
|
async function createRates (accountUrls: string[], video: VideoInstance, rate: VideoRateType) {
|
|
|
|
let rateCounts = 0
|
|
|
|
const tasks: Bluebird<any>[] = []
|
|
|
|
|
|
|
|
for (const accountUrl of accountUrls) {
|
|
|
|
const account = await getOrCreateAccountAndServer(accountUrl)
|
|
|
|
const p = db.AccountVideoRate
|
|
|
|
.create({
|
|
|
|
videoId: video.id,
|
|
|
|
accountId: 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
|
|
|
|
}
|