Server: add association between author and user

This commit is contained in:
Chocobozzz 2016-12-29 10:33:36 +01:00
parent 319d072e8e
commit 4712081f2a
6 changed files with 35 additions and 14 deletions

View File

@ -84,11 +84,13 @@ function addRemoteVideo (videoToCreateData, fromHost, finalCallback) {
const query = { const query = {
where: { where: {
name: username, name: username,
podId: pod.id podId: pod.id,
userId: null
}, },
defaults: { defaults: {
name: username, name: username,
podId: pod.id podId: pod.id,
userId: null
}, },
transaction: t transaction: t
} }

View File

@ -95,23 +95,26 @@ function addVideo (req, res, next) {
}, },
function findOrCreateAuthor (t, callback) { function findOrCreateAuthor (t, callback) {
const username = res.locals.oauth.token.user.username const user = res.locals.oauth.token.User
const query = { const query = {
where: { where: {
name: username, name: user.username,
podId: null podId: null,
userId: user.id
}, },
defaults: { defaults: {
name: username, name: user.username,
podId: null // null because it is OUR pod podId: null, // null because it is OUR pod
userId: user.id
}, },
transaction: t transaction: t
} }
db.Author.findOrCreate(query).asCallback(function (err, result) { db.Author.findOrCreate(query).asCallback(function (err, result) {
// [ instance, wasCreated ] const authorInstance = result[0]
return callback(err, t, result[0])
return callback(err, t, authorInstance)
}) })
}, },

View File

@ -42,10 +42,10 @@ function checkMissedConfig () {
} }
function clientsExist (callback) { function clientsExist (callback) {
db.OAuthClient.list(function (err, clients) { db.OAuthClient.countTotal(function (err, totalClients) {
if (err) return callback(err) if (err) return callback(err)
return callback(null, clients.length !== 0) return callback(null, totalClients !== 0)
}) })
} }

View File

@ -23,6 +23,9 @@ module.exports = function (sequelize, DataTypes) {
}, },
{ {
fields: [ 'podId' ] fields: [ 'podId' ]
},
{
fields: [ 'userId' ]
} }
], ],
classMethods: { classMethods: {
@ -44,4 +47,12 @@ function associate (models) {
}, },
onDelete: 'cascade' onDelete: 'cascade'
}) })
this.belongsTo(models.User, {
foreignKey: {
name: 'userId',
allowNull: true
},
onDelete: 'cascade'
})
} }

View File

@ -30,8 +30,8 @@ module.exports = function (sequelize, DataTypes) {
} }
], ],
classMethods: { classMethods: {
countTotal,
getByIdAndSecret, getByIdAndSecret,
list,
loadFirstClient loadFirstClient
} }
} }
@ -42,8 +42,8 @@ module.exports = function (sequelize, DataTypes) {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function list (callback) { function countTotal (callback) {
return this.findAll().asCallback(callback) return this.count().asCallback(callback)
} }
function loadFirstClient (callback) { function loadFirstClient (callback) {

View File

@ -94,6 +94,11 @@ function toFormatedJSON () {
// ------------------------------ STATICS ------------------------------ // ------------------------------ STATICS ------------------------------
function associate (models) { function associate (models) {
this.hasOne(models.Author, {
foreignKey: 'userId',
onDelete: 'cascade'
})
this.hasMany(models.OAuthToken, { this.hasMany(models.OAuthToken, {
foreignKey: 'userId', foreignKey: 'userId',
onDelete: 'cascade' onDelete: 'cascade'