diff --git a/packages/core/api/vault.js b/packages/core/api/vault.js index 347afa809..51f23167b 100644 --- a/packages/core/api/vault.js +++ b/packages/core/api/vault.js @@ -1,4 +1,4 @@ -import { EV } from "../common"; +import { EV, sendCheckUserStatusEvent } from "../common"; import getId from "../utils/id"; const ERASE_TIME = 1000 * 60 * 30; @@ -75,6 +75,8 @@ export default class Vault { * @param {string} noteId The id of the note to lock */ async add(noteId) { + if (!(await sendCheckUserStatusEvent("vault:check"))) return; + await this._check(); await this._lockNote(noteId); } diff --git a/packages/core/collections/notebooks.js b/packages/core/collections/notebooks.js index 66821821c..21f893989 100644 --- a/packages/core/collections/notebooks.js +++ b/packages/core/collections/notebooks.js @@ -3,6 +3,7 @@ import fuzzysearch from "fuzzysearch"; import Notebook from "../models/notebook"; import sort from "fast-sort"; import getId from "../utils/id"; +import { sendCheckUserStatusEvent } from "../common"; var tfun = require("transfun/transfun.js").tfun; if (!tfun) { tfun = global.tfun; @@ -11,6 +12,11 @@ if (!tfun) { export default class Notebooks extends Collection { async add(notebookArg) { if (!notebookArg) throw new Error("Notebook cannot be undefined or null."); + if ( + this.all.length === 3 && + !(await sendCheckUserStatusEvent("notebook:add")) + ) + return; if (notebookArg.remote) { return await this._collection.addItem(notebookArg); diff --git a/packages/core/common.js b/packages/core/common.js index 00f9824e6..a8f82df62 100644 --- a/packages/core/common.js +++ b/packages/core/common.js @@ -1,3 +1,9 @@ import EventManager from "./utils/event-manager"; export const EV = new EventManager(); + +export async function sendCheckUserStatusEvent(type) { + const results = await EV.publishWithResult("user:checkStatus", type); + if (typeof results === "boolean") return results; + return results.some((r) => r.type === type && r.result === true); +} diff --git a/packages/core/database/backup.js b/packages/core/database/backup.js index 386833329..3948acf03 100644 --- a/packages/core/database/backup.js +++ b/packages/core/database/backup.js @@ -1,4 +1,5 @@ import SparkMD5 from "spark-md5"; +import { sendCheckUserStatusEvent } from "../common.js"; const invalidKeys = ["user", "t", "lastBackupTime"]; const validTypes = ["mobile", "web", "node"]; @@ -21,8 +22,12 @@ export default class Backup { * @param {boolean} encrypt */ async export(type, encrypt = false) { + if (encrypt && !(await sendCheckUserStatusEvent("backup:encrypt"))) return; + if (!validTypes.some((t) => t === type)) - throw new Error("Invalid type. It must be one of 'mobile' or 'web'."); + throw new Error( + "Invalid type. It must be one of 'mobile' or 'web' or 'node'." + ); const keys = (await this._db.context.getAllKeys()).filter( (key) => !invalidKeys.some((t) => t === key) diff --git a/packages/core/models/note.js b/packages/core/models/note.js index 2dea0fa99..c049767cd 100644 --- a/packages/core/models/note.js +++ b/packages/core/models/note.js @@ -2,6 +2,7 @@ import MarkdownBuilder from "../utils/templates/markdown/builder"; import HTMLBuilder from "../utils/templates/html/builder"; import TextBuilder from "../utils/templates/text/builder"; import { getContentFromData } from "../content-types"; +import { EV, sendCheckUserStatusEvent } from "../common"; export default class Note { /** @@ -59,6 +60,10 @@ export default class Note { }; const { data, type } = await this._db.content.raw(this._note.contentId); let content = getContentFromData(type, data); + + if (to !== "txt" && !(await sendCheckUserStatusEvent("note:export"))) + return; + switch (to) { case "html": templateData.content = content.toHTML(); @@ -78,16 +83,24 @@ export default class Note { return this._db.content.get(this._note.contentId); } - color(color) { - return addTag.call(this, color, "colors"); + async color(color) { + if (!(await sendCheckUserStatusEvent("note:colors"))) return; + return await addTag.call(this, color, "colors"); } + uncolor(color) { return removeTag.call(this, color, "colors"); } - tag(tag) { - return addTag.call(this, tag, "tags"); + async tag(tag) { + if ( + this._db.tags.all.length === 5 && + !(await sendCheckUserStatusEvent("note:tags")) + ) + return; + return await addTag.call(this, tag, "tags"); } + untag(tag) { return removeTag.call(this, tag, "tags"); } diff --git a/packages/core/utils/event-manager.js b/packages/core/utils/event-manager.js index 29cd5c7af..08d031990 100644 --- a/packages/core/utils/event-manager.js +++ b/packages/core/utils/event-manager.js @@ -33,5 +33,12 @@ class EventManager { handler(...args); }); } + + async publishWithResult(name, ...args) { + if (!this._registry[name]) return true; + const handlers = this._registry[name]; + if (handlers.length <= 0) return true; + return await Promise.all(handlers.map((handler) => handler(...args))); + } } export default EventManager;