Begin moving video channel to actor
[oweals/peertube.git] / server / lib / activitypub / process / process-update.ts
1 import * as Bluebird from 'bluebird'
2 import { ActivityUpdate } from '../../../../shared/models/activitypub'
3 import { logger, resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers'
4 import { sequelizeTypescript } from '../../../initializers'
5 import { ActorModel } from '../../../models/activitypub/actor'
6 import { TagModel } from '../../../models/video/tag'
7 import { VideoModel } from '../../../models/video/video'
8 import { VideoFileModel } from '../../../models/video/video-file'
9 import { getOrCreateActorAndServerAndModel } from '../actor'
10 import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc'
11
12 async function processUpdateActivity (activity: ActivityUpdate) {
13   const actor = await getOrCreateActorAndServerAndModel(activity.actor)
14
15   if (activity.object.type === 'Video') {
16     return processUpdateVideo(actor, activity)
17   }
18
19   return
20 }
21
22 // ---------------------------------------------------------------------------
23
24 export {
25   processUpdateActivity
26 }
27
28 // ---------------------------------------------------------------------------
29
30 function processUpdateVideo (actor: ActorModel, activity: ActivityUpdate) {
31   const options = {
32     arguments: [ actor, activity ],
33     errorMessage: 'Cannot update the remote video with many retries'
34   }
35
36   return retryTransactionWrapper(updateRemoteVideo, options)
37 }
38
39 async function updateRemoteVideo (actor: ActorModel, activity: ActivityUpdate) {
40   const videoAttributesToUpdate = activity.object
41
42   logger.debug('Updating remote video "%s".', videoAttributesToUpdate.uuid)
43   let videoInstance: VideoModel
44   let videoFieldsSave: object
45
46   try {
47     await sequelizeTypescript.transaction(async t => {
48       const sequelizeOptions = {
49         transaction: t
50       }
51
52       const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(videoAttributesToUpdate.id, t)
53       if (!videoInstance) throw new Error('Video ' + videoAttributesToUpdate.id + ' not found.')
54
55       const videoChannel = videoInstance.VideoChannel
56       if (videoChannel.Account.Actor.id !== actor.id) {
57         throw new Error('Account ' + actor.url + ' does not own video channel ' + videoChannel.Actor.url)
58       }
59
60       const videoData = await videoActivityObjectToDBAttributes(videoChannel, videoAttributesToUpdate, activity.to, activity.cc)
61       videoInstance.set('name', videoData.name)
62       videoInstance.set('category', videoData.category)
63       videoInstance.set('licence', videoData.licence)
64       videoInstance.set('language', videoData.language)
65       videoInstance.set('nsfw', videoData.nsfw)
66       videoInstance.set('privacy', videoData.privacy)
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
73       await videoInstance.save(sequelizeOptions)
74
75       // Remove old video files
76       const videoFileDestroyTasks: Bluebird<void>[] = []
77       for (const videoFile of videoInstance.VideoFiles) {
78         videoFileDestroyTasks.push(videoFile.destroy(sequelizeOptions))
79       }
80       await Promise.all(videoFileDestroyTasks)
81
82       const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoInstance, videoAttributesToUpdate)
83       const tasks: Bluebird<any>[] = videoFileAttributes.map(f => VideoFileModel.create(f))
84       await Promise.all(tasks)
85
86       const tags = videoAttributesToUpdate.tag.map(t => t.name)
87       const tagInstances = await TagModel.findOrCreateTags(tags, t)
88       await videoInstance.$set('Tags', tagInstances, sequelizeOptions)
89     })
90
91     logger.info('Remote video with uuid %s updated', videoAttributesToUpdate.uuid)
92   } catch (err) {
93     if (videoInstance !== undefined && videoFieldsSave !== undefined) {
94       resetSequelizeInstance(videoInstance, videoFieldsSave)
95     }
96
97     // This is just a debug because we will retry the insert
98     logger.debug('Cannot update the remote video.', err)
99     throw err
100   }
101 }