]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-update.ts
Move models to typescript-sequelize
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-update.ts
CommitLineData
25ed141c 1import * as Bluebird from 'bluebird'
54141398 2import { VideoChannelObject, VideoTorrentObject } from '../../../../shared'
3fd3ab2d
C
3import { ActivityUpdate } from '../../../../shared/models/activitypub'
4import { logger, resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers'
5import { sequelizeTypescript } from '../../../initializers'
6import { AccountModel } from '../../../models/account/account'
7import { TagModel } from '../../../models/video/tag'
8import { VideoModel } from '../../../models/video/video'
9import { VideoChannelModel } from '../../../models/video/video-channel'
10import { VideoFileModel } from '../../../models/video/video-file'
0f91ae62 11import { getOrCreateAccountAndServer } from '../account'
25ed141c 12import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc'
0d0e8dd0
C
13
14async function processUpdateActivity (activity: ActivityUpdate) {
0f91ae62 15 const account = await getOrCreateAccountAndServer(activity.actor)
e4f97bab 16
e4f97bab 17 if (activity.object.type === 'Video') {
0d0e8dd0 18 return processUpdateVideo(account, activity.object)
e4f97bab 19 } else if (activity.object.type === 'VideoChannel') {
0d0e8dd0 20 return processUpdateVideoChannel(account, activity.object)
e4f97bab 21 }
0d0e8dd0 22
b1cbc0dd 23 return
e4f97bab
C
24}
25
26// ---------------------------------------------------------------------------
27
28export {
29 processUpdateActivity
30}
31
32// ---------------------------------------------------------------------------
33
3fd3ab2d 34function processUpdateVideo (account: AccountModel, video: VideoTorrentObject) {
0d0e8dd0
C
35 const options = {
36 arguments: [ account, video ],
37 errorMessage: 'Cannot update the remote video with many retries'
38 }
e4f97bab 39
0d0e8dd0 40 return retryTransactionWrapper(updateRemoteVideo, options)
e4f97bab
C
41}
42
3fd3ab2d 43async function updateRemoteVideo (account: AccountModel, videoAttributesToUpdate: VideoTorrentObject) {
0d0e8dd0 44 logger.debug('Updating remote video "%s".', videoAttributesToUpdate.uuid)
3fd3ab2d 45 let videoInstance: VideoModel
0d0e8dd0
C
46 let videoFieldsSave: object
47
48 try {
3fd3ab2d 49 await sequelizeTypescript.transaction(async t => {
0d0e8dd0
C
50 const sequelizeOptions = {
51 transaction: t
52 }
53
3fd3ab2d 54 const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(videoAttributesToUpdate.id, t)
0d0e8dd0
C
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
d7d5611c 61 const videoData = await videoActivityObjectToDBAttributes(videoInstance.VideoChannel, videoAttributesToUpdate)
0d0e8dd0
C
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)
0d0e8dd0
C
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
79d5caf9 84 const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoInstance, videoAttributesToUpdate)
3fd3ab2d 85 const tasks: Bluebird<any>[] = videoFileAttributes.map(f => VideoFileModel.create(f))
0d0e8dd0
C
86 await Promise.all(tasks)
87
88 const tags = videoAttributesToUpdate.tag.map(t => t.name)
3fd3ab2d
C
89 const tagInstances = await TagModel.findOrCreateTags(tags, t)
90 await videoInstance.$set('Tags', tagInstances, sequelizeOptions)
0d0e8dd0
C
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
3fd3ab2d 105async function processUpdateVideoChannel (account: AccountModel, videoChannel: VideoChannelObject) {
0d0e8dd0
C
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
3fd3ab2d 114async function updateRemoteVideoChannel (account: AccountModel, videoChannel: VideoChannelObject) {
0d0e8dd0
C
115 logger.debug('Updating remote video channel "%s".', videoChannel.uuid)
116
3fd3ab2d 117 await sequelizeTypescript.transaction(async t => {
0d0e8dd0
C
118 const sequelizeOptions = { transaction: t }
119
3fd3ab2d 120 const videoChannelInstance = await VideoChannelModel.loadByUrl(videoChannel.id)
0d0e8dd0
C
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 })
e4f97bab 134
0d0e8dd0 135 logger.info('Remote video channel with uuid %s updated', videoChannel.uuid)
e4f97bab 136}