1 import * as Bluebird from 'bluebird'
2 import { VideoChannelObject, VideoTorrentObject } from '../../../../shared'
3 import { ActivityUpdate } from '../../../../shared/models/activitypub'
4 import { logger, resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers'
5 import { sequelizeTypescript } from '../../../initializers'
6 import { AccountModel } from '../../../models/account/account'
7 import { TagModel } from '../../../models/video/tag'
8 import { VideoModel } from '../../../models/video/video'
9 import { VideoChannelModel } from '../../../models/video/video-channel'
10 import { VideoFileModel } from '../../../models/video/video-file'
11 import { getOrCreateAccountAndServer } from '../account'
12 import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc'
14 async function processUpdateActivity (activity: ActivityUpdate) {
15 const account = await getOrCreateAccountAndServer(activity.actor)
17 if (activity.object.type === 'Video') {
18 return processUpdateVideo(account, activity.object)
19 } else if (activity.object.type === 'VideoChannel') {
20 return processUpdateVideoChannel(account, activity.object)
26 // ---------------------------------------------------------------------------
32 // ---------------------------------------------------------------------------
34 function processUpdateVideo (account: AccountModel, video: VideoTorrentObject) {
36 arguments: [ account, video ],
37 errorMessage: 'Cannot update the remote video with many retries'
40 return retryTransactionWrapper(updateRemoteVideo, options)
43 async function updateRemoteVideo (account: AccountModel, videoAttributesToUpdate: VideoTorrentObject) {
44 logger.debug('Updating remote video "%s".', videoAttributesToUpdate.uuid)
45 let videoInstance: VideoModel
46 let videoFieldsSave: object
49 await sequelizeTypescript.transaction(async t => {
50 const sequelizeOptions = {
54 const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(videoAttributesToUpdate.id, t)
55 if (!videoInstance) throw new Error('Video ' + videoAttributesToUpdate.id + ' not found.')
57 if (videoInstance.VideoChannel.Account.id !== account.id) {
58 throw new Error('Account ' + account.url + ' does not own video channel ' + videoInstance.VideoChannel.url)
61 const videoData = await videoActivityObjectToDBAttributes(videoInstance.VideoChannel, videoAttributesToUpdate)
62 videoInstance.set('name', videoData.name)
63 videoInstance.set('category', videoData.category)
64 videoInstance.set('licence', videoData.licence)
65 videoInstance.set('language', videoData.language)
66 videoInstance.set('nsfw', videoData.nsfw)
67 videoInstance.set('description', videoData.description)
68 videoInstance.set('duration', videoData.duration)
69 videoInstance.set('createdAt', videoData.createdAt)
70 videoInstance.set('updatedAt', videoData.updatedAt)
71 videoInstance.set('views', videoData.views)
72 // videoInstance.set('likes', videoData.likes)
73 // videoInstance.set('dislikes', videoData.dislikes)
75 await videoInstance.save(sequelizeOptions)
77 // Remove old video files
78 const videoFileDestroyTasks: Bluebird<void>[] = []
79 for (const videoFile of videoInstance.VideoFiles) {
80 videoFileDestroyTasks.push(videoFile.destroy(sequelizeOptions))
82 await Promise.all(videoFileDestroyTasks)
84 const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoInstance, videoAttributesToUpdate)
85 const tasks: Bluebird<any>[] = videoFileAttributes.map(f => VideoFileModel.create(f))
86 await Promise.all(tasks)
88 const tags = videoAttributesToUpdate.tag.map(t => t.name)
89 const tagInstances = await TagModel.findOrCreateTags(tags, t)
90 await videoInstance.$set('Tags', tagInstances, sequelizeOptions)
93 logger.info('Remote video with uuid %s updated', videoAttributesToUpdate.uuid)
95 if (videoInstance !== undefined && videoFieldsSave !== undefined) {
96 resetSequelizeInstance(videoInstance, videoFieldsSave)
99 // This is just a debug because we will retry the insert
100 logger.debug('Cannot update the remote video.', err)
105 async function processUpdateVideoChannel (account: AccountModel, videoChannel: VideoChannelObject) {
107 arguments: [ account, videoChannel ],
108 errorMessage: 'Cannot update the remote video channel with many retries.'
111 await retryTransactionWrapper(updateRemoteVideoChannel, options)
114 async function updateRemoteVideoChannel (account: AccountModel, videoChannel: VideoChannelObject) {
115 logger.debug('Updating remote video channel "%s".', videoChannel.uuid)
117 await sequelizeTypescript.transaction(async t => {
118 const sequelizeOptions = { transaction: t }
120 const videoChannelInstance = await VideoChannelModel.loadByUrl(videoChannel.id)
121 if (!videoChannelInstance) throw new Error('Video ' + videoChannel.id + ' not found.')
123 if (videoChannelInstance.Account.id !== account.id) {
124 throw new Error('Account ' + account.id + ' does not own video channel ' + videoChannelInstance.url)
127 videoChannelInstance.set('name', videoChannel.name)
128 videoChannelInstance.set('description', videoChannel.content)
129 videoChannelInstance.set('createdAt', videoChannel.published)
130 videoChannelInstance.set('updatedAt', videoChannel.updated)
132 await videoChannelInstance.save(sequelizeOptions)
135 logger.info('Remote video channel with uuid %s updated', videoChannel.uuid)