2019-07-19 13:51:57 -07:00
|
|
|
let db = require('@config/database')
|
|
|
|
|
2019-07-30 12:21:12 -07:00
|
|
|
let Tag = module.exports = {}
|
2019-07-19 13:51:57 -07:00
|
|
|
|
2020-02-11 21:29:56 -08:00
|
|
|
Tag.userTags = (userId, searchQuery, searchTags, fastFilters) => {
|
2020-01-02 17:26:55 -08:00
|
|
|
return new Promise((resolve, reject) => {
|
2020-02-11 21:29:56 -08:00
|
|
|
|
|
|
|
let query = `
|
|
|
|
SELECT
|
|
|
|
tag.id,
|
|
|
|
text,
|
|
|
|
COUNT(note_tag.note_id) as usages
|
|
|
|
FROM tag
|
2020-01-02 17:26:55 -08:00
|
|
|
JOIN note_tag ON tag.id = note_tag.tag_id
|
2020-02-11 21:29:56 -08:00
|
|
|
JOIN note On note.id = note_tag.note_id
|
2020-01-02 17:26:55 -08:00
|
|
|
WHERE note_tag.user_id = ?
|
2020-02-11 21:29:56 -08:00
|
|
|
`
|
|
|
|
|
|
|
|
//Show shared notes
|
2020-02-13 17:08:46 -08:00
|
|
|
if(fastFilters && fastFilters.onlyShowSharedNotes == 1){
|
2020-02-11 21:29:56 -08:00
|
|
|
query += ' AND note.share_user_id IS NOT NULL' //Show Archived
|
|
|
|
} else {
|
|
|
|
query += ' AND note.share_user_id IS NULL'
|
|
|
|
}
|
|
|
|
|
|
|
|
//Show archived notes, only if fast filter is set, default to not archived
|
2020-02-13 17:08:46 -08:00
|
|
|
if(fastFilters && fastFilters.onlyArchived == 1){
|
2020-02-11 21:29:56 -08:00
|
|
|
query += ' AND note.archived = 1' //Show Archived
|
|
|
|
} else {
|
|
|
|
query += ' AND note.archived = 0' //Exclude archived
|
|
|
|
}
|
|
|
|
|
|
|
|
query += ` GROUP BY tag.id
|
2020-02-13 17:08:46 -08:00
|
|
|
ORDER BY usages DESC, text ASC`
|
2020-02-11 21:29:56 -08:00
|
|
|
|
|
|
|
|
|
|
|
db.promise()
|
|
|
|
.query(query, [userId])
|
2020-01-02 17:26:55 -08:00
|
|
|
.then( (rows, fields) => {
|
|
|
|
resolve(rows[0])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2019-07-19 13:51:57 -07:00
|
|
|
|
2019-07-30 12:21:12 -07:00
|
|
|
Tag.removeTagFromNote = (userId, tagId) => {
|
2019-07-19 13:51:57 -07:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
db.promise()
|
2019-07-30 13:27:26 -07:00
|
|
|
.query(`DELETE FROM note_tag WHERE id = ? AND user_id = ? LIMIT 1;`, [tagId, userId])
|
2019-07-19 13:51:57 -07:00
|
|
|
.then((rows, fields) => {
|
|
|
|
resolve(rows[0]) //Return new ID
|
|
|
|
})
|
|
|
|
.catch(console.log)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-30 12:21:12 -07:00
|
|
|
Tag.addToNote = (userId, noteId, tagText) => {
|
2019-07-19 13:51:57 -07:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
|
|
//Lookup tag
|
2019-07-30 12:21:12 -07:00
|
|
|
Tag.lookup(tagText)
|
2019-07-19 13:51:57 -07:00
|
|
|
.then( lookup => {
|
|
|
|
|
|
|
|
//Tag does not exist, insert new tag, then associate it with a note
|
|
|
|
if(lookup.length == 0){
|
|
|
|
//Insert new tag
|
2019-07-30 12:21:12 -07:00
|
|
|
Tag.add(tagText)
|
2019-07-19 13:51:57 -07:00
|
|
|
.then( newTagId => {
|
2019-07-30 12:21:12 -07:00
|
|
|
Tag.associateWithNote(userId, noteId, newTagId)
|
2019-07-19 13:51:57 -07:00
|
|
|
.then( result => {
|
|
|
|
resolve(result)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
//Tag already exists, associate it with a note
|
|
|
|
if(lookup.length > 0){
|
2019-07-30 12:21:12 -07:00
|
|
|
Tag.associateWithNote(userId, noteId, lookup[0].id)
|
2019-07-19 13:51:57 -07:00
|
|
|
.then( result => {
|
|
|
|
resolve(result)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-30 12:21:12 -07:00
|
|
|
Tag.associateWithNote = (userId, noteId, tagId) => {
|
2019-07-19 13:51:57 -07:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
|
|
//Check if tag already exists on note before adding note
|
|
|
|
db.promise()
|
2019-07-30 13:27:26 -07:00
|
|
|
.query(`SELECT * FROM note_tag WHERE note_id = ? AND tag_id = ? AND user_id = ?;`, [noteId, tagId, userId])
|
2019-07-19 13:51:57 -07:00
|
|
|
.then((rows, fields) => {
|
|
|
|
|
|
|
|
//If matching tag does not exist on note
|
|
|
|
if(rows[0].length == 0){
|
|
|
|
|
|
|
|
//Add tag to note
|
|
|
|
db.promise()
|
2019-07-30 13:27:26 -07:00
|
|
|
.query(`INSERT INTO note_tag (note_id, tag_id, user_id) VALUES (?,?,?);`, [noteId, tagId, userId])
|
2019-07-19 13:51:57 -07:00
|
|
|
.then((rows, fields) => {
|
|
|
|
resolve(rows[0])
|
|
|
|
})
|
|
|
|
.catch(console.log)
|
|
|
|
} else {
|
|
|
|
|
|
|
|
reject('Error: Tag already exists on note.')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(console.log)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-30 12:21:12 -07:00
|
|
|
Tag.add = (tagText) => {
|
2019-07-19 13:51:57 -07:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
db.promise()
|
2019-07-30 13:27:26 -07:00
|
|
|
.query(`INSERT INTO tag (text, hash) VALUES (?,?);`, [tagText,0])
|
2019-07-19 13:51:57 -07:00
|
|
|
.then((rows, fields) => {
|
|
|
|
resolve(rows[0].insertId) //Return new ID
|
|
|
|
})
|
|
|
|
.catch(console.log)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-10 09:44:43 -08:00
|
|
|
//
|
|
|
|
// Get all tags AND tags associated to note
|
|
|
|
//
|
2019-07-30 12:21:12 -07:00
|
|
|
Tag.get = (userId, noteId) => {
|
2019-07-19 13:51:57 -07:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-09-10 11:10:11 -07:00
|
|
|
|
2020-02-10 09:44:43 -08:00
|
|
|
Tag.userTags(userId).then(userTags => {
|
|
|
|
db.promise()
|
|
|
|
.query(`SELECT tag_id as tagId, id as entryId
|
|
|
|
FROM note_tag
|
|
|
|
WHERE user_id = ? AND note_id = ?;`, [userId, noteId])
|
|
|
|
.then((rows, fields) => {
|
2019-09-10 11:10:11 -07:00
|
|
|
|
2020-02-10 09:44:43 -08:00
|
|
|
//pull IDs out of returned results
|
|
|
|
// let ids = rows[0].map( item => {})
|
|
|
|
|
|
|
|
resolve({'noteTagIds':rows[0], 'allTags':userTags }) //Return all tags found by query
|
|
|
|
})
|
|
|
|
.catch(console.log)
|
2019-07-19 13:51:57 -07:00
|
|
|
})
|
2020-02-10 09:44:43 -08:00
|
|
|
|
2019-07-19 13:51:57 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-10 09:44:43 -08:00
|
|
|
//
|
|
|
|
// Get all tags for a note and concatinate into a string 'all, tags, like, this'
|
|
|
|
//
|
2019-07-30 12:21:12 -07:00
|
|
|
Tag.string = (userId, noteId) => {
|
2019-07-24 11:06:50 -07:00
|
|
|
return new Promise((resolve, reject) => {
|
2020-02-10 09:44:43 -08:00
|
|
|
db.promise()
|
|
|
|
.query(`SELECT GROUP_CONCAT(DISTINCT tag.text SEPARATOR ', ') as text
|
|
|
|
FROM note_tag
|
|
|
|
JOIN tag ON note_tag.tag_id = tag.id
|
|
|
|
WHERE user_id = ? AND note_id = ?;`, [userId, noteId])
|
|
|
|
.then((rows, fields) => {
|
2019-07-24 11:06:50 -07:00
|
|
|
|
2020-02-10 09:44:43 -08:00
|
|
|
let finalText = rows[0][0]['text']
|
|
|
|
if(finalText == null){
|
|
|
|
finalText = ''
|
|
|
|
}
|
2019-07-24 11:06:50 -07:00
|
|
|
|
2020-02-10 09:44:43 -08:00
|
|
|
return resolve(finalText) //Return all tags found by query
|
2019-07-24 11:06:50 -07:00
|
|
|
})
|
2020-02-10 09:44:43 -08:00
|
|
|
.catch(console.log)
|
2019-07-24 11:06:50 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-30 12:21:12 -07:00
|
|
|
Tag.lookup = (tagText) => {
|
2019-07-19 13:51:57 -07:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
db.promise()
|
2019-07-30 13:27:26 -07:00
|
|
|
.query(`SELECT * FROM tag WHERE text = ?;`, [tagText])
|
2019-07-19 13:51:57 -07:00
|
|
|
.then((rows, fields) => {
|
|
|
|
resolve(rows[0]) //Return all tags found by query
|
|
|
|
})
|
|
|
|
.catch(console.log)
|
|
|
|
})
|
2019-07-21 09:28:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//Suggest note tags - don't suggest tags already on note
|
2019-07-30 12:21:12 -07:00
|
|
|
Tag.suggest = (userId, noteId, tagText) => {
|
2019-07-21 09:28:07 -07:00
|
|
|
|
|
|
|
tagText += '%'
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
db.promise()
|
2019-07-30 13:27:26 -07:00
|
|
|
.query(`SELECT text FROM note_tag
|
|
|
|
JOIN tag ON note_tag.tag_id = tag.id
|
|
|
|
WHERE note_tag.user_id = ?
|
|
|
|
AND tag.text LIKE ?
|
|
|
|
AND note_tag.tag_id NOT IN (
|
|
|
|
SELECT note_tag.tag_id FROM note_tag WHERE note_tag.note_id = ?
|
2019-07-21 09:28:07 -07:00
|
|
|
)
|
|
|
|
GROUP BY text
|
2019-09-10 11:10:11 -07:00
|
|
|
LIMIT 6`, [userId, tagText, noteId])
|
2019-07-21 09:28:07 -07:00
|
|
|
.then((rows, fields) => {
|
|
|
|
resolve(rows[0]) //Return new ID
|
|
|
|
})
|
|
|
|
.catch(console.log)
|
|
|
|
})
|
2019-07-30 13:27:26 -07:00
|
|
|
}
|
|
|
|
|
2019-09-10 11:10:11 -07:00
|
|
|
//Latest Tags - don't suggest tags already on note
|
2019-07-30 13:27:26 -07:00
|
|
|
Tag.latest = (userId, noteId) => {
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
db.promise()
|
|
|
|
.query(`SELECT tag.text FROM note_tag
|
|
|
|
JOIN tag ON note_tag.tag_id = tag.id
|
|
|
|
JOIN note ON note_tag.note_id = note.id
|
2020-02-23 21:59:13 -08:00
|
|
|
JOIN note_raw_text ON (note_raw_text.id = note.note_raw_text_id)
|
2019-07-30 13:27:26 -07:00
|
|
|
WHERE note_tag.user_id = ?
|
|
|
|
AND note_tag.tag_id NOT IN (
|
|
|
|
SELECT note_tag.tag_id FROM note_tag WHERE note_tag.note_id = ?
|
|
|
|
)
|
2020-02-23 21:59:13 -08:00
|
|
|
GROUP BY tag.text, note_raw_text.updated
|
|
|
|
ORDER BY note_raw_text.updated DESC
|
2019-09-10 11:10:11 -07:00
|
|
|
LIMIT 8;`, [userId, noteId])
|
2019-07-30 13:27:26 -07:00
|
|
|
.then((rows, fields) => {
|
2019-09-10 11:10:11 -07:00
|
|
|
resolve(rows[0]) //Return found tags
|
2019-07-30 13:27:26 -07:00
|
|
|
})
|
|
|
|
.catch(console.log)
|
|
|
|
})
|
2019-07-19 13:51:57 -07:00
|
|
|
}
|