Don't forward view, send updates instead
authorChocobozzz <me@florianbigard.com>
Thu, 15 Nov 2018 15:18:12 +0000 (16:18 +0100)
committerChocobozzz <me@florianbigard.com>
Thu, 15 Nov 2018 15:38:37 +0000 (16:38 +0100)
To avoid inconsistencies in the federation, now the origin server will
tell other instances what is the correct number of views

server/controllers/api/videos/index.ts
server/lib/activitypub/process/process-create.ts
server/lib/job-queue/handlers/video-views.ts
server/lib/job-queue/job-queue.ts

index 6641544066cd858c6133f2498234e8222bbbd121..e654bdd0962e7c0c2209dc9f91086d315e57aecd 100644 (file)
@@ -405,7 +405,11 @@ async function viewVideo (req: express.Request, res: express.Response) {
 
   const serverActor = await getServerActor()
 
-  await sendCreateView(serverActor, videoInstance, undefined)
+  // Send the event to the origin server
+  // If we own the video, we'll send an update event when we'll process the views (in our job queue)
+  if (videoInstance.isOwned() === false) {
+    await sendCreateView(serverActor, videoInstance, undefined)
+  }
 
   return res.status(204).end()
 }
index 920d02cd20ff270fd819233ebede5264b1cf8d7a..214e14546f943d36669a08e8351104a251f776ac 100644 (file)
@@ -13,7 +13,8 @@ import { forwardVideoRelatedActivity } from '../send/utils'
 import { Redis } from '../../redis'
 import { createOrUpdateCacheFile } from '../cache-file'
 import { immutableAssign } from '../../../tests/utils'
-import { getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from '../url'
+import { getVideoDislikeActivityPubUrl } from '../url'
+import { VideoModel } from '../../../models/video/video'
 
 async function processCreateActivity (activity: ActivityCreate, byActor: ActorModel) {
   const activityObject = activity.object
@@ -87,19 +88,10 @@ async function processCreateDislike (byActor: ActorModel, activity: ActivityCrea
 async function processCreateView (byActor: ActorModel, activity: ActivityCreate) {
   const view = activity.object as ViewObject
 
-  const options = {
-    videoObject: view.object,
-    fetchType: 'only-video' as 'only-video'
-  }
-  const { video } = await getOrCreateVideoAndAccountAndChannel(options)
+  const video = await VideoModel.loadByUrl(view.object)
+  if (!video || video.isOwned() === false) return
 
   await Redis.Instance.addVideoView(video.id)
-
-  if (video.isOwned()) {
-    // Don't resend the activity to the sender
-    const exceptions = [ byActor ]
-    await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
-  }
 }
 
 async function processCacheFile (byActor: ActorModel, activity: ActivityCreate) {
index cf180a11a28e5730e5b3179138b5d60f5fb1c3b7..2ceec2342c70859a87f83f8896370b2d81c819c8 100644 (file)
@@ -3,8 +3,9 @@ import { logger } from '../../../helpers/logger'
 import { VideoModel } from '../../../models/video/video'
 import { VideoViewModel } from '../../../models/video/video-views'
 import { isTestInstance } from '../../../helpers/core-utils'
+import { federateVideoIfNeeded } from '../../activitypub'
 
-async function processVideosViewsViews () {
+async function processVideosViews () {
   const lastHour = new Date()
 
   // In test mode, we run this function multiple times per hour, so we don't want the values of the previous hour
@@ -36,6 +37,9 @@ async function processVideosViewsViews () {
             views,
             videoId
           })
+
+          const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId)
+          await federateVideoIfNeeded(video, false)
         } catch (err) {
           logger.debug('Cannot create video views for video %d in hour %d. Maybe the video does not exist anymore?', videoId, hour)
         }
@@ -51,5 +55,5 @@ async function processVideosViewsViews () {
 // ---------------------------------------------------------------------------
 
 export {
-  processVideosViewsViews
+  processVideosViews
 }
index 0696ba43c4b9c2ea3c81ba7747c25cf4a6687ddc..4cfd4d253fd18495d992485d40f5ab7efd20e15f 100644 (file)
@@ -10,7 +10,7 @@ import { EmailPayload, processEmail } from './handlers/email'
 import { processVideoFile, processVideoFileImport, VideoFileImportPayload, VideoFilePayload } from './handlers/video-file'
 import { ActivitypubFollowPayload, processActivityPubFollow } from './handlers/activitypub-follow'
 import { processVideoImport, VideoImportPayload } from './handlers/video-import'
-import { processVideosViewsViews } from './handlers/video-views'
+import { processVideosViews } from './handlers/video-views'
 
 type CreateJobArgument =
   { type: 'activitypub-http-broadcast', payload: ActivitypubHttpBroadcastPayload } |
@@ -32,7 +32,7 @@ const handlers: { [ id in JobType ]: (job: Bull.Job) => Promise<any>} = {
   'video-file': processVideoFile,
   'email': processEmail,
   'video-import': processVideoImport,
-  'videos-views': processVideosViewsViews
+  'videos-views': processVideosViews
 }
 
 const jobTypes: JobType[] = [