fix outbox, add indicator for message status

This commit is contained in:
marlon-erler
2024-07-21 14:15:26 +02:00
parent ca8f1d3348
commit cb243bca62
8 changed files with 160 additions and 28 deletions
+14 -1
View File
@@ -451,10 +451,19 @@ body[showing-chat] #chat-page-wrapper {
padding: var(--control-padding); padding: var(--control-padding);
} }
.message-bubble .main .timestamp, .message-bubble .main .timestamp,
.message-bubble .main .timestamp .icon,
.message-bubble .main .sender-name { .message-bubble .main .sender-name {
font-size: 0.8rem; font-size: 0.8rem;
opacity: 0.6; opacity: 0.6;
} }
.message-bubble .main .timestamp {
display: flex;
align-items: center;
gap: var(--gap-between-tiles);
}
.message-bubble .main .timestamp .icon {
display: none;
}
.message-bubble .main .sender-name { .message-bubble .main .sender-name {
font-weight: bold; font-weight: bold;
} }
@@ -467,9 +476,13 @@ body[showing-chat] #chat-page-wrapper {
.message-bubble[sentbyuser] { .message-bubble[sentbyuser] {
align-items: flex-end; align-items: flex-end;
} }
.message-bubble[sentbyuser] .main { .message-bubble[sentbyuser] .main,
.message-bubble[sentbyuser] .main .timestamp {
flex-direction: row-reverse; flex-direction: row-reverse;
} }
.message-bubble[sentbyuser] .main .timestamp .icon {
display: flex;
}
.message-bubble[sentbyuser] .main span { .message-bubble[sentbyuser] .main span {
text-align: right; text-align: right;
} }
+54 -11
View File
@@ -563,7 +563,8 @@
storeColor = () => { storeColor = () => {
this.storageModel.store(this.colorPath, this.color); this.storageModel.store(this.colorPath, this.color);
}; };
addMessage = (chatMessage) => { addMessage = async (chatMessage) => {
await this.decryptMessage(chatMessage);
const messagePath = this.getMessagePath(chatMessage.id); const messagePath = this.getMessagePath(chatMessage.id);
this.storageModel.storeStringifiable(messagePath, chatMessage); this.storageModel.storeStringifiable(messagePath, chatMessage);
this.chatMessageHandler(chatMessage); this.chatMessageHandler(chatMessage);
@@ -641,13 +642,19 @@
senderName, senderName,
body body
); );
this.addMessage(chatMessage);
this.connectionModel.sendMessageOrStore(chatMessage); this.connectionModel.sendMessageOrStore(chatMessage);
return true; return true;
}; };
handleMessage = async (body) => { handleMessage = (body) => {
const chatMessage = _ChatModel.parseMessage(body); const chatMessage = _ChatModel.parseMessage(body);
if (chatMessage == null) return; if (chatMessage == null) return;
await this.decryptMessage(chatMessage); chatMessage.status = "received" /* Received */;
this.addMessage(chatMessage);
};
handleMessageSent = (chatMessage) => {
chatMessage.status = "sent" /* Sent */;
this.addMessage(chatMessage);
}; };
decryptMessage = async (chatMessage) => { decryptMessage = async (chatMessage) => {
const decryptedBody = await decryptString( const decryptedBody = await decryptString(
@@ -655,7 +662,6 @@
this.info.encryptionKey this.info.encryptionKey
); );
chatMessage.body = decryptedBody; chatMessage.body = decryptedBody;
this.addMessage(chatMessage);
}; };
setMessageHandler = (handler) => { setMessageHandler = (handler) => {
this.chatMessageHandler = handler; this.chatMessageHandler = handler;
@@ -673,6 +679,7 @@
this.restoreInfo(); this.restoreInfo();
this.restoreColor(); this.restoreColor();
this.subscribe(); this.subscribe();
connectionModel2.setMessageSentHandler(this.handleMessageSent);
} }
// utility // utility
static generateChatInfo = (primaryChannel) => { static generateChatInfo = (primaryChannel) => {
@@ -690,7 +697,8 @@
channel, channel,
sender, sender,
body, body,
dateSent: createTimestamp() dateSent: createTimestamp(),
status: "outbox" /* Outbox */
}; };
}; };
static parseMessage = (string) => { static parseMessage = (string) => {
@@ -795,6 +803,7 @@
sender; sender;
dateSent; dateSent;
body = new State(""); body = new State("");
status = new State(void 0);
sentByUser; sentByUser;
// methods // methods
copyMessage = () => { copyMessage = () => {
@@ -812,6 +821,7 @@
this.sender = this.chatMessage.sender; this.sender = this.chatMessage.sender;
this.dateSent = new Date(this.chatMessage.dateSent).toLocaleString(); this.dateSent = new Date(this.chatMessage.dateSent).toLocaleString();
this.body.value = this.chatMessage.body; this.body.value = this.chatMessage.body;
this.status.value = this.chatMessage.status;
}; };
// init // init
constructor(chatViewModel, chatMessage, sentByUser) { constructor(chatViewModel, chatMessage, sentByUser) {
@@ -864,6 +874,7 @@
// view // view
open = () => { open = () => {
this.chatListViewModel.openChat(this); this.chatListViewModel.openChat(this);
this.restoreMessages();
}; };
close = () => { close = () => {
this.chatListViewModel.closeChat(); this.chatListViewModel.closeChat();
@@ -878,6 +889,7 @@
const existingChatMessageViewModel = this.chatMessageViewModels.value.get(chatMessage.id); const existingChatMessageViewModel = this.chatMessageViewModels.value.get(chatMessage.id);
if (existingChatMessageViewModel != void 0) { if (existingChatMessageViewModel != void 0) {
existingChatMessageViewModel.body.value = chatMessage.body; existingChatMessageViewModel.body.value = chatMessage.body;
existingChatMessageViewModel.status.value = chatMessage.status;
} else { } else {
this.chatMessageViewModels.set(chatMessage.id, chatMessageModel); this.chatMessageViewModels.set(chatMessage.id, chatMessageModel);
} }
@@ -918,6 +930,7 @@
}; };
// messaging // messaging
sendMessage = () => { sendMessage = () => {
if (this.cannotSendMessage.value == true) return;
this.sendMessageFromBody(this.composingMessage.value); this.sendMessageFromBody(this.composingMessage.value);
this.composingMessage.value = ""; this.composingMessage.value = "";
}; };
@@ -925,7 +938,9 @@
this.chatModel.sendMessage(body); this.chatModel.sendMessage(body);
}; };
decryptMessage = async (messageViewModel) => { decryptMessage = async (messageViewModel) => {
await this.chatModel.decryptMessage(messageViewModel.chatMessage); const chatMessage = messageViewModel.chatMessage;
await this.chatModel.decryptMessage(chatMessage);
this.chatModel.addMessage(chatMessage);
messageViewModel.loadData(); messageViewModel.loadData();
}; };
// restore // restore
@@ -947,6 +962,11 @@
this.secondaryChannels.add(secondaryChannel); this.secondaryChannels.add(secondaryChannel);
} }
}; };
restoreMessages = () => {
for (const chatMessage of this.chatModel.messages) {
this.addChatMessage(chatMessage);
}
};
// init // init
constructor(chatModel, storageModel2, settingsViewModel2, chatListViewModel2) { constructor(chatModel, storageModel2, settingsViewModel2, chatListViewModel2) {
this.chatModel = chatModel; this.chatModel = chatModel;
@@ -967,9 +987,6 @@
this.restoreSecondaryChannels(); this.restoreSecondaryChannels();
this.restorePageSelection(); this.restorePageSelection();
this.updateIndex(); this.updateIndex();
for (const chatMessage of chatModel.messages) {
this.addChatMessage(chatMessage);
}
this.cannotSendMessage = createProxyState( this.cannotSendMessage = createProxyState(
[this.settingsViewModel.username, this.composingMessage], [this.settingsViewModel.username, this.composingMessage],
() => this.settingsViewModel.username.value == "" || this.composingMessage.value == "" () => this.settingsViewModel.username.value == "" || this.composingMessage.value == ""
@@ -1189,6 +1206,21 @@
function openModal() { function openModal() {
isInfoModalOpen.value = true; isInfoModalOpen.value = true;
} }
const statusIcon = createProxyState(
[chatMessageViewModel.status],
() => {
switch (chatMessageViewModel.status.value) {
case "outbox" /* Outbox */:
return "hourglass_top";
case "sent" /* Sent */:
return "check";
case "received" /* Received */:
return "done_all";
default:
return "warning";
}
}
);
return /* @__PURE__ */ createElement( return /* @__PURE__ */ createElement(
"div", "div",
{ {
@@ -1201,7 +1233,7 @@
class: "body", class: "body",
"subscribe:innerText": chatMessageViewModel.body "subscribe:innerText": chatMessageViewModel.body
} }
), /* @__PURE__ */ createElement("span", { class: "timestamp ellipsis" }, chatMessageViewModel.dateSent)), /* @__PURE__ */ createElement("div", { class: "button-container" }, /* @__PURE__ */ createElement( ), /* @__PURE__ */ createElement("span", { class: "timestamp ellipsis" }, /* @__PURE__ */ createElement("span", { class: "icon", "subscribe:innerText": statusIcon }), chatMessageViewModel.dateSent)), /* @__PURE__ */ createElement("div", { class: "button-container" }, /* @__PURE__ */ createElement(
"button", "button",
{ {
"on:click": openModal, "on:click": openModal,
@@ -1528,6 +1560,7 @@
// UTILITY METHODS // UTILITY METHODS
send(messageObject) { send(messageObject) {
if (this.ws == void 0) return false; if (this.ws == void 0) return false;
if (this.ws.readyState != 1) return false;
const messageString = JSON.stringify(messageObject); const messageString = JSON.stringify(messageObject);
this.ws.send(messageString); this.ws.send(messageString);
return true; return true;
@@ -1617,6 +1650,8 @@
}; };
messageHandler = () => { messageHandler = () => {
}; };
messageSentHandler = () => {
};
channelsToSubscribe = /* @__PURE__ */ new Set(); channelsToSubscribe = /* @__PURE__ */ new Set();
// connection // connection
connect = (address) => { connect = (address) => {
@@ -1691,7 +1726,12 @@
}; };
tryToSendMessage = (chatMessage) => { tryToSendMessage = (chatMessage) => {
const stringifiedBody = stringify(chatMessage); const stringifiedBody = stringify(chatMessage);
return this.udn.sendMessage(chatMessage.channel, stringifiedBody); const isSent = this.udn.sendMessage(
chatMessage.channel,
stringifiedBody
);
if (isSent) this.messageSentHandler(chatMessage);
return isSent;
}; };
// storage // storage
getAddressPath = (address) => { getAddressPath = (address) => {
@@ -1720,6 +1760,9 @@
setMessageHandler = (handler) => { setMessageHandler = (handler) => {
this.messageHandler = handler; this.messageHandler = handler;
}; };
setMessageSentHandler = (handler) => {
this.messageSentHandler = handler;
};
// setup // setup
constructor(storageModel2) { constructor(storageModel2) {
this.udn = new UDNFrontend(); this.udn = new UDNFrontend();
+5 -1
View File
@@ -2,7 +2,7 @@
import * as React from "bloatless-react"; import * as React from "bloatless-react";
import { ChatMessage, ChatModel } from "./chatModel"; import { ChatMessage, ChatMessageStatus, ChatModel } from "./chatModel";
import ChatViewModel from "../ViewModel/chatViewModel"; import ChatViewModel from "../ViewModel/chatViewModel";
@@ -15,6 +15,9 @@ export default class ChatMessageViewModel {
sender: string; sender: string;
dateSent: string; dateSent: string;
body: React.State<string> = new React.State(""); body: React.State<string> = new React.State("");
status: React.State<ChatMessageStatus | any> = new React.State<
ChatMessageStatus | undefined
>(undefined);
sentByUser: boolean; sentByUser: boolean;
// methods // methods
@@ -34,6 +37,7 @@ export default class ChatMessageViewModel {
this.sender = this.chatMessage.sender; this.sender = this.chatMessage.sender;
this.dateSent = new Date(this.chatMessage.dateSent).toLocaleString(); this.dateSent = new Date(this.chatMessage.dateSent).toLocaleString();
this.body.value = this.chatMessage.body; this.body.value = this.chatMessage.body;
this.status.value = this.chatMessage.status;
}; };
// init // init
+23 -5
View File
@@ -7,7 +7,6 @@ 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 { Message } from "udn-frontend";
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";
@@ -89,7 +88,8 @@ export class ChatModel {
this.storageModel.store(this.colorPath, this.color); this.storageModel.store(this.colorPath, this.color);
}; };
addMessage = (chatMessage: ChatMessage): void => { addMessage = async (chatMessage: ChatMessage): Promise<void> => {
await this.decryptMessage(chatMessage);
const messagePath: string[] = this.getMessagePath(chatMessage.id); const messagePath: string[] = this.getMessagePath(chatMessage.id);
this.storageModel.storeStringifiable(messagePath, chatMessage); this.storageModel.storeStringifiable(messagePath, chatMessage);
this.chatMessageHandler(chatMessage); this.chatMessageHandler(chatMessage);
@@ -187,15 +187,22 @@ export class ChatModel {
body body
); );
this.addMessage(chatMessage);
this.connectionModel.sendMessageOrStore(chatMessage); this.connectionModel.sendMessageOrStore(chatMessage);
return true; return true;
}; };
handleMessage = async (body: string): Promise<void> => { handleMessage = (body: string): void => {
const chatMessage: ChatMessage | null = ChatModel.parseMessage(body); const chatMessage: ChatMessage | null = ChatModel.parseMessage(body);
if (chatMessage == null) return; if (chatMessage == null) return;
await this.decryptMessage(chatMessage); chatMessage.status = ChatMessageStatus.Received;
this.addMessage(chatMessage);
};
handleMessageSent = (chatMessage: ChatMessage): void => {
chatMessage.status = ChatMessageStatus.Sent;
this.addMessage(chatMessage);
}; };
decryptMessage = async (chatMessage: ChatMessage): Promise<void> => { decryptMessage = async (chatMessage: ChatMessage): Promise<void> => {
@@ -204,7 +211,6 @@ export class ChatModel {
this.info.encryptionKey this.info.encryptionKey
); );
chatMessage.body = decryptedBody; chatMessage.body = decryptedBody;
this.addMessage(chatMessage);
}; };
setMessageHandler = (handler: (chatMessage: ChatMessage) => void): void => { setMessageHandler = (handler: (chatMessage: ChatMessage) => void): void => {
@@ -232,6 +238,8 @@ export class ChatModel {
this.restoreInfo(); this.restoreInfo();
this.restoreColor(); this.restoreColor();
this.subscribe(); this.subscribe();
connectionModel.setMessageSentHandler(this.handleMessageSent);
} }
// utility // utility
@@ -258,6 +266,8 @@ export class ChatModel {
sender, sender,
body, body,
dateSent: createTimestamp(), dateSent: createTimestamp(),
status: ChatMessageStatus.Outbox,
}; };
}; };
@@ -281,6 +291,12 @@ export interface ChatInfo {
hasUnreadMessages: boolean; hasUnreadMessages: boolean;
} }
export enum ChatMessageStatus {
Outbox = "outbox",
Sent = "sent",
Received = "received",
}
export interface ChatMessage { export interface ChatMessage {
dataVersion: "v2"; dataVersion: "v2";
@@ -291,6 +307,8 @@ export interface ChatMessage {
body: string; body: string;
dateSent: string; dateSent: string;
status?: ChatMessageStatus;
stringifiedObject?: string; stringifiedObject?: string;
} }
+15 -3
View File
@@ -22,6 +22,7 @@ export default class ConnectionModel {
connectionChangeHandler: () => void = () => {}; connectionChangeHandler: () => void = () => {};
messageHandler: (data: Message) => void = () => {}; messageHandler: (data: Message) => void = () => {};
messageSentHandler: (chatMessage: ChatMessage) => void = () => {};
channelsToSubscribe: Set<string> = new Set(); channelsToSubscribe: Set<string> = new Set();
@@ -62,7 +63,7 @@ export default class ConnectionModel {
sendSubscriptionRequest = (): void => { sendSubscriptionRequest = (): void => {
if (this.isConnected == false) return; if (this.isConnected == false) return;
for (const channel of this.channelsToSubscribe) { for (const channel of this.channelsToSubscribe) {
this.udn.subscribe(channel); this.udn.subscribe(channel);
} }
@@ -102,7 +103,7 @@ export default class ConnectionModel {
const messages: ChatMessage[] = this.getOutboxMessags(); const messages: ChatMessage[] = this.getOutboxMessags();
for (const message of messages) { for (const message of messages) {
const isSent = this.tryToSendMessage(message); const isSent: boolean = this.tryToSendMessage(message);
if (isSent == false) return; if (isSent == false) return;
this.removeFromOutbox(message); this.removeFromOutbox(message);
@@ -119,7 +120,12 @@ export default class ConnectionModel {
tryToSendMessage = (chatMessage: ChatMessage): boolean => { tryToSendMessage = (chatMessage: ChatMessage): boolean => {
const stringifiedBody: string = stringify(chatMessage); const stringifiedBody: string = stringify(chatMessage);
return this.udn.sendMessage(chatMessage.channel, stringifiedBody); const isSent: boolean = this.udn.sendMessage(
chatMessage.channel,
stringifiedBody
);
if (isSent) this.messageSentHandler(chatMessage);
return isSent;
}; };
// storage // storage
@@ -158,6 +164,12 @@ export default class ConnectionModel {
this.messageHandler = handler; this.messageHandler = handler;
}; };
setMessageSentHandler = (
handler: (chatMessage: ChatMessage) => void
): void => {
this.messageSentHandler = handler;
};
// setup // setup
constructor(storageModel: StorageModel) { constructor(storageModel: StorageModel) {
// create frontend // create frontend
+18 -2
View File
@@ -118,11 +118,22 @@
} }
.message-bubble .main .timestamp, .message-bubble .main .timestamp,
.message-bubble .main .timestamp .icon,
.message-bubble .main .sender-name { .message-bubble .main .sender-name {
font-size: 0.8rem; font-size: 0.8rem;
opacity: 0.6; opacity: 0.6;
} }
.message-bubble .main .timestamp {
display: flex;
align-items: center;
gap: var(--gap-between-tiles);
}
.message-bubble .main .timestamp .icon {
display: none;
}
.message-bubble .main .sender-name { .message-bubble .main .sender-name {
font-weight: bold; font-weight: bold;
} }
@@ -139,10 +150,15 @@
align-items: flex-end; align-items: flex-end;
} }
.message-bubble[sentbyuser] .main { .message-bubble[sentbyuser] .main,
.message-bubble[sentbyuser] .main .timestamp {
flex-direction: row-reverse; flex-direction: row-reverse;
} }
.message-bubble[sentbyuser] .main .timestamp .icon {
display: flex;
}
.message-bubble[sentbyuser] .main span { .message-bubble[sentbyuser] .main span {
text-align: right; text-align: right;
} }
+18
View File
@@ -1,6 +1,7 @@
import * as React from "bloatless-react"; import * as React from "bloatless-react";
import { ChatMessageInfoModal } from "./chatMessageInfoModal"; import { ChatMessageInfoModal } from "./chatMessageInfoModal";
import { ChatMessageStatus } from "../../Model/chatModel";
import ChatMessageViewModel from "../../Model/chatMessageViewModel"; import ChatMessageViewModel from "../../Model/chatMessageViewModel";
import { translations } from "../translations"; import { translations } from "../translations";
@@ -10,6 +11,22 @@ export function ChatMessage(chatMessageViewModel: ChatMessageViewModel) {
isInfoModalOpen.value = true; isInfoModalOpen.value = true;
} }
const statusIcon = React.createProxyState(
[chatMessageViewModel.status],
() => {
switch (chatMessageViewModel.status.value) {
case ChatMessageStatus.Outbox:
return "hourglass_top";
case ChatMessageStatus.Sent:
return "check";
case ChatMessageStatus.Received:
return "done_all";
default:
return "warning";
}
}
);
return ( return (
<div <div
class="message-bubble" class="message-bubble"
@@ -25,6 +42,7 @@ export function ChatMessage(chatMessageViewModel: ChatMessageViewModel) {
subscribe:innerText={chatMessageViewModel.body} subscribe:innerText={chatMessageViewModel.body}
></span> ></span>
<span class="timestamp ellipsis"> <span class="timestamp ellipsis">
<span class="icon" subscribe:innerText={statusIcon}></span>
{chatMessageViewModel.dateSent} {chatMessageViewModel.dateSent}
</span> </span>
</div> </div>
+13 -5
View File
@@ -63,6 +63,7 @@ export default class ChatViewModel {
// view // view
open = (): void => { open = (): void => {
this.chatListViewModel.openChat(this); this.chatListViewModel.openChat(this);
this.restoreMessages();
}; };
close = (): void => { close = (): void => {
@@ -81,6 +82,7 @@ export default class ChatViewModel {
this.chatMessageViewModels.value.get(chatMessage.id); this.chatMessageViewModels.value.get(chatMessage.id);
if (existingChatMessageViewModel != undefined) { if (existingChatMessageViewModel != undefined) {
existingChatMessageViewModel.body.value = chatMessage.body; existingChatMessageViewModel.body.value = chatMessage.body;
existingChatMessageViewModel.status.value = chatMessage.status;
} else { } else {
this.chatMessageViewModels.set(chatMessage.id, chatMessageModel); this.chatMessageViewModels.set(chatMessage.id, chatMessageModel);
} }
@@ -131,6 +133,8 @@ export default class ChatViewModel {
// messaging // messaging
sendMessage = (): void => { sendMessage = (): void => {
if (this.cannotSendMessage.value == true) return;
this.sendMessageFromBody(this.composingMessage.value); this.sendMessageFromBody(this.composingMessage.value);
this.composingMessage.value = ""; this.composingMessage.value = "";
}; };
@@ -142,7 +146,9 @@ export default class ChatViewModel {
decryptMessage = async ( decryptMessage = async (
messageViewModel: ChatMessageViewModel messageViewModel: ChatMessageViewModel
): Promise<void> => { ): Promise<void> => {
await this.chatModel.decryptMessage(messageViewModel.chatMessage); const chatMessage: ChatMessage = messageViewModel.chatMessage;
await this.chatModel.decryptMessage(chatMessage);
this.chatModel.addMessage(chatMessage);
messageViewModel.loadData(); messageViewModel.loadData();
}; };
@@ -168,6 +174,12 @@ export default class ChatViewModel {
} }
}; };
restoreMessages = (): void => {
for (const chatMessage of this.chatModel.messages) {
this.addChatMessage(chatMessage);
}
}
// init // init
constructor( constructor(
chatModel: ChatModel, chatModel: ChatModel,
@@ -202,10 +214,6 @@ export default class ChatViewModel {
this.restorePageSelection(); this.restorePageSelection();
this.updateIndex(); this.updateIndex();
for (const chatMessage of chatModel.messages) {
this.addChatMessage(chatMessage);
}
// guards // guards
this.cannotSendMessage = React.createProxyState( this.cannotSendMessage = React.createProxyState(
[this.settingsViewModel.username, this.composingMessage], [this.settingsViewModel.username, this.composingMessage],