771021f0ce7612b85cc9053f518a6587946206ca
[oweals/peertube.git] / server / lib / activitypub / process / process-update.ts
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'
13
14 async function processUpdateActivity (activity: ActivityUpdate) {
15   const account = await getOrCreateAccountAndServer(activity.actor)
16
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)
21   }
22
23   return
24 }
25
26 // ---------------------------------------------------------------------------
27
28 export {
29   processUpdateActivity
30 }
31
32 // ---------------------------------------------------------------------------
33
34 function processUpdateVideo (account: AccountModel, video: VideoTorrentObject) {
35   const options = {
36     arguments: [ account, video ],
37     errorMessage: 'Cannot update the remote video with many retries'
38   }
39
40   return retryTransactionWrapper(updateRemoteVideo, options)
41 }
42
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
47
48   try {
49     await sequelizeTypescript.transaction(async t => {
50       const sequelizeOptions = {
51         transaction: t
52       }
53
54       const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(videoAttributesToUpdate.id, t)
55       if (!videoInstance) throw new Error('Video ' + videoAttributesToUpdate.id + ' not found.')
56
57       if (videoInstance.VideoChannel.Account.id !== account.id) {
58         throw new Error('Account ' + account.url + ' does not own video channel ' + videoInstance.VideoChannel.url)
59       }
60
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)
74
75       await videoInstance.save(sequelizeOptions)
76
77       // Remove old video files
78       const videoFileDestroyTasks: Bluebird<void>[] = []
79       for (const videoFile of videoInstance.VideoFiles) {
80         videoFileDestroyTasks.push(videoFile.destroy(sequelizeOptions))
81       }
82       await Promise.all(videoFileDestroyTasks)
83
84       const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoInstance, videoAttributesToUpdate)
85       const tasks: Bluebird<any>[] = videoFileAttributes.map(f => VideoFileModel.create(f))
86       await Promise.all(tasks)
87
88       const tags = videoAttributesToUpdate.tag.map(t => t.name)
89       const tagInstances = await TagModel.findOrCreateTags(tags, t)
90       await videoInstance.$set('Tags', tagInstances, sequelizeOptions)
91     })
92
93     logger.info('Remote video with uuid %s updated', videoAttributesToUpdate.uuid)
94   } catch (err) {
95     if (videoInstance !== undefined && videoFieldsSave !== undefined) {
96       resetSequelizeInstance(videoInstance, videoFieldsSave)
97     }
98
99     // This is just a debug because we will retry the insert
100     logger.debug('Cannot update the remote video.', err)
101     throw err
102   }
103 }
104
105 async function processUpdateVideoChannel (account: AccountModel, videoChannel: VideoChannelObject) {
106   const options = {
107     arguments: [ account, videoChannel ],
108     errorMessage: 'Cannot update the remote video channel with many retries.'
109   }
110
111   await retryTransactionWrapper(updateRemoteVideoChannel, options)
112 }
113
114 async function updateRemoteVideoChannel (account: AccountModel, videoChannel: VideoChannelObject) {
115   logger.debug('Updating remote video channel "%s".', videoChannel.uuid)
116
117   await sequelizeTypescript.transaction(async t => {
118     const sequelizeOptions = { transaction: t }
119
120     const videoChannelInstance = await VideoChannelModel.loadByUrl(videoChannel.id)
121     if (!videoChannelInstance) throw new Error('Video ' + videoChannel.id + ' not found.')
122
123     if (videoChannelInstance.Account.id !== account.id) {
124       throw new Error('Account ' + account.id + ' does not own video channel ' + videoChannelInstance.url)
125     }
126
127     videoChannelInstance.set('name', videoChannel.name)
128     videoChannelInstance.set('description', videoChannel.content)
129     videoChannelInstance.set('createdAt', videoChannel.published)
130     videoChannelInstance.set('updatedAt', videoChannel.updated)
131
132     await videoChannelInstance.save(sequelizeOptions)
133   })
134
135   logger.info('Remote video channel with uuid %s updated', videoChannel.uuid)
136 }