2024-07-09 05:26:54 +02:00
|
|
|
import * as React from "bloatless-react";
|
|
|
|
|
|
2024-07-09 21:56:00 +02:00
|
|
|
import {
|
|
|
|
|
UDN,
|
|
|
|
|
chatIds,
|
|
|
|
|
chats,
|
|
|
|
|
isConnected,
|
2024-07-11 20:16:28 +02:00
|
|
|
outboxItemCount,
|
2024-07-10 15:40:25 +02:00
|
|
|
selectedChat,
|
2024-07-09 21:56:00 +02:00
|
|
|
senderName,
|
|
|
|
|
updateMailbox,
|
2024-07-12 02:24:48 +02:00
|
|
|
usedObjectCategories,
|
2024-07-09 21:56:00 +02:00
|
|
|
} from "../Model/model";
|
2024-07-09 05:26:54 +02:00
|
|
|
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 { storageKeys } from "../utility";
|
2024-07-11 21:02:28 +02:00
|
|
|
import { translation } from "../translations";
|
2024-07-12 02:30:50 +02:00
|
|
|
import { viewTypes } from "../Views/Objects/objectPane";
|
2024-07-09 05:26:54 +02:00
|
|
|
|
|
|
|
|
// 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;
|
2024-07-11 00:47:58 +02:00
|
|
|
|
2024-07-11 13:11:22 +02:00
|
|
|
messageObjectString?: string;
|
2024-07-11 00:47:58 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-11 01:47:43 +02:00
|
|
|
// object
|
|
|
|
|
export interface MessageObject {
|
2024-07-11 00:47:58 +02:00
|
|
|
id: string;
|
|
|
|
|
title: string;
|
2024-07-11 16:45:45 +02:00
|
|
|
contentVersions: { [key: string]: MessageObjectContent };
|
2024-07-11 00:47:58 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-11 14:03:15 +02:00
|
|
|
export interface MessageObjectContent {
|
|
|
|
|
isoDateVersionCreated: string;
|
2024-07-11 15:08:25 +02:00
|
|
|
id: string;
|
|
|
|
|
|
|
|
|
|
noteContent?: string;
|
2024-07-11 21:30:57 +02:00
|
|
|
priority?: number;
|
2024-07-11 22:27:15 +02:00
|
|
|
categoryName?: string;
|
2024-07-12 14:30:30 +02:00
|
|
|
status?: string;
|
2024-07-11 22:27:15 +02:00
|
|
|
date?: string;
|
|
|
|
|
time?: string;
|
2024-07-11 14:03:15 +02:00
|
|
|
}
|
2024-07-11 13:05:02 +02:00
|
|
|
|
2024-07-09 05:26:54 +02:00
|
|
|
// chat
|
|
|
|
|
export class Chat {
|
|
|
|
|
id: string;
|
|
|
|
|
|
2024-07-12 02:30:50 +02:00
|
|
|
viewType: React.State<keyof typeof viewTypes>;
|
|
|
|
|
|
2024-07-09 11:39:23 +02:00
|
|
|
isSubscribed = new React.State(false);
|
2024-07-09 17:23:29 +02:00
|
|
|
primaryChannel = new React.State("");
|
2024-07-10 15:40:25 +02:00
|
|
|
hasUnreadMessages: React.State<boolean>;
|
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-11 01:47:43 +02:00
|
|
|
objects: React.MapState<MessageObject>;
|
2024-07-10 16:37:42 +02:00
|
|
|
outbox: React.ListState<ChatMessage>;
|
2024-07-11 02:09:44 +02:00
|
|
|
objectOutbox: React.MapState<MessageObject>;
|
2024-07-10 16:37:42 +02:00
|
|
|
isOutBoxEmpty: React.State<boolean>;
|
2024-07-09 05:26:54 +02:00
|
|
|
|
|
|
|
|
composingMessage: React.State<string>;
|
2024-07-09 17:23:29 +02:00
|
|
|
primaryChannelInput: React.State<string>;
|
2024-07-10 15:40:25 +02:00
|
|
|
newSecondaryChannelName: React.State<string>;
|
2024-07-09 11:39:23 +02:00
|
|
|
|
|
|
|
|
cannotSendMessage: React.State<boolean>;
|
2024-07-10 15:08:06 +02:00
|
|
|
cannotResendMessage: 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 17:23:29 +02:00
|
|
|
cannotUndoChannel: React.State<boolean>;
|
2024-07-09 21:56:00 +02:00
|
|
|
cannotClearMessages: React.State<boolean>;
|
2024-07-11 02:29:00 +02:00
|
|
|
cannotClearObjects: React.State<boolean>;
|
2024-07-09 05:26:54 +02:00
|
|
|
|
|
|
|
|
// init
|
|
|
|
|
constructor(id: string = React.UUID()) {
|
2024-07-09 15:15:17 +02:00
|
|
|
this.id = id;
|
|
|
|
|
|
2024-07-12 02:30:50 +02:00
|
|
|
this.viewType = React.restoreState<keyof typeof viewTypes>(
|
|
|
|
|
storageKeys.viewType(id),
|
|
|
|
|
"all"
|
|
|
|
|
);
|
|
|
|
|
|
2024-07-09 15:15:17 +02:00
|
|
|
// channels
|
2024-07-09 17:23:29 +02:00
|
|
|
this.primaryChannel = React.restoreState(
|
2024-07-09 05:26:54 +02:00
|
|
|
storageKeys.primaryChannel(id),
|
|
|
|
|
""
|
|
|
|
|
);
|
|
|
|
|
this.secondaryChannels = React.restoreListState(
|
|
|
|
|
storageKeys.secondaryChannels(id)
|
|
|
|
|
);
|
|
|
|
|
|
2024-07-09 15:15:17 +02:00
|
|
|
// messaging
|
2024-07-10 15:40:25 +02:00
|
|
|
this.hasUnreadMessages = React.restoreState(
|
|
|
|
|
storageKeys.hasUnread(id),
|
|
|
|
|
false
|
|
|
|
|
);
|
2024-07-09 05:26:54 +02:00
|
|
|
this.encryptionKey = React.restoreState(storageKeys.encyptionKey(id), "");
|
|
|
|
|
this.messages = React.restoreListState(storageKeys.messages(id));
|
2024-07-11 01:47:43 +02:00
|
|
|
this.objects = React.restoreMapState(storageKeys.objects(id));
|
2024-07-10 16:37:42 +02:00
|
|
|
this.outbox = React.restoreListState(storageKeys.outbox(id));
|
2024-07-11 02:09:44 +02:00
|
|
|
this.objectOutbox = React.restoreMapState(storageKeys.itemOutbox(id));
|
2024-07-10 16:37:42 +02:00
|
|
|
this.isOutBoxEmpty = React.createProxyState(
|
|
|
|
|
[this.outbox],
|
|
|
|
|
() => this.outbox.value.size == 0
|
|
|
|
|
);
|
2024-07-09 05:26:54 +02:00
|
|
|
|
2024-07-11 20:16:28 +02:00
|
|
|
// outbox
|
|
|
|
|
React.bulkSubscribe([this.outbox, this.objectOutbox], () =>
|
|
|
|
|
this.updateOutboxCount()
|
|
|
|
|
);
|
|
|
|
|
this.updateOutboxCount();
|
|
|
|
|
|
2024-07-09 17:23:29 +02:00
|
|
|
// inputs
|
2024-07-09 05:26:54 +02:00
|
|
|
this.composingMessage = React.restoreState(
|
|
|
|
|
storageKeys.composingMessage(id),
|
|
|
|
|
""
|
|
|
|
|
);
|
|
|
|
|
this.newSecondaryChannelName = new React.State("");
|
2024-07-09 17:23:29 +02:00
|
|
|
this.primaryChannelInput = new React.State(this.primaryChannel.value);
|
2024-07-09 05:26:54 +02:00
|
|
|
|
2024-07-09 15:15:17 +02:00
|
|
|
// guards
|
2024-07-09 05:26:54 +02:00
|
|
|
this.cannotSendMessage = React.createProxyState(
|
2024-07-10 16:37:42 +02:00
|
|
|
[this.primaryChannel, this.composingMessage, senderName],
|
2024-07-09 05:26:54 +02:00
|
|
|
() =>
|
2024-07-09 17:23:29 +02:00
|
|
|
this.primaryChannel.value == "" ||
|
2024-07-09 05:26:54 +02:00
|
|
|
this.composingMessage.value == "" ||
|
2024-07-10 16:37:42 +02:00
|
|
|
senderName.value == ""
|
2024-07-09 05:26:54 +02:00
|
|
|
);
|
2024-07-10 15:08:06 +02:00
|
|
|
this.cannotResendMessage = React.createProxyState(
|
2024-07-10 16:37:42 +02:00
|
|
|
[this.primaryChannel, senderName],
|
|
|
|
|
() => this.primaryChannel.value == "" || senderName.value == ""
|
2024-07-10 15:08:06 +02:00
|
|
|
);
|
2024-07-09 05:26:54 +02:00
|
|
|
this.cannotAddSecondaryChannel = React.createProxyState(
|
|
|
|
|
[this.newSecondaryChannelName],
|
|
|
|
|
() => this.newSecondaryChannelName.value == ""
|
|
|
|
|
);
|
2024-07-09 11:39:23 +02:00
|
|
|
this.cannotSetChannel = React.createProxyState(
|
2024-07-09 17:23:29 +02:00
|
|
|
[this.primaryChannelInput, this.primaryChannel],
|
2024-07-09 11:39:23 +02:00
|
|
|
() =>
|
|
|
|
|
this.primaryChannelInput.value == "" ||
|
2024-07-09 17:23:29 +02:00
|
|
|
this.primaryChannelInput.value == this.primaryChannel.value
|
|
|
|
|
);
|
|
|
|
|
this.cannotUndoChannel = React.createProxyState(
|
|
|
|
|
[this.primaryChannelInput, this.primaryChannel],
|
2024-07-09 17:32:51 +02:00
|
|
|
() => this.primaryChannelInput.value == this.primaryChannel.value
|
2024-07-09 11:39:23 +02:00
|
|
|
);
|
2024-07-09 21:56:00 +02:00
|
|
|
this.cannotClearMessages = React.createProxyState(
|
|
|
|
|
[this.messages],
|
|
|
|
|
() => this.messages.value.size == 0
|
|
|
|
|
);
|
2024-07-11 02:29:00 +02:00
|
|
|
this.cannotClearObjects = React.createProxyState(
|
|
|
|
|
[this.objects],
|
|
|
|
|
() => this.objects.value.size == 0
|
|
|
|
|
);
|
2024-07-09 11:39:23 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-09 17:32:51 +02:00
|
|
|
// general
|
|
|
|
|
deleteSelf = () => {
|
|
|
|
|
Object.values(storageKeys).forEach((storageKey) => {
|
|
|
|
|
localStorage.removeItem(storageKey(this.id));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
chats.remove(this);
|
|
|
|
|
chatIds.remove(this.id);
|
2024-07-09 18:44:13 +02:00
|
|
|
};
|
2024-07-09 17:32:51 +02:00
|
|
|
|
2024-07-09 11:39:23 +02:00
|
|
|
// handlers
|
2024-07-09 17:23:29 +02:00
|
|
|
onmessage = async (data: Message): Promise<void> => {
|
2024-07-09 18:44:13 +02:00
|
|
|
if (!data.messageChannel) return;
|
|
|
|
|
const channels = data.messageChannel.split("/");
|
2024-07-09 18:51:46 +02:00
|
|
|
if (channels.indexOf(this.primaryChannel.value) == -1) return;
|
2024-07-09 11:39:23 +02:00
|
|
|
|
|
|
|
|
if (data.subscribed != undefined) this.handleSubscription(data.subscribed);
|
|
|
|
|
if (!data.messageBody) return;
|
2024-07-11 16:45:45 +02:00
|
|
|
|
2024-07-11 13:11:22 +02:00
|
|
|
const message: ChatMessage = JSON.parse(data.messageBody);
|
|
|
|
|
const { sender, body, channel, isoDate, messageObjectString } = message;
|
2024-07-11 00:47:58 +02:00
|
|
|
|
2024-07-11 13:11:22 +02:00
|
|
|
if (messageObjectString) {
|
|
|
|
|
const decryptedMessageObjectString = await decryptString(
|
|
|
|
|
messageObjectString,
|
2024-07-11 12:25:45 +02:00
|
|
|
this.encryptionKey.value
|
|
|
|
|
);
|
2024-07-11 13:11:22 +02:00
|
|
|
const messageObject = JSON.parse(decryptedMessageObjectString);
|
2024-07-11 21:02:28 +02:00
|
|
|
if (messageObject.id) return this.handleMessageObject(messageObject);
|
2024-07-11 00:47:58 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-09 17:23:29 +02:00
|
|
|
this.handleMessage({
|
|
|
|
|
sender,
|
|
|
|
|
body: await decryptString(body, this.encryptionKey.value),
|
|
|
|
|
channel,
|
|
|
|
|
isoDate,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handleSubscription = (isSubscribed: boolean): void => {
|
2024-07-09 11:39:23 +02:00
|
|
|
this.isSubscribed.value = isSubscribed;
|
2024-07-10 16:37:42 +02:00
|
|
|
|
|
|
|
|
if (isSubscribed == true) {
|
|
|
|
|
this.sendMessagesInOutbox();
|
|
|
|
|
}
|
2024-07-09 17:23:29 +02:00
|
|
|
};
|
2024-07-09 11:39:23 +02:00
|
|
|
|
2024-07-09 17:23:29 +02:00
|
|
|
handleMessage = (chatMessage: ChatMessage): void => {
|
2024-07-09 11:39:23 +02:00
|
|
|
this.messages.add(chatMessage);
|
2024-07-10 15:40:25 +02:00
|
|
|
if (selectedChat.value != this) this.hasUnreadMessages.value = true;
|
2024-07-09 17:23:29 +02:00
|
|
|
};
|
2024-07-09 05:26:54 +02:00
|
|
|
|
2024-07-11 01:47:43 +02:00
|
|
|
handleMessageObject = (messageObject: MessageObject): void => {
|
2024-07-11 13:05:02 +02:00
|
|
|
this.addObject(messageObject);
|
2024-07-10 18:03:14 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-11 12:25:45 +02:00
|
|
|
// sending
|
|
|
|
|
sendMessagesInOutbox = () => {
|
|
|
|
|
this.outbox.value.forEach(async (message) => {
|
|
|
|
|
const isSent = await this.sendMessage(message);
|
|
|
|
|
if (isSent == true) this.outbox.remove(message);
|
|
|
|
|
});
|
2024-07-11 13:11:22 +02:00
|
|
|
this.objectOutbox.value.forEach(async (messageObject) => {
|
|
|
|
|
const chatMessage = await this.createChatMessage("", messageObject);
|
2024-07-11 12:25:45 +02:00
|
|
|
const isSent = await this.sendMessage(chatMessage);
|
2024-07-11 13:11:22 +02:00
|
|
|
if (isSent == true) this.objectOutbox.remove(messageObject.id);
|
2024-07-11 12:25:45 +02:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-11 20:16:28 +02:00
|
|
|
updateOutboxCount() {
|
|
|
|
|
outboxItemCount.value =
|
|
|
|
|
this.outbox.value.size + this.objectOutbox.value.size;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-11 00:47:58 +02:00
|
|
|
// messages
|
2024-07-11 01:47:43 +02:00
|
|
|
createChatMessage = async (
|
2024-07-11 00:47:58 +02:00
|
|
|
messageText: string,
|
2024-07-11 01:47:43 +02:00
|
|
|
messageObject?: MessageObject
|
2024-07-11 00:47:58 +02:00
|
|
|
): Promise<ChatMessage> => {
|
2024-07-09 05:26:54 +02:00
|
|
|
// get channels
|
|
|
|
|
const secondaryChannelNames: string[] = [
|
|
|
|
|
...this.secondaryChannels.value.values(),
|
2024-07-09 18:44:13 +02:00
|
|
|
];
|
2024-07-09 05:26:54 +02:00
|
|
|
const allChannelNames: string[] = [
|
2024-07-09 17:23:29 +02:00
|
|
|
this.primaryChannel.value,
|
2024-07-09 05:26:54 +02:00
|
|
|
...secondaryChannelNames,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// create object
|
|
|
|
|
const joinedChannelName = allChannelNames.join("/");
|
2024-07-11 01:47:43 +02:00
|
|
|
const chatMessage: ChatMessage = {
|
2024-07-09 11:39:23 +02:00
|
|
|
channel: joinedChannelName,
|
|
|
|
|
sender: senderName.value,
|
2024-07-10 16:47:29 +02:00
|
|
|
body: messageText,
|
2024-07-09 11:39:23 +02:00
|
|
|
isoDate: new Date().toISOString(),
|
|
|
|
|
};
|
2024-07-11 00:47:58 +02:00
|
|
|
|
2024-07-11 12:25:45 +02:00
|
|
|
if (messageObject)
|
2024-07-11 13:11:22 +02:00
|
|
|
chatMessage.messageObjectString = await encryptString(
|
2024-07-11 12:25:45 +02:00
|
|
|
JSON.stringify(messageObject),
|
|
|
|
|
this.encryptionKey.value
|
|
|
|
|
);
|
2024-07-11 01:47:43 +02:00
|
|
|
return chatMessage;
|
2024-07-11 00:47:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
sendMessageFromComposer = async (): Promise<void> => {
|
|
|
|
|
if (this.cannotSendMessage.value == true) return;
|
|
|
|
|
|
|
|
|
|
await this.sendMessageFromText(this.composingMessage.value);
|
|
|
|
|
this.composingMessage.value = "";
|
2024-07-10 16:37:42 +02:00
|
|
|
};
|
2024-07-09 05:26:54 +02:00
|
|
|
|
2024-07-11 00:47:58 +02:00
|
|
|
sendMessageFromText = async (text: string): Promise<void> => {
|
2024-07-11 01:47:43 +02:00
|
|
|
const chatMessage = await this.createChatMessage(text);
|
2024-07-11 02:09:44 +02:00
|
|
|
this.outbox.add(chatMessage);
|
|
|
|
|
this.sendMessagesInOutbox();
|
2024-07-11 00:47:58 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-11 01:47:43 +02:00
|
|
|
resendMessage = (chatMessage: ChatMessage) => {
|
2024-07-11 00:47:58 +02:00
|
|
|
if (this.cannotResendMessage.value == true) return;
|
2024-07-11 01:47:43 +02:00
|
|
|
this.sendMessageFromText(chatMessage.body);
|
2024-07-11 00:47:58 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-09 17:23:29 +02:00
|
|
|
clearMessages = (): void => {
|
2024-07-09 05:26:54 +02:00
|
|
|
this.messages.clear();
|
2024-07-09 17:23:29 +02:00
|
|
|
};
|
2024-07-09 05:26:54 +02:00
|
|
|
|
2024-07-11 01:47:43 +02:00
|
|
|
deleteMessage = (chatMessage: ChatMessage): void => {
|
|
|
|
|
this.messages.remove(chatMessage);
|
2024-07-09 17:23:29 +02:00
|
|
|
};
|
2024-07-09 05:26:54 +02:00
|
|
|
|
2024-07-11 01:47:43 +02:00
|
|
|
deleteOutboxMessage = (chatMessage: ChatMessage): void => {
|
|
|
|
|
this.outbox.remove(chatMessage);
|
2024-07-10 16:37:42 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-11 01:47:43 +02:00
|
|
|
decryptReceivedMessage = async (chatMessage: ChatMessage): Promise<void> => {
|
2024-07-11 02:09:44 +02:00
|
|
|
chatMessage.body = await decryptString(
|
|
|
|
|
chatMessage.body,
|
|
|
|
|
this.encryptionKey.value
|
|
|
|
|
);
|
2024-07-09 05:26:54 +02:00
|
|
|
this.messages.callSubscriptions();
|
2024-07-09 17:23:29 +02:00
|
|
|
};
|
2024-07-09 05:26:54 +02:00
|
|
|
|
2024-07-11 12:25:45 +02:00
|
|
|
sendMessage = async (chatMessage: ChatMessage): Promise<boolean> => {
|
|
|
|
|
if (isConnected.value == false || this.isSubscribed.value == false)
|
|
|
|
|
return false;
|
2024-07-11 02:09:44 +02:00
|
|
|
|
|
|
|
|
const encryptedBody =
|
|
|
|
|
this.encryptionKey.value == ""
|
|
|
|
|
? chatMessage.body
|
|
|
|
|
: await encryptString(chatMessage.body, this.encryptionKey.value);
|
|
|
|
|
|
|
|
|
|
chatMessage.body = encryptedBody;
|
|
|
|
|
|
|
|
|
|
const messageString = JSON.stringify(chatMessage);
|
|
|
|
|
UDN.sendMessage(chatMessage.channel, messageString);
|
|
|
|
|
|
2024-07-11 12:25:45 +02:00
|
|
|
return true;
|
2024-07-11 02:09:44 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-11 01:47:43 +02:00
|
|
|
// objects
|
2024-07-11 13:05:02 +02:00
|
|
|
createObjectFromTitle = (title: string): MessageObject => {
|
2024-07-11 16:45:45 +02:00
|
|
|
const firstObjectContent = this.createObjectContent();
|
|
|
|
|
const messageObject: MessageObject = {
|
2024-07-11 13:05:02 +02:00
|
|
|
id: React.UUID(),
|
|
|
|
|
title,
|
2024-07-11 16:45:45 +02:00
|
|
|
contentVersions: {},
|
2024-07-11 13:05:02 +02:00
|
|
|
};
|
2024-07-11 16:45:45 +02:00
|
|
|
this.addObjectContent(messageObject, firstObjectContent);
|
|
|
|
|
return messageObject;
|
2024-07-11 13:05:02 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-11 16:45:45 +02:00
|
|
|
createObjectContent = () => {
|
|
|
|
|
return {
|
|
|
|
|
id: React.UUID(),
|
|
|
|
|
isoDateVersionCreated: new Date().toISOString(),
|
|
|
|
|
};
|
2024-07-11 13:05:02 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-11 16:45:45 +02:00
|
|
|
addObjectContent = (
|
|
|
|
|
messageObject: MessageObject,
|
|
|
|
|
content: MessageObjectContent
|
|
|
|
|
): void => {
|
|
|
|
|
messageObject.contentVersions[content.id] = content;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
addObject = (messageObject: MessageObject): void => {
|
|
|
|
|
const existingObject = this.objects.value.get(messageObject.id);
|
|
|
|
|
if (existingObject) {
|
|
|
|
|
existingObject.title = messageObject.title;
|
|
|
|
|
Object.values(messageObject.contentVersions).forEach((content) => {
|
|
|
|
|
this.addObjectContent(existingObject, content);
|
|
|
|
|
});
|
2024-07-11 20:16:28 +02:00
|
|
|
this.objects.set(existingObject.id, existingObject);
|
2024-07-11 16:45:45 +02:00
|
|
|
} else {
|
|
|
|
|
this.objects.set(messageObject.id, messageObject);
|
2024-07-11 15:08:25 +02:00
|
|
|
}
|
2024-07-12 02:24:48 +02:00
|
|
|
|
|
|
|
|
const latest = this.getMostRecentContent(messageObject);
|
|
|
|
|
if (!latest || !latest.categoryName) return;
|
|
|
|
|
usedObjectCategories.add(latest.categoryName);
|
2024-07-11 13:05:02 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-11 13:11:22 +02:00
|
|
|
addObjectAndSend = (messageObject: MessageObject): void => {
|
|
|
|
|
this.addObject(messageObject);
|
|
|
|
|
this.sendObject(messageObject);
|
2024-07-11 02:29:00 +02:00
|
|
|
};
|
2024-07-11 02:09:44 +02:00
|
|
|
|
2024-07-11 13:11:22 +02:00
|
|
|
sendObject = (messageObject: MessageObject): void => {
|
|
|
|
|
this.objectOutbox.set(messageObject.id, messageObject);
|
2024-07-11 02:09:44 +02:00
|
|
|
this.sendMessagesInOutbox();
|
2024-07-11 02:29:00 +02:00
|
|
|
};
|
2024-07-11 00:47:58 +02:00
|
|
|
|
2024-07-11 13:11:22 +02:00
|
|
|
deleteObject = (messageObject: MessageObject): void => {
|
|
|
|
|
this.objects.remove(messageObject.id);
|
|
|
|
|
this.objectOutbox.remove(messageObject.id);
|
2024-07-11 02:29:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
resendObjects = (): void => {
|
2024-07-11 13:05:02 +02:00
|
|
|
this.objects.value.forEach((messageObject) => {
|
|
|
|
|
this.sendObject(messageObject);
|
2024-07-11 02:29:00 +02:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
clearObjects = (): void => {
|
|
|
|
|
this.objects.clear();
|
|
|
|
|
};
|
2024-07-11 00:47:58 +02:00
|
|
|
|
2024-07-11 16:45:45 +02:00
|
|
|
getSortedContents = (
|
2024-07-11 13:05:02 +02:00
|
|
|
messageObject: MessageObject
|
2024-07-11 16:45:45 +02:00
|
|
|
): MessageObjectContent[] => {
|
|
|
|
|
const contents = Object.values(messageObject.contentVersions);
|
|
|
|
|
contents.sort((a, b) =>
|
2024-07-11 20:53:53 +02:00
|
|
|
a.isoDateVersionCreated > b.isoDateVersionCreated ? -1 : 1
|
2024-07-11 16:45:45 +02:00
|
|
|
);
|
|
|
|
|
return contents;
|
2024-07-11 15:08:25 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-11 16:45:45 +02:00
|
|
|
getMostRecentContentId = (messageObject: MessageObject): string => {
|
|
|
|
|
const contents = Object.values(messageObject.contentVersions);
|
|
|
|
|
return contents[contents.length - 1].id;
|
2024-07-11 15:08:25 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-11 20:16:28 +02:00
|
|
|
getMostRecentContent = (
|
|
|
|
|
messageObject: MessageObject
|
|
|
|
|
): MessageObjectContent => {
|
2024-07-11 16:45:45 +02:00
|
|
|
const id = this.getMostRecentContentId(messageObject);
|
|
|
|
|
return this.getObjectContentFromId(messageObject, id);
|
2024-07-11 20:16:28 +02:00
|
|
|
};
|
2024-07-11 16:45:45 +02:00
|
|
|
|
|
|
|
|
getObjectContentFromId = (
|
|
|
|
|
messageObject: MessageObject,
|
|
|
|
|
contentId: string
|
|
|
|
|
): MessageObjectContent => {
|
|
|
|
|
return messageObject.contentVersions[contentId];
|
2024-07-11 13:05:02 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-11 21:02:28 +02:00
|
|
|
getObjectTitle = (messageObject: MessageObject) => {
|
|
|
|
|
return messageObject.title || translation.untitledObject;
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-09 05:26:54 +02:00
|
|
|
// channel
|
2024-07-09 17:23:29 +02:00
|
|
|
setChannel = (): void => {
|
2024-07-09 11:39:23 +02:00
|
|
|
if (this.cannotSetChannel.value == true) return;
|
2024-07-09 17:23:29 +02:00
|
|
|
this.primaryChannel.value = this.primaryChannelInput.value;
|
|
|
|
|
UDN.subscribe(this.primaryChannel.value);
|
2024-07-09 20:20:25 +02:00
|
|
|
updateMailbox();
|
2024-07-09 17:23:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
undoChannelChange = (): void => {
|
|
|
|
|
this.primaryChannelInput.value = this.primaryChannel.value;
|
|
|
|
|
};
|
2024-07-09 05:26:54 +02:00
|
|
|
|
2024-07-09 17:23:29 +02:00
|
|
|
addSecondaryChannel = (): void => {
|
2024-07-09 05:26:54 +02:00
|
|
|
if (this.cannotAddSecondaryChannel.value == true) return;
|
|
|
|
|
|
|
|
|
|
this.secondaryChannels.add(this.newSecondaryChannelName.value);
|
|
|
|
|
this.newSecondaryChannelName.value = "";
|
2024-07-09 17:23:29 +02:00
|
|
|
};
|
2024-07-09 05:26:54 +02:00
|
|
|
|
2024-07-09 17:23:29 +02:00
|
|
|
removeSecondaryChannel = (channel: string): void => {
|
2024-07-09 05:26:54 +02:00
|
|
|
this.secondaryChannels.remove(channel);
|
2024-07-09 17:23:29 +02:00
|
|
|
};
|
2024-07-09 05:26:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// METHODS
|
|
|
|
|
export function createChatWithName(name: string): void {
|
|
|
|
|
const newChat = new Chat();
|
2024-07-09 15:15:17 +02:00
|
|
|
chats.add(newChat);
|
2024-07-09 17:23:29 +02:00
|
|
|
|
|
|
|
|
newChat.primaryChannel.value = name;
|
2024-07-09 19:37:59 +02:00
|
|
|
newChat.primaryChannelInput.value = name;
|
2024-07-09 17:23:29 +02:00
|
|
|
chatIds.add(newChat.id);
|
|
|
|
|
|
|
|
|
|
UDN.subscribe(name);
|
2024-07-09 20:20:25 +02:00
|
|
|
updateMailbox();
|
2024-07-09 05:26:54 +02:00
|
|
|
}
|