Files
VoelinClient/crates/voelin-ui/src/home.rs
T
faumarayandClaude Sonnet 5.5 2d6cbf66af Split the social controller: the friends, the bell, the search and the social events into voelin-ui-vm
social/logic.rs now has the contacts as rows (contact_item, people), the
buttons of a contact and what they ask the window to do next, the bell
(notify, the rows, read, clear, open), the search with what each result
opens, and what an engine event does to the friends, the pokes, the offline
messages and the notices (event). events::handle runs that first, then
events::apply, so a front end has one entry point; Social<D> is the state
those keep, with what the messages page keeps as D for now. Refresh got
people, conversations and dm, the host got dm_shown and now_ms, and
Persist.Settings pages are told apart from the new notices. The Slint side
(social/bind.rs) writes the Bridge, shows the desktop toasts and navigates.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-21 13:41:29 +03:00

357 lines
11 KiB
Rust

//! 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/`, the recent chats in
//! `messages.rs`.
use std::path::{Path, PathBuf};
use slint::ComponentHandle;
use voelin_core::VoiceState;
use crate::app::{App, Bridge, Page};
use crate::vm;
use crate::vm::bridge::{HappeningItem, LiveItem, RecordingItem, ResumeItem, ServerItem};
use crate::vm::home::ResumeAction;
use crate::vm::social::{event_when, plain};
/// What a "What's Happening" entry opens.
#[derive(Clone, Debug, PartialEq)]
pub(crate) enum Happening {
Event(i64, i64),
News(i64),
}
impl App {
/// Streams on our servers: those the server lists (any channel; we can
/// watch them at once), the gateways' directories, and clients flagged
/// streaming.
fn live_rooms(&self) -> Vec<LiveItem> {
let mut rooms = Vec::new();
for (b, backdrop) in
self.state.bookmarks.iter().zip(vm::servers::tints(&self.state.bookmarks))
{
let Some(view) = self.state.sessions.get(&b.id) else { continue };
let channel_of = |cid: Option<u64>| {
cid.and_then(|c| view.presence.channels.get(&c))
.map(|c| vm::tree::channel_title(c).0.to_owned())
.unwrap_or_default()
};
let item = |id: String,
title: String,
client: Option<u16>,
uid: Option<&str>,
name: String| {
let avatar = match (client, uid) {
(Some(c), _) if view.avatar(c).is_some() => vm::avatar::image(view.avatar(c)),
(_, Some(uid)) => self.avatar_of(uid),
_ => vm::ImageRef::default(),
};
LiveItem {
session: b.id as i32,
id: id.into(),
title: title.into(),
initials: vm::avatar::initials(&name).into(),
tint: vm::avatar::tint(&name),
avatar,
streamer: name.into(),
server: b.name.clone().into(),
backdrop,
..Default::default()
}
};
let mut covered: Vec<u16> = Vec::new();
if view.streams_available() {
for s in &view.streams {
let name = view.nickname(s.streamer);
let title =
if s.name.is_empty() { format!("{name}'s stream") } else { s.name.clone() };
let mut room = item(s.id.clone(), title, Some(s.streamer), None, name);
room.channel = channel_of(view.channel_of(s.streamer)).into();
room.viewers = view.stream_viewers.get(&s.id).map_or(0, |v| *v as i32);
room.kind = vm::streams::kind_label(&s.kind).into();
room.watchable = view.state.own_client != Some(s.streamer);
covered.push(s.streamer);
rooms.push(room);
}
}
for e in &view.extra.directory {
if e.client_id.is_some_and(|c| covered.contains(&c)) {
continue;
}
let title = if e.title.is_empty() {
format!("{}'s stream", e.streamer.name)
} else {
e.title.clone()
};
let mut room = item(
e.id.clone(),
title,
e.client_id,
Some(&e.streamer.uid),
e.streamer.name.clone(),
);
room.channel = channel_of(e.channel).into();
room.viewers = e.viewers.map_or(0, |v| v as i32);
room.kind = vm::streams::directory_kind_label(&e.kind).into();
covered.extend(e.client_id);
rooms.push(room);
}
for c in view.presence.clients.values() {
if c.streaming != Some(true)
|| covered.contains(&c.id)
|| Some(c.id) == view.state.own_client
{
continue;
}
let mut room = item(
format!("client/{}", c.id),
format!("{} is live", c.nickname),
Some(c.id),
c.uid.as_deref(),
c.nickname.clone(),
);
room.channel = channel_of(Some(c.channel)).into();
rooms.push(room);
}
}
rooms
}
/// Events, scheduled streams and server news of our servers, what is
/// soonest first.
pub(crate) fn happenings(&self) -> Vec<(HappeningItem, Happening)> {
let now = crate::social::now_ms();
let mut events: Vec<(i64, HappeningItem, Happening)> = Vec::new();
let mut news = Vec::new();
for b in &self.state.bookmarks {
let Some(view) = self.state.sessions.get(&b.id) else { continue };
for e in &view.extra.events {
let ends = e.spec.end_ms.unwrap_or(e.spec.start_ms + 3 * 3_600_000);
if ends < now {
continue;
}
let stream = e.spec.kind == voelin_gateway_proto::EventKind::Stream;
events.push((
e.spec.start_ms,
HappeningItem {
kind: if stream { "stream" } else { "event" }.into(),
title: e.spec.title.clone().into(),
subtitle: if stream {
format!("Scheduled stream · {}", b.name)
} else {
format!("Event · {}", b.name)
}
.into(),
when: event_when(e.spec.start_ms, e.spec.end_ms).into(),
session: b.id as i32,
id: e.id as i32,
live: e.live_stream.is_some(),
},
Happening::Event(b.id, e.id),
));
}
let details = &view.presence.server;
for (title, text) in [
(format!("Welcome to {}", b.name), &details.welcome_message),
(format!("News of {}", b.name), &details.host_message),
] {
let text = plain(text);
if !text.is_empty() {
news.push((
HappeningItem {
kind: "news".into(),
title: title.into(),
subtitle: text.into(),
session: b.id as i32,
..Default::default()
},
Happening::News(b.id),
));
}
}
}
events.sort_by_key(|(start, ..)| *start);
events
.into_iter()
.map(|(_, item, h)| (item, h))
.take(6)
.chain(news.into_iter().take(3))
.collect()
}
/// Live rooms and what is happening (friends: `refresh_people`, chats:
/// `refresh_chats`).
pub(crate) fn refresh_home(&self) {
let m = &self.models.social;
m.live.sync(&self.live_rooms());
let items: Vec<HappeningItem> = self.happenings().into_iter().map(|(i, _)| i).collect();
m.happenings.sync(&items);
}
/// A live room's card: watch it at once in our channel, else go to its
/// server (and the streamer's channel, watching once there).
pub(crate) fn watch_live(&mut self, session: i64, id: &str) {
self.show_server(session);
let Some(view) = self.state.sessions.get(&session) else { return };
if view.streams_available() && view.streams.iter().any(|s| s.id == id) {
self.watch_stream(id.to_owned());
return;
}
let client = view
.extra
.directory
.iter()
.find(|e| e.id == id)
.and_then(|e| e.client_id)
.or_else(|| id.strip_prefix("client/").and_then(|c| c.parse().ok()));
match client {
Some(client) if view.state.voice == VoiceState::Connected => self.watch_client(client),
_ => self.set_status("Connect with voice to watch."),
}
}
pub(crate) fn open_happening(&mut self, index: i32) {
let Some((_, target)) =
usize::try_from(index).ok().and_then(|i| self.happenings().into_iter().nth(i))
else {
return;
};
match target {
Happening::Event(session, _) => {
self.select_server(session);
self.navigate(|nav| nav.invoke_show(Page::Events));
}
Happening::News(session) => self.show_server(session),
}
}
/// Connect a bookmark with voice (the Join buttons).
pub(crate) fn connect_server(&mut self, id: i64) {
self.select_server(id);
self.connect_voice();
}
fn last_resume(&self) -> Option<vm::home::Resume> {
let voice =
|id| self.state.sessions.get(&id).map_or(VoiceState::Disconnected, |v| v.state.voice);
vm::home::resume(self.state.settings.last_voice.as_ref(), &self.state.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);
}
}
/// The folder of recordings and clips.
fn recordings_dir(&self) -> PathBuf {
voelin_core::studio::recording_dir(&self.prefs)
}
/// List the recordings and clips (when the Library opens).
pub(crate) fn load_library(&mut self) {
// Sample data shows sample recordings, not the user's.
if self.demo_ui {
if let Some(ui) = self.ui.upgrade() {
ui.global::<Bridge>().set_recordings_dir("~/Videos/Voelin".into());
}
return;
}
let dir = self.recordings_dir();
let items = recordings_in(&dir);
self.models.social.recordings.sync(&items);
if let Some(ui) = self.ui.upgrade() {
ui.global::<Bridge>().set_recordings_dir(dir.display().to_string().into());
}
}
pub(crate) fn open_recording(&mut self, path: &str) {
if let Err(e) = crate::app::open_path(Path::new(path)) {
self.set_status(format!("Cannot open {path}: {e}"));
}
}
pub(crate) fn open_recordings_folder(&mut self) {
let dir = self.recordings_dir();
if let Err(e) = std::fs::create_dir_all(&dir).and_then(|()| crate::app::open_path(&dir)) {
self.set_status(format!("Cannot open {}: {e}", dir.display()));
}
}
}
/// The videos in `dir`, newest first.
pub(crate) fn recordings_in(dir: &Path) -> Vec<RecordingItem> {
let Ok(entries) = std::fs::read_dir(dir) else { return Vec::new() };
let mut files: Vec<(std::time::SystemTime, RecordingItem)> = entries
.flatten()
.filter_map(|e| {
let path = e.path();
let ext = path.extension()?.to_str()?.to_ascii_lowercase();
if !["webm", "mkv", "mp4", "mov"].contains(&ext.as_str()) {
return None;
}
let meta = e.metadata().ok()?;
let modified = meta.modified().ok()?;
let name = path.file_name()?.to_string_lossy().to_string();
let when = chrono::DateTime::<chrono::Local>::from(modified).format("%-d %b %H:%M");
Some((
modified,
RecordingItem {
clip: name.to_lowercase().contains("clip"),
detail: format!("{} · {when}", vm::chat::size_text(meta.len())).into(),
name: name.into(),
path: path.display().to_string().into(),
},
))
})
.collect();
files.sort_by_key(|(t, _)| std::cmp::Reverse(*t));
files.into_iter().map(|(_, item)| item).collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn library_lists_videos() {
let dir = std::env::temp_dir().join(format!("voelin-library-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("raid.webm"), b"1234").unwrap();
std::fs::write(dir.join("clip-boss.mkv"), b"12").unwrap();
std::fs::write(dir.join("notes.txt"), b"x").unwrap();
let items = recordings_in(&dir);
let mut names: Vec<String> = items.iter().map(|i| i.name.to_string()).collect();
names.sort();
assert_eq!(names, ["clip-boss.mkv", "raid.webm"]);
assert!(items.iter().any(|i| i.clip && i.detail.starts_with("2 B")));
assert!(recordings_in(&dir.join("missing")).is_empty());
std::fs::remove_dir_all(&dir).unwrap();
}
}