mirror of
https://github.com/streetwriters/notesnook.git
synced 2026-02-23 19:49:56 +01:00
core: ensure session content remains consistent when new session is created
New sessions can be created with title or content only, hence we need to pull the missing data from the note itself to keep session history consistent with previous sessions
This commit is contained in:
committed by
Abdullah Atta
parent
fea5818951
commit
6b2bf369ff
@@ -251,7 +251,6 @@ test("note history item can be created by setting note title and content both",
|
||||
content = await db.noteHistory.sessionContent.get(
|
||||
items[0].sessionContentId
|
||||
);
|
||||
console.log(content);
|
||||
|
||||
expect(content.data).toBe(TEST_NOTE.content.data);
|
||||
expect(content.title).toBe("Test note");
|
||||
@@ -314,3 +313,48 @@ test("restoring an old session should replace note's content and title", () =>
|
||||
);
|
||||
}
|
||||
));
|
||||
|
||||
test.only("note history item has consistent note and title data between sessions", () =>
|
||||
noteTest({
|
||||
...TEST_NOTE,
|
||||
title: "Test note",
|
||||
sessionId: "notesession1"
|
||||
}).then(async ({ db, id }) => {
|
||||
expect(await db.noteHistory.get(id).count()).toBe(1);
|
||||
const history = db.noteHistory.get(id);
|
||||
let items = await history.items();
|
||||
let content = await db.noteHistory.sessionContent.get(
|
||||
items[0].sessionContentId
|
||||
);
|
||||
expect(await db.noteHistory.collection.count()).toBe(1);
|
||||
expect(content.data).toBe(TEST_NOTE.content.data);
|
||||
expect(content.title).toBe("Test note");
|
||||
|
||||
await db.notes.add({
|
||||
id: id,
|
||||
content: TEST_NOTE.content,
|
||||
sessionId: "notesession2"
|
||||
});
|
||||
|
||||
items = await history.items();
|
||||
content = await db.noteHistory.sessionContent.get(
|
||||
items[1].sessionContentId
|
||||
);
|
||||
|
||||
expect(content.data).toBe(TEST_NOTE.content.data);
|
||||
expect(content.title).toBe("Test note");
|
||||
|
||||
await db.notes.add({
|
||||
id: id,
|
||||
title: "Test note updated",
|
||||
sessionId: "notesession3"
|
||||
});
|
||||
|
||||
items = await history.items();
|
||||
content = await db.noteHistory.sessionContent.get(
|
||||
items[2].sessionContentId
|
||||
);
|
||||
|
||||
expect(content.data).toBe(TEST_NOTE.content.data);
|
||||
expect(content.title).toBe("Test note updated");
|
||||
}));
|
||||
|
||||
@@ -206,7 +206,7 @@ export class Notes implements ICollection {
|
||||
this.totalNotes++;
|
||||
}
|
||||
|
||||
if (item.sessionId) {
|
||||
if (item.sessionId && typeof item.title === "string") {
|
||||
await this.db.noteHistory.add(item.sessionId, {
|
||||
title: item.title,
|
||||
noteId: id
|
||||
|
||||
@@ -44,7 +44,7 @@ export class SessionContent implements ICollection {
|
||||
|
||||
async add<TLocked extends boolean>(
|
||||
sessionId: string,
|
||||
content: Partial<NoteContent<TLocked>> & { title?: string },
|
||||
content: Partial<NoteContent<TLocked>> & { title?: string; noteId: string },
|
||||
locked?: TLocked
|
||||
) {
|
||||
if (!sessionId || !content) return;
|
||||
@@ -52,8 +52,10 @@ export class SessionContent implements ICollection {
|
||||
// locked || isCipher(content.data)
|
||||
// ? content.data
|
||||
// : await this.db.compressor().compress(content.data);
|
||||
|
||||
const sessionContentItemId = makeSessionContentId(sessionId);
|
||||
const sessionContentExists = await this.collection.exists(
|
||||
sessionContentItemId
|
||||
);
|
||||
const sessionItem: Partial<SessionContentItem> = {
|
||||
type: "sessioncontent",
|
||||
id: sessionContentItemId,
|
||||
@@ -67,13 +69,29 @@ export class SessionContent implements ICollection {
|
||||
if (content.data && content.type) {
|
||||
sessionItem.data = content.data;
|
||||
sessionItem.contentType = content.type;
|
||||
|
||||
if (typeof content.title !== "string" && !sessionContentExists) {
|
||||
const note = await this.db.notes.note(content.noteId);
|
||||
sessionItem.title = note?.title;
|
||||
}
|
||||
}
|
||||
|
||||
if (content.title) {
|
||||
sessionItem.title = content.title;
|
||||
|
||||
if (!content.data && !content.type && !sessionContentExists) {
|
||||
const note = await this.db.notes.note(content.noteId);
|
||||
if (note?.contentId) {
|
||||
const noteContent = await this.db.content.get(note?.contentId);
|
||||
if (noteContent) {
|
||||
sessionItem.data = noteContent?.data;
|
||||
sessionItem.contentType = noteContent?.type;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (await this.collection.exists(sessionContentItemId)) {
|
||||
if (sessionContentExists) {
|
||||
this.collection.update([sessionContentItemId], sessionItem);
|
||||
} else {
|
||||
await this.collection.upsert(sessionItem as SessionContentItem);
|
||||
|
||||
Reference in New Issue
Block a user