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