From 44298baa935916c61d8402122ed5801b1d973acd Mon Sep 17 00:00:00 2001 From: Max Wu Date: Mon, 26 Feb 2018 16:46:59 +0800 Subject: Add migration for LZString compressed note id in history Signed-off-by: Max Wu --- lib/history.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'lib/history.js') diff --git a/lib/history.js b/lib/history.js index f46ff49f..f3d4440e 100644 --- a/lib/history.js +++ b/lib/history.js @@ -1,6 +1,7 @@ 'use strict' // history // external modules +var LZString = require('lz-string') // core var config = require('./config') @@ -27,7 +28,26 @@ function getHistory (userid, callback) { } var history = {} if (user.history) { - history = parseHistoryToObject(JSON.parse(user.history)) + 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++) { + let item = history[i] + // try to parse in base64url + let id = models.Note.decodeNoteId(item.id) + if (!id || !models.Note.checkNoteIdValid(id)) { + // try to parse in LZString if it can't be parsed in base64url + try { + id = LZString.decompressFromBase64(item.id) + } catch (err) { + id = null + } + if (id && models.Note.checkNoteIdValid(id)) { + // replace the note id to base64url encoded note id + history[i].id = models.Note.encodeNoteId(id) + } + } + } + history = parseHistoryToObject(history) } if (config.debug) { logger.info('read history success: ' + user.id) -- cgit v1.2.3 From 16cb842b946d55668318c08cf2e0aed001b9f855 Mon Sep 17 00:00:00 2001 From: Max Wu Date: Sat, 10 Mar 2018 16:51:00 +0800 Subject: Improve history migration performance Signed-off-by: Max Wu --- lib/history.js | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) (limited to 'lib/history.js') diff --git a/lib/history.js b/lib/history.js index f3d4440e..c7d2472c 100644 --- a/lib/history.js +++ b/lib/history.js @@ -31,20 +31,14 @@ function getHistory (userid, callback) { 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++) { - let item = history[i] - // try to parse in base64url - let id = models.Note.decodeNoteId(item.id) - if (!id || !models.Note.checkNoteIdValid(id)) { - // try to parse in LZString if it can't be parsed in base64url - try { - id = LZString.decompressFromBase64(item.id) - } catch (err) { - id = null - } + try { + let id = LZString.decompressFromBase64(history[i].id) if (id && models.Note.checkNoteIdValid(id)) { - // replace the note id to base64url encoded note id history[i].id = models.Note.encodeNoteId(id) } + } catch (err) { + // most error here comes from LZString, ignore + logger.error(err) } } history = parseHistoryToObject(history) -- cgit v1.2.3