]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/videos/index.ts
Fix views system behind a proxy
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / index.ts
index c9334676e7ee982e807dd335fcd056534a16282c..c0a8ac118a22ec569ad4fbd0e0df6e04e5875451 100644 (file)
@@ -3,7 +3,7 @@ import { extname, join } from 'path'
 import { VideoCreate, VideoPrivacy, VideoUpdate } from '../../../../shared'
 import { renamePromise } from '../../../helpers/core-utils'
 import { retryTransactionWrapper } from '../../../helpers/database-utils'
-import { getVideoFileHeight } from '../../../helpers/ffmpeg-utils'
+import { getVideoFileResolution } from '../../../helpers/ffmpeg-utils'
 import { processImage } from '../../../helpers/image-utils'
 import { logger } from '../../../helpers/logger'
 import { createReqFiles, getFormattedObjects, getServerActor, resetSequelizeInstance } from '../../../helpers/utils'
@@ -20,8 +20,9 @@ import {
   VIDEO_PRIVACIES
 } from '../../../initializers'
 import { fetchRemoteVideoDescription, getVideoActivityPubUrl, shareVideoByServerAndChannel } from '../../../lib/activitypub'
-import { sendCreateVideo, sendCreateViewToOrigin, sendCreateViewToVideoFollowers, sendUpdateVideo } from '../../../lib/activitypub/send'
+import { sendCreateVideo, sendCreateView, sendUpdateVideo } from '../../../lib/activitypub/send'
 import { JobQueue } from '../../../lib/job-queue'
+import { Redis } from '../../../lib/redis'
 import {
   asyncMiddleware,
   authenticate,
@@ -186,11 +187,11 @@ async function addVideo (req: express.Request, res: express.Response, videoPhysi
   const video = new VideoModel(videoData)
   video.url = getVideoActivityPubUrl(video)
 
-  const videoFileHeight = await getVideoFileHeight(videoPhysicalFile.path)
+  const { videoFileResolution } = await getVideoFileResolution(videoPhysicalFile.path)
 
   const videoFileData = {
     extname: extname(videoPhysicalFile.filename),
-    resolution: videoFileHeight,
+    resolution: videoFileResolution,
     size: videoPhysicalFile.size
   }
   const videoFile = new VideoFileModel(videoFileData)
@@ -352,14 +353,19 @@ function getVideo (req: express.Request, res: express.Response) {
 async function viewVideo (req: express.Request, res: express.Response) {
   const videoInstance = res.locals.video
 
+  const ip = req.headers['x-real-ip'] as string || req.ip
+  const exists = await Redis.Instance.isViewExists(ip, videoInstance.uuid)
+  if (exists) {
+    logger.debug('View for ip %s and video %s already exists.', ip, videoInstance.uuid)
+    return res.status(204).end()
+  }
+
   await videoInstance.increment('views')
+  await Redis.Instance.setView(ip, videoInstance.uuid)
+
   const serverAccount = await getServerActor()
 
-  if (videoInstance.isOwned()) {
-    await sendCreateViewToVideoFollowers(serverAccount, videoInstance, undefined)
-  } else {
-    await sendCreateViewToOrigin(serverAccount, videoInstance, undefined)
-  }
+  await sendCreateView(serverAccount, videoInstance, undefined)
 
   return res.status(204).end()
 }
@@ -378,7 +384,7 @@ async function getVideoDescription (req: express.Request, res: express.Response)
 }
 
 async function listVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
-  const resultList = await VideoModel.listForApi(req.query.start, req.query.count, req.query.sort)
+  const resultList = await VideoModel.listForApi(req.query.start, req.query.count, req.query.sort, req.query.filter)
 
   return res.json(getFormattedObjects(resultList.data, resultList.total))
 }