]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/author.js
Update bittorrent-tracker and standard to v9
[github/Chocobozzz/PeerTube.git] / server / models / author.js
index 5835ada994a5f7fe984546b3841d6d51c7a32526..34b0130971e12db40f7cd4043f2985734d3ba6f5 100644 (file)
@@ -25,11 +25,18 @@ module.exports = function (sequelize, DataTypes) {
           fields: [ 'podId' ]
         },
         {
-          fields: [ 'userId' ]
+          fields: [ 'userId' ],
+          unique: true
+        },
+        {
+          fields: [ 'name', 'podId' ],
+          unique: true
         }
       ],
       classMethods: {
-        associate
+        associate,
+
+        findOrCreateAuthor
       }
     }
   )
@@ -56,3 +63,30 @@ 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) {
+    if (err) return callback(err)
+
+    // [ instance, wasCreated ]
+    return callback(null, result[0])
+  })
+}