aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/author.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-12-29 18:02:03 +0100
committerChocobozzz <florian.bigard@gmail.com>2016-12-29 18:02:03 +0100
commit4ff0d86208dafbdd07beb6286fd93c795db8a95f (patch)
treef24b12065afd2e9ec0c89f806961803dfcb678a8 /server/models/author.js
parentd396a937b642d616beb72dde54c0c2d37c7e8c30 (diff)
downloadPeerTube-4ff0d86208dafbdd07beb6286fd93c795db8a95f.tar.gz
PeerTube-4ff0d86208dafbdd07beb6286fd93c795db8a95f.tar.zst
PeerTube-4ff0d86208dafbdd07beb6286fd93c795db8a95f.zip
Server: little refractoring
Diffstat (limited to 'server/models/author.js')
-rw-r--r--server/models/author.js29
1 files changed, 28 insertions, 1 deletions
diff --git a/server/models/author.js b/server/models/author.js
index 5835ada99..7d15fb6ec 100644
--- a/server/models/author.js
+++ b/server/models/author.js
@@ -29,7 +29,9 @@ module.exports = function (sequelize, DataTypes) {
29 } 29 }
30 ], 30 ],
31 classMethods: { 31 classMethods: {
32 associate 32 associate,
33
34 findOrCreateAuthor
33 } 35 }
34 } 36 }
35 ) 37 )
@@ -56,3 +58,28 @@ function associate (models) {
56 onDelete: 'cascade' 58 onDelete: 'cascade'
57 }) 59 })
58} 60}
61
62function findOrCreateAuthor (name, podId, userId, transaction, callback) {
63 if (!callback) {
64 callback = transaction
65 transaction = null
66 }
67
68 const author = {
69 name,
70 podId,
71 userId
72 }
73
74 const query = {
75 where: author,
76 defaults: author
77 }
78
79 if (transaction) query.transaction = transaction
80
81 this.findOrCreate(query).asCallback(function (err, result) {
82 // [ instance, wasCreated ]
83 return callback(err, result[0])
84 })
85}