]> 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 250ad3952b27e297712c75c0cf9da3bedc96edc2..7bd41f7eea7836bfc3ec5d1309a36a53bdb89500 100644 (file)
@@ -1,5 +1,6 @@
 'use strict'
 
+const async = require('async')
 const config = require('config')
 const mongoose = require('mongoose')
 
@@ -80,15 +81,9 @@ function get (id, callback) {
   })
 }
 
-function list (start, count, callback) {
-  VideosDB.find({}).skip(start).limit(start + count).exec(function (err, videosList) {
-    if (err) {
-      logger.error('Cannot get the list of the videos.')
-      return callback(err)
-    }
-
-    return callback(null, videosList)
-  })
+function list (start, count, sort, callback) {
+  const query = {}
+  return findWithCount(query, start, count, sort, callback)
 }
 
 function listFromUrl (fromUrl, callback) {
@@ -129,18 +124,37 @@ function removeByIds (ids, callback) {
   VideosDB.remove({ _id: { $in: ids } }, callback)
 }
 
-function search (name, start, count, callback) {
-  VideosDB.find({ name: new RegExp(name) }).skip(start).limit(start + count)
-  .exec(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)
+  })
+}