]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/process-create.ts
Continue activitypub
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process-create.ts
index 114ff1848f23ab192ff290ed2322867060aa7d5c..471674eada856f9788bf7bf98b1c9cbf6aa55ac6 100644 (file)
@@ -1,23 +1,23 @@
-import {
-  ActivityCreate,
-  VideoTorrentObject,
-  VideoChannelObject
-} from '../../../shared'
+import { ActivityCreate, VideoChannelObject, VideoTorrentObject } from '../../../shared'
+import { ActivityAdd } from '../../../shared/models/activitypub/activity'
+import { generateThumbnailFromUrl, logger, retryTransactionWrapper } from '../../helpers'
 import { database as db } from '../../initializers'
-import { logger, retryTransactionWrapper } from '../../helpers'
+import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc'
+import Bluebird = require('bluebird')
+import { AccountInstance } from '../../models/account/account-interface'
+import { getActivityPubUrl, getOrCreateAccount } from '../../helpers/activitypub'
 
-function processCreateActivity (activity: ActivityCreate) {
+async function processCreateActivity (activity: ActivityCreate) {
   const activityObject = activity.object
   const activityType = activityObject.type
+  const account = await getOrCreateAccount(activity.actor)
 
-  if (activityType === 'Video') {
-    return processCreateVideo(activityObject as VideoTorrentObject)
-  } else if (activityType === 'VideoChannel') {
-    return processCreateVideoChannel(activityObject as VideoChannelObject)
+  if (activityType === 'VideoChannel') {
+    return processCreateVideoChannel(account, activityObject as VideoChannelObject)
   }
 
   logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
-  return Promise.resolve()
+  return Promise.resolve(undefined)
 }
 
 // ---------------------------------------------------------------------------
@@ -28,77 +28,37 @@ export {
 
 // ---------------------------------------------------------------------------
 
-function processCreateVideo (video: VideoTorrentObject) {
+function processCreateVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) {
   const options = {
-    arguments: [ video ],
-    errorMessage: 'Cannot insert the remote video with many retries.'
+    arguments: [ account, videoChannelToCreateData ],
+    errorMessage: 'Cannot insert the remote video channel with many retries.'
   }
 
-  return retryTransactionWrapper(addRemoteVideo, options)
+  return retryTransactionWrapper(addRemoteVideoChannel, options)
 }
 
-async function addRemoteVideo (videoToCreateData: VideoTorrentObject) {
-  logger.debug('Adding remote video %s.', videoToCreateData.url)
+async function addRemoteVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) {
+  logger.debug('Adding remote video channel "%s".', videoChannelToCreateData.uuid)
 
   await db.sequelize.transaction(async t => {
-    const sequelizeOptions = {
-      transaction: t
-    }
-
-    const videoFromDatabase = await db.Video.loadByUUID(videoToCreateData.uuid)
-    if (videoFromDatabase) throw new Error('UUID already exists.')
-
-    const videoChannel = await db.VideoChannel.loadByHostAndUUID(fromPod.host, videoToCreateData.channelUUID, t)
-    if (!videoChannel) throw new Error('Video channel ' + videoToCreateData.channelUUID + ' not found.')
-
-    const tags = videoToCreateData.tags
-    const tagInstances = await db.Tag.findOrCreateTags(tags, t)
-
-    const videoData = {
-      name: videoToCreateData.name,
-      uuid: videoToCreateData.uuid,
-      category: videoToCreateData.category,
-      licence: videoToCreateData.licence,
-      language: videoToCreateData.language,
-      nsfw: videoToCreateData.nsfw,
-      description: videoToCreateData.truncatedDescription,
-      channelId: videoChannel.id,
-      duration: videoToCreateData.duration,
-      createdAt: videoToCreateData.createdAt,
-      // FIXME: updatedAt does not seems to be considered by Sequelize
-      updatedAt: videoToCreateData.updatedAt,
-      views: videoToCreateData.views,
-      likes: videoToCreateData.likes,
-      dislikes: videoToCreateData.dislikes,
+    let videoChannel = await db.VideoChannel.loadByUUIDOrUrl(videoChannelToCreateData.uuid, videoChannelToCreateData.id, t)
+    if (videoChannel) throw new Error('Video channel with this URL/UUID already exists.')
+
+    const videoChannelData = {
+      name: videoChannelToCreateData.name,
+      description: videoChannelToCreateData.content,
+      uuid: videoChannelToCreateData.uuid,
+      createdAt: videoChannelToCreateData.published,
+      updatedAt: videoChannelToCreateData.updated,
       remote: true,
-      privacy: videoToCreateData.privacy
-    }
-
-    const video = db.Video.build(videoData)
-    await db.Video.generateThumbnailFromData(video, videoToCreateData.thumbnailData)
-    const videoCreated = await video.save(sequelizeOptions)
-
-    const tasks = []
-    for (const fileData of videoToCreateData.files) {
-      const videoFileInstance = db.VideoFile.build({
-        extname: fileData.extname,
-        infoHash: fileData.infoHash,
-        resolution: fileData.resolution,
-        size: fileData.size,
-        videoId: videoCreated.id
-      })
-
-      tasks.push(videoFileInstance.save(sequelizeOptions))
+      accountId: account.id
     }
 
-    await Promise.all(tasks)
+    videoChannel = db.VideoChannel.build(videoChannelData)
+    videoChannel.url = getActivityPubUrl('videoChannel', videoChannel.uuid)
 
-    await videoCreated.setTags(tagInstances, sequelizeOptions)
+    await videoChannel.save({ transaction: t })
   })
 
-  logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid)
-}
-
-function processCreateVideoChannel (videoChannel: VideoChannelObject) {
-
+  logger.info('Remote video channel with uuid %s inserted.', videoChannelToCreateData.uuid)
 }