]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/process/process-announce.ts
Fix bad to/cc when undo dislike
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-announce.ts
index 656db08a92185d5750488c709aa4d72ccbf0e2d7..2aa9ad5c745ed7892094fea53aaabddb6a5b7e69 100644 (file)
@@ -1,34 +1,23 @@
-import { ActivityAnnounce } from '../../../../shared/models/activitypub/activity'
-import { getOrCreateAccount } from '../../../helpers/activitypub'
+import { ActivityAdd, ActivityAnnounce, ActivityCreate } from '../../../../shared/models/activitypub/activity'
+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 { forwardActivity } from '../send/misc'
 import { processAddActivity } from './process-add'
 import { processCreateActivity } from './process-create'
 
 async function processAnnounceActivity (activity: ActivityAnnounce) {
   const announcedActivity = activity.object
-  const accountAnnouncer = await getOrCreateAccount(activity.actor)
+  const accountAnnouncer = await getOrCreateAccountAndServer(activity.actor)
 
   if (announcedActivity.type === 'Create' && announcedActivity.object.type === 'VideoChannel') {
-    // Add share entry
-    const videoChannel: VideoChannelInstance = await processCreateActivity(announcedActivity)
-    await db.VideoChannelShare.create({
-      accountId: accountAnnouncer.id,
-      videoChannelId: videoChannel.id
-    })
-
-    return undefined
+    return processVideoChannelShare(accountAnnouncer, activity)
   } else if (announcedActivity.type === 'Add' && announcedActivity.object.type === 'Video') {
-    // Add share entry
-    const video: VideoInstance = await processAddActivity(announcedActivity)
-    await db.VideoShare.create({
-      accountId: accountAnnouncer.id,
-      videoId: video.id
-    })
-
-    return undefined
+    return processVideoShare(accountAnnouncer, activity)
   }
 
   logger.warn(
@@ -44,3 +33,78 @@ async function processAnnounceActivity (activity: ActivityAnnounce) {
 export {
   processAnnounceActivity
 }
+
+// ---------------------------------------------------------------------------
+
+function processVideoChannelShare (accountAnnouncer: AccountInstance, activity: ActivityAnnounce) {
+  const options = {
+    arguments: [ accountAnnouncer, activity ],
+    errorMessage: 'Cannot share the video channel with many retries.'
+  }
+
+  return retryTransactionWrapper(shareVideoChannel, 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
+    })
+
+    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.'
+  }
+
+  return retryTransactionWrapper(shareVideo, options)
+}
+
+function shareVideo (accountAnnouncer: AccountInstance, activity: ActivityAnnounce) {
+  const announcedActivity = activity.object as ActivityAdd
+
+  return db.sequelize.transaction(async t => {
+    // Add share entry
+    const video: VideoInstance = await processAddActivity(announcedActivity)
+
+    const share = {
+      accountId: accountAnnouncer.id,
+      videoId: video.id
+    }
+
+    const [ , created ] = await db.VideoShare.findOrCreate({
+      where: share,
+      defaults: share,
+      transaction: t
+    })
+
+    if (video.isOwned() && created === true) {
+      // Don't resend the activity to the sender
+      const exceptions = [ accountAnnouncer ]
+      await forwardActivity(activity, t, exceptions)
+    }
+
+    return undefined
+  })
+}