diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-12-29 10:33:36 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-12-29 10:56:07 +0100 |
commit | 4712081f2a5f48749cf125d729e78b926ab28d6d (patch) | |
tree | b0f2f380f6fe5403fb89fb5cfdbe9deafea1c889 /server | |
parent | 319d072e8eb7266cd8d33e0bb2fb5ebe76c487d1 (diff) | |
download | PeerTube-4712081f2a5f48749cf125d729e78b926ab28d6d.tar.gz PeerTube-4712081f2a5f48749cf125d729e78b926ab28d6d.tar.zst PeerTube-4712081f2a5f48749cf125d729e78b926ab28d6d.zip |
Server: add association between author and user
Diffstat (limited to 'server')
-rw-r--r-- | server/controllers/api/remote.js | 6 | ||||
-rw-r--r-- | server/controllers/api/videos.js | 17 | ||||
-rw-r--r-- | server/initializers/checker.js | 4 | ||||
-rw-r--r-- | server/models/author.js | 11 | ||||
-rw-r--r-- | server/models/oauth-client.js | 6 | ||||
-rw-r--r-- | server/models/user.js | 5 |
6 files changed, 35 insertions, 14 deletions
diff --git a/server/controllers/api/remote.js b/server/controllers/api/remote.js index 2cf916ff3..94d6e740e 100644 --- a/server/controllers/api/remote.js +++ b/server/controllers/api/remote.js | |||
@@ -84,11 +84,13 @@ function addRemoteVideo (videoToCreateData, fromHost, finalCallback) { | |||
84 | const query = { | 84 | const query = { |
85 | where: { | 85 | where: { |
86 | name: username, | 86 | name: username, |
87 | podId: pod.id | 87 | podId: pod.id, |
88 | userId: null | ||
88 | }, | 89 | }, |
89 | defaults: { | 90 | defaults: { |
90 | name: username, | 91 | name: username, |
91 | podId: pod.id | 92 | podId: pod.id, |
93 | userId: null | ||
92 | }, | 94 | }, |
93 | transaction: t | 95 | transaction: t |
94 | } | 96 | } |
diff --git a/server/controllers/api/videos.js b/server/controllers/api/videos.js index 992f03db0..170224634 100644 --- a/server/controllers/api/videos.js +++ b/server/controllers/api/videos.js | |||
@@ -95,23 +95,26 @@ function addVideo (req, res, next) { | |||
95 | }, | 95 | }, |
96 | 96 | ||
97 | function findOrCreateAuthor (t, callback) { | 97 | function findOrCreateAuthor (t, callback) { |
98 | const username = res.locals.oauth.token.user.username | 98 | const user = res.locals.oauth.token.User |
99 | 99 | ||
100 | const query = { | 100 | const query = { |
101 | where: { | 101 | where: { |
102 | name: username, | 102 | name: user.username, |
103 | podId: null | 103 | podId: null, |
104 | userId: user.id | ||
104 | }, | 105 | }, |
105 | defaults: { | 106 | defaults: { |
106 | name: username, | 107 | name: user.username, |
107 | podId: null // null because it is OUR pod | 108 | podId: null, // null because it is OUR pod |
109 | userId: user.id | ||
108 | }, | 110 | }, |
109 | transaction: t | 111 | transaction: t |
110 | } | 112 | } |
111 | 113 | ||
112 | db.Author.findOrCreate(query).asCallback(function (err, result) { | 114 | db.Author.findOrCreate(query).asCallback(function (err, result) { |
113 | // [ instance, wasCreated ] | 115 | const authorInstance = result[0] |
114 | return callback(err, t, result[0]) | 116 | |
117 | return callback(err, t, authorInstance) | ||
115 | }) | 118 | }) |
116 | }, | 119 | }, |
117 | 120 | ||
diff --git a/server/initializers/checker.js b/server/initializers/checker.js index 2753604dc..6471bb4f1 100644 --- a/server/initializers/checker.js +++ b/server/initializers/checker.js | |||
@@ -42,10 +42,10 @@ function checkMissedConfig () { | |||
42 | } | 42 | } |
43 | 43 | ||
44 | function clientsExist (callback) { | 44 | function clientsExist (callback) { |
45 | db.OAuthClient.list(function (err, clients) { | 45 | db.OAuthClient.countTotal(function (err, totalClients) { |
46 | if (err) return callback(err) | 46 | if (err) return callback(err) |
47 | 47 | ||
48 | return callback(null, clients.length !== 0) | 48 | return callback(null, totalClients !== 0) |
49 | }) | 49 | }) |
50 | } | 50 | } |
51 | 51 | ||
diff --git a/server/models/author.js b/server/models/author.js index 8f5b598c8..5835ada99 100644 --- a/server/models/author.js +++ b/server/models/author.js | |||
@@ -23,6 +23,9 @@ module.exports = function (sequelize, DataTypes) { | |||
23 | }, | 23 | }, |
24 | { | 24 | { |
25 | fields: [ 'podId' ] | 25 | fields: [ 'podId' ] |
26 | }, | ||
27 | { | ||
28 | fields: [ 'userId' ] | ||
26 | } | 29 | } |
27 | ], | 30 | ], |
28 | classMethods: { | 31 | classMethods: { |
@@ -44,4 +47,12 @@ function associate (models) { | |||
44 | }, | 47 | }, |
45 | onDelete: 'cascade' | 48 | onDelete: 'cascade' |
46 | }) | 49 | }) |
50 | |||
51 | this.belongsTo(models.User, { | ||
52 | foreignKey: { | ||
53 | name: 'userId', | ||
54 | allowNull: true | ||
55 | }, | ||
56 | onDelete: 'cascade' | ||
57 | }) | ||
47 | } | 58 | } |
diff --git a/server/models/oauth-client.js b/server/models/oauth-client.js index 758c4cf2f..021a34007 100644 --- a/server/models/oauth-client.js +++ b/server/models/oauth-client.js | |||
@@ -30,8 +30,8 @@ module.exports = function (sequelize, DataTypes) { | |||
30 | } | 30 | } |
31 | ], | 31 | ], |
32 | classMethods: { | 32 | classMethods: { |
33 | countTotal, | ||
33 | getByIdAndSecret, | 34 | getByIdAndSecret, |
34 | list, | ||
35 | loadFirstClient | 35 | loadFirstClient |
36 | } | 36 | } |
37 | } | 37 | } |
@@ -42,8 +42,8 @@ module.exports = function (sequelize, DataTypes) { | |||
42 | 42 | ||
43 | // --------------------------------------------------------------------------- | 43 | // --------------------------------------------------------------------------- |
44 | 44 | ||
45 | function list (callback) { | 45 | function countTotal (callback) { |
46 | return this.findAll().asCallback(callback) | 46 | return this.count().asCallback(callback) |
47 | } | 47 | } |
48 | 48 | ||
49 | function loadFirstClient (callback) { | 49 | function loadFirstClient (callback) { |
diff --git a/server/models/user.js b/server/models/user.js index 631cd96c9..36ed723cc 100644 --- a/server/models/user.js +++ b/server/models/user.js | |||
@@ -94,6 +94,11 @@ function toFormatedJSON () { | |||
94 | // ------------------------------ STATICS ------------------------------ | 94 | // ------------------------------ STATICS ------------------------------ |
95 | 95 | ||
96 | function associate (models) { | 96 | function associate (models) { |
97 | this.hasOne(models.Author, { | ||
98 | foreignKey: 'userId', | ||
99 | onDelete: 'cascade' | ||
100 | }) | ||
101 | |||
97 | this.hasMany(models.OAuthToken, { | 102 | this.hasMany(models.OAuthToken, { |
98 | foreignKey: 'userId', | 103 | foreignKey: 'userId', |
99 | onDelete: 'cascade' | 104 | onDelete: 'cascade' |