diff options
Diffstat (limited to 'server/lib/activitypub/process/process-update.ts')
-rw-r--r-- | server/lib/activitypub/process/process-update.ts | 99 |
1 files changed, 4 insertions, 95 deletions
diff --git a/server/lib/activitypub/process/process-update.ts b/server/lib/activitypub/process/process-update.ts index 11226e275..07a5ff92f 100644 --- a/server/lib/activitypub/process/process-update.ts +++ b/server/lib/activitypub/process/process-update.ts | |||
@@ -1,4 +1,3 @@ | |||
1 | import * as Bluebird from 'bluebird' | ||
2 | import { ActivityUpdate, VideoTorrentObject } from '../../../../shared/models/activitypub' | 1 | import { ActivityUpdate, VideoTorrentObject } from '../../../../shared/models/activitypub' |
3 | import { ActivityPubActor } from '../../../../shared/models/activitypub/activitypub-actor' | 2 | import { ActivityPubActor } from '../../../../shared/models/activitypub/activitypub-actor' |
4 | import { resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers/database-utils' | 3 | import { resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers/database-utils' |
@@ -6,19 +5,10 @@ import { logger } from '../../../helpers/logger' | |||
6 | import { sequelizeTypescript } from '../../../initializers' | 5 | import { sequelizeTypescript } from '../../../initializers' |
7 | import { AccountModel } from '../../../models/account/account' | 6 | import { AccountModel } from '../../../models/account/account' |
8 | import { ActorModel } from '../../../models/activitypub/actor' | 7 | import { ActorModel } from '../../../models/activitypub/actor' |
9 | import { TagModel } from '../../../models/video/tag' | ||
10 | import { VideoChannelModel } from '../../../models/video/video-channel' | 8 | import { VideoChannelModel } from '../../../models/video/video-channel' |
11 | import { VideoFileModel } from '../../../models/video/video-file' | ||
12 | import { fetchAvatarIfExists, getOrCreateActorAndServerAndModel, updateActorAvatarInstance, updateActorInstance } from '../actor' | 9 | import { fetchAvatarIfExists, getOrCreateActorAndServerAndModel, updateActorAvatarInstance, updateActorInstance } from '../actor' |
13 | import { | 10 | import { getOrCreateVideoAndAccountAndChannel, getOrCreateVideoChannel, updateVideoFromAP } from '../videos' |
14 | generateThumbnailFromUrl, | ||
15 | getOrCreateAccountAndVideoAndChannel, | ||
16 | getOrCreateVideoChannel, | ||
17 | videoActivityObjectToDBAttributes, | ||
18 | videoFileActivityUrlToDBAttributes | ||
19 | } from '../videos' | ||
20 | import { sanitizeAndCheckVideoTorrentObject } from '../../../helpers/custom-validators/activitypub/videos' | 11 | import { sanitizeAndCheckVideoTorrentObject } from '../../../helpers/custom-validators/activitypub/videos' |
21 | import { VideoCaptionModel } from '../../../models/video/video-caption' | ||
22 | 12 | ||
23 | async function processUpdateActivity (activity: ActivityUpdate) { | 13 | async function processUpdateActivity (activity: ActivityUpdate) { |
24 | const actor = await getOrCreateActorAndServerAndModel(activity.actor) | 14 | const actor = await getOrCreateActorAndServerAndModel(activity.actor) |
@@ -49,91 +39,10 @@ async function processUpdateVideo (actor: ActorModel, activity: ActivityUpdate) | |||
49 | return undefined | 39 | return undefined |
50 | } | 40 | } |
51 | 41 | ||
52 | const res = await getOrCreateAccountAndVideoAndChannel(videoObject.id) | 42 | const { video } = await getOrCreateVideoAndAccountAndChannel(videoObject.id) |
43 | const channelActor = await getOrCreateVideoChannel(videoObject) | ||
53 | 44 | ||
54 | // Fetch video channel outside the transaction | 45 | return updateVideoFromAP(video, videoObject, actor, channelActor, activity.to) |
55 | const newVideoChannelActor = await getOrCreateVideoChannel(videoObject) | ||
56 | const newVideoChannel = newVideoChannelActor.VideoChannel | ||
57 | |||
58 | logger.debug('Updating remote video "%s".', videoObject.uuid) | ||
59 | let videoInstance = res.video | ||
60 | let videoFieldsSave: any | ||
61 | |||
62 | try { | ||
63 | await sequelizeTypescript.transaction(async t => { | ||
64 | const sequelizeOptions = { | ||
65 | transaction: t | ||
66 | } | ||
67 | |||
68 | videoFieldsSave = videoInstance.toJSON() | ||
69 | |||
70 | // Check actor has the right to update the video | ||
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) | ||
74 | } | ||
75 | |||
76 | const videoData = await videoActivityObjectToDBAttributes(newVideoChannel, videoObject, activity.to) | ||
77 | videoInstance.set('name', videoData.name) | ||
78 | videoInstance.set('uuid', videoData.uuid) | ||
79 | videoInstance.set('url', videoData.url) | ||
80 | videoInstance.set('category', videoData.category) | ||
81 | videoInstance.set('licence', videoData.licence) | ||
82 | videoInstance.set('language', videoData.language) | ||
83 | videoInstance.set('description', videoData.description) | ||
84 | videoInstance.set('support', videoData.support) | ||
85 | videoInstance.set('nsfw', videoData.nsfw) | ||
86 | videoInstance.set('commentsEnabled', videoData.commentsEnabled) | ||
87 | videoInstance.set('waitTranscoding', videoData.waitTranscoding) | ||
88 | videoInstance.set('state', videoData.state) | ||
89 | videoInstance.set('duration', videoData.duration) | ||
90 | videoInstance.set('createdAt', videoData.createdAt) | ||
91 | videoInstance.set('updatedAt', videoData.updatedAt) | ||
92 | videoInstance.set('views', videoData.views) | ||
93 | videoInstance.set('privacy', videoData.privacy) | ||
94 | videoInstance.set('channelId', videoData.channelId) | ||
95 | |||
96 | await videoInstance.save(sequelizeOptions) | ||
97 | |||
98 | // Don't block on request | ||
99 | generateThumbnailFromUrl(videoInstance, videoObject.icon) | ||
100 | .catch(err => logger.warn('Cannot generate thumbnail of %s.', videoObject.id, { err })) | ||
101 | |||
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 | |||
109 | const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoInstance, videoObject) | ||
110 | const tasks = videoFileAttributes.map(f => VideoFileModel.create(f, sequelizeOptions)) | ||
111 | await Promise.all(tasks) | ||
112 | |||
113 | // Update Tags | ||
114 | const tags = videoObject.tag.map(tag => tag.name) | ||
115 | const tagInstances = await TagModel.findOrCreateTags(tags, t) | ||
116 | await videoInstance.$set('Tags', tagInstances, sequelizeOptions) | ||
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) | ||
125 | }) | ||
126 | |||
127 | logger.info('Remote video with uuid %s updated', videoObject.uuid) | ||
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 | ||
134 | logger.debug('Cannot update the remote video.', { err }) | ||
135 | throw err | ||
136 | } | ||
137 | } | 46 | } |
138 | 47 | ||
139 | async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) { | 48 | async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) { |