2019-12-19 21:50:50 -08:00
|
|
|
let db = require('@config/database')
|
|
|
|
|
|
|
|
let Note = require('@models/Note')
|
|
|
|
|
|
|
|
let QuickNote = module.exports = {}
|
|
|
|
|
|
|
|
|
2020-05-15 16:12:09 -07:00
|
|
|
QuickNote.get = (userId, masterKey) => {
|
2019-12-19 21:50:50 -08:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
|
|
db.promise()
|
|
|
|
.query(`
|
2020-07-13 22:31:02 -07:00
|
|
|
SELECT note.id FROM note WHERE quick_note = 1 AND user_id = ? LIMIT 1`, [userId])
|
2019-12-19 21:50:50 -08:00
|
|
|
.then((rows, fields) => {
|
|
|
|
|
|
|
|
//Quick Note is set, return note text
|
2020-05-15 16:12:09 -07:00
|
|
|
if(rows[0][0] != undefined){
|
2020-07-13 22:31:02 -07:00
|
|
|
|
2020-05-15 16:12:09 -07:00
|
|
|
let noteId = rows[0][0].id
|
2020-07-13 22:31:02 -07:00
|
|
|
return resolve({'noteId':noteId})
|
|
|
|
|
2020-05-15 16:12:09 -07:00
|
|
|
} else {
|
2020-07-13 22:31:02 -07:00
|
|
|
//Or create a new note and get the id
|
|
|
|
let finalId = null
|
|
|
|
return Note.create(userId, 'Scratch Pad', '', masterKey)
|
|
|
|
.then(insertedId => {
|
|
|
|
finalId = insertedId
|
|
|
|
db.promise().query('UPDATE note SET quick_note = 1 WHERE id = ? AND user_id = ?',[insertedId, userId])
|
|
|
|
.then((rows, fields) => {
|
|
|
|
|
|
|
|
return resolve({'noteId':finalId})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-12-19 21:50:50 -08:00
|
|
|
}
|
|
|
|
|
2020-05-15 16:12:09 -07:00
|
|
|
|
2019-12-19 21:50:50 -08:00
|
|
|
})
|
|
|
|
.catch(console.log)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-15 16:12:09 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-05-18 00:45:35 -07:00
|
|
|
QuickNote.update = (userId, pushText, masterKey) => {
|
2019-12-19 21:50:50 -08:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
2020-05-15 16:12:09 -07:00
|
|
|
let finalId = null
|
|
|
|
let finalText = ''
|
|
|
|
|
2019-12-19 21:50:50 -08:00
|
|
|
//Process pushText, split at \n (new lines), put <p> tags around each new line
|
2020-05-15 16:12:09 -07:00
|
|
|
let broken = '<p>' +
|
2019-12-19 21:50:50 -08:00
|
|
|
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
|
|
|
|
|
2020-05-15 16:12:09 -07:00
|
|
|
//Turn links into actual linx
|
|
|
|
clean = QuickNote.makeUrlLink(clean)
|
|
|
|
|
2019-12-19 21:50:50 -08:00
|
|
|
if(clean == ''){ clean = ' ' }
|
|
|
|
let newLine = ''
|
|
|
|
if(index > 0){ newLine = '<br>' }
|
|
|
|
|
|
|
|
//Return line wrapped in p tags
|
|
|
|
return `${newLine}<span>${clean}</span>`
|
|
|
|
|
2020-05-15 16:12:09 -07:00
|
|
|
}).join('') + '</p><p><br></p>'
|
2019-12-19 21:50:50 -08:00
|
|
|
|
2020-05-15 16:12:09 -07:00
|
|
|
QuickNote.get(userId, masterKey)
|
|
|
|
.then(noteObject => {
|
2019-12-19 21:50:50 -08:00
|
|
|
|
2020-05-15 16:12:09 -07:00
|
|
|
if(noteObject == null){
|
2019-12-19 21:50:50 -08:00
|
|
|
|
2020-05-15 16:12:09 -07:00
|
|
|
finalText += broken
|
2019-12-19 21:50:50 -08:00
|
|
|
|
2020-05-18 00:45:35 -07:00
|
|
|
return Note.create(userId, 'Quick Note', finalText, masterKey)
|
2020-05-15 16:12:09 -07:00
|
|
|
.then(insertedId => {
|
|
|
|
finalId = insertedId
|
|
|
|
return db.promise().query('UPDATE note SET quick_note = 1 WHERE id = ? AND user_id = ?',[insertedId, userId])
|
2019-12-19 21:50:50 -08:00
|
|
|
})
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2020-05-15 16:12:09 -07:00
|
|
|
finalText += (broken + noteObject.text)
|
|
|
|
finalId = noteObject.id
|
2020-05-18 00:45:35 -07:00
|
|
|
return Note.update(userId, noteObject.id, finalText, noteObject.title, noteObject.color, noteObject.pinned, noteObject.archived, null, masterKey)
|
2019-12-19 21:50:50 -08:00
|
|
|
}
|
|
|
|
})
|
2020-05-15 16:12:09 -07:00
|
|
|
.then( saveResults => {
|
|
|
|
return resolve(true)
|
|
|
|
})
|
2019-12-19 21:50:50 -08:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|