aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/process-add.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/activitypub/process-add.ts')
-rw-r--r--server/lib/activitypub/process-add.ts22
1 files changed, 13 insertions, 9 deletions
diff --git a/server/lib/activitypub/process-add.ts b/server/lib/activitypub/process-add.ts
index 98e414dbb..df7139d46 100644
--- a/server/lib/activitypub/process-add.ts
+++ b/server/lib/activitypub/process-add.ts
@@ -5,6 +5,8 @@ import { database as db } from '../../initializers'
5import { AccountInstance } from '../../models/account/account-interface' 5import { AccountInstance } from '../../models/account/account-interface'
6import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc' 6import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc'
7import Bluebird = require('bluebird') 7import Bluebird = require('bluebird')
8import { getOrCreateVideoChannel } from '../../helpers/activitypub'
9import { VideoChannelInstance } from '../../models/video/video-channel-interface'
8 10
9async function processAddActivity (activity: ActivityAdd) { 11async function processAddActivity (activity: ActivityAdd) {
10 const activityObject = activity.object 12 const activityObject = activity.object
@@ -12,7 +14,10 @@ async function processAddActivity (activity: ActivityAdd) {
12 const account = await getOrCreateAccount(activity.actor) 14 const account = await getOrCreateAccount(activity.actor)
13 15
14 if (activityType === 'Video') { 16 if (activityType === 'Video') {
15 return processAddVideo(account, activity.id, activityObject as VideoTorrentObject) 17 const videoChannelUrl = activity.target
18 const videoChannel = await getOrCreateVideoChannel(account, videoChannelUrl)
19
20 return processAddVideo(account, videoChannel, activityObject as VideoTorrentObject)
16 } 21 }
17 22
18 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id }) 23 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
@@ -27,16 +32,16 @@ export {
27 32
28// --------------------------------------------------------------------------- 33// ---------------------------------------------------------------------------
29 34
30function processAddVideo (account: AccountInstance, videoChannelUrl: string, video: VideoTorrentObject) { 35function processAddVideo (account: AccountInstance, videoChannel: VideoChannelInstance, video: VideoTorrentObject) {
31 const options = { 36 const options = {
32 arguments: [ account, videoChannelUrl, video ], 37 arguments: [ account, videoChannel, video ],
33 errorMessage: 'Cannot insert the remote video with many retries.' 38 errorMessage: 'Cannot insert the remote video with many retries.'
34 } 39 }
35 40
36 return retryTransactionWrapper(addRemoteVideo, options) 41 return retryTransactionWrapper(addRemoteVideo, options)
37} 42}
38 43
39async function addRemoteVideo (account: AccountInstance, videoChannelUrl: string, videoToCreateData: VideoTorrentObject) { 44function addRemoteVideo (account: AccountInstance, videoChannel: VideoChannelInstance, videoToCreateData: VideoTorrentObject) {
40 logger.debug('Adding remote video %s.', videoToCreateData.url) 45 logger.debug('Adding remote video %s.', videoToCreateData.url)
41 46
42 return db.sequelize.transaction(async t => { 47 return db.sequelize.transaction(async t => {
@@ -44,9 +49,6 @@ async function addRemoteVideo (account: AccountInstance, videoChannelUrl: string
44 transaction: t 49 transaction: t
45 } 50 }
46 51
47 const videoChannel = await db.VideoChannel.loadByUrl(videoChannelUrl, t)
48 if (!videoChannel) throw new Error('Video channel not found.')
49
50 if (videoChannel.Account.id !== account.id) throw new Error('Video channel is not owned by this account.') 52 if (videoChannel.Account.id !== account.id) throw new Error('Video channel is not owned by this account.')
51 53
52 const videoData = await videoActivityObjectToDBAttributes(videoChannel, videoToCreateData, t) 54 const videoData = await videoActivityObjectToDBAttributes(videoChannel, videoToCreateData, t)
@@ -59,8 +61,11 @@ async function addRemoteVideo (account: AccountInstance, videoChannelUrl: string
59 const videoCreated = await video.save(sequelizeOptions) 61 const videoCreated = await video.save(sequelizeOptions)
60 62
61 const videoFileAttributes = await videoFileActivityUrlToDBAttributes(videoCreated, videoToCreateData) 63 const videoFileAttributes = await videoFileActivityUrlToDBAttributes(videoCreated, videoToCreateData)
64 if (videoFileAttributes.length === 0) {
65 throw new Error('Cannot find valid files for video %s ' + videoToCreateData.url)
66 }
62 67
63 const tasks: Bluebird<any>[] = videoFileAttributes.map(f => db.VideoFile.create(f)) 68 const tasks: Bluebird<any>[] = videoFileAttributes.map(f => db.VideoFile.create(f, { transaction: t }))
64 await Promise.all(tasks) 69 await Promise.all(tasks)
65 70
66 const tags = videoToCreateData.tag.map(t => t.name) 71 const tags = videoToCreateData.tag.map(t => t.name)
@@ -71,5 +76,4 @@ async function addRemoteVideo (account: AccountInstance, videoChannelUrl: string
71 76
72 return videoCreated 77 return videoCreated
73 }) 78 })
74
75} 79}