]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/process/process-create.ts
Add concept of video state, and add ability to wait transcoding before
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-create.ts
index 08d61996a2ff7bac2bfe08cb689b8b037f33c158..38dacf772aae083ad5472eee2b33e3ec3f6e4445 100644 (file)
@@ -1,20 +1,17 @@
-import * as Bluebird from 'bluebird'
 import { ActivityCreate, VideoTorrentObject } from '../../../../shared'
 import { DislikeObject, VideoAbuseObject, ViewObject } from '../../../../shared/models/activitypub/objects'
 import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object'
-import { VideoRateType } from '../../../../shared/models/videos'
 import { retryTransactionWrapper } from '../../../helpers/database-utils'
 import { logger } from '../../../helpers/logger'
 import { sequelizeTypescript } from '../../../initializers'
 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
 import { ActorModel } from '../../../models/activitypub/actor'
-import { VideoModel } from '../../../models/video/video'
 import { VideoAbuseModel } from '../../../models/video/video-abuse'
 import { VideoCommentModel } from '../../../models/video/video-comment'
 import { getOrCreateActorAndServerAndModel } from '../actor'
-import { forwardActivity, getActorsInvolvedInVideo } from '../send/misc'
-import { addVideoComments, resolveThread } from '../video-comments'
-import { addVideoShares, getOrCreateAccountAndVideoAndChannel } from '../videos'
+import { resolveThread } from '../video-comments'
+import { getOrCreateAccountAndVideoAndChannel } from '../videos'
+import { forwardActivity, forwardVideoRelatedActivity } from '../send/utils'
 
 async function processCreateActivity (activity: ActivityCreate) {
   const activityObject = activity.object
@@ -53,57 +50,9 @@ async function processCreateVideo (
 
   const { video } = await getOrCreateAccountAndVideoAndChannel(videoToCreateData, actor)
 
-  // Process outside the transaction because we could fetch remote data
-  if (videoToCreateData.likes && Array.isArray(videoToCreateData.likes.orderedItems)) {
-    logger.info('Adding likes of video %s.', video.uuid)
-    await createRates(videoToCreateData.likes.orderedItems, video, 'like')
-  }
-
-  if (videoToCreateData.dislikes && Array.isArray(videoToCreateData.dislikes.orderedItems)) {
-    logger.info('Adding dislikes of video %s.', video.uuid)
-    await createRates(videoToCreateData.dislikes.orderedItems, video, 'dislike')
-  }
-
-  if (videoToCreateData.shares && Array.isArray(videoToCreateData.shares.orderedItems)) {
-    logger.info('Adding shares of video %s.', video.uuid)
-    await addVideoShares(video, videoToCreateData.shares.orderedItems)
-  }
-
-  if (videoToCreateData.comments && Array.isArray(videoToCreateData.comments.orderedItems)) {
-    logger.info('Adding comments of video %s.', video.uuid)
-    await addVideoComments(video, videoToCreateData.comments.orderedItems)
-  }
-
   return video
 }
 
-async function createRates (actorUrls: string[], video: VideoModel, rate: VideoRateType) {
-  let rateCounts = 0
-  const tasks: Bluebird<any>[] = []
-
-  for (const actorUrl of actorUrls) {
-    const actor = await getOrCreateActorAndServerAndModel(actorUrl)
-    const p = AccountVideoRateModel
-      .create({
-        videoId: video.id,
-        accountId: actor.Account.id,
-        type: rate
-      })
-      .then(() => rateCounts += 1)
-
-    tasks.push(p)
-  }
-
-  await Promise.all(tasks)
-
-  logger.info('Adding %d %s to video %s.', rateCounts, rate, video.uuid)
-
-  // This is "likes" and "dislikes"
-  await video.increment(rate + 's', { by: rateCounts })
-
-  return
-}
-
 async function processCreateDislike (byActor: ActorModel, activity: ActivityCreate) {
   const options = {
     arguments: [ byActor, activity ],
@@ -137,7 +86,8 @@ async function createVideoDislike (byActor: ActorModel, activity: ActivityCreate
     if (video.isOwned() && created === true) {
       // Don't resend the activity to the sender
       const exceptions = [ byActor ]
-      await forwardActivity(activity, t, exceptions)
+
+      await forwardVideoRelatedActivity(activity, t, exceptions, video)
     }
   })
 }
@@ -147,8 +97,8 @@ async function processCreateView (byActor: ActorModel, activity: ActivityCreate)
 
   const { video } = await getOrCreateAccountAndVideoAndChannel(view.object)
 
-  const account = await ActorModel.loadByUrl(view.actor)
-  if (!account) throw new Error('Unknown account ' + view.actor)
+  const actor = await ActorModel.loadByUrl(view.actor)
+  if (!actor) throw new Error('Unknown actor ' + view.actor)
 
   await video.increment('views')
 
@@ -240,11 +190,7 @@ async function createVideoComment (byActor: ActorModel, activity: ActivityCreate
       // Don't resend the activity to the sender
       const exceptions = [ byActor ]
 
-      // Mastodon does not add our announces in audience, so we forward to them manually
-      const additionalActors = await getActorsInvolvedInVideo(video, t)
-      const additionalFollowerUrls = additionalActors.map(a => a.followersUrl)
-
-      await forwardActivity(activity, t, exceptions, additionalFollowerUrls)
+      await forwardVideoRelatedActivity(activity, t, exceptions, video)
     }
   })
 }