]>
Commit | Line | Data |
---|---|---|
25ed141c | 1 | import * as Bluebird from 'bluebird' |
3fd3ab2d | 2 | import { ActivityUpdate } from '../../../../shared/models/activitypub' |
da854ddd C |
3 | import { retryTransactionWrapper } from '../../../helpers/database-utils' |
4 | import { logger } from '../../../helpers/logger' | |
5 | import { resetSequelizeInstance } from '../../../helpers/utils' | |
3fd3ab2d | 6 | import { sequelizeTypescript } from '../../../initializers' |
50d6de9c | 7 | import { ActorModel } from '../../../models/activitypub/actor' |
3fd3ab2d C |
8 | import { TagModel } from '../../../models/video/tag' |
9 | import { VideoModel } from '../../../models/video/video' | |
3fd3ab2d | 10 | import { VideoFileModel } from '../../../models/video/video-file' |
50d6de9c | 11 | import { getOrCreateActorAndServerAndModel } from '../actor' |
25ed141c | 12 | import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc' |
0d0e8dd0 C |
13 | |
14 | async function processUpdateActivity (activity: ActivityUpdate) { | |
50d6de9c | 15 | const actor = await getOrCreateActorAndServerAndModel(activity.actor) |
e4f97bab | 16 | |
e4f97bab | 17 | if (activity.object.type === 'Video') { |
50d6de9c | 18 | return processUpdateVideo(actor, activity) |
e4f97bab | 19 | } |
0d0e8dd0 | 20 | |
b1cbc0dd | 21 | return |
e4f97bab C |
22 | } |
23 | ||
24 | // --------------------------------------------------------------------------- | |
25 | ||
26 | export { | |
27 | processUpdateActivity | |
28 | } | |
29 | ||
30 | // --------------------------------------------------------------------------- | |
31 | ||
50d6de9c | 32 | function processUpdateVideo (actor: ActorModel, activity: ActivityUpdate) { |
0d0e8dd0 | 33 | const options = { |
50d6de9c | 34 | arguments: [ actor, activity ], |
0d0e8dd0 C |
35 | errorMessage: 'Cannot update the remote video with many retries' |
36 | } | |
e4f97bab | 37 | |
0d0e8dd0 | 38 | return retryTransactionWrapper(updateRemoteVideo, options) |
e4f97bab C |
39 | } |
40 | ||
50d6de9c C |
41 | async function updateRemoteVideo (actor: ActorModel, activity: ActivityUpdate) { |
42 | const videoAttributesToUpdate = activity.object | |
43 | ||
0d0e8dd0 | 44 | logger.debug('Updating remote video "%s".', videoAttributesToUpdate.uuid) |
3fd3ab2d | 45 | let videoInstance: VideoModel |
0d0e8dd0 C |
46 | let videoFieldsSave: object |
47 | ||
48 | try { | |
3fd3ab2d | 49 | await sequelizeTypescript.transaction(async t => { |
0d0e8dd0 C |
50 | const sequelizeOptions = { |
51 | transaction: t | |
52 | } | |
53 | ||
3fd3ab2d | 54 | const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(videoAttributesToUpdate.id, t) |
0d0e8dd0 C |
55 | if (!videoInstance) throw new Error('Video ' + videoAttributesToUpdate.id + ' not found.') |
56 | ||
50d6de9c C |
57 | const videoChannel = videoInstance.VideoChannel |
58 | if (videoChannel.Account.Actor.id !== actor.id) { | |
59 | throw new Error('Account ' + actor.url + ' does not own video channel ' + videoChannel.Actor.url) | |
0d0e8dd0 C |
60 | } |
61 | ||
50d6de9c | 62 | const videoData = await videoActivityObjectToDBAttributes(videoChannel, videoAttributesToUpdate, activity.to, activity.cc) |
0d0e8dd0 C |
63 | videoInstance.set('name', videoData.name) |
64 | videoInstance.set('category', videoData.category) | |
65 | videoInstance.set('licence', videoData.licence) | |
66 | videoInstance.set('language', videoData.language) | |
67 | videoInstance.set('nsfw', videoData.nsfw) | |
50d6de9c | 68 | videoInstance.set('privacy', videoData.privacy) |
0d0e8dd0 C |
69 | videoInstance.set('description', videoData.description) |
70 | videoInstance.set('duration', videoData.duration) | |
71 | videoInstance.set('createdAt', videoData.createdAt) | |
72 | videoInstance.set('updatedAt', videoData.updatedAt) | |
73 | videoInstance.set('views', videoData.views) | |
0d0e8dd0 C |
74 | |
75 | await videoInstance.save(sequelizeOptions) | |
76 | ||
77 | // Remove old video files | |
78 | const videoFileDestroyTasks: Bluebird<void>[] = [] | |
79 | for (const videoFile of videoInstance.VideoFiles) { | |
80 | videoFileDestroyTasks.push(videoFile.destroy(sequelizeOptions)) | |
81 | } | |
82 | await Promise.all(videoFileDestroyTasks) | |
83 | ||
79d5caf9 | 84 | const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoInstance, videoAttributesToUpdate) |
3fd3ab2d | 85 | const tasks: Bluebird<any>[] = videoFileAttributes.map(f => VideoFileModel.create(f)) |
0d0e8dd0 C |
86 | await Promise.all(tasks) |
87 | ||
88 | const tags = videoAttributesToUpdate.tag.map(t => t.name) | |
3fd3ab2d C |
89 | const tagInstances = await TagModel.findOrCreateTags(tags, t) |
90 | await videoInstance.$set('Tags', tagInstances, sequelizeOptions) | |
0d0e8dd0 C |
91 | }) |
92 | ||
93 | logger.info('Remote video with uuid %s updated', videoAttributesToUpdate.uuid) | |
94 | } catch (err) { | |
95 | if (videoInstance !== undefined && videoFieldsSave !== undefined) { | |
96 | resetSequelizeInstance(videoInstance, videoFieldsSave) | |
97 | } | |
98 | ||
99 | // This is just a debug because we will retry the insert | |
100 | logger.debug('Cannot update the remote video.', err) | |
101 | throw err | |
102 | } | |
103 | } |