]>
Commit | Line | Data |
---|---|---|
0d0e8dd0 C |
1 | import { VideoTorrentObject } from '../../../shared' |
2 | import { ActivityAdd } from '../../../shared/models/activitypub/activity' | |
3 | import { generateThumbnailFromUrl, logger, retryTransactionWrapper, getOrCreateAccount } from '../../helpers' | |
4 | import { database as db } from '../../initializers' | |
5 | import { AccountInstance } from '../../models/account/account-interface' | |
6 | import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc' | |
7 | import Bluebird = require('bluebird') | |
8 | ||
9 | async 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 | ||
24 | export { | |
25 | processAddActivity | |
26 | } | |
27 | ||
28 | // --------------------------------------------------------------------------- | |
29 | ||
30 | function 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 | ||
39 | async function addRemoteVideo (account: AccountInstance, videoChannelUrl: string, videoToCreateData: VideoTorrentObject) { | |
40 | logger.debug('Adding remote video %s.', videoToCreateData.url) | |
41 | ||
d8465018 | 42 | return db.sequelize.transaction(async t => { |
0d0e8dd0 C |
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) | |
d8465018 C |
69 | |
70 | logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid) | |
71 | ||
72 | return videoCreated | |
0d0e8dd0 C |
73 | }) |
74 | ||
0d0e8dd0 | 75 | } |