]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process-add.ts
Follow works
[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')
8
9async function processAddActivity (activity: ActivityAdd) {
10 const activityObject = activity.object
11 const activityType = activityObject.type
12 const account = await getOrCreateAccount(activity.actor)
13
14 if (activityType === 'Video') {
15 return processAddVideo(account, activity.id, activityObject as VideoTorrentObject)
16 }
17
18 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
19 return Promise.resolve(undefined)
20}
21
22// ---------------------------------------------------------------------------
23
24export {
25 processAddActivity
26}
27
28// ---------------------------------------------------------------------------
29
30function processAddVideo (account: AccountInstance, videoChannelUrl: string, video: VideoTorrentObject) {
31 const options = {
ce548a10 32 arguments: [ account, videoChannelUrl, video ],
0d0e8dd0
C
33 errorMessage: 'Cannot insert the remote video with many retries.'
34 }
35
36 return retryTransactionWrapper(addRemoteVideo, options)
37}
38
39async function addRemoteVideo (account: AccountInstance, videoChannelUrl: string, videoToCreateData: VideoTorrentObject) {
40 logger.debug('Adding remote video %s.', videoToCreateData.url)
41
42 await db.sequelize.transaction(async t => {
43 const sequelizeOptions = {
44 transaction: t
45 }
46
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.')
51
52 const videoData = await videoActivityObjectToDBAttributes(videoChannel, videoToCreateData, t)
53 const video = db.Video.build(videoData)
54
55 // Don't block on request
56 generateThumbnailFromUrl(video, videoToCreateData.icon)
350e31d6 57 .catch(err => logger.warn('Cannot generate thumbnail of %s.', videoToCreateData.id, err))
0d0e8dd0
C
58
59 const videoCreated = await video.save(sequelizeOptions)
60
61 const videoFileAttributes = await videoFileActivityUrlToDBAttributes(videoCreated, videoToCreateData)
62
63 const tasks: Bluebird<any>[] = videoFileAttributes.map(f => db.VideoFile.create(f))
64 await Promise.all(tasks)
65
66 const tags = videoToCreateData.tag.map(t => t.name)
67 const tagInstances = await db.Tag.findOrCreateTags(tags, t)
68 await videoCreated.setTags(tagInstances, sequelizeOptions)
69 })
70
71 logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid)
72}