integrate FileModel with ChatModel

This commit is contained in:
marlon-erler
2024-07-21 21:45:18 +02:00
parent b37f1caa66
commit da757d10d1
3 changed files with 67 additions and 80 deletions
+51 -78
View File
@@ -301,14 +301,14 @@
var StorageModel = class _StorageModel { var StorageModel = class _StorageModel {
storageEntryTree = {}; storageEntryTree = {};
// basic // basic
store = (pathComponents, value) => { write = (pathComponents, value) => {
const pathString = _StorageModel.pathComponentsToString( const pathString = _StorageModel.pathComponentsToString(
...pathComponents ...pathComponents
); );
localStorage.setItem(pathString, value); localStorage.setItem(pathString, value);
this.updateTree(...pathComponents); this.updateTree(...pathComponents);
}; };
restore = (pathComponents) => { read = (pathComponents) => {
const pathString = _StorageModel.pathComponentsToString( const pathString = _StorageModel.pathComponentsToString(
...pathComponents ...pathComponents
); );
@@ -345,12 +345,12 @@
return [...Object.keys(currentParent).sort(localeCompare)]; return [...Object.keys(currentParent).sort(localeCompare)];
}; };
// stringifiable // stringifiable
storeStringifiable = (pathComponents, value) => { writeStringifiable = (pathComponents, value) => {
const valueString = stringify(value); const valueString = stringify(value);
this.store(pathComponents, valueString); this.write(pathComponents, valueString);
}; };
restoreStringifiable = (pathComponents) => { readStringifiable = (pathComponents) => {
const valueString = this.restore(pathComponents); const valueString = this.read(pathComponents);
if (!valueString) return null; if (!valueString) return null;
return parse(valueString); return parse(valueString);
}; };
@@ -412,10 +412,7 @@
firstDayOfWeek: [DATA_VERSION, "settings", "first-day-of-week"], firstDayOfWeek: [DATA_VERSION, "settings", "first-day-of-week"],
// history // history
previousAddresses: [DATA_VERSION, "history", "previous-addresses"], previousAddresses: [DATA_VERSION, "history", "previous-addresses"],
previousObjectCategories: [DATA_VERSION, "history", "object-categories"], // chat
previousObjectStatuses: [DATA_VERSION, "history", "object-statuses"],
previousObjectFilters: [DATA_VERSION, "history", "object-filters"],
// chat etc
chats: [DATA_VERSION, "chat"], chats: [DATA_VERSION, "chat"],
chatInfo: (id) => [DATA_VERSION, "chat", id, "info"], chatInfo: (id) => [DATA_VERSION, "chat", id, "info"],
chatLastUsedPage: (id) => [ chatLastUsedPage: (id) => [
@@ -425,8 +422,7 @@
"last-used-page" "last-used-page"
], ],
chatColor: (id) => [DATA_VERSION, "chat", id, "color"], chatColor: (id) => [DATA_VERSION, "chat", id, "color"],
chatMessages: (id) => [DATA_VERSION, "chat", id, "messages"], chatMessages: (id) => [DATA_VERSION, "chat", id, "messages"]
chatObjects: (id) => [DATA_VERSION, "chat", id, "objects"]
}; };
// src/Model/Utility/crypto.ts // src/Model/Utility/crypto.ts
@@ -542,15 +538,9 @@
get messageDirPath() { get messageDirPath() {
return storageKeys.chatMessages(this.id); return storageKeys.chatMessages(this.id);
} }
get objectDirPath() {
return storageKeys.chatObjects(this.id);
}
getMessagePath = (id) => { getMessagePath = (id) => {
return [...this.messageDirPath, id]; return [...this.messageDirPath, id];
}; };
getObjectPath = (id) => {
return [...this.objectDirPath, id];
};
// set // set
setPrimaryChannel = (primaryChannel) => { setPrimaryChannel = (primaryChannel) => {
this.info.primaryChannel = primaryChannel; this.info.primaryChannel = primaryChannel;
@@ -572,39 +562,35 @@
}; };
// store & add // store & add
storeInfo = () => { storeInfo = () => {
this.storageModel.storeStringifiable(this.infoPath, this.info); this.storageModel.writeStringifiable(this.infoPath, this.info);
}; };
storeColor = () => { storeColor = () => {
this.storageModel.store(this.colorPath, this.color); this.storageModel.write(this.colorPath, this.color);
}; };
addMessage = async (chatMessage) => { addMessage = async (chatMessage) => {
await this.decryptMessage(chatMessage); await this.decryptMessage(chatMessage);
const messagePath = this.getMessagePath(chatMessage.id); const messagePath = this.getMessagePath(chatMessage.id);
this.storageModel.storeStringifiable(messagePath, chatMessage); this.storageModel.writeStringifiable(messagePath, chatMessage);
this.chatMessageHandler(chatMessage); this.chatMessageHandler(chatMessage);
}; };
addObject = (chatObject) => {
const objectPath = this.getObjectPath(chatObject.id);
this.storageModel.storeStringifiable(objectPath, chatObject);
};
// delete // delete
delete = () => { delete = () => {
this.chatListModel.untrackChat(this); this.chatListModel.untrackChat(this);
const dirPath = [...storageKeys.chats, this.id]; const dirPath = [...storageKeys.chats, this.id];
this.storageModel.removeRecursive(dirPath); this.storageModel.removeRecursive(dirPath);
}; };
// restore // load
restoreInfo = () => { loadInfo = () => {
const info = this.storageModel.restoreStringifiable(this.infoPath); const info = this.storageModel.readStringifiable(this.infoPath);
if (info != null) { if (info != null) {
this.info = info; this.info = info;
} else { } else {
this.info = _ChatModel.generateChatInfo("0"); this.info = _ChatModel.generateChatInfo("0");
} }
}; };
restoreColor = () => { loadColor = () => {
const path = this.colorPath; const path = this.colorPath;
const color = this.storageModel.restore(path); const color = this.storageModel.read(path);
if (!color) { if (!color) {
this.color = "standard" /* Standard */; this.color = "standard" /* Standard */;
} else { } else {
@@ -617,7 +603,7 @@
const messages = []; const messages = [];
for (const messageId of messageIds) { for (const messageId of messageIds) {
const messagePath = this.getMessagePath(messageId); const messagePath = this.getMessagePath(messageId);
const message = this.storageModel.restoreStringifiable(messagePath); const message = this.storageModel.readStringifiable(messagePath);
if (checkIsValidObject(message) == false) continue; if (checkIsValidObject(message) == false) continue;
messages.push(message); messages.push(message);
} }
@@ -626,19 +612,6 @@
); );
return sorted; return sorted;
} }
get objects() {
const objectIds = this.storageModel.list(this.objectDirPath);
if (!Array.isArray(objectIds)) return [];
const objects = [];
for (const objectId of objectIds) {
const objectPathComponents = this.getObjectPath(objectId);
const object = this.storageModel.restoreStringifiable(objectPathComponents);
if (checkIsValidObject(object) == false) continue;
if (object.id != objectId) continue;
objects.push(object);
}
return objects;
}
// messaging // messaging
sendMessage = async (body) => { sendMessage = async (body) => {
const senderName = this.settingsModel.username; const senderName = this.settingsModel.username;
@@ -690,8 +663,8 @@
this.settingsModel = settingsModel2; this.settingsModel = settingsModel2;
this.storageModel = storageModel2; this.storageModel = storageModel2;
this.chatListModel = chatListModel2; this.chatListModel = chatListModel2;
this.restoreInfo(); this.loadInfo();
this.restoreColor(); this.loadColor();
this.subscribe(); this.subscribe();
connectionModel2.setMessageSentHandler(this.handleMessageSent); connectionModel2.setMessageSentHandler(this.handleMessageSent);
} }
@@ -783,7 +756,7 @@
getIndexOfPrimaryChannel(primaryChannel) { getIndexOfPrimaryChannel(primaryChannel) {
return this.sortedPrimaryChannels.indexOf(primaryChannel); return this.sortedPrimaryChannels.indexOf(primaryChannel);
} }
// restore // load
loadChats = () => { loadChats = () => {
const chatDir = storageKeys.chats; const chatDir = storageKeys.chats;
const chatIds = this.storageModel.list(chatDir); const chatIds = this.storageModel.list(chatDir);
@@ -888,7 +861,7 @@
// view // view
open = () => { open = () => {
this.chatListViewModel.openChat(this); this.chatListViewModel.openChat(this);
this.restoreMessages(); this.loadMessages();
}; };
close = () => { close = () => {
this.chatListViewModel.closeChat(); this.chatListViewModel.closeChat();
@@ -918,7 +891,7 @@
this.secondaryChannels.add(this.newSecondaryChannelInput.value); this.secondaryChannels.add(this.newSecondaryChannelInput.value);
this.newSecondaryChannelInput.value = ""; this.newSecondaryChannelInput.value = "";
this.storeSecondaryChannels(); this.storeSecondaryChannels();
this.restoreSecondaryChannels(); this.loadSecondaryChannels();
}; };
removeSecondaryChannel = (secondaryChannel) => { removeSecondaryChannel = (secondaryChannel) => {
this.secondaryChannels.remove(secondaryChannel); this.secondaryChannels.remove(secondaryChannel);
@@ -940,7 +913,7 @@
remove = () => { remove = () => {
this.close(); this.close();
this.chatModel.delete(); this.chatModel.delete();
this.chatListViewModel.restoreChats(); this.chatListViewModel.loadChats();
}; };
// messaging // messaging
sendMessage = () => { sendMessage = () => {
@@ -957,18 +930,18 @@
this.chatModel.addMessage(chatMessage); this.chatModel.addMessage(chatMessage);
messageViewModel.loadData(); messageViewModel.loadData();
}; };
// restore // load
restorePageSelection = () => { loadPageSelection = () => {
const path = storageKeys.chatLastUsedPage(this.chatModel.id); const path = storageKeys.chatLastUsedPage(this.chatModel.id);
const lastUsedPage = this.storageModel.restore(path); const lastUsedPage = this.storageModel.read(path);
if (lastUsedPage != null) { if (lastUsedPage != null) {
this.selectedPage.value = lastUsedPage; this.selectedPage.value = lastUsedPage;
} }
this.selectedPage.subscribeSilent((newPage) => { this.selectedPage.subscribeSilent((newPage) => {
this.storageModel.store(path, newPage); this.storageModel.write(path, newPage);
}); });
}; };
restoreSecondaryChannels = () => { loadSecondaryChannels = () => {
this.secondaryChannels.clear(); this.secondaryChannels.clear();
for (const secondaryChannel of this.chatModel.info.secondaryChannels.sort( for (const secondaryChannel of this.chatModel.info.secondaryChannels.sort(
localeCompare localeCompare
@@ -976,7 +949,7 @@
this.secondaryChannels.add(secondaryChannel); this.secondaryChannels.add(secondaryChannel);
} }
}; };
restoreMessages = () => { loadMessages = () => {
for (const chatMessage of this.chatModel.messages) { for (const chatMessage of this.chatModel.messages) {
this.addChatMessage(chatMessage); this.addChatMessage(chatMessage);
} }
@@ -998,8 +971,8 @@
() => this.encryptionKeyInput.value == this.chatModel.info.encryptionKey () => this.encryptionKeyInput.value == this.chatModel.info.encryptionKey
); );
this.color.value = chatModel.color; this.color.value = chatModel.color;
this.restoreSecondaryChannels(); this.loadSecondaryChannels();
this.restorePageSelection(); this.loadPageSelection();
this.updateIndex(); this.updateIndex();
this.cannotSendMessage = createProxyState( this.cannotSendMessage = createProxyState(
[this.settingsViewModel.username, this.composingMessage], [this.settingsViewModel.username, this.composingMessage],
@@ -1057,8 +1030,8 @@
chatViewModel.updateIndex(); chatViewModel.updateIndex();
} }
}; };
// restore // load
restoreChats = () => { loadChats = () => {
this.chatViewModels.clear(); this.chatViewModels.clear();
for (const chatModel of this.chatListModel.chatModels.values()) { for (const chatModel of this.chatListModel.chatModels.values()) {
const chatViewModel = this.createChatViewModel(chatModel); const chatViewModel = this.createChatViewModel(chatModel);
@@ -1070,7 +1043,7 @@
this.chatListModel = chatListModel2; this.chatListModel = chatListModel2;
this.storageModel = storageModel2; this.storageModel = storageModel2;
this.settingsViewModel = settingsViewModel2; this.settingsViewModel = settingsViewModel2;
this.restoreChats(); this.loadChats();
} }
}; };
@@ -1733,7 +1706,7 @@
connectMailbox = () => { connectMailbox = () => {
if (this.address == void 0) return; if (this.address == void 0) return;
const path = [...storageKeys.mailboxes, this.address]; const path = [...storageKeys.mailboxes, this.address];
const mailboxId = this.storageModel.restore(path); const mailboxId = this.storageModel.read(path);
console.log("connecting mailbox", mailboxId); console.log("connecting mailbox", mailboxId);
if (mailboxId == null) return this.requestNewMailbox(); if (mailboxId == null) return this.requestNewMailbox();
this.udn.connectMailbox(mailboxId); this.udn.connectMailbox(mailboxId);
@@ -1741,7 +1714,7 @@
storeMailbox = (mailboxId) => { storeMailbox = (mailboxId) => {
if (this.address == void 0) return; if (this.address == void 0) return;
const path = [...storageKeys.mailboxes, this.address]; const path = [...storageKeys.mailboxes, this.address];
this.storageModel.store(path, mailboxId); this.storageModel.write(path, mailboxId);
}; };
// subscription // subscription
addChannel = (channel) => { addChannel = (channel) => {
@@ -1761,7 +1734,7 @@
const messageIds = this.storageModel.list(outboxPath); const messageIds = this.storageModel.list(outboxPath);
let messages = []; let messages = [];
for (const messageId of messageIds) { for (const messageId of messageIds) {
const message = this.storageModel.restoreStringifiable([ const message = this.storageModel.readStringifiable([
...outboxPath, ...outboxPath,
messageId messageId
]); ]);
@@ -1773,7 +1746,7 @@
}; };
addToOutbox = (chatMessage) => { addToOutbox = (chatMessage) => {
const messagePath = [...storageKeys.outbox, chatMessage.id]; const messagePath = [...storageKeys.outbox, chatMessage.id];
this.storageModel.storeStringifiable(messagePath, chatMessage); this.storageModel.writeStringifiable(messagePath, chatMessage);
}; };
removeFromOutbox = (chatMessage) => { removeFromOutbox = (chatMessage) => {
const messagePath = [...storageKeys.outbox, chatMessage.id]; const messagePath = [...storageKeys.outbox, chatMessage.id];
@@ -1809,9 +1782,9 @@
}; };
storeAddress = (address) => { storeAddress = (address) => {
const addressPath = this.getAddressPath(address); const addressPath = this.getAddressPath(address);
this.storageModel.store(addressPath, ""); this.storageModel.write(addressPath, "");
const reconnectAddressPath = storageKeys.reconnectAddress; const reconnectAddressPath = storageKeys.reconnectAddress;
this.storageModel.store(reconnectAddressPath, address); this.storageModel.write(reconnectAddressPath, address);
}; };
removeAddress = (address) => { removeAddress = (address) => {
const addressPath = this.getAddressPath(address); const addressPath = this.getAddressPath(address);
@@ -1857,7 +1830,7 @@
console.log(`using mailbox ${mailboxId}`); console.log(`using mailbox ${mailboxId}`);
}; };
const reconnectAddressPath = storageKeys.reconnectAddress; const reconnectAddressPath = storageKeys.reconnectAddress;
const reconnectAddress = storageModel2.restore(reconnectAddressPath); const reconnectAddress = storageModel2.read(reconnectAddressPath);
if (reconnectAddress != null) { if (reconnectAddress != null) {
this.connect(reconnectAddress); this.connect(reconnectAddress);
} }
@@ -2067,29 +2040,29 @@
setName(newValue) { setName(newValue) {
this.username = newValue; this.username = newValue;
const path = storageKeys.username; const path = storageKeys.username;
this.storageModel.store(path, newValue); this.storageModel.write(path, newValue);
} }
setFirstDayOfWeek(newValue) { setFirstDayOfWeek(newValue) {
this.firstDayOfWeek = newValue; this.firstDayOfWeek = newValue;
const path = storageKeys.firstDayOfWeek; const path = storageKeys.firstDayOfWeek;
this.storageModel.storeStringifiable(path, newValue); this.storageModel.writeStringifiable(path, newValue);
} }
// restore // load
restoreUsername() { loadUsernam() {
const path = storageKeys.username; const path = storageKeys.username;
const content = this.storageModel.restore(path); const content = this.storageModel.read(path);
this.username = content ?? ""; this.username = content ?? "";
} }
restoreFirstDayofWeek() { loadFirstDayofWeek() {
const path = storageKeys.firstDayOfWeek; const path = storageKeys.firstDayOfWeek;
const content = this.storageModel.restoreStringifiable(path); const content = this.storageModel.readStringifiable(path);
this.firstDayOfWeek = content ?? 0; this.firstDayOfWeek = content ?? 0;
} }
// init // init
constructor(storageModel2) { constructor(storageModel2) {
this.storageModel = storageModel2; this.storageModel = storageModel2;
this.restoreUsername(); this.loadUsernam();
this.restoreFirstDayofWeek(); this.loadFirstDayofWeek();
} }
}; };
@@ -2229,7 +2202,7 @@
// methods // methods
getSelectedItemContent = () => { getSelectedItemContent = () => {
const path = StorageModel.stringToPathComponents(this.selectedPath.value); const path = StorageModel.stringToPathComponents(this.selectedPath.value);
const content = this.storageModel.restore(path); const content = this.storageModel.read(path);
return (content ?? translations.storage.notAFile) || translations.storage.contentEmpty; return (content ?? translations.storage.notAFile) || translations.storage.contentEmpty;
}; };
deleteSelectedItem = () => { deleteSelectedItem = () => {
+5
View File
@@ -7,6 +7,7 @@ import { decryptString, encryptString } from "./Utility/crypto";
import ChatListModel from "./chatListModel"; import ChatListModel from "./chatListModel";
import { Color } from "../ViewModel/colors"; import { Color } from "../ViewModel/colors";
import ConnectionModel from "./connectionModel"; import ConnectionModel from "./connectionModel";
import FileModel from "./fileModel";
import SettingsModel from "./settingsModel"; import SettingsModel from "./settingsModel";
import { checkIsValidObject } from "./Utility/typeSafety"; import { checkIsValidObject } from "./Utility/typeSafety";
import { v4 } from "uuid"; import { v4 } from "uuid";
@@ -17,6 +18,8 @@ export default class ChatModel {
settingsModel: SettingsModel; settingsModel: SettingsModel;
chatListModel: ChatListModel; chatListModel: ChatListModel;
fileModel: FileModel;
// data // data
id: string; id: string;
info: ChatInfo; info: ChatInfo;
@@ -210,6 +213,8 @@ export default class ChatModel {
this.subscribe(); this.subscribe();
connectionModel.setMessageSentHandler(this.handleMessageSent); connectionModel.setMessageSentHandler(this.handleMessageSent);
this.fileModel = new FileModel(this);
} }
// utility // utility
+10 -1
View File
@@ -1,6 +1,15 @@
// this file is responsible for managing files within chats. // this file is responsible for managing files within chats.
export default class FileModel {} import ChatModel from "./chatModel";
export default class FileModel {
chatModel: ChatModel;
// init
constructor(chatModel: ChatModel) {
this.chatModel = chatModel;
}
}
// types // types
export interface File { export interface File {