]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/author.js
Server: do not break remote videos processing on error
[github/Chocobozzz/PeerTube.git] / server / models / author.js
index 5835ada994a5f7fe984546b3841d6d51c7a32526..7d15fb6ecd92ba4baa28a8c6f9efacd5e5f521d2 100644 (file)
@@ -29,7 +29,9 @@ module.exports = function (sequelize, DataTypes) {
         }
       ],
       classMethods: {
-        associate
+        associate,
+
+        findOrCreateAuthor
       }
     }
   )
@@ -56,3 +58,28 @@ function associate (models) {
     onDelete: 'cascade'
   })
 }
+
+function findOrCreateAuthor (name, podId, userId, transaction, callback) {
+  if (!callback) {
+    callback = transaction
+    transaction = null
+  }
+
+  const author = {
+    name,
+    podId,
+    userId
+  }
+
+  const query = {
+    where: author,
+    defaults: author
+  }
+
+  if (transaction) query.transaction = transaction
+
+  this.findOrCreate(query).asCallback(function (err, result) {
+    // [ instance, wasCreated ]
+    return callback(err, result[0])
+  })
+}