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) => {
|
||||
this.chatModels.add(chatModel);
|
||||
this.updateIndices();
|
||||
@@ -60,22 +76,6 @@ export default class ChatListModel {
|
||||
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
|
||||
loadChats = (): void => {
|
||||
const chatDir = storageKeys.chats;
|
||||
|
||||
+82
-83
@@ -31,13 +31,6 @@ export default class ChatModel {
|
||||
|
||||
chatMessageHandler: (chatMessage: ChatMessage) => void = () => {};
|
||||
|
||||
// sorting
|
||||
get index(): number {
|
||||
return this.chatListModel.getIndexOfPrimaryChannel(
|
||||
this.info.primaryChannel
|
||||
);
|
||||
}
|
||||
|
||||
// paths
|
||||
get infoPath(): string[] {
|
||||
return storageKeys.chatInfo(this.id);
|
||||
@@ -55,7 +48,14 @@ export default class ChatModel {
|
||||
return [...this.messageDirPath, id];
|
||||
};
|
||||
|
||||
// set
|
||||
// sorting
|
||||
get index(): number {
|
||||
return this.chatListModel.getIndexOfPrimaryChannel(
|
||||
this.info.primaryChannel
|
||||
);
|
||||
}
|
||||
|
||||
// settings
|
||||
setPrimaryChannel = (primaryChannel: string): void => {
|
||||
this.info.primaryChannel = primaryChannel;
|
||||
this.storeInfo();
|
||||
@@ -78,81 +78,6 @@ export default class ChatModel {
|
||||
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
|
||||
sendMessage = async (body: string, file?: File): Promise<boolean> => {
|
||||
const senderName = this.settingsModel.username;
|
||||
@@ -215,6 +140,80 @@ export default class ChatModel {
|
||||
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
|
||||
constructor(
|
||||
storageModel: StorageModel,
|
||||
|
||||
@@ -25,19 +25,7 @@ export default class ConnectionModel {
|
||||
|
||||
channelsToSubscribe: Set<string> = new Set();
|
||||
|
||||
// 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);
|
||||
};
|
||||
|
||||
// handlers
|
||||
handleMessage = (data: Message): void => {
|
||||
this.messageHandler(data);
|
||||
};
|
||||
@@ -55,6 +43,19 @@ export default class ConnectionModel {
|
||||
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
|
||||
requestNewMailbox = (): void => {
|
||||
console.trace("requesting mailbox");
|
||||
@@ -178,7 +179,7 @@ export default class ConnectionModel {
|
||||
return this.storageModel.list(dirPath);
|
||||
}
|
||||
|
||||
// handlers
|
||||
// other
|
||||
setConnectionChangeHandler = (handler: () => void): void => {
|
||||
this.connectionChangeHandler = handler;
|
||||
};
|
||||
@@ -193,7 +194,7 @@ export default class ConnectionModel {
|
||||
this.messageSentHandler = handler;
|
||||
};
|
||||
|
||||
// setup
|
||||
// init
|
||||
constructor(storageModel: StorageModel) {
|
||||
// create frontend
|
||||
this.udn = new UDNFrontend();
|
||||
|
||||
@@ -5,6 +5,7 @@ import StorageModel, { storageKeys } from "./storageModel";
|
||||
export default class SettingsModel {
|
||||
storageModel: StorageModel;
|
||||
|
||||
// data
|
||||
username: 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 {
|
||||
DATA_VERSION,
|
||||
|
||||
Reference in New Issue
Block a user