add logic for sending and receiving items
This commit is contained in:
Vendored
+103
-24
@@ -118,7 +118,48 @@
|
|||||||
}
|
}
|
||||||
// stringification
|
// stringification
|
||||||
toString() {
|
toString() {
|
||||||
const array = [...this.value];
|
const array = [...this.value.values()];
|
||||||
|
const json = JSON.stringify(array);
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var MapState = class extends State {
|
||||||
|
additionHandlers = /* @__PURE__ */ new Set();
|
||||||
|
removalHandlers = /* @__PURE__ */ new Map();
|
||||||
|
// init
|
||||||
|
constructor(initialItems) {
|
||||||
|
super(new Map(initialItems));
|
||||||
|
}
|
||||||
|
// list
|
||||||
|
set(key, item) {
|
||||||
|
this.value.set(key, item);
|
||||||
|
this.additionHandlers.forEach((handler) => handler(item));
|
||||||
|
this.callSubscriptions();
|
||||||
|
}
|
||||||
|
remove(key) {
|
||||||
|
const item = this.value.get(key);
|
||||||
|
if (!item) return;
|
||||||
|
this.value.delete(key);
|
||||||
|
this.callSubscriptions();
|
||||||
|
if (!this.removalHandlers.has(item)) return;
|
||||||
|
this.removalHandlers.get(item)(item);
|
||||||
|
this.removalHandlers.delete(item);
|
||||||
|
}
|
||||||
|
clear() {
|
||||||
|
this.value.clear();
|
||||||
|
this.callSubscriptions();
|
||||||
|
}
|
||||||
|
// handlers
|
||||||
|
handleAddition(handler) {
|
||||||
|
this.additionHandlers.add(handler);
|
||||||
|
[...this.value.values()].forEach(handler);
|
||||||
|
}
|
||||||
|
handleRemoval(item, handler) {
|
||||||
|
this.removalHandlers.set(item, handler);
|
||||||
|
}
|
||||||
|
// stringification
|
||||||
|
toString() {
|
||||||
|
const array = [...this.value.entries()];
|
||||||
const json = JSON.stringify(array);
|
const json = JSON.stringify(array);
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
@@ -143,9 +184,8 @@
|
|||||||
persistState(localStorageKey, state);
|
persistState(localStorageKey, state);
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
function restoreListState(localStorageKey) {
|
function restoreListState(localStorageKey, initialItems = []) {
|
||||||
const storedString = localStorage.getItem(localStorageKey) ?? "";
|
const storedString = localStorage.getItem(localStorageKey) ?? "";
|
||||||
let initialItems = [];
|
|
||||||
try {
|
try {
|
||||||
const array = JSON.parse(storedString);
|
const array = JSON.parse(storedString);
|
||||||
if (!Array.isArray(array)) throw "";
|
if (!Array.isArray(array)) throw "";
|
||||||
@@ -156,6 +196,18 @@
|
|||||||
persistState(localStorageKey, state);
|
persistState(localStorageKey, state);
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
function restoreMapState(localStorageKey, initialItems = []) {
|
||||||
|
const storedString = localStorage.getItem(localStorageKey) ?? "";
|
||||||
|
try {
|
||||||
|
const array = JSON.parse(storedString);
|
||||||
|
if (!Array.isArray(array)) throw "";
|
||||||
|
initialItems = array;
|
||||||
|
} catch {
|
||||||
|
}
|
||||||
|
const state = new MapState(initialItems);
|
||||||
|
persistState(localStorageKey, state);
|
||||||
|
return state;
|
||||||
|
}
|
||||||
function createElement(tagName, attributes = {}, ...children) {
|
function createElement(tagName, attributes = {}, ...children) {
|
||||||
const element = document.createElement(tagName);
|
const element = document.createElement(tagName);
|
||||||
if (attributes != null)
|
if (attributes != null)
|
||||||
@@ -346,6 +398,9 @@
|
|||||||
messages(id) {
|
messages(id) {
|
||||||
return id + "messages";
|
return id + "messages";
|
||||||
},
|
},
|
||||||
|
items(id) {
|
||||||
|
return id + "items";
|
||||||
|
},
|
||||||
outbox(id) {
|
outbox(id) {
|
||||||
return id + "outbox";
|
return id + "outbox";
|
||||||
},
|
},
|
||||||
@@ -363,6 +418,7 @@
|
|||||||
secondaryChannels;
|
secondaryChannels;
|
||||||
encryptionKey;
|
encryptionKey;
|
||||||
messages;
|
messages;
|
||||||
|
items;
|
||||||
outbox;
|
outbox;
|
||||||
isOutBoxEmpty;
|
isOutBoxEmpty;
|
||||||
composingMessage;
|
composingMessage;
|
||||||
@@ -390,6 +446,7 @@
|
|||||||
);
|
);
|
||||||
this.encryptionKey = restoreState(storageKeys.encyptionKey(id), "");
|
this.encryptionKey = restoreState(storageKeys.encyptionKey(id), "");
|
||||||
this.messages = restoreListState(storageKeys.messages(id));
|
this.messages = restoreListState(storageKeys.messages(id));
|
||||||
|
this.items = restoreMapState(storageKeys.items(id));
|
||||||
this.outbox = restoreListState(storageKeys.outbox(id));
|
this.outbox = restoreListState(storageKeys.outbox(id));
|
||||||
this.isOutBoxEmpty = createProxyState(
|
this.isOutBoxEmpty = createProxyState(
|
||||||
[this.outbox],
|
[this.outbox],
|
||||||
@@ -441,7 +498,12 @@
|
|||||||
if (channels.indexOf(this.primaryChannel.value) == -1) return;
|
if (channels.indexOf(this.primaryChannel.value) == -1) return;
|
||||||
if (data.subscribed != void 0) this.handleSubscription(data.subscribed);
|
if (data.subscribed != void 0) this.handleSubscription(data.subscribed);
|
||||||
if (!data.messageBody) return;
|
if (!data.messageBody) return;
|
||||||
const { sender, body, channel, isoDate } = JSON.parse(data.messageBody);
|
const { sender, body, channel, isoDate, itemData } = JSON.parse(
|
||||||
|
data.messageBody
|
||||||
|
);
|
||||||
|
if (itemData != void 0) {
|
||||||
|
return this.handleItem(itemData);
|
||||||
|
}
|
||||||
this.handleMessage({
|
this.handleMessage({
|
||||||
sender,
|
sender,
|
||||||
body: await decryptString(body, this.encryptionKey.value),
|
body: await decryptString(body, this.encryptionKey.value),
|
||||||
@@ -459,21 +521,11 @@
|
|||||||
this.messages.add(chatMessage);
|
this.messages.add(chatMessage);
|
||||||
if (selectedChat.value != this) this.hasUnreadMessages.value = true;
|
if (selectedChat.value != this) this.hasUnreadMessages.value = true;
|
||||||
};
|
};
|
||||||
|
handleItem = (itemData) => {
|
||||||
|
console.log(itemData);
|
||||||
|
};
|
||||||
// messages
|
// messages
|
||||||
sendNewMessage = async () => {
|
createMessage = async (messageText, itemData) => {
|
||||||
if (this.cannotSendMessage.value == true) return;
|
|
||||||
await this.sendMessageText(this.composingMessage.value);
|
|
||||||
this.composingMessage.value = "";
|
|
||||||
};
|
|
||||||
sendMessageText = async (text) => {
|
|
||||||
const message = await this.createMessage(text);
|
|
||||||
this.sendExistingMessage(message);
|
|
||||||
};
|
|
||||||
resendMessage = (message) => {
|
|
||||||
if (this.cannotResendMessage.value == true) return;
|
|
||||||
this.sendMessageText(message.body);
|
|
||||||
};
|
|
||||||
createMessage = async (messageText) => {
|
|
||||||
const secondaryChannelNames = [
|
const secondaryChannelNames = [
|
||||||
...this.secondaryChannels.value.values()
|
...this.secondaryChannels.value.values()
|
||||||
];
|
];
|
||||||
@@ -482,14 +534,29 @@
|
|||||||
...secondaryChannelNames
|
...secondaryChannelNames
|
||||||
];
|
];
|
||||||
const joinedChannelName = allChannelNames.join("/");
|
const joinedChannelName = allChannelNames.join("/");
|
||||||
return {
|
const messageObject = {
|
||||||
channel: joinedChannelName,
|
channel: joinedChannelName,
|
||||||
sender: senderName.value,
|
sender: senderName.value,
|
||||||
body: messageText,
|
body: messageText,
|
||||||
isoDate: (/* @__PURE__ */ new Date()).toISOString()
|
isoDate: (/* @__PURE__ */ new Date()).toISOString()
|
||||||
};
|
};
|
||||||
|
if (itemData) messageObject.itemData = itemData;
|
||||||
|
return messageObject;
|
||||||
};
|
};
|
||||||
sendExistingMessage = async (chatMessage) => {
|
sendMessageFromComposer = async () => {
|
||||||
|
if (this.cannotSendMessage.value == true) return;
|
||||||
|
await this.sendMessageFromText(this.composingMessage.value);
|
||||||
|
this.composingMessage.value = "";
|
||||||
|
};
|
||||||
|
sendMessageFromText = async (text) => {
|
||||||
|
const message = await this.createMessage(text);
|
||||||
|
this.sendMessage(message);
|
||||||
|
};
|
||||||
|
resendMessage = (message) => {
|
||||||
|
if (this.cannotResendMessage.value == true) return;
|
||||||
|
this.sendMessageFromText(message.body);
|
||||||
|
};
|
||||||
|
sendMessage = async (chatMessage) => {
|
||||||
if (isConnected.value == true && this.isSubscribed.value == true) {
|
if (isConnected.value == true && this.isSubscribed.value == true) {
|
||||||
const encrypted = this.encryptionKey.value == "" ? chatMessage.body : await encryptString(chatMessage.body, this.encryptionKey.value);
|
const encrypted = this.encryptionKey.value == "" ? chatMessage.body : await encryptString(chatMessage.body, this.encryptionKey.value);
|
||||||
chatMessage.body = encrypted;
|
chatMessage.body = encrypted;
|
||||||
@@ -501,7 +568,7 @@
|
|||||||
};
|
};
|
||||||
sendMessagesInOutbox = () => {
|
sendMessagesInOutbox = () => {
|
||||||
this.outbox.value.forEach((message) => {
|
this.outbox.value.forEach((message) => {
|
||||||
this.sendExistingMessage(message);
|
this.sendMessage(message);
|
||||||
this.outbox.remove(message);
|
this.outbox.remove(message);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@@ -518,6 +585,12 @@
|
|||||||
message.body = await decryptString(message.body, this.encryptionKey.value);
|
message.body = await decryptString(message.body, this.encryptionKey.value);
|
||||||
this.messages.callSubscriptions();
|
this.messages.callSubscriptions();
|
||||||
};
|
};
|
||||||
|
// items
|
||||||
|
async createItem(item) {
|
||||||
|
this.items.set(item.id, item);
|
||||||
|
const message = await this.createMessage("", item);
|
||||||
|
this.sendMessage(message);
|
||||||
|
}
|
||||||
// channel
|
// channel
|
||||||
setChannel = () => {
|
setChannel = () => {
|
||||||
if (this.cannotSetChannel.value == true) return;
|
if (this.cannotSetChannel.value == true) return;
|
||||||
@@ -1031,7 +1104,13 @@
|
|||||||
|
|
||||||
// src/Views/chatToolView.tsx
|
// src/Views/chatToolView.tsx
|
||||||
function ChatToolView(chat) {
|
function ChatToolView(chat) {
|
||||||
return /* @__PURE__ */ createElement("div", { class: "chat-tool-view" }, /* @__PURE__ */ createElement("span", null, "Tools"));
|
function createItem() {
|
||||||
|
chat.createItem({
|
||||||
|
id: UUID(),
|
||||||
|
title: "new item"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return /* @__PURE__ */ createElement("div", { class: "chat-tool-view" }, /* @__PURE__ */ createElement("button", { "on:click": createItem }, "+"), /* @__PURE__ */ createElement("hr", null));
|
||||||
}
|
}
|
||||||
|
|
||||||
// src/Views/messageComposer.tsx
|
// src/Views/messageComposer.tsx
|
||||||
@@ -1043,13 +1122,13 @@
|
|||||||
style: "max-width: unset",
|
style: "max-width: unset",
|
||||||
placeholder: translation.composerPlaceholder,
|
placeholder: translation.composerPlaceholder,
|
||||||
"bind:value": chat.composingMessage,
|
"bind:value": chat.composingMessage,
|
||||||
"on:enter": chat.sendNewMessage
|
"on:enter": chat.sendMessageFromComposer
|
||||||
}
|
}
|
||||||
), /* @__PURE__ */ createElement(
|
), /* @__PURE__ */ createElement(
|
||||||
"button",
|
"button",
|
||||||
{
|
{
|
||||||
class: "primary",
|
class: "primary",
|
||||||
"on:click": chat.sendNewMessage,
|
"on:click": chat.sendMessageFromComposer,
|
||||||
"toggle:disabled": chat.cannotSendMessage
|
"toggle:disabled": chat.cannotSendMessage
|
||||||
},
|
},
|
||||||
/* @__PURE__ */ createElement("span", { class: "icon" }, "send")
|
/* @__PURE__ */ createElement("span", { class: "icon" }, "send")
|
||||||
|
|||||||
+61
-22
@@ -21,6 +21,18 @@ export interface ChatMessage {
|
|||||||
sender: string;
|
sender: string;
|
||||||
body: string;
|
body: string;
|
||||||
isoDate: string;
|
isoDate: string;
|
||||||
|
|
||||||
|
itemData?: Item;
|
||||||
|
}
|
||||||
|
|
||||||
|
// item
|
||||||
|
export interface Item {
|
||||||
|
id: string;
|
||||||
|
title: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface NoteItem extends Item {
|
||||||
|
body: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
// chat
|
// chat
|
||||||
@@ -34,6 +46,7 @@ export class Chat {
|
|||||||
|
|
||||||
encryptionKey: React.State<string>;
|
encryptionKey: React.State<string>;
|
||||||
messages: React.ListState<ChatMessage>;
|
messages: React.ListState<ChatMessage>;
|
||||||
|
items: React.MapState<Item>;
|
||||||
outbox: React.ListState<ChatMessage>;
|
outbox: React.ListState<ChatMessage>;
|
||||||
isOutBoxEmpty: React.State<boolean>;
|
isOutBoxEmpty: React.State<boolean>;
|
||||||
|
|
||||||
@@ -68,6 +81,7 @@ export class Chat {
|
|||||||
);
|
);
|
||||||
this.encryptionKey = React.restoreState(storageKeys.encyptionKey(id), "");
|
this.encryptionKey = React.restoreState(storageKeys.encyptionKey(id), "");
|
||||||
this.messages = React.restoreListState(storageKeys.messages(id));
|
this.messages = React.restoreListState(storageKeys.messages(id));
|
||||||
|
this.items = React.restoreMapState(storageKeys.items(id));
|
||||||
this.outbox = React.restoreListState(storageKeys.outbox(id));
|
this.outbox = React.restoreListState(storageKeys.outbox(id));
|
||||||
this.isOutBoxEmpty = React.createProxyState(
|
this.isOutBoxEmpty = React.createProxyState(
|
||||||
[this.outbox],
|
[this.outbox],
|
||||||
@@ -133,7 +147,14 @@ export class Chat {
|
|||||||
if (data.subscribed != undefined) this.handleSubscription(data.subscribed);
|
if (data.subscribed != undefined) this.handleSubscription(data.subscribed);
|
||||||
|
|
||||||
if (!data.messageBody) return;
|
if (!data.messageBody) return;
|
||||||
const { sender, body, channel, isoDate } = JSON.parse(data.messageBody);
|
const { sender, body, channel, isoDate, itemData } = JSON.parse(
|
||||||
|
data.messageBody
|
||||||
|
);
|
||||||
|
|
||||||
|
if (itemData != undefined) {
|
||||||
|
return this.handleItem(itemData);
|
||||||
|
}
|
||||||
|
|
||||||
this.handleMessage({
|
this.handleMessage({
|
||||||
sender,
|
sender,
|
||||||
body: await decryptString(body, this.encryptionKey.value),
|
body: await decryptString(body, this.encryptionKey.value),
|
||||||
@@ -155,25 +176,15 @@ export class Chat {
|
|||||||
if (selectedChat.value != this) this.hasUnreadMessages.value = true;
|
if (selectedChat.value != this) this.hasUnreadMessages.value = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
handleItem = (itemData: Item): void => {
|
||||||
|
console.log(itemData);
|
||||||
|
};
|
||||||
|
|
||||||
// messages
|
// messages
|
||||||
sendNewMessage = async (): Promise<void> => {
|
createMessage = async (
|
||||||
if (this.cannotSendMessage.value == true) return;
|
messageText: string,
|
||||||
|
itemData?: Item
|
||||||
await this.sendMessageText(this.composingMessage.value);
|
): Promise<ChatMessage> => {
|
||||||
this.composingMessage.value = "";
|
|
||||||
};
|
|
||||||
|
|
||||||
sendMessageText = async (text: string): Promise<void> => {
|
|
||||||
const message = await this.createMessage(text);
|
|
||||||
this.sendExistingMessage(message);
|
|
||||||
};
|
|
||||||
|
|
||||||
resendMessage = (message: ChatMessage) => {
|
|
||||||
if (this.cannotResendMessage.value == true) return;
|
|
||||||
this.sendMessageText(message.body);
|
|
||||||
};
|
|
||||||
|
|
||||||
createMessage = async (messageText: string): Promise<ChatMessage> => {
|
|
||||||
// get channels
|
// get channels
|
||||||
const secondaryChannelNames: string[] = [
|
const secondaryChannelNames: string[] = [
|
||||||
...this.secondaryChannels.value.values(),
|
...this.secondaryChannels.value.values(),
|
||||||
@@ -185,15 +196,35 @@ export class Chat {
|
|||||||
|
|
||||||
// create object
|
// create object
|
||||||
const joinedChannelName = allChannelNames.join("/");
|
const joinedChannelName = allChannelNames.join("/");
|
||||||
return {
|
const messageObject: ChatMessage = {
|
||||||
channel: joinedChannelName,
|
channel: joinedChannelName,
|
||||||
sender: senderName.value,
|
sender: senderName.value,
|
||||||
body: messageText,
|
body: messageText,
|
||||||
isoDate: new Date().toISOString(),
|
isoDate: new Date().toISOString(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (itemData) messageObject.itemData = itemData;
|
||||||
|
return messageObject;
|
||||||
};
|
};
|
||||||
|
|
||||||
sendExistingMessage = async (chatMessage: ChatMessage): Promise<void> => {
|
sendMessageFromComposer = async (): Promise<void> => {
|
||||||
|
if (this.cannotSendMessage.value == true) return;
|
||||||
|
|
||||||
|
await this.sendMessageFromText(this.composingMessage.value);
|
||||||
|
this.composingMessage.value = "";
|
||||||
|
};
|
||||||
|
|
||||||
|
sendMessageFromText = async (text: string): Promise<void> => {
|
||||||
|
const message = await this.createMessage(text);
|
||||||
|
this.sendMessage(message);
|
||||||
|
};
|
||||||
|
|
||||||
|
resendMessage = (message: ChatMessage) => {
|
||||||
|
if (this.cannotResendMessage.value == true) return;
|
||||||
|
this.sendMessageFromText(message.body);
|
||||||
|
};
|
||||||
|
|
||||||
|
sendMessage = async (chatMessage: ChatMessage): Promise<void> => {
|
||||||
if (isConnected.value == true && this.isSubscribed.value == true) {
|
if (isConnected.value == true && this.isSubscribed.value == true) {
|
||||||
const encrypted =
|
const encrypted =
|
||||||
this.encryptionKey.value == ""
|
this.encryptionKey.value == ""
|
||||||
@@ -211,7 +242,7 @@ export class Chat {
|
|||||||
|
|
||||||
sendMessagesInOutbox = () => {
|
sendMessagesInOutbox = () => {
|
||||||
this.outbox.value.forEach((message) => {
|
this.outbox.value.forEach((message) => {
|
||||||
this.sendExistingMessage(message);
|
this.sendMessage(message);
|
||||||
this.outbox.remove(message);
|
this.outbox.remove(message);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@@ -233,6 +264,14 @@ export class Chat {
|
|||||||
this.messages.callSubscriptions();
|
this.messages.callSubscriptions();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// items
|
||||||
|
async createItem(item: Item): Promise<void> {
|
||||||
|
this.items.set(item.id, item);
|
||||||
|
|
||||||
|
const message = await this.createMessage("", item);
|
||||||
|
this.sendMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
// channel
|
// channel
|
||||||
setChannel = (): void => {
|
setChannel = (): void => {
|
||||||
if (this.cannotSetChannel.value == true) return;
|
if (this.cannotSetChannel.value == true) return;
|
||||||
|
|||||||
@@ -4,9 +4,18 @@ import { Chat } from "../Model/chatModel";
|
|||||||
import { translation } from "../translations";
|
import { translation } from "../translations";
|
||||||
|
|
||||||
export function ChatToolView(chat: Chat) {
|
export function ChatToolView(chat: Chat) {
|
||||||
|
function createItem() {
|
||||||
|
chat.createItem({
|
||||||
|
id: React.UUID(),
|
||||||
|
title: "new item",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div class="chat-tool-view">
|
<div class="chat-tool-view">
|
||||||
<span>Tools</span>
|
<button on:click={createItem}>+</button>
|
||||||
|
<hr></hr>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,11 +12,11 @@ export function MessageComposer(chat: Chat) {
|
|||||||
style="max-width: unset"
|
style="max-width: unset"
|
||||||
placeholder={translation.composerPlaceholder}
|
placeholder={translation.composerPlaceholder}
|
||||||
bind:value={chat.composingMessage}
|
bind:value={chat.composingMessage}
|
||||||
on:enter={chat.sendNewMessage}
|
on:enter={chat.sendMessageFromComposer}
|
||||||
></input>
|
></input>
|
||||||
<button
|
<button
|
||||||
class="primary"
|
class="primary"
|
||||||
on:click={chat.sendNewMessage}
|
on:click={chat.sendMessageFromComposer}
|
||||||
toggle:disabled={chat.cannotSendMessage}
|
toggle:disabled={chat.cannotSendMessage}
|
||||||
>
|
>
|
||||||
<span class="icon">send</span>
|
<span class="icon">send</span>
|
||||||
|
|||||||
@@ -19,6 +19,10 @@ export const storageKeys = {
|
|||||||
return id + "messages";
|
return id + "messages";
|
||||||
},
|
},
|
||||||
|
|
||||||
|
items(id: string): string {
|
||||||
|
return id + "items";
|
||||||
|
},
|
||||||
|
|
||||||
outbox(id: string): string {
|
outbox(id: string): string {
|
||||||
return id + "outbox";
|
return id + "outbox";
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user