cleanup
This commit is contained in:
@@ -33,7 +33,23 @@ export default class ChatListModel {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// store & add
|
// sorting
|
||||||
|
updateIndices = () => {
|
||||||
|
this.sortedPrimaryChannels = [];
|
||||||
|
|
||||||
|
let allChannels: string[] = [];
|
||||||
|
for (const chatModel of this.chatModels) {
|
||||||
|
allChannels.push(chatModel.info.primaryChannel);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.sortedPrimaryChannels = allChannels.sort(localeCompare);
|
||||||
|
};
|
||||||
|
|
||||||
|
getIndexOfPrimaryChannel(primaryChannel: string): number {
|
||||||
|
return this.sortedPrimaryChannels.indexOf(primaryChannel);
|
||||||
|
}
|
||||||
|
|
||||||
|
// storage
|
||||||
addChatModel = (chatModel: ChatModel) => {
|
addChatModel = (chatModel: ChatModel) => {
|
||||||
this.chatModels.add(chatModel);
|
this.chatModels.add(chatModel);
|
||||||
this.updateIndices();
|
this.updateIndices();
|
||||||
@@ -60,22 +76,6 @@ export default class ChatListModel {
|
|||||||
this.updateIndices();
|
this.updateIndices();
|
||||||
};
|
};
|
||||||
|
|
||||||
// sorting
|
|
||||||
updateIndices = () => {
|
|
||||||
this.sortedPrimaryChannels = [];
|
|
||||||
|
|
||||||
let allChannels: string[] = [];
|
|
||||||
for (const chatModel of this.chatModels) {
|
|
||||||
allChannels.push(chatModel.info.primaryChannel);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.sortedPrimaryChannels = allChannels.sort(localeCompare);
|
|
||||||
};
|
|
||||||
|
|
||||||
getIndexOfPrimaryChannel(primaryChannel: string): number {
|
|
||||||
return this.sortedPrimaryChannels.indexOf(primaryChannel);
|
|
||||||
}
|
|
||||||
|
|
||||||
// load
|
// load
|
||||||
loadChats = (): void => {
|
loadChats = (): void => {
|
||||||
const chatDir = storageKeys.chats;
|
const chatDir = storageKeys.chats;
|
||||||
|
|||||||
+82
-83
@@ -31,13 +31,6 @@ export default class ChatModel {
|
|||||||
|
|
||||||
chatMessageHandler: (chatMessage: ChatMessage) => void = () => {};
|
chatMessageHandler: (chatMessage: ChatMessage) => void = () => {};
|
||||||
|
|
||||||
// sorting
|
|
||||||
get index(): number {
|
|
||||||
return this.chatListModel.getIndexOfPrimaryChannel(
|
|
||||||
this.info.primaryChannel
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// paths
|
// paths
|
||||||
get infoPath(): string[] {
|
get infoPath(): string[] {
|
||||||
return storageKeys.chatInfo(this.id);
|
return storageKeys.chatInfo(this.id);
|
||||||
@@ -55,7 +48,14 @@ export default class ChatModel {
|
|||||||
return [...this.messageDirPath, id];
|
return [...this.messageDirPath, id];
|
||||||
};
|
};
|
||||||
|
|
||||||
// set
|
// sorting
|
||||||
|
get index(): number {
|
||||||
|
return this.chatListModel.getIndexOfPrimaryChannel(
|
||||||
|
this.info.primaryChannel
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// settings
|
||||||
setPrimaryChannel = (primaryChannel: string): void => {
|
setPrimaryChannel = (primaryChannel: string): void => {
|
||||||
this.info.primaryChannel = primaryChannel;
|
this.info.primaryChannel = primaryChannel;
|
||||||
this.storeInfo();
|
this.storeInfo();
|
||||||
@@ -78,81 +78,6 @@ export default class ChatModel {
|
|||||||
this.storeColor();
|
this.storeColor();
|
||||||
};
|
};
|
||||||
|
|
||||||
// store & add
|
|
||||||
storeInfo = (): void => {
|
|
||||||
this.storageModel.writeStringifiable(this.infoPath, this.info);
|
|
||||||
};
|
|
||||||
|
|
||||||
storeColor = (): void => {
|
|
||||||
this.storageModel.write(this.colorPath, this.color);
|
|
||||||
};
|
|
||||||
|
|
||||||
addMessage = async (chatMessage: ChatMessage): Promise<void> => {
|
|
||||||
await this.decryptMessage(chatMessage);
|
|
||||||
|
|
||||||
// message
|
|
||||||
if (chatMessage.body != "") {
|
|
||||||
const messagePath: string[] = this.getMessagePath(chatMessage.id);
|
|
||||||
this.storageModel.writeStringifiable(messagePath, chatMessage);
|
|
||||||
this.chatMessageHandler(chatMessage);
|
|
||||||
}
|
|
||||||
|
|
||||||
// file
|
|
||||||
this.fileModel.handleStringifiedFile(chatMessage.stringifiedFile);
|
|
||||||
};
|
|
||||||
|
|
||||||
// delete
|
|
||||||
delete = () => {
|
|
||||||
// untrack
|
|
||||||
this.chatListModel.untrackChat(this);
|
|
||||||
|
|
||||||
// delete
|
|
||||||
const dirPath: string[] = [...storageKeys.chats, this.id];
|
|
||||||
this.storageModel.removeRecursively(dirPath);
|
|
||||||
};
|
|
||||||
|
|
||||||
// load
|
|
||||||
loadInfo = (): void => {
|
|
||||||
const info: ChatInfo | null = this.storageModel.readStringifiable(
|
|
||||||
this.infoPath,
|
|
||||||
ChatInfoReference
|
|
||||||
);
|
|
||||||
if (info != null) {
|
|
||||||
this.info = info;
|
|
||||||
} else {
|
|
||||||
this.info = ChatModel.generateChatInfo("0");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
loadColor = (): void => {
|
|
||||||
const path: string[] = this.colorPath;
|
|
||||||
const color: string | null = this.storageModel.read(path);
|
|
||||||
if (!color) {
|
|
||||||
this.color = Color.Standard;
|
|
||||||
} else {
|
|
||||||
this.color = color as any;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
get messages(): ChatMessage[] {
|
|
||||||
const messageIds: string[] = this.storageModel.list(this.messageDirPath);
|
|
||||||
if (!Array.isArray(messageIds)) return [];
|
|
||||||
|
|
||||||
const chatMessages: ChatMessage[] = [];
|
|
||||||
for (const messageId of messageIds) {
|
|
||||||
const messagePath: string[] = this.getMessagePath(messageId);
|
|
||||||
const chatMessage: ChatMessage | any =
|
|
||||||
this.storageModel.readStringifiable(messagePath, ChatMessageReference);
|
|
||||||
|
|
||||||
chatMessages.push(chatMessage);
|
|
||||||
}
|
|
||||||
|
|
||||||
const sorted = chatMessages.sort((a, b) =>
|
|
||||||
a.dateSent.localeCompare(b.dateSent)
|
|
||||||
);
|
|
||||||
return sorted;
|
|
||||||
}
|
|
||||||
|
|
||||||
// messaging
|
// messaging
|
||||||
sendMessage = async (body: string, file?: File): Promise<boolean> => {
|
sendMessage = async (body: string, file?: File): Promise<boolean> => {
|
||||||
const senderName = this.settingsModel.username;
|
const senderName = this.settingsModel.username;
|
||||||
@@ -215,6 +140,80 @@ export default class ChatModel {
|
|||||||
this.connectionModel.addChannel(this.info.primaryChannel);
|
this.connectionModel.addChannel(this.info.primaryChannel);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// storage
|
||||||
|
storeInfo = (): void => {
|
||||||
|
this.storageModel.writeStringifiable(this.infoPath, this.info);
|
||||||
|
};
|
||||||
|
|
||||||
|
storeColor = (): void => {
|
||||||
|
this.storageModel.write(this.colorPath, this.color);
|
||||||
|
};
|
||||||
|
|
||||||
|
addMessage = async (chatMessage: ChatMessage): Promise<void> => {
|
||||||
|
await this.decryptMessage(chatMessage);
|
||||||
|
|
||||||
|
// message
|
||||||
|
if (chatMessage.body != "") {
|
||||||
|
const messagePath: string[] = this.getMessagePath(chatMessage.id);
|
||||||
|
this.storageModel.writeStringifiable(messagePath, chatMessage);
|
||||||
|
this.chatMessageHandler(chatMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
// file
|
||||||
|
this.fileModel.handleStringifiedFile(chatMessage.stringifiedFile);
|
||||||
|
};
|
||||||
|
|
||||||
|
delete = () => {
|
||||||
|
// untrack
|
||||||
|
this.chatListModel.untrackChat(this);
|
||||||
|
|
||||||
|
// delete
|
||||||
|
const dirPath: string[] = [...storageKeys.chats, this.id];
|
||||||
|
this.storageModel.removeRecursively(dirPath);
|
||||||
|
};
|
||||||
|
|
||||||
|
// load
|
||||||
|
loadInfo = (): void => {
|
||||||
|
const info: ChatInfo | null = this.storageModel.readStringifiable(
|
||||||
|
this.infoPath,
|
||||||
|
ChatInfoReference
|
||||||
|
);
|
||||||
|
if (info != null) {
|
||||||
|
this.info = info;
|
||||||
|
} else {
|
||||||
|
this.info = ChatModel.generateChatInfo("0");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
loadColor = (): void => {
|
||||||
|
const path: string[] = this.colorPath;
|
||||||
|
const color: string | null = this.storageModel.read(path);
|
||||||
|
if (!color) {
|
||||||
|
this.color = Color.Standard;
|
||||||
|
} else {
|
||||||
|
this.color = color as any;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
get messages(): ChatMessage[] {
|
||||||
|
const messageIds: string[] = this.storageModel.list(this.messageDirPath);
|
||||||
|
if (!Array.isArray(messageIds)) return [];
|
||||||
|
|
||||||
|
const chatMessages: ChatMessage[] = [];
|
||||||
|
for (const messageId of messageIds) {
|
||||||
|
const messagePath: string[] = this.getMessagePath(messageId);
|
||||||
|
const chatMessage: ChatMessage | any =
|
||||||
|
this.storageModel.readStringifiable(messagePath, ChatMessageReference);
|
||||||
|
|
||||||
|
chatMessages.push(chatMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
const sorted = chatMessages.sort((a, b) =>
|
||||||
|
a.dateSent.localeCompare(b.dateSent)
|
||||||
|
);
|
||||||
|
return sorted;
|
||||||
|
}
|
||||||
|
|
||||||
// init
|
// init
|
||||||
constructor(
|
constructor(
|
||||||
storageModel: StorageModel,
|
storageModel: StorageModel,
|
||||||
|
|||||||
@@ -25,19 +25,7 @@ export default class ConnectionModel {
|
|||||||
|
|
||||||
channelsToSubscribe: Set<string> = new Set();
|
channelsToSubscribe: Set<string> = new Set();
|
||||||
|
|
||||||
// connection
|
// handlers
|
||||||
connect = (address: string): void => {
|
|
||||||
this.udn.connect(address);
|
|
||||||
};
|
|
||||||
|
|
||||||
disconnect = (): void => {
|
|
||||||
this.udn.disconnect();
|
|
||||||
|
|
||||||
// do not reconnect
|
|
||||||
const reconnectAddressPath: string[] = storageKeys.reconnectAddress;
|
|
||||||
this.storageModel.remove(reconnectAddressPath);
|
|
||||||
};
|
|
||||||
|
|
||||||
handleMessage = (data: Message): void => {
|
handleMessage = (data: Message): void => {
|
||||||
this.messageHandler(data);
|
this.messageHandler(data);
|
||||||
};
|
};
|
||||||
@@ -55,6 +43,19 @@ export default class ConnectionModel {
|
|||||||
this.sendMessagesInOutbox();
|
this.sendMessagesInOutbox();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// connection
|
||||||
|
connect = (address: string): void => {
|
||||||
|
this.udn.connect(address);
|
||||||
|
};
|
||||||
|
|
||||||
|
disconnect = (): void => {
|
||||||
|
this.udn.disconnect();
|
||||||
|
|
||||||
|
// do not reconnect
|
||||||
|
const reconnectAddressPath: string[] = storageKeys.reconnectAddress;
|
||||||
|
this.storageModel.remove(reconnectAddressPath);
|
||||||
|
};
|
||||||
|
|
||||||
// mailbox
|
// mailbox
|
||||||
requestNewMailbox = (): void => {
|
requestNewMailbox = (): void => {
|
||||||
console.trace("requesting mailbox");
|
console.trace("requesting mailbox");
|
||||||
@@ -178,7 +179,7 @@ export default class ConnectionModel {
|
|||||||
return this.storageModel.list(dirPath);
|
return this.storageModel.list(dirPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
// handlers
|
// other
|
||||||
setConnectionChangeHandler = (handler: () => void): void => {
|
setConnectionChangeHandler = (handler: () => void): void => {
|
||||||
this.connectionChangeHandler = handler;
|
this.connectionChangeHandler = handler;
|
||||||
};
|
};
|
||||||
@@ -193,7 +194,7 @@ export default class ConnectionModel {
|
|||||||
this.messageSentHandler = handler;
|
this.messageSentHandler = handler;
|
||||||
};
|
};
|
||||||
|
|
||||||
// setup
|
// init
|
||||||
constructor(storageModel: StorageModel) {
|
constructor(storageModel: StorageModel) {
|
||||||
// create frontend
|
// create frontend
|
||||||
this.udn = new UDNFrontend();
|
this.udn = new UDNFrontend();
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import StorageModel, { storageKeys } from "./storageModel";
|
|||||||
export default class SettingsModel {
|
export default class SettingsModel {
|
||||||
storageModel: StorageModel;
|
storageModel: StorageModel;
|
||||||
|
|
||||||
|
// data
|
||||||
username: string;
|
username: string;
|
||||||
firstDayOfWeek: string;
|
firstDayOfWeek: string;
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
// this file is responsible for reading and writing persistent data; all storage shall be handled by this file.
|
// this file is responsible for reading and writing persistent data.
|
||||||
|
|
||||||
import {
|
import {
|
||||||
DATA_VERSION,
|
DATA_VERSION,
|
||||||
|
|||||||
Reference in New Issue
Block a user