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