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";
|
2021-09-29 09:53:50 +05:00
|
|
|
import { 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;
|
2022-02-25 15:28:12 +05:00
|
|
|
|
|
|
|
|
if (typeof content.data === "object") {
|
|
|
|
|
if (typeof content.data.data === "string")
|
|
|
|
|
content.data = content.data.data;
|
2022-03-09 10:42:28 +05:00
|
|
|
else if (!content.data.iv && !content.data.cipher)
|
2022-02-25 15:28:12 +05:00
|
|
|
content.data = `<p>Content is invalid: ${JSON.stringify(
|
|
|
|
|
content.data
|
|
|
|
|
)}</p>`;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-26 12:15:34 +05:00
|
|
|
if (content.remote || content.deleted || content.migrated)
|
|
|
|
|
return await this._collection.addItem(
|
|
|
|
|
await this.extractAttachments(content)
|
|
|
|
|
);
|
2020-04-15 11:53:57 +05:00
|
|
|
|
2022-03-11 12:40:42 +05:00
|
|
|
const oldContent = await this.raw(content.id);
|
2021-05-27 08:32:32 +05:00
|
|
|
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,
|
2021-12-20 14:37:06 +05:00
|
|
|
dateEdited: content.dateEdited || Date.now(),
|
2020-04-15 11:53:57 +05:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-19 11:30:05 +05:00
|
|
|
const id = content.id || getId();
|
2021-12-22 10:05:42 +05:00
|
|
|
|
|
|
|
|
const contentItem = await this.extractAttachments({
|
|
|
|
|
noteId: content.noteId,
|
|
|
|
|
id,
|
|
|
|
|
type: content.type,
|
|
|
|
|
data: content.data || content,
|
|
|
|
|
dateEdited: content.dateEdited,
|
|
|
|
|
dateCreated: content.dateCreated,
|
|
|
|
|
dateModified: content.dateModified,
|
|
|
|
|
localOnly: !!content.localOnly,
|
|
|
|
|
conflicted: content.conflicted,
|
|
|
|
|
dateResolved: content.dateResolved,
|
|
|
|
|
});
|
|
|
|
|
await this._collection.addItem(contentItem);
|
|
|
|
|
|
|
|
|
|
if (content.sessionId) {
|
|
|
|
|
await this._db.noteHistory.add(contentItem.noteId, content.sessionId, {
|
|
|
|
|
data: contentItem.data,
|
|
|
|
|
type: contentItem.type,
|
|
|
|
|
});
|
|
|
|
|
}
|
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-10-05 13:23:27 +05:00
|
|
|
async raw(id) {
|
2020-03-19 12:38:33 +05:00
|
|
|
const content = await this._collection.getItem(id);
|
|
|
|
|
if (!content) return;
|
2021-10-05 13:23:27 +05:00
|
|
|
return 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);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-31 12:14:59 +05:00
|
|
|
exists(id) {
|
|
|
|
|
return this._collection.exists(id);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-20 09:28:58 +05:00
|
|
|
async all() {
|
|
|
|
|
return Object.values(
|
|
|
|
|
await this._collection.getItems(this._collection.indexer.indices)
|
|
|
|
|
);
|
2020-03-19 14:03:29 +05:00
|
|
|
}
|
2021-07-03 14:50:59 +05:00
|
|
|
|
2021-10-05 13:23:27 +05:00
|
|
|
insertMedia(contentItem) {
|
|
|
|
|
return this._insert(contentItem, this._db.attachments.read);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
insertPlaceholders(contentItem, placeholder) {
|
|
|
|
|
return this._insert(contentItem, () => placeholder);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-22 11:52:56 +05:00
|
|
|
async downloadMedia(groupId, contentItem, notify = true) {
|
2021-09-15 02:16:55 +05:00
|
|
|
const content = getContentFromData(contentItem.type, contentItem.data);
|
2021-10-05 13:23:27 +05:00
|
|
|
contentItem.data = await content.insertMedia((hash, { total, current }) => {
|
2021-11-18 15:17:02 +05:00
|
|
|
const attachment = this._db.attachments.attachment(hash);
|
|
|
|
|
if (!attachment) return;
|
|
|
|
|
|
|
|
|
|
const progressData = {
|
|
|
|
|
total,
|
|
|
|
|
current,
|
|
|
|
|
groupId,
|
|
|
|
|
};
|
2021-12-01 20:18:14 +05:00
|
|
|
|
2021-10-22 11:52:56 +05:00
|
|
|
return this._db.attachments._downloadMedia(
|
2021-12-01 20:18:14 +05:00
|
|
|
attachment,
|
2021-11-18 15:17:02 +05:00
|
|
|
progressData,
|
2021-10-22 11:52:56 +05:00
|
|
|
notify
|
|
|
|
|
);
|
2021-09-15 02:16:55 +05:00
|
|
|
});
|
|
|
|
|
return contentItem;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-05 13:23:27 +05:00
|
|
|
async _insert(contentItem, getData) {
|
2022-02-19 13:23:27 +05:00
|
|
|
if (!contentItem || !getData) return;
|
2021-10-05 13:23:27 +05:00
|
|
|
const content = getContentFromData(contentItem.type, contentItem.data);
|
|
|
|
|
contentItem.data = await content.insertMedia(getData);
|
|
|
|
|
return contentItem;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-28 13:01:05 +05:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {string} id
|
|
|
|
|
* @param {string[]} hashes
|
|
|
|
|
* @returns {Promise<any>}
|
|
|
|
|
*/
|
|
|
|
|
async removeAttachments(id, hashes) {
|
|
|
|
|
const contentItem = await this.raw(id);
|
|
|
|
|
const content = getContentFromData(contentItem.type, contentItem.data);
|
|
|
|
|
contentItem.data = content.removeAttachments(hashes);
|
|
|
|
|
await this.add(contentItem);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-18 08:52:08 +05:00
|
|
|
/**
|
|
|
|
|
*
|
2021-09-29 09:53:50 +05:00
|
|
|
* @param {any} contentItem
|
|
|
|
|
* @returns {Promise<any>}
|
2021-09-18 08:52:08 +05:00
|
|
|
*/
|
2021-09-29 09:53:50 +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;
|
2021-09-29 09:53:50 +05:00
|
|
|
const content = getContentFromData(contentItem.type, contentItem.data);
|
2022-03-16 22:05:19 +05:00
|
|
|
if (!content) return contentItem;
|
2021-11-25 09:46:45 +05:00
|
|
|
const { data, attachments } = await content.extractAttachments(
|
|
|
|
|
(data, type) => this._db.attachments.save(data, type)
|
|
|
|
|
);
|
2021-09-15 02:16:55 +05:00
|
|
|
|
2021-10-01 11:15:38 +05:00
|
|
|
const noteAttachments = allAttachments.filter((attachment) =>
|
|
|
|
|
hasItem(attachment.noteIds, contentItem.noteId)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const toDelete = noteAttachments.filter((attachment) => {
|
2021-12-11 10:32:11 +05:00
|
|
|
return attachments.every(
|
|
|
|
|
(a) => a.hash && a.hash !== attachment.metadata.hash
|
|
|
|
|
);
|
2021-10-01 11:15:38 +05:00
|
|
|
});
|
|
|
|
|
|
2021-10-30 13:49:41 +05:00
|
|
|
const toAdd = attachments.filter((attachment) => {
|
2021-12-11 10:32:11 +05:00
|
|
|
return (
|
|
|
|
|
attachment.hash &&
|
|
|
|
|
noteAttachments.every((a) => attachment.hash !== a.metadata.hash)
|
|
|
|
|
);
|
2021-09-29 09:53:50 +05:00
|
|
|
});
|
2021-09-15 02:16:55 +05:00
|
|
|
|
2021-10-01 11:15:38 +05:00
|
|
|
for (let attachment of toDelete) {
|
|
|
|
|
await this._db.attachments.delete(
|
|
|
|
|
attachment.metadata.hash,
|
|
|
|
|
contentItem.noteId
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (let attachment of toAdd) {
|
|
|
|
|
await this._db.attachments.add(attachment, contentItem.noteId);
|
2021-07-03 14:50:59 +05:00
|
|
|
}
|
2021-09-29 09:53:50 +05:00
|
|
|
|
2021-11-02 12:43:56 +05:00
|
|
|
if (toAdd.length > 0) {
|
2021-12-20 14:37:06 +05:00
|
|
|
contentItem.dateModified = Date.now();
|
2021-11-02 11:57:20 +05:00
|
|
|
}
|
2021-11-03 18:39:25 +05:00
|
|
|
contentItem.data = data;
|
2021-09-29 09:53:50 +05:00
|
|
|
return contentItem;
|
2021-07-03 14:50:59 +05:00
|
|
|
}
|
2022-01-07 09:17:27 +05:00
|
|
|
|
|
|
|
|
async cleanup() {
|
|
|
|
|
const indices = this._collection.indexer.indices;
|
|
|
|
|
await this._db.notes.init();
|
|
|
|
|
const notes = this._db.notes._collection.getRaw();
|
|
|
|
|
if (!notes.length && indices.length > 0) return [];
|
|
|
|
|
let ids = [];
|
|
|
|
|
for (let contentId of indices) {
|
|
|
|
|
const noteIndex = notes.findIndex((note) => note.contentId === contentId);
|
|
|
|
|
const isOrphaned = noteIndex === -1;
|
|
|
|
|
if (isOrphaned) {
|
|
|
|
|
ids.push(contentId);
|
|
|
|
|
await this._collection.deleteItem(contentId);
|
|
|
|
|
} else if (notes[noteIndex].localOnly) {
|
|
|
|
|
ids.push(contentId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ids;
|
|
|
|
|
}
|
2020-03-19 11:30:05 +05:00
|
|
|
}
|