]>
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 C |
11 | import { TagModel } from '../../../models/video/tag' |
12 | import { VideoModel } from '../../../models/video/video' | |
3fd3ab2d | 13 | import { VideoFileModel } from '../../../models/video/video-file' |
a5625b41 | 14 | import { fetchAvatarIfExists, getOrCreateActorAndServerAndModel, updateActorAvatarInstance, updateActorInstance } from '../actor' |
25ed141c | 15 | import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc' |
0d0e8dd0 C |
16 | |
17 | async function processUpdateActivity (activity: ActivityUpdate) { | |
50d6de9c | 18 | const actor = await getOrCreateActorAndServerAndModel(activity.actor) |
e4f97bab | 19 | |
e4f97bab | 20 | if (activity.object.type === 'Video') { |
50d6de9c | 21 | return processUpdateVideo(actor, activity) |
265ba139 C |
22 | } else if (activity.object.type === 'Person') { |
23 | return processUpdateAccount(actor, activity) | |
e4f97bab | 24 | } |
0d0e8dd0 | 25 | |
b1cbc0dd | 26 | return |
e4f97bab C |
27 | } |
28 | ||
29 | // --------------------------------------------------------------------------- | |
30 | ||
31 | export { | |
32 | processUpdateActivity | |
33 | } | |
34 | ||
35 | // --------------------------------------------------------------------------- | |
36 | ||
50d6de9c | 37 | function processUpdateVideo (actor: ActorModel, activity: ActivityUpdate) { |
0d0e8dd0 | 38 | const options = { |
50d6de9c | 39 | arguments: [ actor, activity ], |
0d0e8dd0 C |
40 | errorMessage: 'Cannot update the remote video with many retries' |
41 | } | |
e4f97bab | 42 | |
0d0e8dd0 | 43 | return retryTransactionWrapper(updateRemoteVideo, options) |
e4f97bab C |
44 | } |
45 | ||
50d6de9c | 46 | async function updateRemoteVideo (actor: ActorModel, activity: ActivityUpdate) { |
265ba139 | 47 | const videoAttributesToUpdate = activity.object as VideoTorrentObject |
50d6de9c | 48 | |
0d0e8dd0 | 49 | logger.debug('Updating remote video "%s".', videoAttributesToUpdate.uuid) |
3fd3ab2d | 50 | let videoInstance: VideoModel |
265ba139 | 51 | let videoFieldsSave: any |
0d0e8dd0 C |
52 | |
53 | try { | |
3fd3ab2d | 54 | await sequelizeTypescript.transaction(async t => { |
0d0e8dd0 C |
55 | const sequelizeOptions = { |
56 | transaction: t | |
57 | } | |
58 | ||
3fd3ab2d | 59 | const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(videoAttributesToUpdate.id, t) |
0d0e8dd0 C |
60 | if (!videoInstance) throw new Error('Video ' + videoAttributesToUpdate.id + ' not found.') |
61 | ||
265ba139 C |
62 | videoFieldsSave = videoInstance.toJSON() |
63 | ||
50d6de9c C |
64 | const videoChannel = videoInstance.VideoChannel |
65 | if (videoChannel.Account.Actor.id !== actor.id) { | |
66 | throw new Error('Account ' + actor.url + ' does not own video channel ' + videoChannel.Actor.url) | |
0d0e8dd0 C |
67 | } |
68 | ||
50d6de9c | 69 | const videoData = await videoActivityObjectToDBAttributes(videoChannel, videoAttributesToUpdate, activity.to, activity.cc) |
0d0e8dd0 C |
70 | videoInstance.set('name', videoData.name) |
71 | videoInstance.set('category', videoData.category) | |
72 | videoInstance.set('licence', videoData.licence) | |
73 | videoInstance.set('language', videoData.language) | |
74 | videoInstance.set('nsfw', videoData.nsfw) | |
4e8c8728 | 75 | videoInstance.set('commentsEnabled', videoData.commentsEnabled) |
50d6de9c | 76 | videoInstance.set('privacy', videoData.privacy) |
0d0e8dd0 C |
77 | videoInstance.set('description', videoData.description) |
78 | videoInstance.set('duration', videoData.duration) | |
79 | videoInstance.set('createdAt', videoData.createdAt) | |
80 | videoInstance.set('updatedAt', videoData.updatedAt) | |
81 | videoInstance.set('views', videoData.views) | |
0d0e8dd0 C |
82 | |
83 | await videoInstance.save(sequelizeOptions) | |
84 | ||
85 | // Remove old video files | |
86 | const videoFileDestroyTasks: Bluebird<void>[] = [] | |
87 | for (const videoFile of videoInstance.VideoFiles) { | |
88 | videoFileDestroyTasks.push(videoFile.destroy(sequelizeOptions)) | |
89 | } | |
90 | await Promise.all(videoFileDestroyTasks) | |
91 | ||
79d5caf9 | 92 | const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoInstance, videoAttributesToUpdate) |
3fd3ab2d | 93 | const tasks: Bluebird<any>[] = videoFileAttributes.map(f => VideoFileModel.create(f)) |
0d0e8dd0 C |
94 | await Promise.all(tasks) |
95 | ||
96 | const tags = videoAttributesToUpdate.tag.map(t => t.name) | |
3fd3ab2d C |
97 | const tagInstances = await TagModel.findOrCreateTags(tags, t) |
98 | await videoInstance.$set('Tags', tagInstances, sequelizeOptions) | |
0d0e8dd0 C |
99 | }) |
100 | ||
101 | logger.info('Remote video with uuid %s updated', videoAttributesToUpdate.uuid) | |
102 | } catch (err) { | |
103 | if (videoInstance !== undefined && videoFieldsSave !== undefined) { | |
104 | resetSequelizeInstance(videoInstance, videoFieldsSave) | |
105 | } | |
106 | ||
107 | // This is just a debug because we will retry the insert | |
108 | logger.debug('Cannot update the remote video.', err) | |
109 | throw err | |
110 | } | |
111 | } | |
265ba139 C |
112 | |
113 | function processUpdateAccount (actor: ActorModel, activity: ActivityUpdate) { | |
114 | const options = { | |
115 | arguments: [ actor, activity ], | |
116 | errorMessage: 'Cannot update the remote account with many retries' | |
117 | } | |
118 | ||
119 | return retryTransactionWrapper(updateRemoteAccount, options) | |
120 | } | |
121 | ||
122 | async function updateRemoteAccount (actor: ActorModel, activity: ActivityUpdate) { | |
123 | const accountAttributesToUpdate = activity.object as ActivityPubActor | |
124 | ||
125 | logger.debug('Updating remote account "%s".', accountAttributesToUpdate.uuid) | |
265ba139 C |
126 | let accountInstance: AccountModel |
127 | let actorFieldsSave: object | |
128 | let accountFieldsSave: object | |
129 | ||
130 | // Fetch icon? | |
131 | const avatarName = await fetchAvatarIfExists(accountAttributesToUpdate) | |
132 | ||
133 | try { | |
134 | await sequelizeTypescript.transaction(async t => { | |
a5625b41 C |
135 | actorFieldsSave = actor.toJSON() |
136 | accountInstance = actor.Account | |
137 | accountFieldsSave = actor.Account.toJSON() | |
265ba139 | 138 | |
a5625b41 | 139 | await updateActorInstance(actor, accountAttributesToUpdate) |
265ba139 | 140 | |
a5625b41 C |
141 | if (avatarName !== undefined) { |
142 | await updateActorAvatarInstance(actor, avatarName, t) | |
265ba139 C |
143 | } |
144 | ||
145 | await actor.save({ transaction: t }) | |
146 | ||
147 | actor.Account.set('name', accountAttributesToUpdate.name || accountAttributesToUpdate.preferredUsername) | |
148 | await actor.Account.save({ transaction: t }) | |
149 | }) | |
150 | ||
151 | logger.info('Remote account with uuid %s updated', accountAttributesToUpdate.uuid) | |
152 | } catch (err) { | |
a5625b41 C |
153 | if (actor !== undefined && actorFieldsSave !== undefined) { |
154 | resetSequelizeInstance(actor, actorFieldsSave) | |
265ba139 C |
155 | } |
156 | ||
157 | if (accountInstance !== undefined && accountFieldsSave !== undefined) { | |
158 | resetSequelizeInstance(accountInstance, accountFieldsSave) | |
159 | } | |
160 | ||
161 | // This is just a debug because we will retry the insert | |
162 | logger.debug('Cannot update the remote account.', err) | |
163 | throw err | |
164 | } | |
165 | } |