Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

feat: impl lock/unlock note

thecodrrd1c38265aa2f
diff --git a/packages/core/api/database.js b/packages/core/api/database.js
index 489cd213a..a18bf6550 100644
--- a/packages/core/api/database.js
+++ b/packages/core/api/database.js
@@ -8,6 +8,7 @@ import {
months,
getLastWeekTimestamp
} from "../utils/date";
+import Encryptor from "sjcl";
const KEYS = {
notes: "notes",
@@ -86,7 +87,7 @@ class Database {
case "year":
return groupBy(
notes,
- note => months[new Date(note.dateCreated).getFullYear()],
+ note => new Date(note.dateCreated).getFullYear().toString(),
special
);
default:
@@ -192,16 +193,30 @@ class Database {
);
}
- //TODO
- lockNote(note) {
- // this.notes[note.dateCreated].content = Encrypt(JSON.stringify(this.notes[note.dateCreated].content))
- // this.notes[note.dateCreated].locked = true
+ async lockNote(noteId, password) {
+ if (!this.notes[noteId]) {
+ throw new Error(`Cannot lock note. Invalid ID: ${noteId} given.`);
+ }
+ this.notes[noteId].content = Encryptor.encrypt(
+ password,
+ JSON.stringify(this.notes[noteId].content),
+ { ks: 256 }
+ );
+ this.notes[noteId].locked = true;
+ await this.storage.write(KEYS.notes, this.notes);
+ return true;
}
- //TODO
- unlockNote(note, perm = false) {
- // this.notes[note.dateCreated].content = JSON.parse(Decrypt(this.notes[note.dateCreated].content))
- // if (perm) { this.notes[note.dateCreated].locked = false }
+ async unlockNote(noteId, password, perm = false) {
+ if (!this.notes[noteId]) {
+ throw new Error(`Cannot unlock note. Invalid ID: ${noteId} given.`);
+ }
+ let decrypted = Encryptor.decrypt(password, this.notes[noteId].content);
+ if (perm) {
+ this.notes[noteId].locked = false;
+ await this.storage.write(KEYS.notes, this.notes);
+ }
+ return { ...this.notes[noteId], content: JSON.parse(decrypted) };
}
getNotebooks() {
@@ -223,15 +238,26 @@ class Database {
}
const id = notebook.dateCreated || Date.now();
- let topics = [makeTopic("General")];
- for (let topic of notebook.topics) {
+ let topics =
+ !notebook.topics || notebook.topics.length <= 0 ? [] : notebook.topics; //
+ if (notebook.topics.findIndex(topic => topic.title === "General") <= -1) {
+ topics.splice(0, 0, makeTopic("General"));
+ }
+ let index = 0;
+
+ for (let topic of topics) {
if (
!topic ||
- topic.trim().length <= 0 ||
- topics.findIndex(t => t.title === topic) > -1 //check for duplicate
- )
+ topics.findIndex(t => t.title === (topic || topic.title)) > -1 //check for duplicate
+ ) {
+ topics.splice(index, 1);
continue;
- topics[topics.length] = makeTopic(topic);
+ }
+ if (typeof topic === "string") {
+ if (topic.trim().length <= 0) topics.splice(index, 1);
+ topics[index] = makeTopic(topic);
+ }
+ index++;
}
this.notebooks[id] = {
@@ -348,7 +374,7 @@ class Database {
async restoreItem(id) {
if (!this.trash.hasOwnProperty(id)) {
- return;
+ throw new Error("Cannot restore: This item is not present in trash.");
}
let type = this.trash[id].dType;
delete this.trash[id].dateDeleted;
@@ -452,12 +478,12 @@ async function editItem(type, id, edit) {
switch (type) {
case "notebook":
case "note":
- col = type == "note" ? this.notes : this.notebooks;
- func = type == "note" ? this.addNote : this.addNotebook;
+ let col = type == "note" ? this.notes : this.notebooks;
+ let func = type == "note" ? this.addNote : this.addNotebook;
if (col[id] === undefined) {
throw new Error(`Wrong ${type} id.`);
}
- await func({ ...col[id], ...edit });
+ await func.call(this, { ...col[id], ...edit });
break;
default:
throw new Error("Invalid type given to pinItem");
diff --git a/packages/core/package.json b/packages/core/package.json
index d34e02fdf..ca21ddcdb 100644
--- a/packages/core/package.json
+++ b/packages/core/package.json
@@ -17,6 +17,7 @@
},
"dependencies": {
"fuzzysearch": "^1.0.3",
+ "sjcl": "^1.0.8",
"transfun": "^1.0.2"
}
}
diff --git a/packages/core/utils/date.js b/packages/core/utils/date.js
index 540be9d0c..98dcade23 100644
--- a/packages/core/utils/date.js
+++ b/packages/core/utils/date.js
@@ -1,11 +1,28 @@
-function getWeeksInMonth(month, year) {
- var weeks = [],
+function getWeeksInMonth(month, year, _start) {
+ let weeks = [],
firstDate = new Date(year, month, 1),
lastDate = new Date(year, month + 1, 0),
numDays = lastDate.getDate();
- var start = 1;
- var end = 7 - firstDate.getDay();
+ let start = 1;
+ let end = 7 - firstDate.getDay();
+ if (firstDate.getDay() === _start) {
+ end = 7;
+ } else {
+ let preMonthEndDay = new Date(year, month, 0);
+ start = preMonthEndDay.getDate() + 1 - firstDate.getDay() + _start;
+ end = 7 - firstDate.getDay() + _start;
+ weeks.push({
+ start: new Date(
+ preMonthEndDay.getFullYear(),
+ preMonthEndDay.getMonth(),
+ start
+ ),
+ end: new Date(year, month, end)
+ });
+ start = end + 1;
+ end = end + 7;
+ }
while (start <= numDays) {
weeks.push({
start: new Date(year, month, start),
@@ -13,7 +30,14 @@ function getWeeksInMonth(month, year) {
});
start = end + 1;
end = end + 7;
- if (end > numDays) end = numDays;
+ if (end > numDays) {
+ end = end - numDays + _start;
+ weeks.push({
+ start: new Date(year, month, start),
+ end: new Date(year, month + 1, end)
+ });
+ break;
+ }
}
return weeks;
}
@@ -21,7 +45,7 @@ function getWeeksInMonth(month, year) {
function getWeeksInYear(year) {
let weeks = [];
for (let i = 0; i <= 11; i++) {
- weeks.concat(...getWeeksInMonth(i, year));
+ weeks = weeks.concat(...getWeeksInMonth(i, year, 0));
}
return weeks;
}
@@ -38,32 +62,33 @@ export function getLastWeekTimestamp() {
return lastSaturday.setDate(t);
}
export const months = [
- "January",
- "February",
- "March",
- "April",
+ "Jan",
+ "Feb",
+ "Mar",
+ "Apr",
"May",
- "June",
- "July",
- "August",
- "September",
- "October",
- "November",
- "December"
+ "Jun",
+ "Jul",
+ "Aug",
+ "Sep",
+ "Oct",
+ "Nov",
+ "Dec"
];
let years = {};
export function getWeekGroupFromTimestamp(timestamp) {
let date = new Date(timestamp);
-
+ date.setHours(0, 0, 0, 0);
if (!years.hasOwnProperty(date.getFullYear())) {
years[date.getFullYear()] = getWeeksInYear(date.getFullYear());
}
let weeks = years[date.getFullYear()];
let week = weeks.find(v => date >= v.start && date <= v.end);
-
//Format: {month} {start} - {end}, {year}
- return `${months[date.getMonth()]} ${week.start} - ${
- week.end
- }, ${date.getFullYear()}`;
+ return `${
+ months[week.start.getMonth()]
+ } ${week.start.getDate()}, ${week.start.getFullYear()} - ${
+ months[week.end.getMonth()]
+ } ${week.end.getDate()}, ${week.end.getFullYear()}`;
}
diff --git a/packages/core/yarn.lock b/packages/core/yarn.lock
index d4b23d7c4..1aaa5b027 100644
--- a/packages/core/yarn.lock
+++ b/packages/core/yarn.lock
@@ -2161,11 +2161,6 @@ fast-deep-equal@^2.0.1:
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-2.0.1.tgz#7b05218ddf9667bf7f370bf7fdb2cb15fdd0aa49"
integrity sha1-ewUhjd+WZ79/Nwv3/bLLFf3Qqkk=
-fast-filter@^1.0.4:
- version "1.0.4"
- resolved "https://registry.yarnpkg.com/fast-filter/-/fast-filter-1.0.4.tgz#db462c3e8e6d3c66305752cdb8831a81c8b275db"
- integrity sha1-20YsPo5tPGYwV1LNuIMagciydds=
-
fast-json-stable-stringify@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz#d5142c0caee6b1189f87d3a76111064f86c8bbf2"
@@ -2176,11 +2171,6 @@ fast-levenshtein@~2.0.6:
resolved "https://registry.yarnpkg.com/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz#3d8a5c66883a16a30ca8643e851f19baa7797917"
integrity sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc=
-fast-sort@^1.6.0:
- version "1.6.0"
- resolved "https://registry.yarnpkg.com/fast-sort/-/fast-sort-1.6.0.tgz#68c6d94ce309ead19d562014159902c80c68d0a0"
- integrity sha512-sjV6dxWdHs14cef7GzJgCWSOagw57cUBYMey+seWeOsU0HgMf2g4Wkdflv65X/8E4UWptUcq9EZ3YqSXUnUy9Q==
-
fb-watchman@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/fb-watchman/-/fb-watchman-2.0.0.tgz#54e9abf7dfa2f26cd9b1636c588c1afc05de5d58"
@@ -4167,6 +4157,11 @@ sisteransi@^1.0.3:
resolved "https://registry.yarnpkg.com/sisteransi/-/sisteransi-1.0.4.tgz#386713f1ef688c7c0304dc4c0632898941cad2e3"
integrity sha512-/ekMoM4NJ59ivGSfKapeG+FWtrmWvA1p6FBZwXrqojw90vJu8lBmrTxCMuBCydKtkaUe2zt4PlxeTKpjwMbyig==
+sjcl@^1.0.8:
+ version "1.0.8"
+ resolved "https://registry.yarnpkg.com/sjcl/-/sjcl-1.0.8.tgz#f2ec8d7dc1f0f21b069b8914a41a8f236b0e252a"
+ integrity sha512-LzIjEQ0S0DpIgnxMEayM1rq9aGwGRG4OnZhCdjx7glTaJtf4zRfpg87ImfjSJjoW9vKpagd82McDOwbRT5kQKQ==
+
slash@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/slash/-/slash-2.0.0.tgz#de552851a1759df3a8f206535442f5ec4ddeab44"