From 91d70702f308b2e37988ab63ed6f77e61ee3f893 Mon Sep 17 00:00:00 2001 From: faumaray Date: Sat, 24 Oct 2026 19:33:29 +0300 Subject: [PATCH] Take the text of an offline message in before the redraw, not after it The social split left the offline message's state change in the front end's `rest` events, which `perform` handled after it drew the conversations: `refresh_chats` drew the open message while `dm.mail` was still empty and the inbox row was still unread, and nothing drew the page again. Before the split `social_event` wrote both ahead of `social_refresh`. `Event::OfflineMessage` is now taken in by `social::logic::event` like the other offline events: the text is `Social::mail`, the inbox row turns read, and a failure is the status line. `events::apply` no longer parks the event in `rest`, so a tape test in `voelin-ui-vm` sees it (the mutation that parks it fails the test). `perform` also handles what is left in `rest` (a setting, the studio's stream, the request answers) before it draws, as every such event ran before the redraw ahead of the split. Co-Authored-By: Claude Sonnet 5.5 --- crates/voelin-ui-vm/src/events/mod.rs | 16 ++-- crates/voelin-ui-vm/src/host.rs | 3 +- crates/voelin-ui-vm/src/social/logic.rs | 99 ++++++++++++++++++++++++- crates/voelin-ui/src/host.rs | 19 +++-- crates/voelin-ui/src/messages.rs | 23 +----- docs/ui.md | 21 ++++-- 6 files changed, 138 insertions(+), 43 deletions(-) diff --git a/crates/voelin-ui-vm/src/events/mod.rs b/crates/voelin-ui-vm/src/events/mod.rs index c8f54c9..c3c5768 100644 --- a/crates/voelin-ui-vm/src/events/mod.rs +++ b/crates/voelin-ui-vm/src/events/mod.rs @@ -11,9 +11,12 @@ //! //! In [`Update::rest`] come back the events whose state is not in the //! [`AppState`]: the Stream Studio's stream (`Host::studio_streams_to`), a -//! setting that changed (the settings service), and the offline messages -//! and their requests, which the messages page keeps (docs/ui.md, -//! "Controllers still to split"). +//! setting that changed (the settings service), and the answers to the +//! offline message requests (the list that failed, a request that is done), +//! which the messages page keeps (docs/ui.md, "Controllers still to split"). +//! A front end handles them before it draws what the update says is to be +//! drawn again. The text of an offline message is not among them: it is +//! state of the [`Social`], changed by [`handle`]. mod gateway; @@ -264,8 +267,11 @@ fn dispatch(state: &mut AppState, cx: &mut Cx<'_>, event: Event) { } // Also flagged on the sink, which the encoder reads. Event::StreamKeyframeRequest { .. } | Event::StreamLayerBitrate { .. } => {} - // What the settings service and the friends make of the rest, the - // front end keeps (see the module's note). + // The text of an offline message is in the `Social` state, which + // `handle` has changed before (`social::logic::event`). + Event::OfflineMessage { .. } => {} + // What the settings service and the offline messages' requests make + // of the rest, the front end keeps (see the module's note). event => cx.update.rest = Some(event), } } diff --git a/crates/voelin-ui-vm/src/host.rs b/crates/voelin-ui-vm/src/host.rs index 67b674e..b0294f2 100644 --- a/crates/voelin-ui-vm/src/host.rs +++ b/crates/voelin-ui-vm/src/host.rs @@ -203,7 +203,8 @@ pub struct Update { pub first_client: bool, /// The event, when it is not (only) a matter of this state: a stream we /// watch or share, the settings service. The front end handles those - /// itself ([`crate::events`] lists them). + /// itself ([`crate::events`] lists them), before it draws what `refresh` + /// names, as what it keeps may be on screen. pub rest: Option, } diff --git a/crates/voelin-ui-vm/src/social/logic.rs b/crates/voelin-ui-vm/src/social/logic.rs index 8427c67..a3745b3 100644 --- a/crates/voelin-ui-vm/src/social/logic.rs +++ b/crates/voelin-ui-vm/src/social/logic.rs @@ -14,7 +14,7 @@ use std::collections::{HashMap, HashSet}; use std::sync::Arc; -use voelin_core::{Command, Contact, Event, FriendSpot, HistorySource, Relation}; +use voelin_core::{Command, Contact, Event, FriendSpot, HistorySource, OfflineMessage, Relation}; use voelin_model::{ChatMessage, ChatTarget}; use voelin_store::Bookmark; @@ -118,6 +118,9 @@ pub struct Social { pub selected: Option, /// What the search results open, by index. pub found: Vec, + /// The text of the offline message that is open on the messages page, + /// once the server has sent it ([`Event::OfflineMessage`]). + pub mail: Option, /// The direct messages. pub dm: D, } @@ -715,9 +718,26 @@ pub fn event( state.sessions.entry(*session as i64).or_default().extra.inbox = list.clone(); touched.conversations = true; } - Event::OfflineMessages { .. } | Event::OfflineMessage { .. } => { + // The text of an offline message is state, so it is taken in here, + // before the update is drawn: the messages page shows it, and the + // inbox row turns read, in the same redraw. + Event::OfflineMessage { session, result, .. } => { + match result { + Ok(message) => { + if let Some(info) = state + .sessions + .get_mut(&(*session as i64)) + .and_then(|v| v.extra.inbox.iter_mut().find(|i| i.id == message.id)) + { + info.read = true; + } + social.mail = Some(message.clone()); + } + Err(e) => cx.status(format!("Could not read the offline message: {e}")), + } touched.conversations = true; } + Event::OfflineMessages { .. } => touched.conversations = true, _ => {} } let r = &mut cx.update.refresh; @@ -1457,6 +1477,81 @@ mod tests { assert!(!st.sessions[&1].extra.inbox_asked); } + /// The messages page draws the text of an open offline message and the + /// inbox row's read flag from the state, in the redraw the event asks + /// for: both have to be state by the time `handle` returns (the front + /// end draws after it, and not before it has taken the event in). + #[test] + fn an_offline_message_is_state_when_the_redraw_it_asks_for_runs() { + let mut st = state(); + let mut social = Soc::default(); + let host = Fake::default(); + let info = |id| OfflineMessageInfo { + id, + from_uid: "alice=".into(), + subject: "hi".into(), + ts_s: 1, + read: false, + }; + let message = |id, text: &str| OfflineMessage { + id, + from_uid: "alice=".into(), + subject: "hi".into(), + text: text.into(), + ts_s: 1, + }; + let ups = run( + &mut st, + &mut social, + &host, + vec![ + Event::OfflineMessages { + session: 1, + request: 1, + result: Ok(vec![info(4), info(5)]), + }, + Event::OfflineMessage { session: 1, request: 2, result: Ok(message(4, "Hello")) }, + ], + ); + let read = + |st: &State| st.sessions[&1].extra.inbox.iter().map(|i| i.read).collect::>(); + assert_eq!(social.mail, Some(message(4, "Hello"))); + assert_eq!(read(&st), [true, false], "only the message that was opened"); + assert!(ups[1].refresh.conversations, "the page is drawn again"); + assert!(ups[1].rest.is_none(), "nothing is left for the front end to do first"); + assert_eq!(ups[1].status, None); + // A message that is no row of the list (or of a session that is not + // known) has its text, and marks nothing. + let ups = run( + &mut st, + &mut social, + &host, + vec![ + Event::OfflineMessage { session: 1, request: 3, result: Ok(message(9, "Other")) }, + Event::OfflineMessage { + session: 8, + request: 4, + result: Ok(message(4, "Elsewhere")), + }, + ], + ); + assert_eq!(social.mail, Some(message(4, "Elsewhere"))); + assert_eq!(read(&st), [true, false]); + assert!(!st.sessions.contains_key(&8), "no session is made up for it"); + assert!(ups.iter().all(|u| u.refresh.conversations && u.rest.is_none())); + // A failure is for the status line, and the text that was open stays. + let ups = run( + &mut st, + &mut social, + &host, + vec![Event::OfflineMessage { session: 1, request: 5, result: Err("gone".into()) }], + ); + assert_eq!(ups[0].status.as_deref(), Some("Could not read the offline message: gone")); + assert_eq!(st.status, "Could not read the offline message: gone"); + assert_eq!(social.mail, Some(message(4, "Elsewhere"))); + assert!(ups[0].refresh.conversations && ups[0].rest.is_none()); + } + fn spot(session: i64, channel: &str) -> FriendSpot { FriendSpot { session: session as u64, diff --git a/crates/voelin-ui/src/host.rs b/crates/voelin-ui/src/host.rs index 7c1d6e4..ce3d648 100644 --- a/crates/voelin-ui/src/host.rs +++ b/crates/voelin-ui/src/host.rs @@ -218,6 +218,11 @@ impl App { notice.uid, ); } + // What the front end keeps itself comes before the drawing: what + // it changes is on screen then. + if let Some(event) = rest { + self.event_rest(event); + } self.refresh_parts(refresh); for (id, target) in chats { self.studio_chat_changed(id, &target); @@ -231,9 +236,6 @@ impl App { if first_client && self.open_client_pending { self.open_first_client(); } - if let Some(event) = rest { - self.event_rest(event); - } } /// Keep what a transition changed. @@ -307,7 +309,9 @@ impl App { } /// The events whose state the view model does not hold: a setting that - /// changed, and the studio's stream. + /// changed, the studio's stream and the requests of the messages page. + /// [`App::perform`] runs this before it draws, so that what these change + /// is what is drawn. fn event_rest(&mut self, event: Event) { match event { Event::SettingChanged { key } => self.setting_changed(&key), @@ -318,11 +322,10 @@ impl App { Event::StreamViewers { session, viewers } if self.studio_streams_to(session as i64) => { self.studio_viewers(viewers); } - // The messages page keeps the offline messages (messages.rs). + // The messages page keeps the offline message requests + // (messages.rs); the text of a message is the view model's + // (`social::logic::event`). Event::OfflineMessages { result: Err(e), .. } => debug!("offline messages: {e}"), - Event::OfflineMessage { session, result, .. } => { - self.mail_arrived(session as i64, result); - } Event::RequestDone { session, request, result } => { self.request_done(session as i64, request, result); } diff --git a/crates/voelin-ui/src/messages.rs b/crates/voelin-ui/src/messages.rs index e782732..4f133e7 100644 --- a/crates/voelin-ui/src/messages.rs +++ b/crates/voelin-ui/src/messages.rs @@ -17,7 +17,7 @@ use std::collections::HashMap; use std::sync::Arc; use slint::ComponentHandle; -use voelin_core::{Command, HistoryMessage, OfflineMessage, OfflineMessageInfo, VoiceState}; +use voelin_core::{Command, HistoryMessage, OfflineMessageInfo, VoiceState}; use voelin_model::ChatTarget; use voelin_store::PageQuery; @@ -55,8 +55,6 @@ pub(crate) struct Dms { pub tab: i32, pub filter: String, pub open: Option, - /// The text of the open offline message. - pub mail: Option, /// Offline message requests in flight: session and what for. pub requests: HashMap, pub next_request: u64, @@ -439,7 +437,7 @@ impl App { mail: None, stored: !live, }); - self.social.dm.mail = None; + self.social.mail = None; self.models.social.dm_lines.sync(&[]); let target = ChatTarget::Private(uid.to_owned()); if let (true, Some(id)) = (live, session) { @@ -645,7 +643,6 @@ impl App { mail_subject: mail.as_ref().map(|m| m.subject.clone()).unwrap_or_default().into(), mail_text: self .social - .dm .mail .as_ref() .map(|m| plain(&m.text)) @@ -763,7 +760,7 @@ impl App { mail: Some((session, m.id)), stored: false, }); - self.social.dm.mail = None; + self.social.mail = None; let request = self.offline_request(session, "get"); if !self.demo_ui { self.engine.send(Command::GetOfflineMessage { @@ -775,20 +772,6 @@ impl App { self.refresh_chats(); } - pub(crate) fn mail_arrived(&mut self, session: i64, result: Result) { - match result { - Ok(m) => { - if let Some(view) = self.state.sessions.get_mut(&session) - && let Some(info) = view.extra.inbox.iter_mut().find(|i| i.id == m.id) - { - info.read = true; - } - self.social.dm.mail = Some(m); - } - Err(e) => self.set_status(format!("Could not read the offline message: {e}")), - } - } - pub(crate) fn request_done(&mut self, session: i64, request: u64, result: Result<(), String>) { let Some((_, what)) = self.social.dm.requests.remove(&request) else { return }; match (what, result) { diff --git a/docs/ui.md b/docs/ui.md index 650d7da..2c146d9 100644 --- a/docs/ui.md +++ b/docs/ui.md @@ -159,13 +159,20 @@ this file is `voelin_ui_vm::chat`. The boundary: `Host` (`events/tests.rs`, `social/logic.rs`). `Update::rest` hands back the events whose state is not in the `AppState`: the Stream Studio's stream (`StreamState` and `StreamViewers` while `Host::studio_streams_to`), - `SettingChanged`, the offline messages and their requests (`messages.rs` - keeps those for now), and the rest that no screen looks at. The streams we - share and watch are in the `AppState` (`share`, `watch`; the capture and - the decoder are the front end's, and an update says when to attach or drop - them). The friends, the pokes, the bell's notices and what the friends page - and the search keep are in `Social` (`social::logic`), which a front end - passes to `handle` besides the `AppState`. + `SettingChanged`, the answers to the offline message requests + (`RequestDone`, a list that failed; `messages.rs` keeps those for now), and + the rest that no screen looks at. `perform` handles `rest` before it draws + what `Update::refresh` names, as what the front end keeps may be on screen. + The text of an offline message is not in `rest`: `Event::OfflineMessage` + changes the `Social` state (`mail`, and the inbox row turns read) in + `social::logic::event`, so the redraw shows it (a tape test in + `social/logic.rs`). The streams we share and watch are in the `AppState` + (`share`, `watch`; the capture and the decoder are the front end's, and an + update says when to attach or drop them). The friends, the pokes, the + bell's notices, the text of the open + offline message and what the friends page and the search keep are in + `Social` (`social::logic`), which a front end passes to `handle` besides + the `AppState`. - **Logic and bind.** An area that interleaves decisions with Bridge writes is split into `/logic.rs` in `voelin-ui-vm` (functions over the settings service, the `AppState` and plain rows; they return rows,