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