finish outbox and send methods
This commit is contained in:
@@ -18,7 +18,6 @@ export class ChatModel {
|
||||
|
||||
messages = new Set<ChatMessage>();
|
||||
objects = new Map<string, ChatObject>();
|
||||
outbox = new Set<ChatMessage>();
|
||||
|
||||
// paths
|
||||
get infoPath(): string[] {
|
||||
@@ -37,10 +36,6 @@ export class ChatModel {
|
||||
return storageKeys.chatObjects(this.id);
|
||||
}
|
||||
|
||||
get outboxDirPath(): string[] {
|
||||
return storageKeys.chatOutbox(this.id);
|
||||
}
|
||||
|
||||
getMessagePath = (id: string): string[] => {
|
||||
return [...this.messageDirPath, id];
|
||||
};
|
||||
|
||||
@@ -4,6 +4,7 @@ import StorageModel, { storageKeys } from "./storageModel";
|
||||
import UDNFrontend, { Message } from "udn-frontend";
|
||||
|
||||
import { ChatMessage } from "./chatModel";
|
||||
import { checkIsValidObject } from "./Utility/typeSafety";
|
||||
import { stringify } from "./Utility/utility";
|
||||
|
||||
export default class ConnectionModel {
|
||||
@@ -37,8 +38,56 @@ export default class ConnectionModel {
|
||||
}
|
||||
};
|
||||
|
||||
// outbox
|
||||
getOutboxMessags = (): ChatMessage[] => {
|
||||
const outboxPath: string[] = storageKeys.outbox;
|
||||
const messageIds: string[] = this.storageModel.list(outboxPath);
|
||||
|
||||
let messages: ChatMessage[] = [];
|
||||
for (const messageId of messageIds) {
|
||||
const message: any = this.storageModel.restoreStringifiable([
|
||||
...outboxPath,
|
||||
messageId,
|
||||
]);
|
||||
if (message == undefined) continue;
|
||||
if (checkIsValidObject(message) == false) continue;
|
||||
|
||||
message.push(message);
|
||||
}
|
||||
|
||||
return messages;
|
||||
};
|
||||
|
||||
addToOutbox = (chatMessage: ChatMessage): void => {
|
||||
const messagePath: string[] = [...storageKeys.outbox, chatMessage.id];
|
||||
this.storageModel.storeStringifiable(messagePath, chatMessage);
|
||||
};
|
||||
|
||||
removeFromOutbox = (chatMessage: ChatMessage): void => {
|
||||
const messagePath: string[] = [...storageKeys.outbox, chatMessage.id];
|
||||
this.storageModel.remove(messagePath);
|
||||
};
|
||||
|
||||
sendMessagesInOutbox = (): void => {
|
||||
const messages: ChatMessage[] = this.getOutboxMessags();
|
||||
|
||||
for (const message of messages) {
|
||||
const isSent = this.tryToSendMessage(message);
|
||||
if (isSent == false) return;
|
||||
|
||||
this.removeFromOutbox(message);
|
||||
}
|
||||
};
|
||||
|
||||
// messaging
|
||||
sendChatMessage = (chatMessage: ChatMessage): boolean => {
|
||||
sendMessageOrStore = (chatMessage: ChatMessage): void => {
|
||||
const isSent = this.tryToSendMessage(chatMessage);
|
||||
if (isSent == true) return;
|
||||
|
||||
this.addToOutbox(chatMessage);
|
||||
};
|
||||
|
||||
tryToSendMessage = (chatMessage: ChatMessage): boolean => {
|
||||
const stringifiedBody: string = stringify(chatMessage);
|
||||
return this.udn.sendMessage(chatMessage.channel, stringifiedBody);
|
||||
};
|
||||
@@ -62,7 +111,7 @@ export default class ConnectionModel {
|
||||
get addresses(): string[] {
|
||||
const dirPath = storageKeys.previousAddresses;
|
||||
return this.storageModel.list(dirPath);
|
||||
};
|
||||
}
|
||||
|
||||
// setup
|
||||
constructor(configuration: ConnectionModelConfiguration) {
|
||||
|
||||
@@ -131,6 +131,7 @@ export type StorageEntry = { [key: string]: StorageEntry };
|
||||
export const storageKeys = {
|
||||
// connection
|
||||
socketAddress: [DATA_VERSION, "connection", "socket-address"],
|
||||
outbox: [DATA_VERSION, "connection", "outbox"],
|
||||
|
||||
// settings
|
||||
username: [DATA_VERSION, "settings", "user-name"],
|
||||
@@ -149,5 +150,4 @@ export const storageKeys = {
|
||||
chatColor: (id: string) => [DATA_VERSION, "chat", id, "color"],
|
||||
chatMessages: (id: string) => [DATA_VERSION, "chat", id, "messages"],
|
||||
chatObjects: (id: string) => [DATA_VERSION, "chat", id, "objects"],
|
||||
chatOutbox: (id: string) => [DATA_VERSION, "chat", id, "outbox"],
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user