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 object if(rows[0][0] != undefined){ let noteId = rows[0][0].id const note = Note.get(userId, noteId, masterKey) .then(noteData => { return resolve(noteData) }) } else { //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}) }) }) } }) .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, '$1'); //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$2'); //Change email addresses to mailto:: links. replacePattern3 = /(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim; replacedText = replacedText.replace(replacePattern3, '$1'); 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

tags around each new line let broken = '

' + 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 link clean = QuickNote.makeUrlLink(clean) if(clean == ''){ clean = ' ' } let newLine = '' if(index > 0){ newLine = '
' } //Return line wrapped in p tags return `${newLine}${clean}` }).join('') + '


' 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(saveResults) }) }) }