* Removed arrows from notification

* Added trash can function
* Tweaked status text to always be the same
* Removed some open second note code
* Edior always focuses on text now
* Added some extra loading note messages
* Notes are now removed from search index when deleted
* Lots more things happen and update in real time on multiple machines
* Shared notes can be reverted
* WAY more tests
* Note Categories are much more reliable
* Lots of code is much cleaner
This commit is contained in:
Max G committed 2020-05-18 07:45:35 +00:00
1 parent e87e8513bc
commit 5096e74a60
18 files changed
+644 -368

No files matched your search

+77 -46
View File
@@ -19,10 +19,8 @@ User.login = (username, password) => {
.query('SELECT * FROM user WHERE username = ? LIMIT 1', [lowerName])
.then((rows, fields) => {
//Pull out user data from database results
const lookedUpUser = rows[0][0];
//User not found, create a new account with set data
// Create New Account
//
if(rows[0].length == 0){
User.create(lowerName, password)
.then( ({token, userId}) => {
@@ -30,7 +28,13 @@ User.login = (username, password) => {
})
}
// Login User
//
if(rows[0].length == 1){
//Pull out user data from database results
const lookedUpUser = rows[0][0]
//hash the password and check for a match
// const salt = new Buffer(lookedUpUser.salt, 'binary')
const salt = Buffer.from(lookedUpUser.salt, 'binary')
@@ -102,37 +106,33 @@ User.create = (username, password) => {
created: currentDate
};
let userId = null
let newMasterKey = null
db.promise()
.query('INSERT INTO user SET ?', new_user)
.then((rows, fields) => {
if(rows[0].affectedRows == 1){
userId = rows[0].insertId
return User.generateMasterKey(userId, password)
})
.then( result => {
const userId = rows[0].insertId
return User.getMasterKey(userId, password)
})
.then(masterKey => {
newMasterKey = masterKey
return User.generateKeypair(userId, newMasterKey)
})
.then(({publicKey, privateKey}) => {
User.generateMasterKey(userId, password)
.then( result => User.getMasterKey(userId, password))
.then(masterKey => {
User.generateKeypair(userId, masterKey)
.then(({publicKey, privateKey}) => {
const token = Auth.createToken(userId, masterKey)
return resolve({token, userId})
})
})
} else {
//Emit Error to user
reject('New user could not be created')
}
const token = Auth.createToken(userId, newMasterKey)
return resolve({token, userId})
})
.catch(console.log)
})
} else {
reject('Username already in use.')
return reject('Username already in use.')
}//END user create
})
.catch(console.log)
@@ -149,22 +149,24 @@ User.getCounts = (userId) => {
db.promise().query(
`SELECT
SUM(pinned = 1 && archived = 0 && share_user_id IS NULL) AS pinnedNotes,
SUM(archived = 1 && share_user_id IS NULL) AS archivedNotes,
SUM(encrypted = 1) AS encryptedNotes,
SUM(share_user_id IS NULL) AS totalNotes,
SUM(share_user_id != ?) AS sharedToNotes,
SUM( (share_user_id != ? && opened IS null) || (share_user_id != ? && note_raw_text.updated > opened) ) AS unreadNotes
SUM(archived = 1 && share_user_id IS NULL && trashed = 0) AS archivedNotes,
SUM(trashed = 1) AS trashedNotes,
SUM(share_user_id IS NULL && trashed = 0) AS totalNotes,
SUM(share_user_id != ? && trashed = 0) AS sharedToNotes,
SUM( (share_user_id != ? && opened IS null && trashed = 0) || (share_user_id != ? && note_raw_text.updated > opened && trashed = 0) ) AS unreadNotes
FROM note
LEFT JOIN note_raw_text ON (note_raw_text.id = note.note_raw_text_id)
WHERE user_id = ?`, [userId, userId, userId, userId])
.then( (rows, fields) => {
Object.assign(countTotals, rows[0][0]) //combine results
//
// @TODO - Figured out if this is useful
// We want, notes shared with user and note user has shared
//
return db.promise().query(
`SELECT count(id) AS sharedFromNotes
FROM note WHERE share_user_id = ?`, [userId]
FROM note WHERE shared = 2 AND user_id = ? AND trashed = 0`, [userId]
)
})
.then( (rows, fields) => {
@@ -228,18 +230,12 @@ User.generateMasterKey = (userId, password) => {
'INSERT INTO user_key (`user_id`, `salt`, `key`, `created`) VALUES (?, ?, ?, ?);',
[userId, salt, encryptedMasterPassword, created]
)
.then((rows, fields)=>{
return Note.encryptEveryNote(userId, masterPassword)
})
.then(results => {
return new Promise((resolve, reject) => { resolve(true) })
return resolve(true)
})
}
})
.then((rows, fields) => {
return resolve(true)
})
.catch(error => {
console.log('Create Master Password Error')
console.log(error)
@@ -261,6 +257,10 @@ User.getMasterKey = (userId, password) => {
const row = rows[0][0]
if(!rows[0] || rows[0].length == 0 || rows[0][0] == undefined){
return reject('Row or salt or something not set')
}
const masterKey = cs.decrypt(password, row['salt'], row['key'])
if(masterKey == null){
@@ -370,25 +370,56 @@ User.getByUserName = (username) => {
User.deleteUser = (userId, password) => {
//Verify user is correct by decryptig master key with password
let deletePromises = []
//Delete user, all notes, all keys
let noteDelete = db.promise().query(`
DELETE note, note_raw_text
FROM note
JOIN note_raw_text ON (note_raw_text.id = note.note_raw_text_id)
WHERE note.user_id = ?
`,[userId])
deletePromises.push(noteDelete)
let userDelete = db.promise().query(`
DELETE FROM user WHERE id = ?
`,[userId])
deletePromises.push(userDelete)
let tables = ['user_key', 'user_encrypted_search_index', 'attachment']
tables.forEach(tableName => {
const query = `DELETE FROM ${tableName} WHERE user_id = ?`
const deleteQuery = db.promise().query(query, [userId])
deletePromises.push(deleteQuery)
})
return Promise.all(deletePromises)
}
User.keyPairTest = (testUserName = 'genMan', password = '1') => {
User.keyPairTest = (testUserName = 'genMan', password = '1', printResults) => {
return new Promise((resolve, reject) => {
let masterKey = null
let testUserId = null
const randomUsername = Math.random().toString(36).substring(2, 15);
const randomPassword = '1'
User.login(testUserName, password)
.then( ({ token, userId }) => {
testUserId = userId
console.log('Test: Create/Login User - Pass')
if(printResults) console.log('Test: Create/Login User '+testUserName+' - Pass')
return User.getMasterKey(testUserId, password)
})
.then(newMasterKey => {
masterKey = newMasterKey
console.log('Test: Generate/Decrypt Master Key - Pass')
if(printResults) console.log('Test: Generate/Decrypt Master Key - Pass')
return User.generateKeypair(testUserId, masterKey)
})
.then(({publicKey, privateKey}) => {
@@ -400,13 +431,13 @@ User.keyPairTest = (testUserName = 'genMan', password = '1') => {
const privateKeyEncrypted = crypto.privateEncrypt(privateKey, Buffer.from(privateKeyMessage, 'utf8')).toString('base64')
const decryptedPrivate = crypto.publicDecrypt(publicKey, Buffer.from(privateKeyEncrypted, 'base64'))
//Conver back to a string
console.log(decryptedPrivate.toString('utf8'))
if(printResults) console.log(decryptedPrivate.toString('utf8'))
//Encrypt with public key
const pubEncrMsc = crypto.publicEncrypt(publicKey, Buffer.from(publicKeyMessage, 'utf8')).toString('base64')
const publicDeccryptMessage = crypto.privateDecrypt(privateKey, Buffer.from(pubEncrMsc, 'base64') )
//Convert it back to string
console.log(publicDeccryptMessage.toString('utf8'))
if(printResults) console.log(publicDeccryptMessage.toString('utf8'))
resolve({testUserId, masterKey})
})