Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

Server: add association between author and user

Chocobozzz4712081f2a5f
diff --git a/server/controllers/api/remote.js b/server/controllers/api/remote.js
index 2cf916ff3a..94d6e740e8 100644
--- a/server/controllers/api/remote.js
+++ b/server/controllers/api/remote.js
@@ -84,11 +84,13 @@ function addRemoteVideo (videoToCreateData, fromHost, finalCallback) {
const query = {
where: {
name: username,
- podId: pod.id
+ podId: pod.id,
+ userId: null
},
defaults: {
name: username,
- podId: pod.id
+ podId: pod.id,
+ userId: null
},
transaction: t
}
diff --git a/server/controllers/api/videos.js b/server/controllers/api/videos.js
index 992f03db0c..1702246349 100644
--- a/server/controllers/api/videos.js
+++ b/server/controllers/api/videos.js
@@ -95,23 +95,26 @@ function addVideo (req, res, next) {
},
function findOrCreateAuthor (t, callback) {
- const username = res.locals.oauth.token.user.username
+ const user = res.locals.oauth.token.User
const query = {
where: {
- name: username,
- podId: null
+ name: user.username,
+ podId: null,
+ userId: user.id
},
defaults: {
- name: username,
- podId: null // null because it is OUR pod
+ name: user.username,
+ podId: null, // null because it is OUR pod
+ userId: user.id
},
transaction: t
}
db.Author.findOrCreate(query).asCallback(function (err, result) {
- // [ instance, wasCreated ]
- return callback(err, t, result[0])
+ const authorInstance = result[0]
+
+ return callback(err, t, authorInstance)
})
},
diff --git a/server/initializers/checker.js b/server/initializers/checker.js
index 2753604dc4..6471bb4f11 100644
--- a/server/initializers/checker.js
+++ b/server/initializers/checker.js
@@ -42,10 +42,10 @@ function checkMissedConfig () {
}
function clientsExist (callback) {
- db.OAuthClient.list(function (err, clients) {
+ db.OAuthClient.countTotal(function (err, totalClients) {
if (err) return callback(err)
- return callback(null, clients.length !== 0)
+ return callback(null, totalClients !== 0)
})
}
diff --git a/server/models/author.js b/server/models/author.js
index 8f5b598c8e..5835ada994 100644
--- a/server/models/author.js
+++ b/server/models/author.js
@@ -23,6 +23,9 @@ module.exports = function (sequelize, DataTypes) {
},
{
fields: [ 'podId' ]
+ },
+ {
+ fields: [ 'userId' ]
}
],
classMethods: {
@@ -44,4 +47,12 @@ function associate (models) {
},
onDelete: 'cascade'
})
+
+ this.belongsTo(models.User, {
+ foreignKey: {
+ name: 'userId',
+ allowNull: true
+ },
+ onDelete: 'cascade'
+ })
}
diff --git a/server/models/oauth-client.js b/server/models/oauth-client.js
index 758c4cf2f6..021a340075 100644
--- a/server/models/oauth-client.js
+++ b/server/models/oauth-client.js
@@ -30,8 +30,8 @@ module.exports = function (sequelize, DataTypes) {
}
],
classMethods: {
+ countTotal,
getByIdAndSecret,
- list,
loadFirstClient
}
}
@@ -42,8 +42,8 @@ module.exports = function (sequelize, DataTypes) {
// ---------------------------------------------------------------------------
-function list (callback) {
- return this.findAll().asCallback(callback)
+function countTotal (callback) {
+ return this.count().asCallback(callback)
}
function loadFirstClient (callback) {
diff --git a/server/models/user.js b/server/models/user.js
index 631cd96c98..36ed723ccd 100644
--- a/server/models/user.js
+++ b/server/models/user.js
@@ -94,6 +94,11 @@ function toFormatedJSON () {
// ------------------------------ STATICS ------------------------------
function associate (models) {
+ this.hasOne(models.Author, {
+ foreignKey: 'userId',
+ onDelete: 'cascade'
+ })
+
this.hasMany(models.OAuthToken, {
foreignKey: 'userId',
onDelete: 'cascade'