aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/process/process-update.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2017-12-14 17:38:41 +0100
committerChocobozzz <me@florianbigard.com>2017-12-19 10:53:16 +0100
commit50d6de9c286abcb34ff4234d56d9cbb803db7665 (patch)
treef1732b27edcd05c7877a8358b8312f1e38c287ed /server/lib/activitypub/process/process-update.ts
parentfadf619ad61a016c1c7fc53de5a8f398a4f77519 (diff)
downloadPeerTube-50d6de9c286abcb34ff4234d56d9cbb803db7665.tar.gz
PeerTube-50d6de9c286abcb34ff4234d56d9cbb803db7665.tar.zst
PeerTube-50d6de9c286abcb34ff4234d56d9cbb803db7665.zip
Begin moving video channel to actor
Diffstat (limited to 'server/lib/activitypub/process/process-update.ts')
-rw-r--r--server/lib/activitypub/process/process-update.ts63
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 @@
1import * as Bluebird from 'bluebird' 1import * as Bluebird from 'bluebird'
2import { VideoChannelObject, VideoTorrentObject } from '../../../../shared'
3import { ActivityUpdate } from '../../../../shared/models/activitypub' 2import { ActivityUpdate } from '../../../../shared/models/activitypub'
4import { logger, resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers' 3import { logger, resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers'
5import { sequelizeTypescript } from '../../../initializers' 4import { sequelizeTypescript } from '../../../initializers'
6import { AccountModel } from '../../../models/account/account' 5import { ActorModel } from '../../../models/activitypub/actor'
7import { TagModel } from '../../../models/video/tag' 6import { TagModel } from '../../../models/video/tag'
8import { VideoModel } from '../../../models/video/video' 7import { VideoModel } from '../../../models/video/video'
9import { VideoChannelModel } from '../../../models/video/video-channel'
10import { VideoFileModel } from '../../../models/video/video-file' 8import { VideoFileModel } from '../../../models/video/video-file'
11import { getOrCreateAccountAndServer } from '../account' 9import { getOrCreateActorAndServerAndModel } from '../actor'
12import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc' 10import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc'
13 11
14async function processUpdateActivity (activity: ActivityUpdate) { 12async 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
34function processUpdateVideo (account: AccountModel, video: VideoTorrentObject) { 30function 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
43async function updateRemoteVideo (account: AccountModel, videoAttributesToUpdate: VideoTorrentObject) { 39async 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
105async 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
114async 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}