Files
CT/src/Model/chatModel.ts
T

193 lines
5.2 KiB
TypeScript
Raw Normal View History

2024-07-09 05:26:54 +02:00
import * as React from "bloatless-react";
import { UDN, isConnected } from "./model";
import { decryptString, encryptString } from "../cryptUtility";
2024-07-09 11:39:23 +02:00
import { Message } from "udn-frontend";
2024-07-09 05:26:54 +02:00
import { senderName } from "./viewModel";
import { storageKeys } from "../utility";
// TYPES
// message
2024-07-09 11:39:23 +02:00
export interface ChatMessage {
2024-07-09 05:26:54 +02:00
channel: string;
sender: string;
body: string;
isoDate: string;
}
// chat
export class Chat {
id: string;
2024-07-09 11:39:23 +02:00
isSubscribed = new React.State(false);
currentChannel = new React.State("");
primaryChannelInput: React.State<string>;
2024-07-09 05:26:54 +02:00
secondaryChannels: React.ListState<string>;
encryptionKey: React.State<string>;
2024-07-09 11:39:23 +02:00
messages: React.ListState<ChatMessage>;
2024-07-09 05:26:54 +02:00
composingMessage: React.State<string>;
newSecondaryChannelName: React.State<string>;
2024-07-09 11:39:23 +02:00
cannotSendMessage: React.State<boolean>;
2024-07-09 05:26:54 +02:00
cannotAddSecondaryChannel: React.State<boolean>;
2024-07-09 11:39:23 +02:00
cannotSetChannel: React.State<boolean>;
2024-07-09 05:26:54 +02:00
// init
constructor(id: string = React.UUID()) {
2024-07-09 11:39:23 +02:00
this.primaryChannelInput = React.restoreState(
2024-07-09 05:26:54 +02:00
storageKeys.primaryChannel(id),
""
);
this.secondaryChannels = React.restoreListState(
storageKeys.secondaryChannels(id)
);
this.encryptionKey = React.restoreState(storageKeys.encyptionKey(id), "");
this.messages = React.restoreListState(storageKeys.messages(id));
this.composingMessage = React.restoreState(
storageKeys.composingMessage(id),
""
);
this.newSecondaryChannelName = new React.State("");
this.cannotSendMessage = React.createProxyState(
2024-07-09 11:39:23 +02:00
[
this.primaryChannelInput,
this.composingMessage,
this.isSubscribed,
isConnected,
],
2024-07-09 05:26:54 +02:00
() =>
2024-07-09 11:39:23 +02:00
this.primaryChannelInput.value == "" ||
2024-07-09 05:26:54 +02:00
this.composingMessage.value == "" ||
2024-07-09 11:39:23 +02:00
this.isSubscribed.value == false ||
2024-07-09 05:26:54 +02:00
isConnected.value == false
);
this.cannotAddSecondaryChannel = React.createProxyState(
[this.newSecondaryChannelName],
() => this.newSecondaryChannelName.value == ""
);
2024-07-09 11:39:23 +02:00
this.cannotSetChannel = React.createProxyState(
[this.primaryChannelInput, this.currentChannel],
() =>
this.primaryChannelInput.value == "" ||
this.primaryChannelInput.value == this.currentChannel.value
);
}
// handlers
onmessage(data: Message): void {
if (data.messageChannel && data.messageChannel != this.currentChannel.value)
return;
if (data.subscribed != undefined) this.handleSubscription(data.subscribed);
if (!data.messageBody) return;
const { sender, body, channel, isoDate } = JSON.parse(data.messageBody);
this.handleMessage({ sender, body, channel, isoDate });
}
handleSubscription(isSubscribed: boolean): void {
this.isSubscribed.value = isSubscribed;
}
handleMessage(chatMessage: ChatMessage): void {
this.messages.add(chatMessage);
2024-07-09 05:26:54 +02:00
}
// messages
async sendMessage(): Promise<void> {
if (this.cannotSendMessage.value == true) return;
// get channels
const secondaryChannelNames: string[] = [
...this.secondaryChannels.value.values(),
].map((channel) => channel);
const allChannelNames: string[] = [
2024-07-09 11:39:23 +02:00
this.primaryChannelInput.value,
2024-07-09 05:26:54 +02:00
...secondaryChannelNames,
];
// encrypt
const encrypted =
(await encryptString(
this.composingMessage.value,
this.encryptionKey.value
)) || this.composingMessage.value;
if (encrypted == undefined) return;
// create object
const joinedChannelName = allChannelNames.join("/");
2024-07-09 11:39:23 +02:00
const messageObject: ChatMessage = {
channel: joinedChannelName,
sender: senderName.value,
body: encrypted,
isoDate: new Date().toISOString(),
};
2024-07-09 05:26:54 +02:00
const messageString = JSON.stringify(messageObject);
// send
UDN.sendMessage(joinedChannelName, messageString);
// clear
this.composingMessage.value = "";
}
clearMessages(): void {
this.messages.clear();
}
2024-07-09 11:39:23 +02:00
deleteMessage(message: ChatMessage): void {
2024-07-09 05:26:54 +02:00
this.messages.remove(message);
}
2024-07-09 11:39:23 +02:00
async decryptReceivedMessage(message: ChatMessage): Promise<void> {
2024-07-09 05:26:54 +02:00
message.body = await decryptString(message.body, this.encryptionKey.value);
this.messages.callSubscriptions();
}
// channel
2024-07-09 11:39:23 +02:00
setChannel(): void {
if (this.cannotSetChannel.value == true) return;
this.currentChannel.value = this.primaryChannelInput.value;
UDN.subscribe(this.currentChannel.value);
}
2024-07-09 05:26:54 +02:00
addSecondaryChannel(): void {
if (this.cannotAddSecondaryChannel.value == true) return;
this.secondaryChannels.add(this.newSecondaryChannelName.value);
this.newSecondaryChannelName.value = "";
}
removeSecondaryChannel(channel: string): void {
this.secondaryChannels.remove(channel);
}
}
// INIT
const chatIds = React.restoreListState<string>("chat-ids");
const chatArray = [...chatIds.value.values()].map((id) => new Chat(id));
export const chats = new React.ListState<Chat>(chatArray);
// METHODS
export function createChatWithName(name: string): void {
const newChat = new Chat();
2024-07-09 11:39:23 +02:00
newChat.primaryChannelInput.value = name;
2024-07-09 05:26:54 +02:00
chatIds.add(newChat.id);
}
export function removeChat(chat: Chat): void {
Object.values(storageKeys).forEach((cb) => {
localStorage.removeItem(cb(chat.id));
});
chats.remove(chat);
chatIds.remove(chat.id);
}