web: fix note editor properties

This commit is contained in:
Abdullah Atta
2023-11-15 15:11:51 +05:00
parent 7e8e981145
commit 2168609577
36 changed files with 1516 additions and 1094 deletions

View File

@@ -22,7 +22,7 @@ import Database from ".";
import { CHECK_IDS, EV, EVENTS, checkIsUserPremium } from "../common";
import { tinyToTiptap } from "../migrations";
import { isCipher } from "../database/crypto";
import { EncryptedContentItem, Note } from "../types";
import { Note } from "../types";
import {
isEncryptedContent,
isUnencryptedContent
@@ -110,6 +110,7 @@ export default class Vault {
try {
const content = await this.decryptContent(
encryptedContent,
note.id,
oldPassword
);
contentItems.push({
@@ -239,18 +240,18 @@ export default class Vault {
}
async decryptContent(
encryptedContent: EncryptedContentItem,
encryptedContent: NoteContent<true>,
noteId: string,
password?: string
) {
if (!password) password = await this.getVaultPassword();
if (
encryptedContent.noteId &&
typeof encryptedContent.data !== "object" &&
!isCipher(encryptedContent.data)
) {
await this.db.notes.add({
id: encryptedContent.noteId,
id: noteId,
locked: false
});
return { data: encryptedContent.data, type: encryptedContent.type };
@@ -330,7 +331,11 @@ export default class Vault {
const encryptedContent = await this.db.content.get(note.contentId);
if (!encryptedContent || !isEncryptedContent(encryptedContent)) return;
const content = await this.decryptContent(encryptedContent, password);
const content = await this.decryptContent(
encryptedContent,
note.id,
password
);
if (perm) {
await this.db.notes.add({

View File

@@ -29,6 +29,7 @@ import { Attachment } from "../types";
import Database from "../api";
import { FilteredSelector, SQLCollection } from "../database/sql-collection";
import { isFalse } from "../database";
import { sql } from "kysely";
export class Attachments implements ICollection {
name = "attachments";
@@ -470,6 +471,13 @@ export class Attachments implements ICollection {
);
}
async totalSize(selector: FilteredSelector<Attachment> = this.all) {
const result = await selector.filter
.select((eb) => eb.fn.sum<number>(sql.raw(`size + 17`)).as("totalSize"))
.executeTakeFirst();
return result?.totalSize;
}
private async encryptKey(key: SerializedKey) {
const encryptionKey = await this._getEncryptionKey();
const encryptedKey = await this.db

View File

@@ -19,7 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import Database from "../api";
import { isCipher } from "../database/crypto";
import { SQLCollection } from "../database/sql-collection";
import { FilteredSelector, SQLCollection } from "../database/sql-collection";
import { HistorySession, isDeleted } from "../types";
import { makeSessionContentId } from "../utils/id";
import { ICollection } from "./collection";
@@ -39,25 +39,31 @@ export class NoteHistory implements ICollection {
await this.sessionContent.init();
}
async get(noteId: string, order: "asc" | "desc" = "desc") {
if (!noteId) return [];
// async get(noteId: string, order: "asc" | "desc" = "desc") {
// if (!noteId) return [];
// const indices = this.collection.indexer.indices;
// const sessionIds = indices.filter((id) => id.startsWith(noteId));
// if (sessionIds.length === 0) return [];
// const history = await this.getSessions(sessionIds);
// // const indices = this.collection.indexer.indices;
// // const sessionIds = indices.filter((id) => id.startsWith(noteId));
// // if (sessionIds.length === 0) return [];
// // const history = await this.getSessions(sessionIds);
// return history.sort(function (a, b) {
// return b.dateModified - a.dateModified;
// });
const history = await this.db
.sql()
.selectFrom("notehistory")
.where("noteId", "==", noteId)
.orderBy(`dateModified ${order}`)
.selectAll()
.execute();
return history as HistorySession[];
// // return history.sort(function (a, b) {
// // return b.dateModified - a.dateModified;
// // });
// const history = await this.db
// .sql()
// .selectFrom("notehistory")
// .where("noteId", "==", noteId)
// .orderBy(`dateModified ${order}`)
// .selectAll()
// .execute();
// return history as HistorySession[];
// }
get(noteId: string) {
return new FilteredSelector<HistorySession>(
"notehistory",
this.db.sql().selectFrom("notehistory").where("noteId", "==", noteId)
);
}
async add(

View File

@@ -70,7 +70,9 @@ export class SessionContent implements ICollection {
});
}
async get(sessionContentId: string) {
async get(
sessionContentId: string
): Promise<NoteContent<boolean> | undefined> {
const session = await this.collection.get(sessionContentId);
if (!session || isDeleted(session)) return;

View File

@@ -38,7 +38,7 @@ const DEFAULT_GROUP_OPTIONS = (key: GroupingKey) =>
sortBy:
key === "trash"
? "dateDeleted"
: key === "tags"
: key === "tags" || key === "reminders"
? "dateCreated"
: key === "reminders"
? "dueDate"

View File

@@ -345,31 +345,37 @@ export default class Backup {
if ("sessionContentId" in item && item.type !== "session")
(item as any).type = "notehistory";
await migrateItem(
item,
version,
CURRENT_DATABASE_VERSION,
item.type,
this.db,
"backup"
);
if (
(await migrateItem(
item,
version,
CURRENT_DATABASE_VERSION,
item.type,
this.db,
"backup"
)) === "skip"
)
continue;
// since items in trash can have their own set of migrations,
// we have to run the migration again to account for that.
if (item.type === "trash" && item.itemType)
await migrateItem(
item as unknown as Note | Notebook,
version,
CURRENT_DATABASE_VERSION,
item.itemType,
this.db,
"backup"
);
if (
(await migrateItem(
item as unknown as Note | Notebook,
version,
CURRENT_DATABASE_VERSION,
item.itemType,
this.db,
"backup"
)) === "skip"
)
continue;
const itemType =
// colors are naively of type "tag" instead of "color" so we have to fix that.
item.type === "tag" && COLORS.includes(item.title.toLowerCase())
? "color"
: "itemType" in item && item.itemType
: item.type === "trash" && "itemType" in item && item.itemType
? item.itemType
: item.type;

View File

@@ -159,7 +159,7 @@ export class NNMigrationProvider implements MigrationProvider {
.addColumn("salt", "text")
.addColumn("size", "integer")
.addColumn("alg", "text")
.addColumn("encryptionKey", "text")
.addColumn("key", "text")
.addColumn("chunkSize", "integer")
.addColumn("hash", "text", (c) => c.unique())
.addColumn("hashType", "text")

View File

@@ -19,3 +19,4 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
export * from "./types";
export { VirtualizedGrouping } from "./utils/virtualized-grouping";
export { DefaultColors } from "./collections/colors";

View File

@@ -61,7 +61,7 @@ type Migration = {
item: MigrationItemMap[P],
db: Database,
migrationType: MigrationType
) => boolean | Promise<boolean> | void;
) => "skip" | boolean | Promise<boolean | "skip"> | void;
};
collection?: (collection: IndexedCollection) => Promise<void> | void;
};
@@ -196,12 +196,22 @@ const migrations: Migration[] = [
.items()
.find((t) => item.title === t.title && t.id !== oldTagId))
)
return false;
return "skip";
const colorCode = ColorToHexCode[item.title];
if (colorCode) {
const newColor = await db.colors.all.find((eb) =>
eb.or([eb("title", "in", [alias, item.title])])
);
if (newColor) return "skip";
(item as unknown as Color).type = "color";
(item as unknown as Color).colorCode = colorCode;
} else {
const newTag = await db.tags.all.find((eb) =>
eb.or([eb("title", "in", [alias, item.title])])
);
if (newTag) return "skip";
}
item.title = alias || item.title;
@@ -305,6 +315,7 @@ const migrations: Migration[] = [
await db.relations.add(item, { id: subNotebookId, type: "notebook" });
}
delete item.topics;
delete item.totalNotes;
return true;
},
shortcut: (item) => {
@@ -409,7 +420,9 @@ export async function migrateItem<TItemType extends MigrationItemType>(
const itemMigrator = migration.items[type];
if (!itemMigrator) continue;
if (await itemMigrator(item, database, migrationType)) {
const result = await itemMigrator(item, database, migrationType);
if (result === "skip") return "skip";
if (result) {
if (item.type && item.type !== type) type = item.type as TItemType;
count++;
}

View File

@@ -25,6 +25,7 @@ export type SortOptions = {
| "dateCreated"
| "dateDeleted"
| "dateEdited"
| "dateModified"
| "title"
| "filename"
| "size"
@@ -197,6 +198,10 @@ export interface Notebook extends BaseItem<"notebook"> {
* @deprecated only kept here for migration purposes.
*/
topics?: Topic[];
/**
* @deprecated only kept here for migration purposes.
*/
totalNotes?: number;
}
/**