SolidScribe/server/models/QuickNote.js
Max G 5096e74a60 * 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
2020-05-18 07:45:35 +00:00

112 lines
3.0 KiB
JavaScript

let db = require('@config/database')
let Note = require('@models/Note')
let QuickNote = module.exports = {}
QuickNote.get = (userId, masterKey) => {
return new Promise((resolve, reject) => {
db.promise()
.query(`
SELECT note.id FROM note WHERE quick_note = 1 AND user_id = ? LIMIT 1
`, [userId])
.then((rows, fields) => {
//Quick Note is set, return note text
if(rows[0][0] != undefined){
let noteId = rows[0][0].id
Note.get(userId, noteId, masterKey)
.then( noteObject => {
return resolve(noteObject)
})
} else {
return resolve(null)
}
})
.catch(console.log)
})
}
QuickNote.newNote = (userId) => {
return new Promise((resolve, reject) => {
db.promise().query('UPDATE note SET quick_note = 0 WHERE quick_note = 1 AND user_id = ?',[userId])
.then((rows, fields) => {
resolve(true)
})
})
}
QuickNote.makeUrlLink = (inputText) => {
var replacedText, replacePattern1, replacePattern2, replacePattern3;
//URLs starting with http://, https://, or ftp://
replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');
//URLs starting with "www." (without // before it, or it'd re-link the ones done above).
replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');
//Change email addresses to mailto:: links.
replacePattern3 = /(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim;
replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');
return replacedText;
}
QuickNote.update = (userId, pushText, masterKey) => {
return new Promise((resolve, reject) => {
let finalId = null
let finalText = ''
//Process pushText, split at \n (new lines), put <p> tags around each new line
let broken = '<p>' +
pushText.split(/\r?\n/).map( (line, index) => {
let clean = line
.replace(/&[#A-Za-z0-9]+;/g,'') //Rip out all HTML entities
.replace(/<[^>]+>/g, '') //Rip out all HTML tags
//Turn links into actual linx
clean = QuickNote.makeUrlLink(clean)
if(clean == ''){ clean = '&nbsp;' }
let newLine = ''
if(index > 0){ newLine = '<br>' }
//Return line wrapped in p tags
return `${newLine}<span>${clean}</span>`
}).join('') + '</p><p><br></p>'
QuickNote.get(userId, masterKey)
.then(noteObject => {
if(noteObject == null){
finalText += broken
return Note.create(userId, 'Quick Note', finalText, masterKey)
.then(insertedId => {
finalId = insertedId
return db.promise().query('UPDATE note SET quick_note = 1 WHERE id = ? AND user_id = ?',[insertedId, userId])
})
} else {
finalText += (broken + noteObject.text)
finalId = noteObject.id
return Note.update(userId, noteObject.id, finalText, noteObject.title, noteObject.color, noteObject.pinned, noteObject.archived, null, masterKey)
}
})
.then( saveResults => {
return resolve(true)
})
})
}