finish outbox and send methods

This commit is contained in:
marlon-erler
2024-07-20 20:03:46 +02:00
parent 92383f772e
commit bdbd2ac8b6
3 changed files with 52 additions and 8 deletions
-5
View File
@@ -18,7 +18,6 @@ export class ChatModel {
messages = new Set<ChatMessage>(); messages = new Set<ChatMessage>();
objects = new Map<string, ChatObject>(); objects = new Map<string, ChatObject>();
outbox = new Set<ChatMessage>();
// paths // paths
get infoPath(): string[] { get infoPath(): string[] {
@@ -37,10 +36,6 @@ export class ChatModel {
return storageKeys.chatObjects(this.id); return storageKeys.chatObjects(this.id);
} }
get outboxDirPath(): string[] {
return storageKeys.chatOutbox(this.id);
}
getMessagePath = (id: string): string[] => { getMessagePath = (id: string): string[] => {
return [...this.messageDirPath, id]; return [...this.messageDirPath, id];
}; };
+51 -2
View File
@@ -4,6 +4,7 @@ import StorageModel, { storageKeys } from "./storageModel";
import UDNFrontend, { Message } from "udn-frontend"; import UDNFrontend, { Message } from "udn-frontend";
import { ChatMessage } from "./chatModel"; import { ChatMessage } from "./chatModel";
import { checkIsValidObject } from "./Utility/typeSafety";
import { stringify } from "./Utility/utility"; import { stringify } from "./Utility/utility";
export default class ConnectionModel { 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 // 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); const stringifiedBody: string = stringify(chatMessage);
return this.udn.sendMessage(chatMessage.channel, stringifiedBody); return this.udn.sendMessage(chatMessage.channel, stringifiedBody);
}; };
@@ -62,7 +111,7 @@ export default class ConnectionModel {
get addresses(): string[] { get addresses(): string[] {
const dirPath = storageKeys.previousAddresses; const dirPath = storageKeys.previousAddresses;
return this.storageModel.list(dirPath); return this.storageModel.list(dirPath);
}; }
// setup // setup
constructor(configuration: ConnectionModelConfiguration) { constructor(configuration: ConnectionModelConfiguration) {
+1 -1
View File
@@ -131,6 +131,7 @@ export type StorageEntry = { [key: string]: StorageEntry };
export const storageKeys = { export const storageKeys = {
// connection // connection
socketAddress: [DATA_VERSION, "connection", "socket-address"], socketAddress: [DATA_VERSION, "connection", "socket-address"],
outbox: [DATA_VERSION, "connection", "outbox"],
// settings // settings
username: [DATA_VERSION, "settings", "user-name"], username: [DATA_VERSION, "settings", "user-name"],
@@ -149,5 +150,4 @@ export const storageKeys = {
chatColor: (id: string) => [DATA_VERSION, "chat", id, "color"], chatColor: (id: string) => [DATA_VERSION, "chat", id, "color"],
chatMessages: (id: string) => [DATA_VERSION, "chat", id, "messages"], chatMessages: (id: string) => [DATA_VERSION, "chat", id, "messages"],
chatObjects: (id: string) => [DATA_VERSION, "chat", id, "objects"], chatObjects: (id: string) => [DATA_VERSION, "chat", id, "objects"],
chatOutbox: (id: string) => [DATA_VERSION, "chat", id, "outbox"],
}; };