diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-11-10 14:34:45 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-11-27 19:40:51 +0100 |
commit | 0d0e8dd0904b380b70e19ebcb4763d65601c4632 (patch) | |
tree | acb625d7c88fbe863fa14bf6783fafe4a8e35137 /server/lib/activitypub/process-add.ts | |
parent | e4f97babf701481b55cc10fb3448feab5f97c867 (diff) | |
download | PeerTube-0d0e8dd0904b380b70e19ebcb4763d65601c4632.tar.gz PeerTube-0d0e8dd0904b380b70e19ebcb4763d65601c4632.tar.zst PeerTube-0d0e8dd0904b380b70e19ebcb4763d65601c4632.zip |
Continue activitypub
Diffstat (limited to 'server/lib/activitypub/process-add.ts')
-rw-r--r-- | server/lib/activitypub/process-add.ts | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/server/lib/activitypub/process-add.ts b/server/lib/activitypub/process-add.ts new file mode 100644 index 000000000..40541aca3 --- /dev/null +++ b/server/lib/activitypub/process-add.ts | |||
@@ -0,0 +1,72 @@ | |||
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 = { | ||
32 | arguments: [ account, videoChannelUrl ,video ], | ||
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 | |||
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) | ||
57 | .catch(err => logger.warning('Cannot generate thumbnail of %s.', videoToCreateData.id, err)) | ||
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 | } | ||