ui: home continues where you left off, with unread mentions
The banner carousel repeated the servers below it, and the quick actions, the friends and the sidebar art appeared twice. Home now shows the banner only before the first server; after it a card with the last voice channel (`ui.last_voice`, Join or Open) and up to three unread mentions. Friends show once, and the sidebar lists the five newest private chats. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AVRENKftQqGoEeswiR9TDQ
This commit is contained in:
1 parent
a3b61bca70
commit
7965187d2c
20 files changed
+561
-372
No files matched your search
@@ -1022,6 +1022,7 @@ impl App {
|
||||
let view = self.sessions.entry(id).or_default();
|
||||
let was_voice = view.state.voice;
|
||||
let was_observe = view.state.observe;
|
||||
let was_channel = view.state.own_channel;
|
||||
view.state = state.clone();
|
||||
if state.voice != VoiceState::Connected {
|
||||
view.focused_own_channel = false;
|
||||
@@ -1060,6 +1061,12 @@ impl App {
|
||||
self.sessions.entry(id).or_default().focused_own_channel = true;
|
||||
}
|
||||
}
|
||||
// Where we are with voice, when it changed here: home's last
|
||||
// place, which `refresh_servers` shows (voice on another
|
||||
// server does not take it back at each of its events).
|
||||
if (state.voice, state.own_channel) != (was_voice, was_channel) {
|
||||
self.remember_voice(id);
|
||||
}
|
||||
self.refresh_servers();
|
||||
self.refresh_toolbar();
|
||||
self.refresh_tree();
|
||||
@@ -1090,9 +1097,19 @@ impl App {
|
||||
// their pictures.
|
||||
let said = crate::chat::server_texts(&view.presence.server)
|
||||
!= crate::chat::server_texts(&presence.server);
|
||||
// Our channel's path: known only now (a new connection),
|
||||
// or renamed.
|
||||
let path = |v: &SessionView| {
|
||||
v.state.own_channel.map(|c| crate::vm::tree::channel_path(&v.presence, c))
|
||||
};
|
||||
let path_before = path(view);
|
||||
view.presence = presence;
|
||||
let renamed = path(view) != path_before;
|
||||
self.apply_client_playback(session as i64);
|
||||
self.join_after_connect(session as i64);
|
||||
if renamed && self.remember_voice(session as i64) {
|
||||
self.refresh_servers();
|
||||
}
|
||||
if self.current == Some(session as i64) {
|
||||
if said {
|
||||
self.fetch_previews(session as i64);
|
||||
|
||||
@@ -26,6 +26,9 @@ pub(super) fn wire(ui: &MainWindow) {
|
||||
bridge.on_connect_server(|id| {
|
||||
with_app(|app| app.connect_server(i64::from(id)));
|
||||
});
|
||||
bridge.on_resume(|| {
|
||||
with_app(|app| app.resume());
|
||||
});
|
||||
|
||||
// Friends.
|
||||
bridge.on_friends_tab(|tab| {
|
||||
|
||||
@@ -28,6 +28,8 @@
|
||||
//! `channel-password` (the password dialog of the first locked channel,
|
||||
//! the sample's Officers; `channel-password:wrong` as after a refused
|
||||
//! one), `actions` (the last message's actions, as if hovered),
|
||||
//! `first-run` (home's banner as before the first server, over the
|
||||
//! sample data),
|
||||
//! `unread` (the current chat read up to five messages before its end:
|
||||
//! the "New" divider, scrolled up to; `unread:end` ten messages before,
|
||||
//! at the list's end, under the bar that counts the new messages),
|
||||
@@ -77,6 +79,7 @@ use voelin_store::{Bookmark, MessageSource};
|
||||
use crate::app::{
|
||||
App, Bridge, MainWindow, MobileTab, Nav, Page, RecordingItem, SettingsSection, with_app,
|
||||
};
|
||||
use crate::settings::LastVoice;
|
||||
|
||||
/// The switches read at start.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
@@ -294,6 +297,8 @@ pub(crate) fn start(ui: &MainWindow, switches: &Switches) -> Running {
|
||||
}
|
||||
// A screenshot cannot hover.
|
||||
"actions" => nav.set_show_actions(true),
|
||||
// Home as before the first server, over the sample data.
|
||||
"first-run" => nav.set_first_run(true),
|
||||
// `end`: further back, so the divider is above the view.
|
||||
"unread" => {
|
||||
with_app(|app| open_unread(app, if arg == "end" { 10 } else { 5 }));
|
||||
@@ -523,6 +528,13 @@ fn demo_ui(app: &mut App) {
|
||||
bookmark(DEMO + 1, "Pixel Lounge", "pixel.example"),
|
||||
bookmark(DEMO + 2, "Dev TS3", "127.0.0.1:9987"),
|
||||
];
|
||||
// Home's "Continue where you left off": a channel of a server without
|
||||
// voice, so it shows Join.
|
||||
app.settings.last_voice = Some(LastVoice {
|
||||
bookmark: DEMO + 1,
|
||||
address: "pixel.example".into(),
|
||||
channel: vec!["Art Corner".into()],
|
||||
});
|
||||
app.current = Some(DEMO);
|
||||
let session = DEMO as u64;
|
||||
|
||||
@@ -1095,8 +1107,8 @@ fn demo_social(app: &mut App) {
|
||||
},
|
||||
});
|
||||
|
||||
// Notifications: a mention, a message, an event, a friend online (and
|
||||
// the poke above).
|
||||
// Notifications: two mentions (unread: home shows them), a message, an
|
||||
// event, a friend online (and the poke above).
|
||||
use crate::social::{NoticeKind, NoticeTarget};
|
||||
app.notify(
|
||||
NoticeKind::Mention,
|
||||
@@ -1106,6 +1118,14 @@ fn demo_social(app: &mut App) {
|
||||
"Talon".into(),
|
||||
Some("demo-6".into()),
|
||||
);
|
||||
app.notify(
|
||||
NoticeKind::Mention,
|
||||
"Zeph mentioned you".into(),
|
||||
"Nova, are you healing tonight? — #Raid Night · Nightfall Guild".into(),
|
||||
NoticeTarget::Chat(DEMO, ChatTarget::Channel(4)),
|
||||
"Zeph".into(),
|
||||
Some("demo-7".into()),
|
||||
);
|
||||
app.notify(
|
||||
NoticeKind::Event,
|
||||
"Late Night Lofi & Chat starts in 50 min".into(),
|
||||
@@ -1132,7 +1152,7 @@ fn demo_social(app: &mut App) {
|
||||
);
|
||||
for (i, n) in app.social.notices.iter_mut().enumerate() {
|
||||
n.ts_ms = now - (i as i64 * 9 + 2) * min;
|
||||
n.unread = i < 3;
|
||||
n.unread = i < 3 || n.kind == NoticeKind::Mention;
|
||||
}
|
||||
app.refresh_notices();
|
||||
|
||||
|
||||
@@ -1,15 +1,20 @@
|
||||
//! The home page (design mockup 01): live streams on our servers, what is
|
||||
//! happening there (events, scheduled streams, server news), and the
|
||||
//! Library of recordings and clips. Friends are in `social.rs`, the recent
|
||||
//! chats in `messages.rs`.
|
||||
//! The home page (design mockup 01): where we were with voice last, live
|
||||
//! streams on our servers, what is happening there (events, scheduled
|
||||
//! streams, server news), and the Library of recordings and clips.
|
||||
//! Friends and the unread mentions are in `social.rs`, the recent chats in
|
||||
//! `messages.rs`.
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use slint::ComponentHandle;
|
||||
use voelin_core::VoiceState;
|
||||
|
||||
use crate::app::{App, Bridge, HappeningItem, LiveItem, Page, RecordingItem};
|
||||
use crate::app::{
|
||||
App, Bridge, HappeningItem, LiveItem, Page, RecordingItem, ResumeItem, ServerItem,
|
||||
};
|
||||
use crate::settings::LastVoice;
|
||||
use crate::vm;
|
||||
use crate::vm::home::ResumeAction;
|
||||
use crate::vm::social::{event_when, plain};
|
||||
|
||||
/// What a "What's Happening" entry opens.
|
||||
@@ -240,6 +245,62 @@ impl App {
|
||||
self.connect_voice();
|
||||
}
|
||||
|
||||
fn last_resume(&self) -> Option<vm::home::Resume> {
|
||||
let voice = |id| self.sessions.get(&id).map_or(VoiceState::Disconnected, |v| v.state.voice);
|
||||
vm::home::resume(self.settings.last_voice.as_ref(), &self.bookmarks, voice)
|
||||
}
|
||||
|
||||
/// Home's "Continue where you left off", its server as the rail shows
|
||||
/// it (`servers`).
|
||||
pub(crate) fn last_place(&self, servers: &[ServerItem]) -> ResumeItem {
|
||||
let Some(resume) = self.last_resume() else { return ResumeItem::default() };
|
||||
let Some(server) = servers.iter().find(|s| i64::from(s.id) == resume.bookmark) else {
|
||||
return ResumeItem::default();
|
||||
};
|
||||
ResumeItem {
|
||||
shown: true,
|
||||
server_id: server.id,
|
||||
server: server.name.clone(),
|
||||
initials: server.initials.clone(),
|
||||
tint: server.tint,
|
||||
icon: server.icon.clone(),
|
||||
channel: resume.title().into(),
|
||||
open: resume.action == ResumeAction::Open,
|
||||
connecting: server.status == "connecting",
|
||||
}
|
||||
}
|
||||
|
||||
/// The last place's button: its server, connected into the channel
|
||||
/// unless voice is there already.
|
||||
pub(crate) fn resume(&mut self) {
|
||||
let Some(resume) = self.last_resume() else { return };
|
||||
self.show_server(resume.bookmark);
|
||||
if resume.action == ResumeAction::Join {
|
||||
self.connect_voice_to(Some(resume.path.join("/")), None, None);
|
||||
}
|
||||
}
|
||||
|
||||
/// Keep where we are with voice in `session` as the last place
|
||||
/// (`ui.last_voice`); whether it changed. Not for the sample sessions.
|
||||
pub(crate) fn remember_voice(&mut self, session: i64) -> bool {
|
||||
if self.demo_ui {
|
||||
return false;
|
||||
}
|
||||
let Some(view) = self.sessions.get(&session) else { return false };
|
||||
let (VoiceState::Connected, Some(cid)) = (view.state.voice, view.state.own_channel) else {
|
||||
return false;
|
||||
};
|
||||
let channel = vm::tree::channel_path(&view.presence, cid);
|
||||
let Some(b) = self.bookmark(session).filter(|_| !channel.is_empty()) else { return false };
|
||||
let last = LastVoice { bookmark: session, address: b.address.clone(), channel };
|
||||
if self.settings.last_voice.as_ref() == Some(&last) {
|
||||
return false;
|
||||
}
|
||||
self.settings.last_voice = Some(last);
|
||||
self.store_settings();
|
||||
true
|
||||
}
|
||||
|
||||
/// The folder of recordings and clips.
|
||||
fn recordings_dir(&self) -> PathBuf {
|
||||
voelin_core::studio::recording_dir(&self.prefs)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! Direct messages (design mockup 02) and the recent chats of the home
|
||||
//! page: every chat the sessions hold and the newest ones in the history
|
||||
//! store, across servers; the open private chat with who it is with; TS3
|
||||
//! and TS6 offline messages (mail the server keeps).
|
||||
//! page and its sidebar: every chat the sessions hold and the newest ones
|
||||
//! in the history store, across servers; the open private chat with who it
|
||||
//! is with; TS3 and TS6 offline messages (mail the server keeps).
|
||||
//!
|
||||
//! A private chat is a chat tab of its session (`ChatTarget::Private`
|
||||
//! stored under the peer's unique id), so opening one selects its server
|
||||
@@ -237,7 +237,8 @@ impl App {
|
||||
}
|
||||
}
|
||||
|
||||
/// The DM list, the recent chats of home and the unread counts.
|
||||
/// The DM list, the recent chats of home, the private chats of its
|
||||
/// sidebar and the unread counts.
|
||||
fn refresh_conversations(&self) {
|
||||
let Some(ui) = self.ui.upgrade() else { return };
|
||||
let bridge = ui.global::<Bridge>();
|
||||
@@ -251,6 +252,15 @@ impl App {
|
||||
.map(|(i, c)| self.conversation_item(i, c))
|
||||
.collect();
|
||||
vm::list::sync(&self.models.social.recent, &recent);
|
||||
// Shown beside other pages: none is the open one.
|
||||
let sidebar: Vec<ConversationItem> = vm::social::sidebar_dms(&dm.chats, 5)
|
||||
.into_iter()
|
||||
.map(|i| ConversationItem {
|
||||
selected: false,
|
||||
..self.conversation_item(i, &dm.chats[i])
|
||||
})
|
||||
.collect();
|
||||
vm::list::sync(&self.models.social.sidebar_dms, &sidebar);
|
||||
let private = |c: &&ChatRef| matches!(c.target, ChatTarget::Private(_));
|
||||
let items: Vec<ConversationItem> = if dm.tab == 2 {
|
||||
dm.inbox
|
||||
|
||||
@@ -397,6 +397,7 @@ impl App {
|
||||
})
|
||||
.collect();
|
||||
vm::list::sync(&self.models.servers, &items);
|
||||
bridge.set_last_place(self.last_place(&items));
|
||||
bridge.set_current_server(self.current.map_or(-1, |id| id as i32));
|
||||
bridge.set_unread_total(self.view().map_or(0, |v| v.unread()));
|
||||
}
|
||||
@@ -531,20 +532,14 @@ impl App {
|
||||
pub(crate) fn invite_link(&mut self) -> String {
|
||||
let bookmark = self.current.and_then(|id| self.bookmark(id)).map(|b| b.address.clone());
|
||||
let path = self.view().and_then(|v| {
|
||||
let mut names = Vec::new();
|
||||
let own = v.presence.channels.get(&v.state.own_channel?)?;
|
||||
let mut channel = own;
|
||||
loop {
|
||||
names.push(channel.name.clone());
|
||||
match v.presence.channels.get(&channel.parent) {
|
||||
Some(parent) if channel.parent != 0 && names.len() < 64 => channel = parent,
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
names.reverse();
|
||||
let cid = v.state.own_channel?;
|
||||
let own = v.presence.channels.get(&cid)?;
|
||||
// The link names the channels as the server has them; the
|
||||
// toast as the tree shows them.
|
||||
Some((names, vm::tree::channel_title(own).0.to_owned()))
|
||||
Some((
|
||||
vm::tree::channel_path(&v.presence, cid),
|
||||
vm::tree::channel_title(own).0.to_owned(),
|
||||
))
|
||||
});
|
||||
let (Some(address), Some((path, title))) = (bookmark, path) else {
|
||||
self.set_status("Join a voice channel to invite others to it");
|
||||
|
||||
@@ -271,6 +271,9 @@ pub struct UiSettings {
|
||||
/// "Continue without an account" was chosen on the login page: it does
|
||||
/// not show at start any more (signing in clears it).
|
||||
pub skip_account_prompt: bool,
|
||||
/// The voice channel we were in last (Home's "Continue where you left
|
||||
/// off").
|
||||
pub last_voice: Option<LastVoice>,
|
||||
}
|
||||
|
||||
impl Default for UiSettings {
|
||||
@@ -283,10 +286,21 @@ impl Default for UiSettings {
|
||||
share: ShareDefaults::default(),
|
||||
crash_reports: false,
|
||||
skip_account_prompt: false,
|
||||
last_voice: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Where we were with voice last: the bookmark with the address it had
|
||||
/// then (one that now points elsewhere does not resume), and the channel's
|
||||
/// path, its names from the top as the server has them.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct LastVoice {
|
||||
pub bookmark: i64,
|
||||
pub address: String,
|
||||
pub channel: Vec<String>,
|
||||
}
|
||||
|
||||
/// The last choices in the share dialog. Frame rate and bitrate are the
|
||||
/// settings `stream.fps` and `stream.bitrate_kbps`; the indices of the
|
||||
/// closest choices of the old lists ([`LEGACY_FPS_CHOICES`],
|
||||
@@ -526,6 +540,15 @@ mod tests {
|
||||
assert!(!parsed.crash_reports, "crash reports are opt-in");
|
||||
assert_eq!(parsed.ptt_key, "Ctrl+Shift+T");
|
||||
assert_eq!(parsed.share, ShareDefaults::default());
|
||||
assert_eq!(parsed.last_voice, None, "older settings have no last channel");
|
||||
let last = LastVoice {
|
||||
bookmark: 3,
|
||||
address: "ts.example".into(),
|
||||
channel: vec!["Games".into(), "Chess".into()],
|
||||
};
|
||||
let settings = UiSettings { last_voice: Some(last.clone()), ..UiSettings::default() };
|
||||
let json = serde_json::to_string(&settings).unwrap();
|
||||
assert_eq!(serde_json::from_str::<UiSettings>(&json).unwrap().last_voice, Some(last));
|
||||
assert_eq!(serde_json::to_value(ThemeChoice::System).unwrap(), "system");
|
||||
assert_eq!(ThemeChoice::parse("light"), ThemeChoice::Light);
|
||||
assert_eq!(ThemeChoice::parse("??"), ThemeChoice::Dark);
|
||||
|
||||
@@ -169,11 +169,13 @@ pub(crate) struct SocialModels {
|
||||
pub blocked: Rc<VecModel<ContactItem>>,
|
||||
pub live: Rc<VecModel<LiveItem>>,
|
||||
pub recent: Rc<VecModel<ConversationItem>>,
|
||||
pub sidebar_dms: Rc<VecModel<ConversationItem>>,
|
||||
pub conversations: Rc<VecModel<ConversationItem>>,
|
||||
pub dm_lines: Rc<VecModel<ChatLine>>,
|
||||
pub happenings: Rc<VecModel<HappeningItem>>,
|
||||
pub recordings: Rc<VecModel<RecordingItem>>,
|
||||
pub notices: Rc<VecModel<NoticeItem>>,
|
||||
pub mentions: Rc<VecModel<NoticeItem>>,
|
||||
pub search: Rc<VecModel<SearchItem>>,
|
||||
pub events: Rc<VecModel<crate::app::EventItem>>,
|
||||
}
|
||||
@@ -187,11 +189,13 @@ impl SocialModels {
|
||||
blocked: Rc::default(),
|
||||
live: Rc::default(),
|
||||
recent: Rc::default(),
|
||||
sidebar_dms: Rc::default(),
|
||||
conversations: Rc::default(),
|
||||
dm_lines: Rc::default(),
|
||||
happenings: Rc::default(),
|
||||
recordings: Rc::default(),
|
||||
notices: Rc::default(),
|
||||
mentions: Rc::default(),
|
||||
search: Rc::default(),
|
||||
events: Rc::default(),
|
||||
};
|
||||
@@ -201,11 +205,13 @@ impl SocialModels {
|
||||
bridge.set_blocked(ModelRc::from(m.blocked.clone()));
|
||||
bridge.set_live_rooms(ModelRc::from(m.live.clone()));
|
||||
bridge.set_recent_chats(ModelRc::from(m.recent.clone()));
|
||||
bridge.set_sidebar_dms(ModelRc::from(m.sidebar_dms.clone()));
|
||||
bridge.set_conversations(ModelRc::from(m.conversations.clone()));
|
||||
bridge.set_dm_messages(ModelRc::from(m.dm_lines.clone()));
|
||||
bridge.set_happenings(ModelRc::from(m.happenings.clone()));
|
||||
bridge.set_recordings(ModelRc::from(m.recordings.clone()));
|
||||
bridge.set_notifications(ModelRc::from(m.notices.clone()));
|
||||
bridge.set_home_mentions(ModelRc::from(m.mentions.clone()));
|
||||
bridge.set_search_results(ModelRc::from(m.search.clone()));
|
||||
bridge.set_events(ModelRc::from(m.events.clone()));
|
||||
m
|
||||
@@ -630,26 +636,31 @@ impl App {
|
||||
self.refresh_notices();
|
||||
}
|
||||
|
||||
/// The bell's notices, and home's unread mentions (the newest three).
|
||||
pub(crate) fn refresh_notices(&self) {
|
||||
let Some(ui) = self.ui.upgrade() else { return };
|
||||
let items: Vec<NoticeItem> = self
|
||||
.social
|
||||
.notices
|
||||
.iter()
|
||||
.map(|n| NoticeItem {
|
||||
key: n.key,
|
||||
kind: n.kind.name().into(),
|
||||
title: n.title.clone().into(),
|
||||
body: n.body.clone().into(),
|
||||
time: ago(n.ts_ms).into(),
|
||||
unread: n.unread,
|
||||
initials: vm::avatar::initials(&n.name).into(),
|
||||
tint: vm::avatar::tint(&n.name),
|
||||
avatar: n.uid.as_deref().map(|u| self.avatar_of(u)).unwrap_or_default(),
|
||||
})
|
||||
.collect();
|
||||
let item = |n: &Notice| NoticeItem {
|
||||
key: n.key,
|
||||
kind: n.kind.name().into(),
|
||||
title: n.title.clone().into(),
|
||||
body: n.body.clone().into(),
|
||||
time: ago(n.ts_ms).into(),
|
||||
unread: n.unread,
|
||||
initials: vm::avatar::initials(&n.name).into(),
|
||||
tint: vm::avatar::tint(&n.name),
|
||||
avatar: n.uid.as_deref().map(|u| self.avatar_of(u)).unwrap_or_default(),
|
||||
};
|
||||
let notices = &self.social.notices;
|
||||
let items: Vec<NoticeItem> = notices.iter().map(item).collect();
|
||||
vm::list::sync(&self.models.social.notices, &items);
|
||||
let unread = self.social.notices.iter().filter(|n| n.unread).count();
|
||||
let mentions: Vec<NoticeItem> = notices
|
||||
.iter()
|
||||
.filter(|n| n.unread && n.kind == NoticeKind::Mention)
|
||||
.take(3)
|
||||
.map(item)
|
||||
.collect();
|
||||
vm::list::sync(&self.models.social.mentions, &mentions);
|
||||
let unread = notices.iter().filter(|n| n.unread).count();
|
||||
ui.global::<Bridge>().set_notifications_unread(unread as i32);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
//! Home's "Continue where you left off": the voice channel we were in last,
|
||||
//! if it can still be reached.
|
||||
|
||||
use voelin_core::VoiceState;
|
||||
use voelin_store::Bookmark;
|
||||
|
||||
use crate::settings::LastVoice;
|
||||
|
||||
/// What the card's button does.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum ResumeAction {
|
||||
/// Voice is connected there: show the server.
|
||||
Open,
|
||||
/// Connect into the channel.
|
||||
Join,
|
||||
}
|
||||
|
||||
/// The card: the server, the channel and the button.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Resume {
|
||||
pub bookmark: i64,
|
||||
/// The channel's path, its names from the top as the server has them.
|
||||
pub path: Vec<String>,
|
||||
pub action: ResumeAction,
|
||||
}
|
||||
|
||||
impl Resume {
|
||||
/// The channel's name as the tree shows it (a spacer's text).
|
||||
pub fn title(&self) -> &str {
|
||||
match self.path.as_slice() {
|
||||
[top] => voelin_model::parse_spacer(top).map_or(top.as_str(), |(_, text)| text),
|
||||
[.., last] => last,
|
||||
[] => "",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Where to continue: the last voice channel, unless its bookmark is gone
|
||||
/// or points at another address now. Open while `voice` says a bookmark is
|
||||
/// connected, else Join.
|
||||
pub fn resume(
|
||||
last: Option<&LastVoice>,
|
||||
bookmarks: &[Bookmark],
|
||||
voice: impl Fn(i64) -> VoiceState,
|
||||
) -> Option<Resume> {
|
||||
let last = last.filter(|l| !l.channel.is_empty())?;
|
||||
bookmarks.iter().find(|b| b.id == last.bookmark && b.address == last.address)?;
|
||||
let action = match voice(last.bookmark) {
|
||||
VoiceState::Connected => ResumeAction::Open,
|
||||
_ => ResumeAction::Join,
|
||||
};
|
||||
Some(Resume { bookmark: last.bookmark, path: last.channel.clone(), action })
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn bookmark(id: i64, address: &str) -> Bookmark {
|
||||
Bookmark { id, address: address.into(), ..Default::default() }
|
||||
}
|
||||
|
||||
fn last(bookmark: i64, address: &str, channel: &[&str]) -> LastVoice {
|
||||
LastVoice {
|
||||
bookmark,
|
||||
address: address.into(),
|
||||
channel: channel.iter().map(|n| n.to_string()).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resume_cases() {
|
||||
let bookmarks = [bookmark(1, "ts.example"), bookmark(2, "pixel.example:9988")];
|
||||
let offline = |_| VoiceState::Disconnected;
|
||||
assert_eq!(resume(None, &bookmarks, offline), None, "nothing stored");
|
||||
assert_eq!(
|
||||
resume(Some(&last(3, "gone.example", &["Lobby"])), &bookmarks, offline),
|
||||
None,
|
||||
"the bookmark was deleted"
|
||||
);
|
||||
assert_eq!(
|
||||
resume(Some(&last(1, "old.example", &["Lobby"])), &bookmarks, offline),
|
||||
None,
|
||||
"the bookmark has another address now"
|
||||
);
|
||||
assert_eq!(resume(Some(&last(1, "ts.example", &[])), &bookmarks, offline), None);
|
||||
|
||||
let place = last(2, "pixel.example:9988", &["Games", "Chess"]);
|
||||
let joined = resume(Some(&place), &bookmarks, offline).unwrap();
|
||||
assert_eq!(joined.bookmark, 2);
|
||||
assert_eq!(joined.path, ["Games", "Chess"]);
|
||||
assert_eq!(joined.action, ResumeAction::Join);
|
||||
assert_eq!(joined.title(), "Chess");
|
||||
let connecting = resume(Some(&place), &bookmarks, |_| VoiceState::Connecting);
|
||||
assert_eq!(connecting.unwrap().action, ResumeAction::Join);
|
||||
// Connected elsewhere is not connected there.
|
||||
let connected = |id| if id == 2 { VoiceState::Connected } else { VoiceState::Disconnected };
|
||||
assert_eq!(resume(Some(&place), &bookmarks, connected).unwrap().action, ResumeAction::Open);
|
||||
let elsewhere = |id| if id == 1 { VoiceState::Connected } else { VoiceState::Disconnected };
|
||||
assert_eq!(resume(Some(&place), &bookmarks, elsewhere).unwrap().action, ResumeAction::Join);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resume_titles() {
|
||||
let title = |path: &[&str]| {
|
||||
let path = path.iter().map(|n| n.to_string()).collect();
|
||||
Resume { bookmark: 1, path, action: ResumeAction::Join }.title().to_owned()
|
||||
};
|
||||
assert_eq!(title(&["Lobby"]), "Lobby");
|
||||
assert_eq!(title(&["[cspacer0]Gaming"]), "Gaming", "a spacer by its text");
|
||||
// Only channels at the top are spacers.
|
||||
assert_eq!(title(&["Games", "[cspacer]Chess"]), "[cspacer]Chess");
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
pub mod avatar;
|
||||
pub mod bbcode;
|
||||
pub mod chat;
|
||||
pub mod home;
|
||||
pub mod list;
|
||||
pub mod servers;
|
||||
pub mod social;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! Texts of the home, friends, messages, bell and events screens: times
|
||||
//! ("12m ago", "Yesterday"), message previews without BBCode (read by
|
||||
//! `vm::bbcode`), mentions, event dates, a poke's message; pure, so they
|
||||
//! are tested here.
|
||||
//! `vm::bbcode`), mentions, event dates, a poke's message, the private
|
||||
//! chats of the home sidebar; pure, so they are tested here.
|
||||
|
||||
use chrono::{DateTime, Local, NaiveDateTime, TimeZone};
|
||||
|
||||
@@ -114,6 +114,17 @@ pub fn poke_message(text: &str) -> String {
|
||||
cut.trim_end().to_owned()
|
||||
}
|
||||
|
||||
/// The private chats of the home sidebar, by their index in `chats`
|
||||
/// (newest first): at most `n`, those with unread messages first.
|
||||
pub fn sidebar_dms(chats: &[crate::messages::ChatRef], n: usize) -> Vec<usize> {
|
||||
let mut private: Vec<usize> = (0..chats.len())
|
||||
.filter(|&i| matches!(chats[i].target, voelin_model::ChatTarget::Private(_)))
|
||||
.collect();
|
||||
private.sort_by_key(|&i| chats[i].unread == 0);
|
||||
private.truncate(n);
|
||||
private
|
||||
}
|
||||
|
||||
/// The channels the search (Ctrl+K) finds for `query`, with their titles:
|
||||
/// a spacer by its text, never a line or an empty spacer.
|
||||
pub fn found_channels<'a>(
|
||||
@@ -273,6 +284,33 @@ mod tests {
|
||||
assert_eq!(found_channels(&channels, "").len(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sidebar_chats() {
|
||||
use voelin_model::ChatTarget;
|
||||
let chat = |target: ChatTarget, unread| crate::messages::ChatRef {
|
||||
session: Some(1),
|
||||
server_uid: None,
|
||||
target,
|
||||
last: None,
|
||||
unread,
|
||||
};
|
||||
let dm = |uid: &str, unread| chat(ChatTarget::Private(uid.into()), unread);
|
||||
let chats = [
|
||||
dm("a", 0),
|
||||
chat(ChatTarget::Server, 4),
|
||||
dm("b", 2),
|
||||
chat(ChatTarget::Channel(3), 1),
|
||||
dm("c", 0),
|
||||
dm("d", 1),
|
||||
dm("e", 0),
|
||||
dm("f", 0),
|
||||
];
|
||||
// Private chats only, unread first, otherwise newest first; capped.
|
||||
assert_eq!(sidebar_dms(&chats, 5), [2, 5, 0, 4, 6]);
|
||||
assert_eq!(sidebar_dms(&chats, 1), [2]);
|
||||
assert!(sidebar_dms(&chats[1..2], 5).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn event_dates() {
|
||||
let start = parse_local("2026-04-25 19:00").unwrap();
|
||||
|
||||
@@ -162,6 +162,24 @@ pub fn listed_title(channel: &ChannelInfo) -> Option<&str> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Channels deeper than this are not followed up (a broken presence could
|
||||
/// loop).
|
||||
const PATH_DEPTH: usize = 64;
|
||||
|
||||
/// The path of a channel, its names from the top as the server has them
|
||||
/// (what an invite link names, what connecting into it takes); empty for a
|
||||
/// channel the presence does not know.
|
||||
pub fn channel_path(presence: &Presence, channel: ChannelId) -> Vec<String> {
|
||||
let mut names = Vec::new();
|
||||
let mut next = presence.channels.get(&channel);
|
||||
while let Some(c) = next.filter(|_| names.len() < PATH_DEPTH) {
|
||||
names.push(c.name.clone());
|
||||
next = presence.channels.get(&c.parent).filter(|_| c.parent != 0);
|
||||
}
|
||||
names.reverse();
|
||||
names
|
||||
}
|
||||
|
||||
/// About as many characters as a fill spacer's row holds: more than the
|
||||
/// widest tree shows.
|
||||
const FILL_CHARS: usize = 200;
|
||||
@@ -827,4 +845,15 @@ mod tests {
|
||||
assert_eq!(found.len(), 1);
|
||||
assert!(found[0].first_in_group && found[0].group == "Guest" && found[0].group_count == 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn channel_paths() {
|
||||
let mut p = presence();
|
||||
assert_eq!(channel_path(&p, 1), ["Lobby"], "a channel at the top");
|
||||
assert_eq!(channel_path(&p, 3), ["Games", "Chess"], "the names from the top");
|
||||
assert!(channel_path(&p, 99).is_empty(), "an unknown channel");
|
||||
// Two channels each other's parent: the path stops.
|
||||
p.channels.get_mut(&2).unwrap().parent = 3;
|
||||
assert_eq!(channel_path(&p, 3).len(), PATH_DEPTH);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user