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