Files
notesnook/packages/core/collections/content.js

109 lines
3.0 KiB
JavaScript
Raw Normal View History

2020-04-16 02:14:53 +05:00
import Collection from "./collection";
2020-03-19 11:30:05 +05:00
import getId from "../utils/id";
2021-09-15 02:16:55 +05:00
import { getContentFromData } from "../content-types";
import { hasItem } from "../utils/array";
import { diffArrays } from "diff";
2020-03-19 11:30:05 +05:00
2020-04-16 02:14:53 +05:00
export default class Content extends Collection {
2020-03-19 11:30:05 +05:00
async add(content) {
if (!content) return;
if (content.deleted || content.migrated)
return await this._collection.addItem(content);
2021-09-20 12:10:36 +05:00
const oldContent = await this.raw(content.id, false);
2021-05-27 08:32:32 +05:00
if (content.id && oldContent) {
content = {
2021-05-27 08:32:32 +05:00
...oldContent,
...content,
};
}
2020-03-19 11:30:05 +05:00
const id = content.id || getId();
await this._collection.addItem(
await this.extractAttachments({
noteId: content.noteId,
id,
type: content.type,
data: content.data || content,
dateEdited: content.dateEdited,
dateCreated: content.dateCreated,
remote: content.remote,
localOnly: !!content.localOnly,
conflicted: content.conflicted,
dateResolved: content.dateResolved,
})
);
2020-03-19 11:30:05 +05:00
return id;
}
async get(id) {
2020-03-19 12:38:33 +05:00
const content = await this.raw(id);
2020-03-19 11:30:05 +05:00
if (!content) return;
return content.data;
}
2021-09-29 10:31:49 +05:00
async raw(id, withAttachments = false) {
2020-03-19 12:38:33 +05:00
const content = await this._collection.getItem(id);
if (!content) return;
2021-09-15 02:16:55 +05:00
return withAttachments ? await this.insertAttachments(content) : content;
2020-03-19 12:38:33 +05:00
}
2020-03-19 11:30:05 +05:00
remove(id) {
if (!id) return;
return this._collection.removeItem(id);
}
2020-03-19 12:38:33 +05:00
multi(ids) {
return this._collection.getItems(ids);
}
2020-03-19 14:03:29 +05:00
all() {
return this._collection.getItems(this._collection.indexer.indices);
2020-03-19 14:03:29 +05:00
}
2021-07-03 14:50:59 +05:00
2021-09-15 02:16:55 +05:00
async insertAttachments(contentItem) {
const content = getContentFromData(contentItem.type, contentItem.data);
contentItem.data = await content.insertAttachments((hash) => {
return this._db.attachments.read(hash);
2021-09-15 02:16:55 +05:00
});
return contentItem;
}
2021-09-18 08:52:08 +05:00
/**
*
* @param {any} contentItem
* @returns {Promise<any>}
2021-09-18 08:52:08 +05:00
*/
async extractAttachments(contentItem) {
if (contentItem.localOnly || typeof contentItem.data !== "string")
return contentItem;
2021-09-15 02:16:55 +05:00
const allAttachments = this._db.attachments.all;
const content = getContentFromData(contentItem.type, contentItem.data);
const { data, attachments } = await content.extractAttachments(
(data, type) => this._db.attachments.save(data, type)
);
2021-09-15 02:16:55 +05:00
const diff = diffArrays(allAttachments, attachments, {
comparator: (left, right) => left.hash === right.metadata.hash,
});
2021-09-15 02:16:55 +05:00
for (const change of diff) {
for (let attachment of change.value) {
const exists = hasItem(attachment.noteIds, contentItem.noteId);
if (change.removed && exists) {
2021-09-15 02:16:55 +05:00
await this._db.attachments.delete(
attachment.metadata.hash,
2021-09-15 02:16:55 +05:00
contentItem.noteId
);
} else if ((!change.removed || change.added) && !exists) {
2021-09-15 02:16:55 +05:00
await this._db.attachments.add(attachment, contentItem.noteId);
}
}
2021-07-03 14:50:59 +05:00
}
contentItem.data = data;
return contentItem;
2021-07-03 14:50:59 +05:00
}
2020-03-19 11:30:05 +05:00
}