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 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Sonnet 5.5 committed 2026-10-24 19:33:29 +03:00
1 parent 81def06144
commit 91d70702f3
6 files changed
+138 -43

No files matched your search

+11 -5
View File
@@ -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<L: Lines>(state: &mut AppState<L>, 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),
}
}
+2 -1
View File
@@ -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<Event>,
}
+97 -2
View File
@@ -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<D> {
pub selected: Option<String>,
/// What the search results open, by index.
pub found: Vec<Found>,
/// The text of the offline message that is open on the messages page,
/// once the server has sent it ([`Event::OfflineMessage`]).
pub mail: Option<OfflineMessage>,
/// The direct messages.
pub dm: D,
}
@@ -715,9 +718,26 @@ pub fn event<L: Lines, D>(
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::<Vec<_>>();
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,
+11 -8
View File
@@ -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);
}
+3 -20
View File
@@ -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<Open>,
/// The text of the open offline message.
pub mail: Option<OfflineMessage>,
/// Offline message requests in flight: session and what for.
pub requests: HashMap<u64, (i64, &'static str)>,
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<OfflineMessage, String>) {
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) {