diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-11-20 09:43:39 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-11-27 19:40:52 +0100 |
commit | 54141398354e6e7b94aa3065a705a1251390111c (patch) | |
tree | 8d30d1b9ea8acbe04f6d404125b04fc0c9897b70 /server/lib/activitypub/process/process-add.ts | |
parent | eb8b27c93e61a896a08923dc1ca3c87ba8cf4948 (diff) | |
download | PeerTube-54141398354e6e7b94aa3065a705a1251390111c.tar.gz PeerTube-54141398354e6e7b94aa3065a705a1251390111c.tar.zst PeerTube-54141398354e6e7b94aa3065a705a1251390111c.zip |
Refractor activity pub lib/helpers
Diffstat (limited to 'server/lib/activitypub/process/process-add.ts')
-rw-r--r-- | server/lib/activitypub/process/process-add.ts | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/server/lib/activitypub/process/process-add.ts b/server/lib/activitypub/process/process-add.ts new file mode 100644 index 000000000..f064c1ab6 --- /dev/null +++ b/server/lib/activitypub/process/process-add.ts | |||
@@ -0,0 +1,87 @@ | |||
1 | import * as Bluebird from 'bluebird' | ||
2 | import { VideoTorrentObject } from '../../../../shared' | ||
3 | import { ActivityAdd } from '../../../../shared/models/activitypub/activity' | ||
4 | import { generateThumbnailFromUrl, getOrCreateAccount, logger, retryTransactionWrapper } from '../../../helpers' | ||
5 | import { getOrCreateVideoChannel } from '../../../helpers/activitypub' | ||
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 { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc' | ||
10 | |||
11 | async 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') { | ||
17 | const videoChannelUrl = activity.target | ||
18 | const videoChannel = await getOrCreateVideoChannel(account, videoChannelUrl) | ||
19 | |||
20 | return processAddVideo(account, activity, videoChannel, activityObject) | ||
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 | |||
29 | export { | ||
30 | processAddActivity | ||
31 | } | ||
32 | |||
33 | // --------------------------------------------------------------------------- | ||
34 | |||
35 | function processAddVideo (account: AccountInstance, activity: ActivityAdd, videoChannel: VideoChannelInstance, video: VideoTorrentObject) { | ||
36 | const options = { | ||
37 | arguments: [ account, activity, videoChannel, video ], | ||
38 | errorMessage: 'Cannot insert the remote video with many retries.' | ||
39 | } | ||
40 | |||
41 | return retryTransactionWrapper(addRemoteVideo, options) | ||
42 | } | ||
43 | |||
44 | function addRemoteVideo ( | ||
45 | account: AccountInstance, | ||
46 | activity: ActivityAdd, | ||
47 | videoChannel: VideoChannelInstance, | ||
48 | videoToCreateData: VideoTorrentObject | ||
49 | ) { | ||
50 | logger.debug('Adding remote video %s.', videoToCreateData.url) | ||
51 | |||
52 | return db.sequelize.transaction(async t => { | ||
53 | const sequelizeOptions = { | ||
54 | transaction: t | ||
55 | } | ||
56 | |||
57 | if (videoChannel.Account.id !== account.id) throw new Error('Video channel is not owned by this account.') | ||
58 | |||
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 | |||
62 | const videoData = await videoActivityObjectToDBAttributes(videoChannel, videoToCreateData, activity.to, activity.cc) | ||
63 | const video = db.Video.build(videoData) | ||
64 | |||
65 | // Don't block on request | ||
66 | generateThumbnailFromUrl(video, videoToCreateData.icon) | ||
67 | .catch(err => logger.warn('Cannot generate thumbnail of %s.', videoToCreateData.id, err)) | ||
68 | |||
69 | const videoCreated = await video.save(sequelizeOptions) | ||
70 | |||
71 | const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoCreated, videoToCreateData) | ||
72 | if (videoFileAttributes.length === 0) { | ||
73 | throw new Error('Cannot find valid files for video %s ' + videoToCreateData.url) | ||
74 | } | ||
75 | |||
76 | const tasks: Bluebird<any>[] = videoFileAttributes.map(f => db.VideoFile.create(f, { transaction: t })) | ||
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) | ||
82 | |||
83 | logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid) | ||
84 | |||
85 | return videoCreated | ||
86 | }) | ||
87 | } | ||