]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/process/process-announce.ts
Support video views/viewers stats in server
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-announce.ts
index 4e50da8d2fe36f02115e65fb3512778e810d3332..9cc87ee27a13fc60a5aa60efa0d323685c1bcc6f 100644 (file)
@@ -1,17 +1,24 @@
+import { getAPId } from '@server/lib/activitypub/activity'
 import { ActivityAnnounce } from '../../../../shared/models/activitypub'
 import { retryTransactionWrapper } from '../../../helpers/database-utils'
-import { sequelizeTypescript } from '../../../initializers'
-import { ActorModel } from '../../../models/activitypub/actor'
-import { VideoModel } from '../../../models/video/video'
+import { logger } from '../../../helpers/logger'
+import { sequelizeTypescript } from '../../../initializers/database'
 import { VideoShareModel } from '../../../models/video/video-share'
-import { getOrCreateActorAndServerAndModel } from '../actor'
-import { forwardVideoRelatedActivity } from '../send/utils'
-import { getOrCreateAccountAndVideoAndChannel } from '../videos'
+import { APProcessorOptions } from '../../../types/activitypub-processor.model'
+import { MActorSignature, MVideoAccountLightBlacklistAllFiles } from '../../../types/models'
+import { Notifier } from '../../notifier'
+import { forwardVideoRelatedActivity } from '../send/shared/send-utils'
+import { getOrCreateAPVideo } from '../videos'
 
-async function processAnnounceActivity (activity: ActivityAnnounce) {
-  const actorAnnouncer = await getOrCreateActorAndServerAndModel(activity.actor)
+async function processAnnounceActivity (options: APProcessorOptions<ActivityAnnounce>) {
+  const { activity, byActor: actorAnnouncer } = options
+  // Only notify if it is not from a fetcher job
+  const notify = options.fromFetch !== true
 
-  return processVideoShare(actorAnnouncer, activity)
+  // Announces on accounts are not supported
+  if (actorAnnouncer.type !== 'Application' && actorAnnouncer.type !== 'Group') return
+
+  return retryTransactionWrapper(processVideoShare, actorAnnouncer, activity, notify)
 }
 
 // ---------------------------------------------------------------------------
@@ -22,23 +29,22 @@ export {
 
 // ---------------------------------------------------------------------------
 
-function processVideoShare (actorAnnouncer: ActorModel, activity: ActivityAnnounce) {
-  const options = {
-    arguments: [ actorAnnouncer, activity ],
-    errorMessage: 'Cannot share the video activity with many retries.'
-  }
-
-  return retryTransactionWrapper(shareVideo, options)
-}
+async function processVideoShare (actorAnnouncer: MActorSignature, activity: ActivityAnnounce, notify: boolean) {
+  const objectUri = getAPId(activity.object)
 
-async function shareVideo (actorAnnouncer: ActorModel, activity: ActivityAnnounce) {
-  const objectUri = typeof activity.object === 'string' ? activity.object : activity.object.id
-  let video: VideoModel
+  let video: MVideoAccountLightBlacklistAllFiles
+  let videoCreated: boolean
 
-  const res = await getOrCreateAccountAndVideoAndChannel(objectUri)
-  video = res.video
+  try {
+    const result = await getOrCreateAPVideo({ videoObject: objectUri })
+    video = result.video
+    videoCreated = result.created
+  } catch (err) {
+    logger.debug('Cannot process share of %s. Maybe this is not a video object, so just skipping.', objectUri, { err })
+    return
+  }
 
-  return sequelizeTypescript.transaction(async t => {
+  await sequelizeTypescript.transaction(async t => {
     // Add share entry
 
     const share = {
@@ -64,4 +70,6 @@ async function shareVideo (actorAnnouncer: ActorModel, activity: ActivityAnnounc
 
     return undefined
   })
+
+  if (videoCreated && notify) Notifier.Instance.notifyOnNewVideoIfNeeded(video)
 }