diff options
| author | Owen Jacobson <owen@grimoire.ca> | 2025-05-14 00:46:13 -0400 |
|---|---|---|
| committer | Owen Jacobson <owen@grimoire.ca> | 2025-05-14 00:46:59 -0400 |
| commit | ae93188f0f4f36086622636ba9ae4810cbd1f8c9 (patch) | |
| tree | 3187eaaf5b6391bfe8e135c829bcee47feed41ea /ui/lib | |
| parent | 63291a543aac293d91f2f64faf47bb48886b59c4 (diff) | |
Move derivation of the synthesized view of channels (and messages) into `session`.
Diffstat (limited to 'ui/lib')
| -rw-r--r-- | ui/lib/session.svelte.js | 52 | ||||
| -rw-r--r-- | ui/lib/state/remote/channels.svelte.js | 5 | ||||
| -rw-r--r-- | ui/lib/state/remote/messages.svelte.js | 7 |
3 files changed, 52 insertions, 12 deletions
diff --git a/ui/lib/session.svelte.js b/ui/lib/session.svelte.js index 21a391d..2dae3c4 100644 --- a/ui/lib/session.svelte.js +++ b/ui/lib/session.svelte.js @@ -6,16 +6,62 @@ import * as api from './apiServer.js'; import * as r from './state/remote/state.svelte.js'; import * as l from './state/local/channels.svelte.js'; import { Watchdog } from './watchdog.js'; +import { DateTime } from 'luxon'; +import { render } from '$lib/markdown.js'; + +class Channel { + static fromRemote({ at, id, name }, messages, meta) { + const sentAt = messages + .filter((message) => message.channel === id) + .map((message) => message.at); + const lastEventAt = Math.max(at, ...sentAt); + const lastReadAt = meta.get(id)?.lastReadAt; + + const hasUnreads = lastReadAt === undefined || lastEventAt > lastReadAt; + return new Channel({ at, id, name, hasUnreads }); + } + + constructor({ at, id, name, hasUnreads }) { + this.at = at; + this.id = id; + this.name = name; + this.hasUnreads = hasUnreads; + } +} + +class Message { + static fromRemote({ id, at, channel, sender, body, renderedBody }, users) { + return new Message({ + id, + at, + channel, + sender: users.get(sender), + body, + renderedBody + }); + } + + constructor({ id, at, channel, sender, body, renderedBody }) { + this.id = id; + this.at = at; + this.channel = channel; + this.sender = sender; + this.body = body; + this.renderedBody = renderedBody; + } +} class Session { remote = $state(); local = $state(); currentUser = $derived(this.remote.currentUser); users = $derived(this.remote.users.all); - channels = $derived(this.remote.channels.all); messages = $derived( - this.remote.messages.all.map((message) => - message.resolve({ sender: (id) => this.users.get(id) }) + this.remote.messages.all.map((message) => Message.fromRemote(message, this.users)) + ); + channels = $derived( + this.remote.channels.all.map((channel) => + Channel.fromRemote(channel, this.messages, this.local.all) ) ); diff --git a/ui/lib/state/remote/channels.svelte.js b/ui/lib/state/remote/channels.svelte.js index b6da31b..8b190dd 100644 --- a/ui/lib/state/remote/channels.svelte.js +++ b/ui/lib/state/remote/channels.svelte.js @@ -1,5 +1,4 @@ import { DateTime } from 'luxon'; -import { SvelteMap } from 'svelte/reactivity'; class Channel { static boot({ at, id, name }) { @@ -18,10 +17,10 @@ class Channel { } export class Channels { - all = $state(); + all = $state([]); static boot(channels) { - const all = new SvelteMap(channels.map((channel) => [channel.id, Channel.boot(channel)])); + const all = channels.map((channel) => Channel.boot(channel)); return new Channels({ all }); } diff --git a/ui/lib/state/remote/messages.svelte.js b/ui/lib/state/remote/messages.svelte.js index c6d31f0..0a081bb 100644 --- a/ui/lib/state/remote/messages.svelte.js +++ b/ui/lib/state/remote/messages.svelte.js @@ -1,7 +1,7 @@ import { DateTime } from 'luxon'; import { render } from '$lib/markdown.js'; -export class Message { +class Message { static boot({ id, at, channel, sender, body }) { return new Message({ id, @@ -21,11 +21,6 @@ export class Message { this.body = body; this.renderedBody = renderedBody; } - - resolve(get) { - const { sender, ...rest } = this; - return new Message({ sender: get.sender(sender), ...rest }); - } } export class Messages { |
