fix: derive consistent md5 ids from tag title

This commit is contained in:
thecodrr
2021-02-20 11:31:44 +05:00
parent 25ee202d64
commit e898db9562
8 changed files with 28 additions and 13 deletions

View File

@@ -14,6 +14,7 @@ class Migrations {
async init() {
this.dbVersion =
(await this._db.context.read("v")) || CURRENT_DATABASE_VERSION;
this._db.context.write("v", this.dbVersion);
}
async migrate() {

View File

@@ -75,10 +75,10 @@ class Settings {
.notebook(pin.data.notebookId)
.topics.topic(pin.data.id)._topic;
} else if (pin.type === "tag") {
item =
this._db.tags.tag(pin.data.id) || this._db.tags.tag(pin.data.title);
item = this._db.tags.tag(pin.data.id);
}
if (item) prev.push(item);
else this.unpin(pin.data.id); // TODO risky.
return prev;
}, []);
}

View File

@@ -145,15 +145,24 @@ export default class Notes extends Collection {
}
tagged(tagId) {
const tag = this._db.tags.tag(tagId);
if (!tag || tag.noteIds.length <= 0) return [];
return tag.noteIds.map((id) => this._collection.getItem(id));
return this._getTagItems(tagId, "tags");
}
colored(colorId) {
const color = this._db.colors.tag(colorId);
if (!color || color.noteIds.length <= 0) return [];
return color.noteIds.map((id) => this._collection.getItem(id));
return this._getTagItems(colorId, "colors");
}
/**
* @private
*/
_getTagItems(tagId, collection) {
const tag = this._db[collection].tag(tagId);
if (!tag || tag.noteIds.length <= 0) return [];
return tag.noteIds.reduce((arr, id) => {
const item = this._collection.getItem(id);
if (item) arr.push(item);
return arr;
}, []);
}
/**

View File

@@ -1,7 +1,5 @@
import Collection from "./collection";
import getId from "../utils/id";
import { qclone } from "qclone";
import set from "../utils/set";
export default class Tags extends Collection {
tag(id) {
@@ -20,7 +18,7 @@ export default class Tags extends Collection {
title: tagId,
};
let id = tag.id || getId();
let id = tag.id || makeId(tag.title);
let notes = tag.noteIds || [];
if (notes.find((id) => id === noteId)) return id;

View File

@@ -32,4 +32,4 @@ export const EVENTS = {
noteRemoved: "note:removed",
};
export const CURRENT_DATABASE_VERSION = 5.1;
export const CURRENT_DATABASE_VERSION = 5.2;

View File

@@ -94,6 +94,7 @@ export default class Backup {
switch (version) {
case CURRENT_DATABASE_VERSION:
case 5.0:
case 5.1:
case 4:
case 4.1:
case 4.2:

View File

@@ -60,7 +60,8 @@ export const migrations = {
return item;
},
},
5.1: {
5.1: {},
5.2: {
note: false,
notebook: false,
tag: false,

View File

@@ -1,3 +1,4 @@
import SparkMD5 from "spark-md5";
/**
*
* @param {number} size
@@ -25,3 +26,7 @@ function cryptoRandom(size, type) {
export default function () {
return cryptoRandom(12, "hex");
}
export function makeId(text) {
return SparkMD5.hash(text);
}