aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-11-30 09:21:11 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-11-30 09:21:11 +0100
commit1f3e9feca2caf68024168b0ea9ed39d8438fa235 (patch)
treeb77b02d11796cf435496fd22b85c430a7ed2479f /server
parent86d13ec2aa94ec10810ddf9c8b33314bd4968791 (diff)
downloadPeerTube-1f3e9feca2caf68024168b0ea9ed39d8438fa235.tar.gz
PeerTube-1f3e9feca2caf68024168b0ea9ed39d8438fa235.tar.zst
PeerTube-1f3e9feca2caf68024168b0ea9ed39d8438fa235.zip
Better view counter
Diffstat (limited to 'server')
-rw-r--r--server/controllers/api/videos/index.ts28
-rw-r--r--server/tests/api/multiple-servers.ts33
-rw-r--r--server/tests/api/single-server.ts5
-rw-r--r--server/tests/utils/videos.ts10
4 files changed, 48 insertions, 28 deletions
diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts
index 244d91914..e2798830e 100644
--- a/server/controllers/api/videos/index.ts
+++ b/server/controllers/api/videos/index.ts
@@ -104,6 +104,10 @@ videosRouter.get('/:id',
104 asyncMiddleware(videosGetValidator), 104 asyncMiddleware(videosGetValidator),
105 getVideo 105 getVideo
106) 106)
107videosRouter.post('/:id/views',
108 asyncMiddleware(videosGetValidator),
109 asyncMiddleware(viewVideo)
110)
107 111
108videosRouter.delete('/:id', 112videosRouter.delete('/:id',
109 authenticate, 113 authenticate,
@@ -311,25 +315,25 @@ async function updateVideo (req: express.Request, res: express.Response) {
311 } 315 }
312} 316}
313 317
314async function getVideo (req: express.Request, res: express.Response) { 318function getVideo (req: express.Request, res: express.Response) {
319 const videoInstance = res.locals.video
320
321 return res.json(videoInstance.toFormattedDetailsJSON())
322}
323
324async function viewVideo (req: express.Request, res: express.Response) {
315 const videoInstance = res.locals.video 325 const videoInstance = res.locals.video
316 326
317 const baseIncrementPromise = videoInstance.increment('views') 327 await videoInstance.increment('views')
318 .then(() => getServerAccount()) 328 const serverAccount = await getServerAccount()
319 329
320 if (videoInstance.isOwned()) { 330 if (videoInstance.isOwned()) {
321 // The increment is done directly in the database, not using the instance value 331 await sendCreateViewToVideoFollowers(serverAccount, videoInstance, undefined)
322 baseIncrementPromise
323 .then(serverAccount => sendCreateViewToVideoFollowers(serverAccount, videoInstance, undefined))
324 .catch(err => logger.error('Cannot add view to video/send view to followers for %s.', videoInstance.uuid, err))
325 } else { 332 } else {
326 baseIncrementPromise 333 await sendCreateViewToOrigin(serverAccount, videoInstance, undefined)
327 .then(serverAccount => sendCreateViewToOrigin(serverAccount, videoInstance, undefined))
328 .catch(err => logger.error('Cannot send view to origin server for %s.', videoInstance.uuid, err))
329 } 334 }
330 335
331 // Do not wait the view system 336 return res.status(204).end()
332 return res.json(videoInstance.toFormattedDetailsJSON())
333} 337}
334 338
335async function getVideoDescription (req: express.Request, res: express.Response) { 339async function getVideoDescription (req: express.Request, res: express.Response) {
diff --git a/server/tests/api/multiple-servers.ts b/server/tests/api/multiple-servers.ts
index 052b0231f..c80ded862 100644
--- a/server/tests/api/multiple-servers.ts
+++ b/server/tests/api/multiple-servers.ts
@@ -25,6 +25,7 @@ import {
25 doubleFollow 25 doubleFollow
26} from '../utils' 26} from '../utils'
27import { createUser } from '../utils/users' 27import { createUser } from '../utils/users'
28import { viewVideo } from '../utils/videos'
28 29
29const expect = chai.expect 30const expect = chai.expect
30 31
@@ -486,10 +487,10 @@ describe('Test multiple servers', function () {
486 this.timeout(10000) 487 this.timeout(10000)
487 488
488 const tasks: Promise<any>[] = [] 489 const tasks: Promise<any>[] = []
489 tasks.push(getVideo(servers[2].url, localVideosServer3[0])) 490 tasks.push(viewVideo(servers[2].url, localVideosServer3[0]))
490 tasks.push(getVideo(servers[2].url, localVideosServer3[0])) 491 tasks.push(viewVideo(servers[2].url, localVideosServer3[0]))
491 tasks.push(getVideo(servers[2].url, localVideosServer3[0])) 492 tasks.push(viewVideo(servers[2].url, localVideosServer3[0]))
492 tasks.push(getVideo(servers[2].url, localVideosServer3[1])) 493 tasks.push(viewVideo(servers[2].url, localVideosServer3[1]))
493 494
494 await Promise.all(tasks) 495 await Promise.all(tasks)
495 496
@@ -502,8 +503,8 @@ describe('Test multiple servers', function () {
502 const video0 = videos.find(v => v.uuid === localVideosServer3[0]) 503 const video0 = videos.find(v => v.uuid === localVideosServer3[0])
503 const video1 = videos.find(v => v.uuid === localVideosServer3[1]) 504 const video1 = videos.find(v => v.uuid === localVideosServer3[1])
504 505
505 expect(video0.views).to.equal(7) 506 expect(video0.views).to.equal(3)
506 expect(video1.views).to.equal(5) 507 expect(video1.views).to.equal(1)
507 } 508 }
508 }) 509 })
509 510
@@ -511,16 +512,16 @@ describe('Test multiple servers', function () {
511 this.timeout(15000) 512 this.timeout(15000)
512 513
513 const tasks: Promise<any>[] = [] 514 const tasks: Promise<any>[] = []
514 tasks.push(getVideo(servers[0].url, remoteVideosServer1[0])) 515 tasks.push(viewVideo(servers[0].url, remoteVideosServer1[0]))
515 tasks.push(getVideo(servers[1].url, remoteVideosServer2[0])) 516 tasks.push(viewVideo(servers[1].url, remoteVideosServer2[0]))
516 tasks.push(getVideo(servers[1].url, remoteVideosServer2[0])) 517 tasks.push(viewVideo(servers[1].url, remoteVideosServer2[0]))
517 tasks.push(getVideo(servers[2].url, remoteVideosServer3[0])) 518 tasks.push(viewVideo(servers[2].url, remoteVideosServer3[0]))
518 tasks.push(getVideo(servers[2].url, remoteVideosServer3[1])) 519 tasks.push(viewVideo(servers[2].url, remoteVideosServer3[1]))
519 tasks.push(getVideo(servers[2].url, remoteVideosServer3[1])) 520 tasks.push(viewVideo(servers[2].url, remoteVideosServer3[1]))
520 tasks.push(getVideo(servers[2].url, remoteVideosServer3[1])) 521 tasks.push(viewVideo(servers[2].url, remoteVideosServer3[1]))
521 tasks.push(getVideo(servers[2].url, localVideosServer3[1])) 522 tasks.push(viewVideo(servers[2].url, localVideosServer3[1]))
522 tasks.push(getVideo(servers[2].url, localVideosServer3[1])) 523 tasks.push(viewVideo(servers[2].url, localVideosServer3[1]))
523 tasks.push(getVideo(servers[2].url, localVideosServer3[1])) 524 tasks.push(viewVideo(servers[2].url, localVideosServer3[1]))
524 525
525 await Promise.all(tasks) 526 await Promise.all(tasks)
526 527
diff --git a/server/tests/api/single-server.ts b/server/tests/api/single-server.ts
index 40e2c64fe..041d13225 100644
--- a/server/tests/api/single-server.ts
+++ b/server/tests/api/single-server.ts
@@ -33,6 +33,7 @@ import {
33 searchVideoWithSort, 33 searchVideoWithSort,
34 updateVideo 34 updateVideo
35} from '../utils' 35} from '../utils'
36import { viewVideo } from '../utils/videos'
36 37
37describe('Test a single server', function () { 38describe('Test a single server', function () {
38 let server: ServerInfo = null 39 let server: ServerInfo = null
@@ -214,6 +215,10 @@ describe('Test a single server', function () {
214 }) 215 })
215 216
216 it('Should have the views updated', async function () { 217 it('Should have the views updated', async function () {
218 await viewVideo(server.url, videoId)
219 await viewVideo(server.url, videoId)
220 await viewVideo(server.url, videoId)
221
217 const res = await getVideo(server.url, videoId) 222 const res = await getVideo(server.url, videoId)
218 223
219 const video = res.body 224 const video = res.body
diff --git a/server/tests/utils/videos.ts b/server/tests/utils/videos.ts
index dababe924..73a9f1a0a 100644
--- a/server/tests/utils/videos.ts
+++ b/server/tests/utils/videos.ts
@@ -55,6 +55,15 @@ function getVideo (url: string, id: number | string, expectedStatus = 200) {
55 .expect(expectedStatus) 55 .expect(expectedStatus)
56} 56}
57 57
58function viewVideo (url: string, id: number | string, expectedStatus = 204) {
59 const path = '/api/v1/videos/' + id + '/views'
60
61 return request(url)
62 .post(path)
63 .set('Accept', 'application/json')
64 .expect(expectedStatus)
65}
66
58function getVideoWithToken (url: string, token: string, id: number | string, expectedStatus = 200) { 67function getVideoWithToken (url: string, token: string, id: number | string, expectedStatus = 200) {
59 const path = '/api/v1/videos/' + id 68 const path = '/api/v1/videos/' + id
60 69
@@ -313,5 +322,6 @@ export {
313 uploadVideo, 322 uploadVideo,
314 updateVideo, 323 updateVideo,
315 rateVideo, 324 rateVideo,
325 viewVideo,
316 parseTorrentVideo 326 parseTorrentVideo
317} 327}