]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/pod.js
Server: transaction serializable for videos
[github/Chocobozzz/PeerTube.git] / server / models / pod.js
index 8e7dd1fd8d7f71a805ff087fd98072ef4f3df9a7..b3c6db8e881aa45e09afe688310ce3d8de6d37c2 100644 (file)
@@ -50,6 +50,7 @@ module.exports = function (sequelize, DataTypes) {
         incrementScores,
         list,
         listAllIds,
+        listRandomPodIdsWithRequest,
         listBadPods,
         load,
         loadByHost,
@@ -134,6 +135,42 @@ function listAllIds (transaction, callback) {
   })
 }
 
+function listRandomPodIdsWithRequest (limit, callback) {
+  const self = this
+
+  self.count().asCallback(function (err, count) {
+    if (err) return callback(err)
+
+    // Optimization...
+    if (count === 0) return callback(null, [])
+
+    let start = Math.floor(Math.random() * count) - limit
+    if (start < 0) start = 0
+
+    const query = {
+      attributes: [ 'id' ],
+      order: [
+        [ 'id', 'ASC' ]
+      ],
+      offset: start,
+      limit: limit,
+      where: {
+        id: {
+          $in: [
+            this.sequelize.literal('SELECT "podId" FROM "RequestToPods"')
+          ]
+        }
+      }
+    }
+
+    return this.findAll(query).asCallback(function (err, pods) {
+      if (err) return callback(err)
+
+      return callback(null, map(pods, 'id'))
+    })
+  })
+}
+
 function listBadPods (callback) {
   const query = {
     where: {