]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process-add.ts
Fix update host script
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process-add.ts
CommitLineData
d7d5611c 1import * as Bluebird from 'bluebird'
0d0e8dd0
C
2import { VideoTorrentObject } from '../../../shared'
3import { ActivityAdd } from '../../../shared/models/activitypub/activity'
d7d5611c
C
4import { generateThumbnailFromUrl, getOrCreateAccount, logger, retryTransactionWrapper } from '../../helpers'
5import { getOrCreateVideoChannel } from '../../helpers/activitypub'
0d0e8dd0
C
6import { database as db } from '../../initializers'
7import { AccountInstance } from '../../models/account/account-interface'
20494f12 8import { VideoChannelInstance } from '../../models/video/video-channel-interface'
d7d5611c 9import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc'
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
79d5caf9 20 return processAddVideo(account, activity, videoChannel, activityObject)
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
9a27cdc2 35function processAddVideo (account: AccountInstance, activity: ActivityAdd, videoChannel: VideoChannelInstance, video: VideoTorrentObject) {
0d0e8dd0 36 const options = {
9a27cdc2 37 arguments: [ account, activity, videoChannel, video ],
0d0e8dd0
C
38 errorMessage: 'Cannot insert the remote video with many retries.'
39 }
40
41 return retryTransactionWrapper(addRemoteVideo, options)
42}
43
9a27cdc2
C
44function addRemoteVideo (
45 account: AccountInstance,
46 activity: ActivityAdd,
47 videoChannel: VideoChannelInstance,
48 videoToCreateData: VideoTorrentObject
49) {
0d0e8dd0
C
50 logger.debug('Adding remote video %s.', videoToCreateData.url)
51
d8465018 52 return db.sequelize.transaction(async t => {
0d0e8dd0
C
53 const sequelizeOptions = {
54 transaction: t
55 }
56
0d0e8dd0
C
57 if (videoChannel.Account.id !== account.id) throw new Error('Video channel is not owned by this account.')
58
d7d5611c
C
59 const videoFromDatabase = await db.Video.loadByUUIDOrURL(videoToCreateData.uuid, videoToCreateData.id, t)
60 if (videoFromDatabase) throw new Error('Video with this UUID/Url already exists.')
61
9a27cdc2 62 const videoData = await videoActivityObjectToDBAttributes(videoChannel, videoToCreateData, activity.to, activity.cc)
0d0e8dd0
C
63 const video = db.Video.build(videoData)
64
65 // Don't block on request
66 generateThumbnailFromUrl(video, videoToCreateData.icon)
350e31d6 67 .catch(err => logger.warn('Cannot generate thumbnail of %s.', videoToCreateData.id, err))
0d0e8dd0
C
68
69 const videoCreated = await video.save(sequelizeOptions)
70
79d5caf9 71 const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoCreated, videoToCreateData)
20494f12
C
72 if (videoFileAttributes.length === 0) {
73 throw new Error('Cannot find valid files for video %s ' + videoToCreateData.url)
74 }
0d0e8dd0 75
20494f12 76 const tasks: Bluebird<any>[] = videoFileAttributes.map(f => db.VideoFile.create(f, { transaction: t }))
0d0e8dd0
C
77 await Promise.all(tasks)
78
79 const tags = videoToCreateData.tag.map(t => t.name)
80 const tagInstances = await db.Tag.findOrCreateTags(tags, t)
81 await videoCreated.setTags(tagInstances, sequelizeOptions)
d8465018
C
82
83 logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid)
84
85 return videoCreated
0d0e8dd0 86 })
0d0e8dd0 87}