]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-update.ts
Add ability to update a video channel
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-update.ts
CommitLineData
25ed141c 1import * as Bluebird from 'bluebird'
3fd3ab2d 2import { ActivityUpdate } from '../../../../shared/models/activitypub'
265ba139
C
3import { ActivityPubActor } from '../../../../shared/models/activitypub/activitypub-actor'
4import { VideoTorrentObject } from '../../../../shared/models/activitypub/objects'
da854ddd
C
5import { retryTransactionWrapper } from '../../../helpers/database-utils'
6import { logger } from '../../../helpers/logger'
7import { resetSequelizeInstance } from '../../../helpers/utils'
3fd3ab2d 8import { sequelizeTypescript } from '../../../initializers'
265ba139 9import { AccountModel } from '../../../models/account/account'
50d6de9c 10import { ActorModel } from '../../../models/activitypub/actor'
3fd3ab2d 11import { TagModel } from '../../../models/video/tag'
2422c46b 12import { VideoChannelModel } from '../../../models/video/video-channel'
3fd3ab2d 13import { VideoFileModel } from '../../../models/video/video-file'
a5625b41 14import { fetchAvatarIfExists, getOrCreateActorAndServerAndModel, updateActorAvatarInstance, updateActorInstance } from '../actor'
e3a682a8 15import {
2422c46b 16 generateThumbnailFromUrl,
0f320037 17 getOrCreateAccountAndVideoAndChannel, getOrCreateVideoChannel,
2422c46b 18 videoActivityObjectToDBAttributes,
e3a682a8
C
19 videoFileActivityUrlToDBAttributes
20} from '../videos'
0d0e8dd0
C
21
22async function processUpdateActivity (activity: ActivityUpdate) {
50d6de9c 23 const actor = await getOrCreateActorAndServerAndModel(activity.actor)
2422c46b 24 const objectType = activity.object.type
e4f97bab 25
2422c46b 26 if (objectType === 'Video') {
50d6de9c 27 return processUpdateVideo(actor, activity)
2422c46b
C
28 } else if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') {
29 return processUpdateActor(actor, activity)
e4f97bab 30 }
0d0e8dd0 31
b1cbc0dd 32 return
e4f97bab
C
33}
34
35// ---------------------------------------------------------------------------
36
37export {
38 processUpdateActivity
39}
40
41// ---------------------------------------------------------------------------
42
50d6de9c 43function processUpdateVideo (actor: ActorModel, activity: ActivityUpdate) {
0d0e8dd0 44 const options = {
50d6de9c 45 arguments: [ actor, activity ],
0d0e8dd0
C
46 errorMessage: 'Cannot update the remote video with many retries'
47 }
e4f97bab 48
0d0e8dd0 49 return retryTransactionWrapper(updateRemoteVideo, options)
e4f97bab
C
50}
51
50d6de9c 52async function updateRemoteVideo (actor: ActorModel, activity: ActivityUpdate) {
265ba139 53 const videoAttributesToUpdate = activity.object as VideoTorrentObject
50d6de9c 54
2ccaeeb3
C
55 const res = await getOrCreateAccountAndVideoAndChannel(videoAttributesToUpdate.id)
56
0f320037
C
57 // Fetch video channel outside the transaction
58 const newVideoChannelActor = await getOrCreateVideoChannel(videoAttributesToUpdate)
59 const newVideoChannel = newVideoChannelActor.VideoChannel
60
0d0e8dd0 61 logger.debug('Updating remote video "%s".', videoAttributesToUpdate.uuid)
2ccaeeb3 62 let videoInstance = res.video
265ba139 63 let videoFieldsSave: any
0d0e8dd0
C
64
65 try {
3fd3ab2d 66 await sequelizeTypescript.transaction(async t => {
0d0e8dd0
C
67 const sequelizeOptions = {
68 transaction: t
69 }
70
265ba139
C
71 videoFieldsSave = videoInstance.toJSON()
72
0f320037 73 // Check actor has the right to update the video
50d6de9c
C
74 const videoChannel = videoInstance.VideoChannel
75 if (videoChannel.Account.Actor.id !== actor.id) {
76 throw new Error('Account ' + actor.url + ' does not own video channel ' + videoChannel.Actor.url)
0d0e8dd0
C
77 }
78
0f320037 79 const videoData = await videoActivityObjectToDBAttributes(newVideoChannel, videoAttributesToUpdate, activity.to)
0d0e8dd0 80 videoInstance.set('name', videoData.name)
9a8cbd82
C
81 videoInstance.set('uuid', videoData.uuid)
82 videoInstance.set('url', videoData.url)
0d0e8dd0
C
83 videoInstance.set('category', videoData.category)
84 videoInstance.set('licence', videoData.licence)
85 videoInstance.set('language', videoData.language)
9a8cbd82 86 videoInstance.set('description', videoData.description)
2422c46b 87 videoInstance.set('support', videoData.support)
0d0e8dd0 88 videoInstance.set('nsfw', videoData.nsfw)
4e8c8728 89 videoInstance.set('commentsEnabled', videoData.commentsEnabled)
0d0e8dd0
C
90 videoInstance.set('duration', videoData.duration)
91 videoInstance.set('createdAt', videoData.createdAt)
92 videoInstance.set('updatedAt', videoData.updatedAt)
93 videoInstance.set('views', videoData.views)
9a8cbd82 94 videoInstance.set('privacy', videoData.privacy)
0f320037 95 videoInstance.set('channelId', videoData.channelId)
0d0e8dd0
C
96
97 await videoInstance.save(sequelizeOptions)
98
e3a682a8
C
99 // Don't block on request
100 generateThumbnailFromUrl(videoInstance, videoAttributesToUpdate.icon)
d5b7d911 101 .catch(err => logger.warn('Cannot generate thumbnail of %s.', videoAttributesToUpdate.id, { err }))
e3a682a8 102
0d0e8dd0
C
103 // Remove old video files
104 const videoFileDestroyTasks: Bluebird<void>[] = []
105 for (const videoFile of videoInstance.VideoFiles) {
106 videoFileDestroyTasks.push(videoFile.destroy(sequelizeOptions))
107 }
108 await Promise.all(videoFileDestroyTasks)
109
79d5caf9 110 const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoInstance, videoAttributesToUpdate)
1f7ab4f3 111 const tasks = videoFileAttributes.map(f => VideoFileModel.create(f))
0d0e8dd0
C
112 await Promise.all(tasks)
113
114 const tags = videoAttributesToUpdate.tag.map(t => t.name)
3fd3ab2d
C
115 const tagInstances = await TagModel.findOrCreateTags(tags, t)
116 await videoInstance.$set('Tags', tagInstances, sequelizeOptions)
0d0e8dd0
C
117 })
118
119 logger.info('Remote video with uuid %s updated', videoAttributesToUpdate.uuid)
120 } catch (err) {
121 if (videoInstance !== undefined && videoFieldsSave !== undefined) {
122 resetSequelizeInstance(videoInstance, videoFieldsSave)
123 }
124
125 // This is just a debug because we will retry the insert
d5b7d911 126 logger.debug('Cannot update the remote video.', { err })
0d0e8dd0
C
127 throw err
128 }
129}
265ba139 130
2422c46b 131function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) {
265ba139
C
132 const options = {
133 arguments: [ actor, activity ],
2422c46b 134 errorMessage: 'Cannot update the remote actor with many retries'
265ba139
C
135 }
136
2422c46b 137 return retryTransactionWrapper(updateRemoteActor, options)
265ba139
C
138}
139
2422c46b
C
140async function updateRemoteActor (actor: ActorModel, activity: ActivityUpdate) {
141 const actorAttributesToUpdate = activity.object as ActivityPubActor
265ba139 142
2422c46b
C
143 logger.debug('Updating remote account "%s".', actorAttributesToUpdate.uuid)
144 let accountOrChannelInstance: AccountModel | VideoChannelModel
265ba139 145 let actorFieldsSave: object
2422c46b 146 let accountOrChannelFieldsSave: object
265ba139
C
147
148 // Fetch icon?
2422c46b 149 const avatarName = await fetchAvatarIfExists(actorAttributesToUpdate)
265ba139
C
150
151 try {
152 await sequelizeTypescript.transaction(async t => {
a5625b41 153 actorFieldsSave = actor.toJSON()
265ba139 154
2422c46b
C
155 if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel
156 else accountOrChannelInstance = actor.Account
157
158 accountOrChannelFieldsSave = accountOrChannelInstance.toJSON()
159
160 await updateActorInstance(actor, actorAttributesToUpdate)
265ba139 161
a5625b41
C
162 if (avatarName !== undefined) {
163 await updateActorAvatarInstance(actor, avatarName, t)
265ba139
C
164 }
165
166 await actor.save({ transaction: t })
167
2422c46b
C
168 accountOrChannelInstance.set('name', actorAttributesToUpdate.name || actorAttributesToUpdate.preferredUsername)
169 accountOrChannelInstance.set('description', actorAttributesToUpdate.summary)
170 accountOrChannelInstance.set('support', actorAttributesToUpdate.support)
171 await accountOrChannelInstance.save({ transaction: t })
265ba139
C
172 })
173
2422c46b 174 logger.info('Remote account with uuid %s updated', actorAttributesToUpdate.uuid)
265ba139 175 } catch (err) {
a5625b41
C
176 if (actor !== undefined && actorFieldsSave !== undefined) {
177 resetSequelizeInstance(actor, actorFieldsSave)
265ba139
C
178 }
179
2422c46b
C
180 if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) {
181 resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave)
265ba139
C
182 }
183
184 // This is just a debug because we will retry the insert
d5b7d911 185 logger.debug('Cannot update the remote account.', { err })
265ba139
C
186 throw err
187 }
188}