]>
Commit | Line | Data |
---|---|---|
1 | import * as Bluebird from 'bluebird' | |
2 | import { VideoTorrentObject } from '../../../../shared' | |
3 | import { ActivityAdd } from '../../../../shared/models/activitypub/activity' | |
4 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | |
5 | import { logger } from '../../../helpers/logger' | |
6 | import { database as db } from '../../../initializers' | |
7 | import { AccountInstance } from '../../../models/account/account-interface' | |
8 | import { VideoChannelInstance } from '../../../models/video/video-channel-interface' | |
9 | import { getOrCreateAccountAndServer } from '../account' | |
10 | import { getOrCreateVideoChannel } from '../video-channels' | |
11 | import { generateThumbnailFromUrl } from '../videos' | |
12 | import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc' | |
13 | ||
14 | async function processAddActivity (activity: ActivityAdd) { | |
15 | const activityObject = activity.object | |
16 | const activityType = activityObject.type | |
17 | const account = await getOrCreateAccountAndServer(activity.actor) | |
18 | ||
19 | if (activityType === 'Video') { | |
20 | const videoChannelUrl = activity.target | |
21 | const videoChannel = await getOrCreateVideoChannel(account, videoChannelUrl) | |
22 | ||
23 | return processAddVideo(account, activity, videoChannel, activityObject) | |
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 | ||
32 | export { | |
33 | processAddActivity | |
34 | } | |
35 | ||
36 | // --------------------------------------------------------------------------- | |
37 | ||
38 | function processAddVideo (account: AccountInstance, activity: ActivityAdd, videoChannel: VideoChannelInstance, video: VideoTorrentObject) { | |
39 | const options = { | |
40 | arguments: [ account, activity, videoChannel, video ], | |
41 | errorMessage: 'Cannot insert the remote video with many retries.' | |
42 | } | |
43 | ||
44 | return retryTransactionWrapper(addRemoteVideo, options) | |
45 | } | |
46 | ||
47 | function addRemoteVideo (account: AccountInstance, | |
48 | activity: ActivityAdd, | |
49 | videoChannel: VideoChannelInstance, | |
50 | videoToCreateData: VideoTorrentObject) { | |
51 | logger.debug('Adding remote video %s.', videoToCreateData.url) | |
52 | ||
53 | return db.sequelize.transaction(async t => { | |
54 | const sequelizeOptions = { | |
55 | transaction: t | |
56 | } | |
57 | ||
58 | if (videoChannel.Account.id !== account.id) throw new Error('Video channel is not owned by this account.') | |
59 | ||
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 | ||
63 | const videoData = await videoActivityObjectToDBAttributes(videoChannel, videoToCreateData, activity.to, activity.cc) | |
64 | const video = db.Video.build(videoData) | |
65 | ||
66 | // Don't block on request | |
67 | generateThumbnailFromUrl(video, videoToCreateData.icon) | |
68 | .catch(err => logger.warn('Cannot generate thumbnail of %s.', videoToCreateData.id, err)) | |
69 | ||
70 | const videoCreated = await video.save(sequelizeOptions) | |
71 | ||
72 | const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoCreated, videoToCreateData) | |
73 | if (videoFileAttributes.length === 0) { | |
74 | throw new Error('Cannot find valid files for video %s ' + videoToCreateData.url) | |
75 | } | |
76 | ||
77 | const tasks: Bluebird<any>[] = videoFileAttributes.map(f => db.VideoFile.create(f, { transaction: t })) | |
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) | |
83 | ||
84 | logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid) | |
85 | ||
86 | return videoCreated | |
87 | }) | |
88 | } |