]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/process/process-announce.ts
Add context on activitypub responses
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-announce.ts
index 2aa9ad5c745ed7892094fea53aaabddb6a5b7e69..bf7d7879db440b91556b01a164e495785dd32b9e 100644 (file)
@@ -1,23 +1,23 @@
-import { ActivityAdd, ActivityAnnounce, ActivityCreate } from '../../../../shared/models/activitypub/activity'
+import { ActivityAnnounce } from '../../../../shared/models/activitypub'
 import { retryTransactionWrapper } from '../../../helpers/database-utils'
 import { logger } from '../../../helpers/logger'
-import { database as db } from '../../../initializers/index'
-import { AccountInstance } from '../../../models/account/account-interface'
-import { VideoInstance } from '../../../models/index'
-import { VideoChannelInstance } from '../../../models/video/video-channel-interface'
-import { getOrCreateAccountAndServer } from '../account'
+import { sequelizeTypescript } from '../../../initializers'
+import { ActorModel } from '../../../models/activitypub/actor'
+import { VideoModel } from '../../../models/video/video'
+import { VideoShareModel } from '../../../models/video/video-share'
+import { getOrCreateActorAndServerAndModel } from '../actor'
 import { forwardActivity } from '../send/misc'
-import { processAddActivity } from './process-add'
+import { getOrCreateAccountAndVideoAndChannel } from '../videos'
 import { processCreateActivity } from './process-create'
 
 async function processAnnounceActivity (activity: ActivityAnnounce) {
   const announcedActivity = activity.object
-  const accountAnnouncer = await getOrCreateAccountAndServer(activity.actor)
+  const actorAnnouncer = await getOrCreateActorAndServerAndModel(activity.actor)
 
-  if (announcedActivity.type === 'Create' && announcedActivity.object.type === 'VideoChannel') {
-    return processVideoChannelShare(accountAnnouncer, activity)
-  } else if (announcedActivity.type === 'Add' && announcedActivity.object.type === 'Video') {
-    return processVideoShare(accountAnnouncer, activity)
+  if (typeof announcedActivity === 'string') {
+    return processVideoShare(actorAnnouncer, activity)
+  } else if (announcedActivity.type === 'Create' && announcedActivity.object.type === 'Video') {
+    return processVideoShare(actorAnnouncer, activity)
   }
 
   logger.warn(
@@ -36,64 +36,35 @@ export {
 
 // ---------------------------------------------------------------------------
 
-function processVideoChannelShare (accountAnnouncer: AccountInstance, activity: ActivityAnnounce) {
+function processVideoShare (actorAnnouncer: ActorModel, activity: ActivityAnnounce) {
   const options = {
-    arguments: [ accountAnnouncer, activity ],
-    errorMessage: 'Cannot share the video channel with many retries.'
+    arguments: [ actorAnnouncer, activity ],
+    errorMessage: 'Cannot share the video activity with many retries.'
   }
 
-  return retryTransactionWrapper(shareVideoChannel, options)
+  return retryTransactionWrapper(shareVideo, options)
 }
 
-async function shareVideoChannel (accountAnnouncer: AccountInstance, activity: ActivityAnnounce) {
-  const announcedActivity = activity.object as ActivityCreate
-
-  return db.sequelize.transaction(async t => {
-    // Add share entry
-    const videoChannel: VideoChannelInstance = await processCreateActivity(announcedActivity)
-    const share = {
-      accountId: accountAnnouncer.id,
-      videoChannelId: videoChannel.id
-    }
-
-    const [ , created ] = await db.VideoChannelShare.findOrCreate({
-      where: share,
-      defaults: share,
-      transaction: t
-    })
+async function shareVideo (actorAnnouncer: ActorModel, activity: ActivityAnnounce) {
+  const announced = activity.object
+  let video: VideoModel
 
-    if (videoChannel.isOwned() && created === true) {
-      // Don't resend the activity to the sender
-      const exceptions = [ accountAnnouncer ]
-      await forwardActivity(activity, t, exceptions)
-    }
-
-    return undefined
-  })
-}
-
-function processVideoShare (accountAnnouncer: AccountInstance, activity: ActivityAnnounce) {
-  const options = {
-    arguments: [ accountAnnouncer, activity ],
-    errorMessage: 'Cannot share the video with many retries.'
+  if (typeof announced === 'string') {
+    const res = await getOrCreateAccountAndVideoAndChannel(announced)
+    video = res.video
+  } else {
+    video = await processCreateActivity(announced)
   }
 
-  return retryTransactionWrapper(shareVideo, options)
-}
-
-function shareVideo (accountAnnouncer: AccountInstance, activity: ActivityAnnounce) {
-  const announcedActivity = activity.object as ActivityAdd
-
-  return db.sequelize.transaction(async t => {
+  return sequelizeTypescript.transaction(async t => {
     // Add share entry
-    const video: VideoInstance = await processAddActivity(announcedActivity)
 
     const share = {
-      accountId: accountAnnouncer.id,
+      actorId: actorAnnouncer.id,
       videoId: video.id
     }
 
-    const [ , created ] = await db.VideoShare.findOrCreate({
+    const [ , created ] = await VideoShareModel.findOrCreate({
       where: share,
       defaults: share,
       transaction: t
@@ -101,7 +72,7 @@ function shareVideo (accountAnnouncer: AccountInstance, activity: ActivityAnnoun
 
     if (video.isOwned() && created === true) {
       // Don't resend the activity to the sender
-      const exceptions = [ accountAnnouncer ]
+      const exceptions = [ actorAnnouncer ]
       await forwardActivity(activity, t, exceptions)
     }