From 2f0b77e8fd02a137047c8975a573626cd76310ff Mon Sep 17 00:00:00 2001 From: Owen Jacobson Date: Wed, 9 Oct 2024 01:43:34 -0400 Subject: Return a flat message list on boot, not nested lists by channel. This is a bit easier to compute, and sets us up nicely for pulling message boot out of the `/api/boot` response entirely. --- hi-ui/src/apiServer.js | 2 +- hi-ui/src/routes/+page.svelte | 14 ++++---------- hi-ui/src/store/logins.js | 3 ++- hi-ui/src/store/messages.js | 16 +++++++++++----- 4 files changed, 18 insertions(+), 17 deletions(-) (limited to 'hi-ui') diff --git a/hi-ui/src/apiServer.js b/hi-ui/src/apiServer.js index 4421ef5..3aa3f1b 100644 --- a/hi-ui/src/apiServer.js +++ b/hi-ui/src/apiServer.js @@ -92,7 +92,7 @@ function onChannelEvent(data) { function onMessageEvent(data) { switch (data.event) { case 'sent': - messages.update((value) => value.addMessage(data.channel, data.at, data.sender, data.body)); + messages.update((value) => value.addMessage(data.channel, data.id, data.at, data.sender, data.body)); break; case 'deleted': messages.update((value) => value.deleteMessage(data.id)); diff --git a/hi-ui/src/routes/+page.svelte b/hi-ui/src/routes/+page.svelte index 39f8b62..dd5f2f7 100644 --- a/hi-ui/src/routes/+page.svelte +++ b/hi-ui/src/routes/+page.svelte @@ -18,20 +18,13 @@ }); function onBooted(boot) { - logins.update((value) => value.addLogins(boot.logins)); currentUser.update(() => ({ id: boot.login.id, username: boot.login.name, })); - let channels = boot.channels.map((channel) => ({ - id: channel.id, - name: channel.name, - })); - channelsList.update((value) => value.setChannels(channels)); - let bootMessages = boot.channels.map((channel) => [channel.id, channel.messages]); - for (let [channel, channelMessages] of bootMessages) { - messages.update((value) => value.addMessages(channel, channelMessages)); - } + logins.update((value) => value.setLogins(boot.logins)); + channelsList.update((value) => value.setChannels(boot.channels)); + messages.update((value) => value.setMessages(boot.messages)); } onMount(async () => { @@ -39,6 +32,7 @@ let response = await boot(); switch (response.status) { case 200: + debugger; onBooted(response.data); subscribeToEvents(response.data.resume_point); break; diff --git a/hi-ui/src/store/logins.js b/hi-ui/src/store/logins.js index 207b757..5b45206 100644 --- a/hi-ui/src/store/logins.js +++ b/hi-ui/src/store/logins.js @@ -8,7 +8,8 @@ export class Logins { return this; } - addLogins(logins) { + setLogins(logins) { + this.logins = {}; for (let { id, name } of logins) { this.addLogin(id, name); } diff --git a/hi-ui/src/store/messages.js b/hi-ui/src/store/messages.js index f9e0856..931b8fb 100644 --- a/hi-ui/src/store/messages.js +++ b/hi-ui/src/store/messages.js @@ -4,16 +4,22 @@ export class Messages { } inChannel(channel) { - return this.channels[channel] || []; + return this.channels[channel] = (this.channels[channel] || []); } - addMessage(channel, at, sender, body) { - this.updateChannel(channel, (messages) => [...messages, { at, sender, body }]); + addMessage(channel, id, at, sender, body) { + this.updateChannel(channel, (messages) => [...messages, { id, at, sender, body }]); return this; } - addMessages(channel, payloads) { - this.updateChannel(channel, (messages) => [...messages, ...payloads]); + setMessages(messages) { + this.channels = {}; + for (let { channel, id, at, sender, body } of messages) { + this.inChannel(channel).push({ id, at, sender, body, }); + } + for (let channel in this.channels) { + this.channels[channel].sort((a, b) => a.at - b.at); + } return this; } -- cgit v1.2.3