aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/process-create.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-11-10 14:34:45 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-11-27 19:40:51 +0100
commit0d0e8dd0904b380b70e19ebcb4763d65601c4632 (patch)
treeacb625d7c88fbe863fa14bf6783fafe4a8e35137 /server/lib/activitypub/process-create.ts
parente4f97babf701481b55cc10fb3448feab5f97c867 (diff)
downloadPeerTube-0d0e8dd0904b380b70e19ebcb4763d65601c4632.tar.gz
PeerTube-0d0e8dd0904b380b70e19ebcb4763d65601c4632.tar.zst
PeerTube-0d0e8dd0904b380b70e19ebcb4763d65601c4632.zip
Continue activitypub
Diffstat (limited to 'server/lib/activitypub/process-create.ts')
-rw-r--r--server/lib/activitypub/process-create.ts104
1 files changed, 32 insertions, 72 deletions
diff --git a/server/lib/activitypub/process-create.ts b/server/lib/activitypub/process-create.ts
index 114ff1848..471674ead 100644
--- a/server/lib/activitypub/process-create.ts
+++ b/server/lib/activitypub/process-create.ts
@@ -1,23 +1,23 @@
1import { 1import { ActivityCreate, VideoChannelObject, VideoTorrentObject } from '../../../shared'
2 ActivityCreate, 2import { ActivityAdd } from '../../../shared/models/activitypub/activity'
3 VideoTorrentObject, 3import { generateThumbnailFromUrl, logger, retryTransactionWrapper } from '../../helpers'
4 VideoChannelObject
5} from '../../../shared'
6import { database as db } from '../../initializers' 4import { database as db } from '../../initializers'
7import { logger, retryTransactionWrapper } from '../../helpers' 5import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc'
6import Bluebird = require('bluebird')
7import { AccountInstance } from '../../models/account/account-interface'
8import { getActivityPubUrl, getOrCreateAccount } from '../../helpers/activitypub'
8 9
9function processCreateActivity (activity: ActivityCreate) { 10async function processCreateActivity (activity: ActivityCreate) {
10 const activityObject = activity.object 11 const activityObject = activity.object
11 const activityType = activityObject.type 12 const activityType = activityObject.type
13 const account = await getOrCreateAccount(activity.actor)
12 14
13 if (activityType === 'Video') { 15 if (activityType === 'VideoChannel') {
14 return processCreateVideo(activityObject as VideoTorrentObject) 16 return processCreateVideoChannel(account, activityObject as VideoChannelObject)
15 } else if (activityType === 'VideoChannel') {
16 return processCreateVideoChannel(activityObject as VideoChannelObject)
17 } 17 }
18 18
19 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id }) 19 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
20 return Promise.resolve() 20 return Promise.resolve(undefined)
21} 21}
22 22
23// --------------------------------------------------------------------------- 23// ---------------------------------------------------------------------------
@@ -28,77 +28,37 @@ export {
28 28
29// --------------------------------------------------------------------------- 29// ---------------------------------------------------------------------------
30 30
31function processCreateVideo (video: VideoTorrentObject) { 31function processCreateVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) {
32 const options = { 32 const options = {
33 arguments: [ video ], 33 arguments: [ account, videoChannelToCreateData ],
34 errorMessage: 'Cannot insert the remote video with many retries.' 34 errorMessage: 'Cannot insert the remote video channel with many retries.'
35 } 35 }
36 36
37 return retryTransactionWrapper(addRemoteVideo, options) 37 return retryTransactionWrapper(addRemoteVideoChannel, options)
38} 38}
39 39
40async function addRemoteVideo (videoToCreateData: VideoTorrentObject) { 40async function addRemoteVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) {
41 logger.debug('Adding remote video %s.', videoToCreateData.url) 41 logger.debug('Adding remote video channel "%s".', videoChannelToCreateData.uuid)
42 42
43 await db.sequelize.transaction(async t => { 43 await db.sequelize.transaction(async t => {
44 const sequelizeOptions = { 44 let videoChannel = await db.VideoChannel.loadByUUIDOrUrl(videoChannelToCreateData.uuid, videoChannelToCreateData.id, t)
45 transaction: t 45 if (videoChannel) throw new Error('Video channel with this URL/UUID already exists.')
46 } 46
47 47 const videoChannelData = {
48 const videoFromDatabase = await db.Video.loadByUUID(videoToCreateData.uuid) 48 name: videoChannelToCreateData.name,
49 if (videoFromDatabase) throw new Error('UUID already exists.') 49 description: videoChannelToCreateData.content,
50 50 uuid: videoChannelToCreateData.uuid,
51 const videoChannel = await db.VideoChannel.loadByHostAndUUID(fromPod.host, videoToCreateData.channelUUID, t) 51 createdAt: videoChannelToCreateData.published,
52 if (!videoChannel) throw new Error('Video channel ' + videoToCreateData.channelUUID + ' not found.') 52 updatedAt: videoChannelToCreateData.updated,
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, 53 remote: true,
74 privacy: videoToCreateData.privacy 54 accountId: account.id
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 } 55 }
93 56
94 await Promise.all(tasks) 57 videoChannel = db.VideoChannel.build(videoChannelData)
58 videoChannel.url = getActivityPubUrl('videoChannel', videoChannel.uuid)
95 59
96 await videoCreated.setTags(tagInstances, sequelizeOptions) 60 await videoChannel.save({ transaction: t })
97 }) 61 })
98 62
99 logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid) 63 logger.info('Remote video channel with uuid %s inserted.', videoChannelToCreateData.uuid)
100}
101
102function processCreateVideoChannel (videoChannel: VideoChannelObject) {
103
104} 64}