Change api output for videos
authorChocobozzz <florian.bigard@gmail.com>
Fri, 18 Mar 2016 15:28:09 +0000 (16:28 +0100)
committerChocobozzz <florian.bigard@gmail.com>
Fri, 18 Mar 2016 15:28:09 +0000 (16:28 +0100)
client/angular/videos/components/list/videos-list.component.html
client/angular/videos/models/video.ts
server/controllers/api/v1/videos.js
server/lib/videos.js
server/middlewares/reqValidators/videos.js
server/tests/api/multiplePods.js
server/tests/api/singlePod.js
server/tests/api/utils.js

index 7ecdacee47c817fd86e3184389d06ddb5df00bcd..38708aff613c0db8f4958ebbe53488d17d9a5bd1 100644 (file)
@@ -1,8 +1,8 @@
 <div *ngFor="#video of videos" class="video">
   <div>
-    <a [routerLink]="['VideosWatch', { id: video._id }]" class="video_name">{{ video.name }}</a>
+    <a [routerLink]="['VideosWatch', { id: video.id }]" class="video_name">{{ video.name }}</a>
     <span class="video_pod_url">{{ video.podUrl }}</span>
-    <span *ngIf="video.namePath !== null" (click)="removeVideo(video._id)" class="video_remove glyphicon glyphicon-remove"></span>
+    <span *ngIf="video.isLocal === true" (click)="removeVideo(video.id)" class="video_remove glyphicon glyphicon-remove"></span>
   </div>
 
   <div class="video_description">
index 2f998c49a40bcf4737f059c09f58d5681b12bf1f..e52c6d8861fb4ad5971358b2e7447e7a552346bf 100644 (file)
@@ -1,6 +1,8 @@
 export interface Video {
-  _id: string;
+  id: string;
   name: string;
   description: string;
   magnetUri: string;
+  podUrl: string;
+  isLocal: boolean;
 }
index 1eea417d413ae0d327d9cf869a27fa808421e196..4384724c144b793c7d260facd305e48e8413ad1e 100644 (file)
@@ -83,14 +83,15 @@ function addVideo (req, res, next) {
 }
 
 function getVideos (req, res, next) {
-  Videos.get(req.params.id, function (err, video) {
+  Videos.get(req.params.id, function (err, video_obj) {
     if (err) return next(err)
 
-    if (video === null) {
-      res.type('json').status(204).end()
+    const state = videos.getVideoState(video_obj)
+    if (state.exist === false) {
+      return res.type('json').status(204).end()
     }
 
-    res.json(video)
+    res.json(getFormatedVideo(video_obj))
   })
 }
 
@@ -98,7 +99,7 @@ function listVideos (req, res, next) {
   Videos.list(function (err, videos_list) {
     if (err) return next(err)
 
-    res.json(videos_list)
+    res.json(getFormatedVideos(videos_list))
   })
 }
 
@@ -127,12 +128,35 @@ function searchVideos (req, res, next) {
   Videos.search(req.params.name, function (err, videos_list) {
     if (err) return next(err)
 
-    res.json(videos_list)
+    res.json(getFormatedVideos(videos_list))
   })
 }
 
 // ---------------------------------------------------------------------------
 
+function getFormatedVideo (video_obj) {
+  const formated_video = {
+    id: video_obj._id,
+    name: video_obj.name,
+    description: video_obj.description,
+    podUrl: video_obj.podUrl,
+    isLocal: videos.getVideoState(video_obj).owned,
+    magnetUri: video_obj.magnetUri
+  }
+
+  return formated_video
+}
+
+function getFormatedVideos (videos_obj) {
+  const formated_videos = []
+
+  videos_obj.forEach(function (video_obj) {
+    formated_videos.push(getFormatedVideo(video_obj))
+  })
+
+  return formated_videos
+}
+
 // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process
 function removeTorrent (magnetUri, callback) {
   try {
index eb3a0125a8bbeec9612b5c179230d72125d387dd..24178561d9c2dbcf0ea751b0c6893b5873f7809f 100644 (file)
@@ -16,14 +16,14 @@ const videos = {
   seedAllExisting: seedAllExisting
 }
 
-function getVideoState (video, callback) {
+function getVideoState (video) {
   const exist = (video !== null)
   let owned = false
   if (exist === true) {
     owned = (video.namePath !== null)
   }
 
-  return callback({ exist: exist, owned: owned })
+  return { exist: exist, owned: owned }
 }
 
 function seed (path, callback) {
index 4057e72cd3a75996243b974740bf011386226170..12e878e81169f8c6ef58e2bdf1ed23e14160b2da 100644 (file)
@@ -35,11 +35,10 @@ function videosGet (req, res, next) {
         res.sendStatus(500)
       }
 
-      videos.getVideoState(video, function (state) {
-        if (state.exist === false) return res.status(404).send('Video not found')
+      const state = videos.getVideoState(video)
+      if (state.exist === false) return res.status(404).send('Video not found')
 
-        next()
-      })
+      next()
     })
   })
 }
@@ -56,12 +55,11 @@ function videosRemove (req, res, next) {
         res.sendStatus(500)
       }
 
-      videos.getVideoState(video, function (state) {
-        if (state.exist === false) return res.status(404).send('Video not found')
-        else if (state.owned === false) return res.status(403).send('Cannot remove video of another pod')
+      const state = videos.getVideoState(video)
+      if (state.exist === false) return res.status(404).send('Video not found')
+      else if (state.owned === false) return res.status(403).send('Cannot remove video of another pod')
 
-        next()
-      })
+      next()
     })
   })
 }
index e8b1826221016119a2e6d849fa1bb884fb42f2db..0e2355a5534d167188945f66a9270659f249547e 100644 (file)
@@ -205,8 +205,8 @@ describe('Test multiple pods', function () {
         if (err) throw err
 
         const video = res.body[0]
-        to_remove.push(res.body[2]._id)
-        to_remove.push(res.body[3]._id)
+        to_remove.push(res.body[2].id)
+        to_remove.push(res.body[3].id)
 
         webtorrent.add(video.magnetUri, function (torrent) {
           expect(torrent.files).to.exist
@@ -300,11 +300,11 @@ describe('Test multiple pods', function () {
           const videos = res.body
           expect(videos).to.be.an('array')
           expect(videos.length).to.equal(2)
-          expect(videos[0]._id).not.to.equal(videos[1]._id)
-          expect(videos[0]._id).not.to.equal(to_remove[0])
-          expect(videos[1]._id).not.to.equal(to_remove[0])
-          expect(videos[0]._id).not.to.equal(to_remove[1])
-          expect(videos[1]._id).not.to.equal(to_remove[1])
+          expect(videos[0].id).not.to.equal(videos[1].id)
+          expect(videos[0].id).not.to.equal(to_remove[0])
+          expect(videos[1].id).not.to.equal(to_remove[0])
+          expect(videos[0].id).not.to.equal(to_remove[1])
+          expect(videos[1].id).not.to.equal(to_remove[1])
 
           callback()
         })
index 14f893f13e6d313d9a82935a2c054923f0577043..0b96f221a0cfe13b8f8eff6d8d47eab18ad4bbde 100644 (file)
@@ -68,7 +68,30 @@ describe('Test a single pod', function () {
       expect(video.podUrl).to.equal('http://localhost:9001')
       expect(video.magnetUri).to.exist
 
-      video_id = video._id
+      video_id = video.id
+
+      webtorrent.add(video.magnetUri, function (torrent) {
+        expect(torrent.files).to.exist
+        expect(torrent.files.length).to.equal(1)
+        expect(torrent.files[0].path).to.exist.and.to.not.equal('')
+
+        done()
+      })
+    })
+  })
+
+  it('Should get the video', function (done) {
+    // Yes, this could be long
+    this.timeout(60000)
+
+    utils.getVideo(url, video_id, function (err, res) {
+      if (err) throw err
+
+      const video = res.body
+      expect(video.name).to.equal('my super name')
+      expect(video.description).to.equal('my super description')
+      expect(video.podUrl).to.equal('http://localhost:9001')
+      expect(video.magnetUri).to.exist
 
       webtorrent.add(video.magnetUri, function (torrent) {
         expect(torrent.files).to.exist
@@ -91,7 +114,6 @@ describe('Test a single pod', function () {
       expect(video.name).to.equal('my super name')
       expect(video.description).to.equal('my super description')
       expect(video.podUrl).to.equal('http://localhost:9001')
-      expect(video.magnetUri).to.exist
 
       done()
     })
index 05142085f148c3199f249abb294417b19c4f3c0f..ea0982e816f86212049b1f157ad57af642813960 100644 (file)
@@ -9,6 +9,7 @@ const request = require('supertest')
 const testUtils = {
   flushTests: flushTests,
   getFriendsList: getFriendsList,
+  getVideo: getVideo,
   getVideosList: getVideosList,
   makeFriends: makeFriends,
   quitFriends: quitFriends,
@@ -36,6 +37,17 @@ function getFriendsList (url, end) {
     .end(end)
 }
 
+function getVideo (url, id, end) {
+  const path = '/api/v1/videos/' + id
+
+  request(url)
+    .get(path)
+    .set('Accept', 'application/json')
+    .expect(200)
+    .expect('Content-Type', /json/)
+    .end(end)
+}
+
 function getVideosList (url, end) {
   const path = '/api/v1/videos'