]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process-add.ts
Server shares user videos
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process-add.ts
CommitLineData
0d0e8dd0
C
1import { VideoTorrentObject } from '../../../shared'
2import { ActivityAdd } from '../../../shared/models/activitypub/activity'
3import { generateThumbnailFromUrl, logger, retryTransactionWrapper, getOrCreateAccount } from '../../helpers'
4import { database as db } from '../../initializers'
5import { AccountInstance } from '../../models/account/account-interface'
6import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc'
7import Bluebird = require('bluebird')
20494f12
C
8import { getOrCreateVideoChannel } from '../../helpers/activitypub'
9import { VideoChannelInstance } from '../../models/video/video-channel-interface'
0d0e8dd0
C
10
11async function processAddActivity (activity: ActivityAdd) {
12 const activityObject = activity.object
13 const activityType = activityObject.type
14 const account = await getOrCreateAccount(activity.actor)
15
16 if (activityType === 'Video') {
20494f12
C
17 const videoChannelUrl = activity.target
18 const videoChannel = await getOrCreateVideoChannel(account, videoChannelUrl)
19
20 return processAddVideo(account, videoChannel, activityObject as VideoTorrentObject)
0d0e8dd0
C
21 }
22
23 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
24 return Promise.resolve(undefined)
25}
26
27// ---------------------------------------------------------------------------
28
29export {
30 processAddActivity
31}
32
33// ---------------------------------------------------------------------------
34
20494f12 35function processAddVideo (account: AccountInstance, videoChannel: VideoChannelInstance, video: VideoTorrentObject) {
0d0e8dd0 36 const options = {
20494f12 37 arguments: [ account, videoChannel, video ],
0d0e8dd0
C
38 errorMessage: 'Cannot insert the remote video with many retries.'
39 }
40
41 return retryTransactionWrapper(addRemoteVideo, options)
42}
43
20494f12 44function addRemoteVideo (account: AccountInstance, videoChannel: VideoChannelInstance, videoToCreateData: VideoTorrentObject) {
0d0e8dd0
C
45 logger.debug('Adding remote video %s.', videoToCreateData.url)
46
d8465018 47 return db.sequelize.transaction(async t => {
0d0e8dd0
C
48 const sequelizeOptions = {
49 transaction: t
50 }
51
0d0e8dd0
C
52 if (videoChannel.Account.id !== account.id) throw new Error('Video channel is not owned by this account.')
53
54 const videoData = await videoActivityObjectToDBAttributes(videoChannel, videoToCreateData, t)
55 const video = db.Video.build(videoData)
56
57 // Don't block on request
58 generateThumbnailFromUrl(video, videoToCreateData.icon)
350e31d6 59 .catch(err => logger.warn('Cannot generate thumbnail of %s.', videoToCreateData.id, err))
0d0e8dd0
C
60
61 const videoCreated = await video.save(sequelizeOptions)
62
63 const videoFileAttributes = await videoFileActivityUrlToDBAttributes(videoCreated, videoToCreateData)
20494f12
C
64 if (videoFileAttributes.length === 0) {
65 throw new Error('Cannot find valid files for video %s ' + videoToCreateData.url)
66 }
0d0e8dd0 67
20494f12 68 const tasks: Bluebird<any>[] = videoFileAttributes.map(f => db.VideoFile.create(f, { transaction: t }))
0d0e8dd0
C
69 await Promise.all(tasks)
70
71 const tags = videoToCreateData.tag.map(t => t.name)
72 const tagInstances = await db.Tag.findOrCreateTags(tags, t)
73 await videoCreated.setTags(tagInstances, sequelizeOptions)
d8465018
C
74
75 logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid)
76
77 return videoCreated
0d0e8dd0 78 })
0d0e8dd0 79}