diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-11-09 17:51:58 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-11-27 19:40:51 +0100 |
commit | e4f97babf701481b55cc10fb3448feab5f97c867 (patch) | |
tree | af37402a594dc5ff09f71ecb0687e8cfe4cdb471 /server/lib/activitypub/process-create.ts | |
parent | 343ad675f2a26c15b86150a9a3552e619d5d44f4 (diff) | |
download | PeerTube-e4f97babf701481b55cc10fb3448feab5f97c867.tar.gz PeerTube-e4f97babf701481b55cc10fb3448feab5f97c867.tar.zst PeerTube-e4f97babf701481b55cc10fb3448feab5f97c867.zip |
Begin activitypub
Diffstat (limited to 'server/lib/activitypub/process-create.ts')
-rw-r--r-- | server/lib/activitypub/process-create.ts | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/server/lib/activitypub/process-create.ts b/server/lib/activitypub/process-create.ts new file mode 100644 index 000000000..114ff1848 --- /dev/null +++ b/server/lib/activitypub/process-create.ts | |||
@@ -0,0 +1,104 @@ | |||
1 | import { | ||
2 | ActivityCreate, | ||
3 | VideoTorrentObject, | ||
4 | VideoChannelObject | ||
5 | } from '../../../shared' | ||
6 | import { database as db } from '../../initializers' | ||
7 | import { logger, retryTransactionWrapper } from '../../helpers' | ||
8 | |||
9 | function processCreateActivity (activity: ActivityCreate) { | ||
10 | const activityObject = activity.object | ||
11 | const activityType = activityObject.type | ||
12 | |||
13 | if (activityType === 'Video') { | ||
14 | return processCreateVideo(activityObject as VideoTorrentObject) | ||
15 | } else if (activityType === 'VideoChannel') { | ||
16 | return processCreateVideoChannel(activityObject as VideoChannelObject) | ||
17 | } | ||
18 | |||
19 | logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id }) | ||
20 | return Promise.resolve() | ||
21 | } | ||
22 | |||
23 | // --------------------------------------------------------------------------- | ||
24 | |||
25 | export { | ||
26 | processCreateActivity | ||
27 | } | ||
28 | |||
29 | // --------------------------------------------------------------------------- | ||
30 | |||
31 | function processCreateVideo (video: VideoTorrentObject) { | ||
32 | const options = { | ||
33 | arguments: [ video ], | ||
34 | errorMessage: 'Cannot insert the remote video with many retries.' | ||
35 | } | ||
36 | |||
37 | return retryTransactionWrapper(addRemoteVideo, options) | ||
38 | } | ||
39 | |||
40 | async function addRemoteVideo (videoToCreateData: VideoTorrentObject) { | ||
41 | logger.debug('Adding remote video %s.', videoToCreateData.url) | ||
42 | |||
43 | await db.sequelize.transaction(async t => { | ||
44 | const sequelizeOptions = { | ||
45 | transaction: t | ||
46 | } | ||
47 | |||
48 | const videoFromDatabase = await db.Video.loadByUUID(videoToCreateData.uuid) | ||
49 | if (videoFromDatabase) throw new Error('UUID already exists.') | ||
50 | |||
51 | const videoChannel = await db.VideoChannel.loadByHostAndUUID(fromPod.host, videoToCreateData.channelUUID, t) | ||
52 | if (!videoChannel) throw new Error('Video channel ' + videoToCreateData.channelUUID + ' not found.') | ||
53 | |||
54 | const tags = videoToCreateData.tags | ||
55 | const tagInstances = await db.Tag.findOrCreateTags(tags, t) | ||
56 | |||
57 | const videoData = { | ||
58 | name: videoToCreateData.name, | ||
59 | uuid: videoToCreateData.uuid, | ||
60 | category: videoToCreateData.category, | ||
61 | licence: videoToCreateData.licence, | ||
62 | language: videoToCreateData.language, | ||
63 | nsfw: videoToCreateData.nsfw, | ||
64 | description: videoToCreateData.truncatedDescription, | ||
65 | channelId: videoChannel.id, | ||
66 | duration: videoToCreateData.duration, | ||
67 | createdAt: videoToCreateData.createdAt, | ||
68 | // FIXME: updatedAt does not seems to be considered by Sequelize | ||
69 | updatedAt: videoToCreateData.updatedAt, | ||
70 | views: videoToCreateData.views, | ||
71 | likes: videoToCreateData.likes, | ||
72 | dislikes: videoToCreateData.dislikes, | ||
73 | remote: true, | ||
74 | privacy: videoToCreateData.privacy | ||
75 | } | ||
76 | |||
77 | const video = db.Video.build(videoData) | ||
78 | await db.Video.generateThumbnailFromData(video, videoToCreateData.thumbnailData) | ||
79 | const videoCreated = await video.save(sequelizeOptions) | ||
80 | |||
81 | const tasks = [] | ||
82 | for (const fileData of videoToCreateData.files) { | ||
83 | const videoFileInstance = db.VideoFile.build({ | ||
84 | extname: fileData.extname, | ||
85 | infoHash: fileData.infoHash, | ||
86 | resolution: fileData.resolution, | ||
87 | size: fileData.size, | ||
88 | videoId: videoCreated.id | ||
89 | }) | ||
90 | |||
91 | tasks.push(videoFileInstance.save(sequelizeOptions)) | ||
92 | } | ||
93 | |||
94 | await Promise.all(tasks) | ||
95 | |||
96 | await videoCreated.setTags(tagInstances, sequelizeOptions) | ||
97 | }) | ||
98 | |||
99 | logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid) | ||
100 | } | ||
101 | |||
102 | function processCreateVideoChannel (videoChannel: VideoChannelObject) { | ||
103 | |||
104 | } | ||