From ba7e19a49893893650b104018ef1c527e80019ce Mon Sep 17 00:00:00 2001 From: bill bittner Date: Thu, 21 Sep 2017 11:00:06 -0700 Subject: [PATCH] fixed associations between User and Certificate --- controllers/publishController.js | 11 ++++-- migrations/Remove-UserId-Add-CertificateId.js | 8 ++-- passport/local-signup.js | 39 +++++++++++-------- speech.js | 4 +- 4 files changed, 37 insertions(+), 25 deletions(-) diff --git a/controllers/publishController.js b/controllers/publishController.js index 39bc43ef..d9de1d43 100644 --- a/controllers/publishController.js +++ b/controllers/publishController.js @@ -7,14 +7,14 @@ module.exports = { publish (publishParams, fileName, fileType) { return new Promise((resolve, reject) => { let publishResults = {}; - // 1. make sure the name is (still) available + // 1. make sure the name is available publishHelpers.checkClaimNameAvailability(publishParams.name) // 2. publish the file .then(result => { if (result === true) { return lbryApi.publishClaim(publishParams); } else { - return new Error('That name has already been claimed on spee.ch.'); + return new Error('That name has already been claimed by spee.ch.'); } }) // 3. upsert File record (update is in case the claim has been published before by this daemon) @@ -40,10 +40,15 @@ module.exports = { name : publishParams.name, claimId: publishResults.claim_id, }; + // create the records return Promise.all([db.upsert(db.File, fileRecord, upsertCriteria, 'File'), db.upsert(db.Claim, fileRecord, upsertCriteria, 'Claim')]); }) - .then(() => { + .then((file, claim) => { logger.debug('File and Claim records successfully created'); + return Promise.all([file.setClaims(claim), claim.setFiles(file)]); + }) + .then(() => { + logger.debug('File and Claim records successfully associated'); resolve(publishResults); // resolve the promise with the result from lbryApi.publishClaim; }) .catch(error => { diff --git a/migrations/Remove-UserId-Add-CertificateId.js b/migrations/Remove-UserId-Add-CertificateId.js index fa90482f..942ac792 100644 --- a/migrations/Remove-UserId-Add-CertificateId.js +++ b/migrations/Remove-UserId-Add-CertificateId.js @@ -18,12 +18,12 @@ module.exports = { down: (queryInterface, Sequelize) => { // logic for reverting the changes const p1 = queryInterface.removeColumn( - 'Certificate', - 'UserId' + 'User', + 'CertificateId' ); const p2 = queryInterface.addColumn( - 'User', - 'CertificateId', + 'Certificate', + 'UserId', { type : Sequelize.STRING, allowNull: true, diff --git a/passport/local-signup.js b/passport/local-signup.js index 0f7b8a0c..1700a3e0 100644 --- a/passport/local-signup.js +++ b/passport/local-signup.js @@ -14,11 +14,21 @@ module.exports = new PassportLocalStrategy( (req, username, password, done) => { logger.debug('new channel signup request'); const address = config.get('WalletConfig.LbryClaimAddress'); - // server-side validaton of raw inputs (username, password) + let user; + let certificate; + // server-side validaton of inputs (username, password) // create the channel and retrieve the metadata return lbryApi.createChannel(username) .then(channelTx => { + // create user record + const userData = { + channelName : username, + channelClaimId: channelTx.claim_id, + password : password, + address, + }; + logger.debug('userData >', userData); // create certificate record const certificateData = { address, @@ -26,24 +36,19 @@ module.exports = new PassportLocalStrategy( name : username, }; logger.debug('certificateData >', certificateData); - return db.Certificate.create(certificateData); + // save user and certificate to db + return Promise.all([db.User.create(userData), db.Certificate.create(certificateData)]); }) - .then(certificate => { - logger.debug('certificate result >', certificate.dataValues); - logger.debug('Certificate record was created successfully'); - // define an object that contains all the user data - const userData = { - channelName : username, - channelClaimId: certificate.claimId, - password : password, - address, - CertificateId : certificate.id, - }; - logger.debug('userData >', userData); - return db.User.create(userData); - }).then(user => { + .then(result => { + user = result[0]; + certificate = result[1]; + logger.debug('user and certificate successfully created'); logger.debug('user result >', user.dataValues); - logger.debug('User record was created successfully'); + logger.debug('certificate result >', certificate.dataValues); + // associate the instances + return Promise.all([certificate.setUser(user), user.setCertificate(certificate)]); + }).then(result => { + logger.debug('user and certificate successfully associated'); return done(null, user); }) .catch(error => { diff --git a/speech.js b/speech.js index 2f8066c1..d6df24f0 100644 --- a/speech.js +++ b/speech.js @@ -45,8 +45,10 @@ passport.serializeUser((user, done) => { passport.deserializeUser((id, done) => { db.User.findOne({ where: { id } }) .then(user => { - done(null, user); // user.dataValues? + done(null, user); + return null; }) + .then() .catch(error => { logger.error('sequelize error', error); done(error, null);