]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/videos.js
Extends the search feature by customizing the search field (name,
[github/Chocobozzz/PeerTube.git] / server / models / videos.js
index eedb6eb587b493c5dd075d9e160d953e26830cd8..7bd41f7eea7836bfc3ec5d1309a36a53bdb89500 100644 (file)
@@ -1,5 +1,6 @@
 'use strict'
 
+const async = require('async')
 const config = require('config')
 const mongoose = require('mongoose')
 
@@ -19,7 +20,11 @@ const videosSchema = mongoose.Schema({
   podUrl: String,
   author: String,
   duration: Number,
-  thumbnail: String
+  thumbnail: String,
+  createdDate: {
+    type: Date,
+    default: Date.now
+  }
 })
 const VideosDB = mongoose.model('videos', videosSchema)
 
@@ -46,13 +51,13 @@ function add (video, callback) {
   const params = video
   params.podUrl = http + '://' + host + ':' + port
 
-  VideosDB.create(params, function (err, video) {
+  VideosDB.create(params, function (err, insertedVideo) {
     if (err) {
       logger.error('Cannot insert this video into database.')
       return callback(err)
     }
 
-    callback(null)
+    callback(null, insertedVideo)
   })
 }
 
@@ -76,15 +81,9 @@ function get (id, callback) {
   })
 }
 
-function list (callback) {
-  VideosDB.find(function (err, videos_list) {
-    if (err) {
-      logger.error('Cannot get the list of the videos.')
-      return callback(err)
-    }
-
-    return callback(null, videos_list)
-  })
+function list (start, count, sort, callback) {
+  const query = {}
+  return findWithCount(query, start, count, sort, callback)
 }
 
 function listFromUrl (fromUrl, callback) {
@@ -105,13 +104,13 @@ function listFromRemotes (callback) {
 
 function listOwned (callback) {
   // If namePath is not null this is *our* video
-  VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) {
+  VideosDB.find({ namePath: { $ne: null } }, function (err, videosList) {
     if (err) {
       logger.error('Cannot get the list of owned videos.')
       return callback(err)
     }
 
-    return callback(null, videos_list)
+    return callback(null, videosList)
   })
 }
 
@@ -125,17 +124,37 @@ function removeByIds (ids, callback) {
   VideosDB.remove({ _id: { $in: ids } }, callback)
 }
 
-function search (name, callback) {
-  VideosDB.find({ name: new RegExp(name) }, function (err, videos) {
-    if (err) {
-      logger.error('Cannot search the videos.')
-      return callback(err)
-    }
+function search (value, field, start, count, sort, callback) {
+  const query = {}
+  // Make an exact search with the magnet
+  if (field === 'magnetUri') {
+    query[field] = value
+  } else {
+    query[field] = new RegExp(value)
+  }
 
-    return callback(null, videos)
-  })
+  findWithCount(query, start, count, sort, callback)
 }
 
 // ---------------------------------------------------------------------------
 
 module.exports = Videos
+
+// ---------------------------------------------------------------------------
+
+function findWithCount (query, start, count, sort, callback) {
+  async.parallel([
+    function (asyncCallback) {
+      VideosDB.find(query).skip(start).limit(start + count).sort(sort).exec(asyncCallback)
+    },
+    function (asyncCallback) {
+      VideosDB.count(query, asyncCallback)
+    }
+  ], function (err, results) {
+    if (err) return callback(err)
+
+    const videos = results[0]
+    const totalVideos = results[1]
+    return callback(null, videos, totalVideos)
+  })
+}