diff options
Diffstat (limited to 'server/lib/activitypub/process/process-update.ts')
-rw-r--r-- | server/lib/activitypub/process/process-update.ts | 63 |
1 files changed, 14 insertions, 49 deletions
diff --git a/server/lib/activitypub/process/process-update.ts b/server/lib/activitypub/process/process-update.ts index 771021f0c..35912ee87 100644 --- a/server/lib/activitypub/process/process-update.ts +++ b/server/lib/activitypub/process/process-update.ts | |||
@@ -1,23 +1,19 @@ | |||
1 | import * as Bluebird from 'bluebird' | 1 | import * as Bluebird from 'bluebird' |
2 | import { VideoChannelObject, VideoTorrentObject } from '../../../../shared' | ||
3 | import { ActivityUpdate } from '../../../../shared/models/activitypub' | 2 | import { ActivityUpdate } from '../../../../shared/models/activitypub' |
4 | import { logger, resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers' | 3 | import { logger, resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers' |
5 | import { sequelizeTypescript } from '../../../initializers' | 4 | import { sequelizeTypescript } from '../../../initializers' |
6 | import { AccountModel } from '../../../models/account/account' | 5 | import { ActorModel } from '../../../models/activitypub/actor' |
7 | import { TagModel } from '../../../models/video/tag' | 6 | import { TagModel } from '../../../models/video/tag' |
8 | import { VideoModel } from '../../../models/video/video' | 7 | import { VideoModel } from '../../../models/video/video' |
9 | import { VideoChannelModel } from '../../../models/video/video-channel' | ||
10 | import { VideoFileModel } from '../../../models/video/video-file' | 8 | import { VideoFileModel } from '../../../models/video/video-file' |
11 | import { getOrCreateAccountAndServer } from '../account' | 9 | import { getOrCreateActorAndServerAndModel } from '../actor' |
12 | import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc' | 10 | import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc' |
13 | 11 | ||
14 | async function processUpdateActivity (activity: ActivityUpdate) { | 12 | async function processUpdateActivity (activity: ActivityUpdate) { |
15 | const account = await getOrCreateAccountAndServer(activity.actor) | 13 | const actor = await getOrCreateActorAndServerAndModel(activity.actor) |
16 | 14 | ||
17 | if (activity.object.type === 'Video') { | 15 | if (activity.object.type === 'Video') { |
18 | return processUpdateVideo(account, activity.object) | 16 | return processUpdateVideo(actor, activity) |
19 | } else if (activity.object.type === 'VideoChannel') { | ||
20 | return processUpdateVideoChannel(account, activity.object) | ||
21 | } | 17 | } |
22 | 18 | ||
23 | return | 19 | return |
@@ -31,16 +27,18 @@ export { | |||
31 | 27 | ||
32 | // --------------------------------------------------------------------------- | 28 | // --------------------------------------------------------------------------- |
33 | 29 | ||
34 | function processUpdateVideo (account: AccountModel, video: VideoTorrentObject) { | 30 | function processUpdateVideo (actor: ActorModel, activity: ActivityUpdate) { |
35 | const options = { | 31 | const options = { |
36 | arguments: [ account, video ], | 32 | arguments: [ actor, activity ], |
37 | errorMessage: 'Cannot update the remote video with many retries' | 33 | errorMessage: 'Cannot update the remote video with many retries' |
38 | } | 34 | } |
39 | 35 | ||
40 | return retryTransactionWrapper(updateRemoteVideo, options) | 36 | return retryTransactionWrapper(updateRemoteVideo, options) |
41 | } | 37 | } |
42 | 38 | ||
43 | async function updateRemoteVideo (account: AccountModel, videoAttributesToUpdate: VideoTorrentObject) { | 39 | async function updateRemoteVideo (actor: ActorModel, activity: ActivityUpdate) { |
40 | const videoAttributesToUpdate = activity.object | ||
41 | |||
44 | logger.debug('Updating remote video "%s".', videoAttributesToUpdate.uuid) | 42 | logger.debug('Updating remote video "%s".', videoAttributesToUpdate.uuid) |
45 | let videoInstance: VideoModel | 43 | let videoInstance: VideoModel |
46 | let videoFieldsSave: object | 44 | let videoFieldsSave: object |
@@ -54,23 +52,23 @@ async function updateRemoteVideo (account: AccountModel, videoAttributesToUpdate | |||
54 | const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(videoAttributesToUpdate.id, t) | 52 | const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(videoAttributesToUpdate.id, t) |
55 | if (!videoInstance) throw new Error('Video ' + videoAttributesToUpdate.id + ' not found.') | 53 | if (!videoInstance) throw new Error('Video ' + videoAttributesToUpdate.id + ' not found.') |
56 | 54 | ||
57 | if (videoInstance.VideoChannel.Account.id !== account.id) { | 55 | const videoChannel = videoInstance.VideoChannel |
58 | throw new Error('Account ' + account.url + ' does not own video channel ' + videoInstance.VideoChannel.url) | 56 | if (videoChannel.Account.Actor.id !== actor.id) { |
57 | throw new Error('Account ' + actor.url + ' does not own video channel ' + videoChannel.Actor.url) | ||
59 | } | 58 | } |
60 | 59 | ||
61 | const videoData = await videoActivityObjectToDBAttributes(videoInstance.VideoChannel, videoAttributesToUpdate) | 60 | const videoData = await videoActivityObjectToDBAttributes(videoChannel, videoAttributesToUpdate, activity.to, activity.cc) |
62 | videoInstance.set('name', videoData.name) | 61 | videoInstance.set('name', videoData.name) |
63 | videoInstance.set('category', videoData.category) | 62 | videoInstance.set('category', videoData.category) |
64 | videoInstance.set('licence', videoData.licence) | 63 | videoInstance.set('licence', videoData.licence) |
65 | videoInstance.set('language', videoData.language) | 64 | videoInstance.set('language', videoData.language) |
66 | videoInstance.set('nsfw', videoData.nsfw) | 65 | videoInstance.set('nsfw', videoData.nsfw) |
66 | videoInstance.set('privacy', videoData.privacy) | ||
67 | videoInstance.set('description', videoData.description) | 67 | videoInstance.set('description', videoData.description) |
68 | videoInstance.set('duration', videoData.duration) | 68 | videoInstance.set('duration', videoData.duration) |
69 | videoInstance.set('createdAt', videoData.createdAt) | 69 | videoInstance.set('createdAt', videoData.createdAt) |
70 | videoInstance.set('updatedAt', videoData.updatedAt) | 70 | videoInstance.set('updatedAt', videoData.updatedAt) |
71 | videoInstance.set('views', videoData.views) | 71 | videoInstance.set('views', videoData.views) |
72 | // videoInstance.set('likes', videoData.likes) | ||
73 | // videoInstance.set('dislikes', videoData.dislikes) | ||
74 | 72 | ||
75 | await videoInstance.save(sequelizeOptions) | 73 | await videoInstance.save(sequelizeOptions) |
76 | 74 | ||
@@ -101,36 +99,3 @@ async function updateRemoteVideo (account: AccountModel, videoAttributesToUpdate | |||
101 | throw err | 99 | throw err |
102 | } | 100 | } |
103 | } | 101 | } |
104 | |||
105 | async function processUpdateVideoChannel (account: AccountModel, videoChannel: VideoChannelObject) { | ||
106 | const options = { | ||
107 | arguments: [ account, videoChannel ], | ||
108 | errorMessage: 'Cannot update the remote video channel with many retries.' | ||
109 | } | ||
110 | |||
111 | await retryTransactionWrapper(updateRemoteVideoChannel, options) | ||
112 | } | ||
113 | |||
114 | async function updateRemoteVideoChannel (account: AccountModel, videoChannel: VideoChannelObject) { | ||
115 | logger.debug('Updating remote video channel "%s".', videoChannel.uuid) | ||
116 | |||
117 | await sequelizeTypescript.transaction(async t => { | ||
118 | const sequelizeOptions = { transaction: t } | ||
119 | |||
120 | const videoChannelInstance = await VideoChannelModel.loadByUrl(videoChannel.id) | ||
121 | if (!videoChannelInstance) throw new Error('Video ' + videoChannel.id + ' not found.') | ||
122 | |||
123 | if (videoChannelInstance.Account.id !== account.id) { | ||
124 | throw new Error('Account ' + account.id + ' does not own video channel ' + videoChannelInstance.url) | ||
125 | } | ||
126 | |||
127 | videoChannelInstance.set('name', videoChannel.name) | ||
128 | videoChannelInstance.set('description', videoChannel.content) | ||
129 | videoChannelInstance.set('createdAt', videoChannel.published) | ||
130 | videoChannelInstance.set('updatedAt', videoChannel.updated) | ||
131 | |||
132 | await videoChannelInstance.save(sequelizeOptions) | ||
133 | }) | ||
134 | |||
135 | logger.info('Remote video channel with uuid %s updated', videoChannel.uuid) | ||
136 | } | ||