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 { diff, hasItem } from "../utils/array";
|
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;
|
2020-12-06 15:35:23 +05:00
|
|
|
if (content.deleted || content.migrated)
|
|
|
|
|
return await this._collection.addItem(content);
|
2020-04-15 11:53:57 +05:00
|
|
|
|
2021-05-27 08:32:32 +05:00
|
|
|
const oldContent = await this.raw(content.id);
|
|
|
|
|
if (content.id && oldContent) {
|
2020-04-15 11:53:57 +05:00
|
|
|
content = {
|
2021-05-27 08:32:32 +05:00
|
|
|
...oldContent,
|
2020-04-15 11:53:57 +05:00
|
|
|
...content,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-19 11:30:05 +05:00
|
|
|
const id = content.id || getId();
|
2020-03-19 12:38:33 +05:00
|
|
|
await this._collection.addItem({
|
|
|
|
|
noteId: content.noteId,
|
|
|
|
|
id,
|
2020-11-04 10:17:37 +05:00
|
|
|
type: content.type,
|
2020-03-29 19:21:55 +05:00
|
|
|
data: content.data || content,
|
2020-04-07 15:26:09 +05:00
|
|
|
dateEdited: content.dateEdited,
|
|
|
|
|
dateCreated: content.dateCreated,
|
|
|
|
|
remote: content.remote,
|
2021-07-03 12:15:02 +05:00
|
|
|
localOnly: !!content.localOnly,
|
2021-07-25 11:31:44 +05:00
|
|
|
conflicted: content.conflicted,
|
2021-08-10 11:59:56 +05:00
|
|
|
dateResolved: content.dateResolved,
|
2020-03-19 12:38:33 +05:00
|
|
|
});
|
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-15 02:16:55 +05:00
|
|
|
async raw(id, withAttachments = true) {
|
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
|
|
|
|
2020-11-16 15:00:52 +05:00
|
|
|
multi(ids) {
|
|
|
|
|
return this._collection.getItems(ids);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-19 14:03:29 +05:00
|
|
|
all() {
|
2020-11-16 15:00:52 +05:00
|
|
|
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.get(hash);
|
|
|
|
|
});
|
|
|
|
|
return contentItem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async extractAttachments(contents) {
|
|
|
|
|
const allAttachments = this._db.attachments.all;
|
|
|
|
|
for (let contentItem of contents) {
|
|
|
|
|
const content = getContentFromData(contentItem.type, contentItem.data);
|
|
|
|
|
const { data, attachments } = await content.extractAttachments(
|
|
|
|
|
(data, type) => this._db.attachments.save(data, type)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await diff(allAttachments, attachments, async (attachment, action) => {
|
|
|
|
|
if (hasItem(attachment.noteIds, contentItem.noteId)) return;
|
|
|
|
|
|
|
|
|
|
if (action === "delete") {
|
|
|
|
|
await this._db.attachments.delete(
|
|
|
|
|
attachment.hash,
|
|
|
|
|
contentItem.noteId
|
|
|
|
|
);
|
|
|
|
|
} else if (action === "insert") {
|
|
|
|
|
await this._db.attachments.add(attachment, contentItem.noteId);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
contentItem.data = data;
|
2021-07-03 14:50:59 +05:00
|
|
|
}
|
2021-09-15 02:16:55 +05:00
|
|
|
return contents;
|
2021-07-03 14:50:59 +05:00
|
|
|
}
|
2020-03-19 11:30:05 +05:00
|
|
|
}
|