Client: add views information and sort
[oweals/peertube.git] / server / models / video.js
index b3060705d9cd981dfd6c4ecd4bf74caa77b6516a..daa273845f5610de2b90f85df4a405a2cda2cb59 100644 (file)
@@ -80,6 +80,15 @@ module.exports = function (sequelize, DataTypes) {
             if (res === false) throw new Error('Video duration is not valid.')
           }
         }
+      },
+      views: {
+        type: DataTypes.INTEGER,
+        allowNull: false,
+        defaultValue: 0,
+        validate: {
+          min: 0,
+          isInt: true
+        }
       }
     },
     {
@@ -101,6 +110,9 @@ module.exports = function (sequelize, DataTypes) {
         },
         {
           fields: [ 'infoHash' ]
+        },
+        {
+          fields: [ 'views' ]
         }
       ],
       classMethods: {
@@ -141,7 +153,8 @@ module.exports = function (sequelize, DataTypes) {
 }
 
 function beforeValidate (video, options, next) {
-  if (video.isOwned()) {
+  // Put a fake infoHash if it does not exists yet
+  if (video.isOwned() && !video.infoHash) {
     // 40 hexa length
     video.infoHash = '0123456789abcdef0123456789abcdef01234567'
   }
@@ -156,8 +169,7 @@ function beforeCreate (video, options, next) {
     const videoPath = pathUtils.join(constants.CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename())
 
     tasks.push(
-      // TODO: refractoring
-      function (callback) {
+      function createVideoTorrent (callback) {
         const options = {
           announceList: [
             [ constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT + '/tracker/socket' ]
@@ -170,7 +182,8 @@ function beforeCreate (video, options, next) {
         createTorrent(videoPath, options, function (err, torrent) {
           if (err) return callback(err)
 
-          fs.writeFile(constants.CONFIG.STORAGE.TORRENTS_DIR + video.getTorrentName(), torrent, function (err) {
+          const filePath = pathUtils.join(constants.CONFIG.STORAGE.TORRENTS_DIR, video.getTorrentName())
+          fs.writeFile(filePath, torrent, function (err) {
             if (err) return callback(err)
 
             const parsedTorrent = parseTorrent(torrent)
@@ -179,10 +192,12 @@ function beforeCreate (video, options, next) {
           })
         })
       },
-      function (callback) {
+
+      function createVideoThumbnail (callback) {
         createThumbnail(video, videoPath, callback)
       },
-      function (callback) {
+
+      function createVIdeoPreview (callback) {
         createPreview(video, videoPath, callback)
       }
     )
@@ -204,19 +219,19 @@ function afterDestroy (video, options, next) {
 
   if (video.isOwned()) {
     tasks.push(
-      function (callback) {
+      function removeVideoFile (callback) {
         removeFile(video, callback)
       },
 
-      function (callback) {
+      function removeVideoTorrent (callback) {
         removeTorrent(video, callback)
       },
 
-      function (callback) {
+      function removeVideoPreview (callback) {
         removePreview(video, callback)
       },
 
-      function (callback) {
+      function removeVideoToFriends (callback) {
         const params = {
           remoteId: video.id
         }
@@ -333,8 +348,9 @@ function toFormatedJSON () {
     magnetUri: this.generateMagnetUri(),
     author: this.Author.name,
     duration: this.duration,
+    views: this.views,
     tags: map(this.Tags, 'name'),
-    thumbnailPath: constants.STATIC_PATHS.THUMBNAILS + '/' + this.getThumbnailName(),
+    thumbnailPath: pathUtils.join(constants.STATIC_PATHS.THUMBNAILS, this.getThumbnailName()),
     createdAt: this.createdAt,
     updatedAt: this.updatedAt
   }
@@ -394,7 +410,7 @@ function generateThumbnailFromData (video, thumbnailData, callback) {
   // Creating the thumbnail for a remote video
 
   const thumbnailName = video.getThumbnailName()
-  const thumbnailPath = constants.CONFIG.STORAGE.THUMBNAILS_DIR + thumbnailName
+  const thumbnailPath = pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, thumbnailName)
   fs.writeFile(thumbnailPath, Buffer.from(thumbnailData, 'binary'), function (err) {
     if (err) return callback(err)
 
@@ -411,7 +427,7 @@ function getDurationFromFile (videoPath, callback) {
 }
 
 function list (callback) {
-  return this.find().asCallback()
+  return this.findAll().asCallback(callback)
 }
 
 function listForApi (start, count, sort, callback) {
@@ -595,15 +611,18 @@ function searchAndPopulateAuthorAndPodAndTags (value, field, start, count, sort,
 // ---------------------------------------------------------------------------
 
 function removeThumbnail (video, callback) {
-  fs.unlink(constants.CONFIG.STORAGE.THUMBNAILS_DIR + video.getThumbnailName(), callback)
+  const thumbnailPath = pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName())
+  fs.unlink(thumbnailPath, callback)
 }
 
 function removeFile (video, callback) {
-  fs.unlink(constants.CONFIG.STORAGE.VIDEOS_DIR + video.getVideoFilename(), callback)
+  const filePath = pathUtils.join(constants.CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename())
+  fs.unlink(filePath, callback)
 }
 
 function removeTorrent (video, callback) {
-  fs.unlink(constants.CONFIG.STORAGE.TORRENTS_DIR + video.getTorrentName(), callback)
+  const torrenPath = pathUtils.join(constants.CONFIG.STORAGE.TORRENTS_DIR, video.getTorrentName())
+  fs.unlink(torrenPath, callback)
 }
 
 function removePreview (video, callback) {