2017-03-14 01:02:43 -04:00
|
|
|
'use strict'
|
2017-03-08 05:45:51 -05:00
|
|
|
// history
|
|
|
|
// external modules
|
2021-02-15 03:42:51 -05:00
|
|
|
const LZString = require('lz-string')
|
2016-10-10 08:51:46 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
// core
|
2021-02-15 03:42:51 -05:00
|
|
|
const logger = require('./logger')
|
|
|
|
const models = require('./models')
|
2019-10-27 08:51:53 -04:00
|
|
|
const errors = require('./errors')
|
2016-10-10 08:51:46 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
// public
|
2021-02-15 03:42:51 -05:00
|
|
|
const History = {
|
2017-03-08 05:45:51 -05:00
|
|
|
historyGet: historyGet,
|
|
|
|
historyPost: historyPost,
|
|
|
|
historyDelete: historyDelete,
|
|
|
|
updateHistory: updateHistory
|
2017-02-03 08:39:08 -05:00
|
|
|
}
|
2016-11-16 00:58:54 -05:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function getHistory (userid, callback) {
|
|
|
|
models.User.findOne({
|
|
|
|
where: {
|
|
|
|
id: userid
|
|
|
|
}
|
|
|
|
}).then(function (user) {
|
|
|
|
if (!user) {
|
|
|
|
return callback(null, null)
|
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
let history = {}
|
2017-03-08 05:45:51 -05:00
|
|
|
if (user.history) {
|
2018-02-26 03:46:59 -05:00
|
|
|
history = JSON.parse(user.history)
|
|
|
|
// migrate LZString encoded note id to base64url encoded note id
|
|
|
|
for (let i = 0, l = history.length; i < l; i++) {
|
2018-07-27 07:44:45 -04:00
|
|
|
// Calculate minimal string length for an UUID that is encoded
|
|
|
|
// base64 encoded and optimize comparsion by using -1
|
|
|
|
// this should make a lot of LZ-String parsing errors obsolete
|
|
|
|
// as we can assume that a nodeId that is 48 chars or longer is a
|
|
|
|
// noteID.
|
|
|
|
const base64UuidLength = ((4 * 36) / 3) - 1
|
|
|
|
if (!(history[i].id.length > base64UuidLength)) {
|
|
|
|
continue
|
|
|
|
}
|
2018-03-10 03:51:00 -05:00
|
|
|
try {
|
2021-02-15 03:42:51 -05:00
|
|
|
const id = LZString.decompressFromBase64(history[i].id)
|
2018-02-26 03:46:59 -05:00
|
|
|
if (id && models.Note.checkNoteIdValid(id)) {
|
|
|
|
history[i].id = models.Note.encodeNoteId(id)
|
|
|
|
}
|
2018-03-10 03:51:00 -05:00
|
|
|
} catch (err) {
|
|
|
|
// most error here comes from LZString, ignore
|
2018-07-27 07:56:07 -04:00
|
|
|
if (err.message === 'Cannot read property \'charAt\' of undefined') {
|
|
|
|
logger.warning('Looks like we can not decode "' + history[i].id + '" with LZString. Can be ignored.')
|
|
|
|
} else {
|
|
|
|
logger.error(err)
|
|
|
|
}
|
2018-02-26 03:46:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
history = parseHistoryToObject(history)
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|
2019-06-08 14:51:24 -04:00
|
|
|
logger.debug(`read history success: ${user.id}`)
|
2017-03-08 05:45:51 -05:00
|
|
|
return callback(null, history)
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error('read history failed: ' + err)
|
|
|
|
return callback(err, null)
|
|
|
|
})
|
2016-11-16 00:58:54 -05:00
|
|
|
}
|
2016-10-10 08:51:46 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function setHistory (userid, history, callback) {
|
|
|
|
models.User.update({
|
|
|
|
history: JSON.stringify(parseHistoryToArray(history))
|
|
|
|
}, {
|
|
|
|
where: {
|
|
|
|
id: userid
|
2016-10-10 08:51:46 -04:00
|
|
|
}
|
2017-03-08 05:45:51 -05:00
|
|
|
}).then(function (count) {
|
|
|
|
return callback(null, count)
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error('set history failed: ' + err)
|
|
|
|
return callback(err, null)
|
|
|
|
})
|
2016-10-10 08:51:46 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function updateHistory (userid, noteId, document, time) {
|
|
|
|
if (userid && noteId && typeof document !== 'undefined') {
|
|
|
|
getHistory(userid, function (err, history) {
|
|
|
|
if (err || !history) return
|
|
|
|
if (!history[noteId]) {
|
|
|
|
history[noteId] = {}
|
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
const noteHistory = history[noteId]
|
|
|
|
const noteInfo = models.Note.parseNoteInfo(document)
|
2017-03-08 05:45:51 -05:00
|
|
|
noteHistory.id = noteId
|
|
|
|
noteHistory.text = noteInfo.title
|
|
|
|
noteHistory.time = time || Date.now()
|
|
|
|
noteHistory.tags = noteInfo.tags
|
|
|
|
setHistory(userid, history, function (err, count) {
|
|
|
|
if (err) {
|
|
|
|
logger.log(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2016-11-16 00:58:54 -05:00
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function parseHistoryToArray (history) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const _history = []
|
2017-03-08 05:45:51 -05:00
|
|
|
Object.keys(history).forEach(function (key) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const item = history[key]
|
2017-03-08 05:45:51 -05:00
|
|
|
_history.push(item)
|
|
|
|
})
|
|
|
|
return _history
|
2016-11-16 00:58:54 -05:00
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function parseHistoryToObject (history) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const _history = {}
|
|
|
|
for (let i = 0, l = history.length; i < l; i++) {
|
|
|
|
const item = history[i]
|
2017-03-08 05:45:51 -05:00
|
|
|
_history[item.id] = item
|
|
|
|
}
|
|
|
|
return _history
|
|
|
|
}
|
|
|
|
|
|
|
|
function historyGet (req, res) {
|
|
|
|
if (req.isAuthenticated()) {
|
|
|
|
getHistory(req.user.id, function (err, history) {
|
2019-10-27 08:51:53 -04:00
|
|
|
if (err) return errors.errorInternalError(res)
|
|
|
|
if (!history) return errors.errorNotFound(res)
|
2017-03-08 05:45:51 -05:00
|
|
|
res.send({
|
|
|
|
history: parseHistoryToArray(history)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
} else {
|
2019-10-27 08:51:53 -04:00
|
|
|
return errors.errorForbidden(res)
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|
2016-10-10 08:51:46 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function historyPost (req, res) {
|
|
|
|
if (req.isAuthenticated()) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const noteId = req.params.noteId
|
2017-03-08 05:45:51 -05:00
|
|
|
if (!noteId) {
|
2021-02-15 03:42:51 -05:00
|
|
|
if (typeof req.body.history === 'undefined') return errors.errorBadRequest(res)
|
2019-06-08 14:51:24 -04:00
|
|
|
logger.debug(`SERVER received history from [${req.user.id}]: ${req.body.history}`)
|
2017-03-08 05:45:51 -05:00
|
|
|
try {
|
2021-02-15 03:42:51 -05:00
|
|
|
const history = JSON.parse(req.body.history)
|
|
|
|
if (Array.isArray(history)) {
|
|
|
|
setHistory(req.user.id, history, function (err, count) {
|
|
|
|
if (err) return errors.errorInternalError(res)
|
|
|
|
res.end()
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
return errors.errorBadRequest(res)
|
|
|
|
}
|
2017-03-08 05:45:51 -05:00
|
|
|
} catch (err) {
|
2019-10-27 08:51:53 -04:00
|
|
|
return errors.errorBadRequest(res)
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|
|
|
|
} else {
|
2021-02-15 03:42:51 -05:00
|
|
|
if (typeof req.body.pinned === 'undefined') return errors.errorBadRequest(res)
|
2017-03-08 05:45:51 -05:00
|
|
|
getHistory(req.user.id, function (err, history) {
|
2019-10-27 08:51:53 -04:00
|
|
|
if (err) return errors.errorInternalError(res)
|
|
|
|
if (!history) return errors.errorNotFound(res)
|
|
|
|
if (!history[noteId]) return errors.errorNotFound(res)
|
2017-03-08 05:45:51 -05:00
|
|
|
if (req.body.pinned === 'true' || req.body.pinned === 'false') {
|
|
|
|
history[noteId].pinned = (req.body.pinned === 'true')
|
|
|
|
setHistory(req.user.id, history, function (err, count) {
|
2019-10-27 08:51:53 -04:00
|
|
|
if (err) return errors.errorInternalError(res)
|
2017-03-08 05:45:51 -05:00
|
|
|
res.end()
|
|
|
|
})
|
2016-10-10 08:51:46 -04:00
|
|
|
} else {
|
2019-10-27 08:51:53 -04:00
|
|
|
return errors.errorBadRequest(res)
|
2016-10-10 08:51:46 -04:00
|
|
|
}
|
2017-03-08 05:45:51 -05:00
|
|
|
})
|
2016-10-10 08:51:46 -04:00
|
|
|
}
|
2017-03-08 05:45:51 -05:00
|
|
|
} else {
|
2019-10-27 08:51:53 -04:00
|
|
|
return errors.errorForbidden(res)
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|
2016-10-10 08:51:46 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function historyDelete (req, res) {
|
|
|
|
if (req.isAuthenticated()) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const noteId = req.params.noteId
|
2017-03-08 05:45:51 -05:00
|
|
|
if (!noteId) {
|
|
|
|
setHistory(req.user.id, [], function (err, count) {
|
2019-10-27 08:51:53 -04:00
|
|
|
if (err) return errors.errorInternalError(res)
|
2017-03-08 05:45:51 -05:00
|
|
|
res.end()
|
|
|
|
})
|
2016-10-10 08:51:46 -04:00
|
|
|
} else {
|
2017-03-08 05:45:51 -05:00
|
|
|
getHistory(req.user.id, function (err, history) {
|
2019-10-27 08:51:53 -04:00
|
|
|
if (err) return errors.errorInternalError(res)
|
|
|
|
if (!history) return errors.errorNotFound(res)
|
2017-03-08 05:45:51 -05:00
|
|
|
delete history[noteId]
|
|
|
|
setHistory(req.user.id, history, function (err, count) {
|
2019-10-27 08:51:53 -04:00
|
|
|
if (err) return errors.errorInternalError(res)
|
2017-03-08 05:45:51 -05:00
|
|
|
res.end()
|
|
|
|
})
|
|
|
|
})
|
2016-10-10 08:51:46 -04:00
|
|
|
}
|
2017-03-08 05:45:51 -05:00
|
|
|
} else {
|
2019-10-27 08:51:53 -04:00
|
|
|
return errors.errorForbidden(res)
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|
2016-10-10 08:51:46 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
module.exports = History
|