Files
VoelinClient/crates/voelin-core/src/query.rs
T
Claude d28ecebe43 voelin-core: files, avatars, icons, pokes, offline messages, contacts
The new UI needs a channel file browser, avatars and icons, pokes,
direct messages to anyone, the offline message inbox and a friends list.
The voice connection can do all of it; the engine did not offer it.

Files (module files): ListFiles, DownloadFile (to disk as <path>.part,
renamed when complete, resumable, or into memory), DownloadChatFile for
a FileRef from a chat message, UploadFile, CancelTransfer, DeleteFiles,
RenameFile, CreateDirectory. Transfers stream in 64 KiB pieces and
report Requested/Started/Progress (files.progress_interval_ms)/Done/
Failed/Cancelled; requests and transfers carry an id the caller picks.

Avatars and icons are fetched by themselves (cache.fetch_images) into a
content-addressed disk cache (module cache: avatars by MD5, icons by id,
LRU bounded by cache.max_mb, 0 = no limit, use times kept as file times,
concurrent downloads of one file deduplicated across sessions) and
reported as AvatarReady / IconReady. SetAvatar uploads ours and
announces its hash, or removes it.

Pokes are a command and a distinct event (they were stored as private
messages before). A failing voice command (e.g. a private message to a
client who left) is now an error event instead of ending the connection.
Offline messages (module offline) list, read, send, delete and mark.

Contacts (module contacts) are engine-wide and stored in the client
database: SetContact / RemoveContact, ContactsChanged, FriendPresence
across all sessions, last seen time and server, per-person volume and
mute applied in every session, blocked authors flagged in chat and
their pokes and private messages hidden or flagged (privacy.block_mode),
and stream.permissions = friends lets friends watch without asking.

ServerDetails and Groups events report the server's details and groups
when they change. The book mapping moved to its own module.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NJvYyRmTjKcb9i8eyYXK5G
2026-09-28 19:50:39 +00:00

91 lines
2.8 KiB
Rust

//! The query source: own ServerQuery credentials, in-process observer and relays.
use tokio::sync::{broadcast, mpsc};
use voelin_model::{ChatMessage, ChatTarget, Presence};
use voelin_observer::{
Observer, ObserverConfig, ObserverEvent, RelayConfig, RelayEvent, RelayPool,
};
pub(crate) enum QueryCmd {
OpenChat(ChatTarget),
CloseChat(ChatTarget),
SendChat { target: ChatTarget, nick: String, text: String },
Stop,
}
pub(crate) enum QueryEvent {
Presence(Box<Presence>),
Chat(ChatMessage),
Error(String),
Disconnected,
}
pub(crate) async fn run(
connect: voelin_query::Connect,
mut commands: mpsc::UnboundedReceiver<QueryCmd>,
events: mpsc::UnboundedSender<QueryEvent>,
) {
let observer = Observer::spawn(ObserverConfig::new(connect.clone()));
let relays = RelayPool::new(RelayConfig::new(connect));
let mut observed = observer.subscribe();
let mut relayed = relays.subscribe();
let mut open = std::collections::HashSet::new();
loop {
tokio::select! {
e = observed.recv() => match e {
Ok(ObserverEvent::Snapshot(_)) | Ok(ObserverEvent::Delta(_)) | Err(broadcast::error::RecvError::Lagged(_)) => {
let presence = observer.presence().read().unwrap().clone();
let _ = events.send(QueryEvent::Presence(Box::new(presence)));
}
Ok(ObserverEvent::Chat(msg)) => {
if open.contains(&msg.target) {
let _ = events.send(QueryEvent::Chat(msg));
}
}
Ok(ObserverEvent::Disconnected(reason)) => {
let _ = events.send(QueryEvent::Error(format!("query connection lost: {reason}")));
}
Err(broadcast::error::RecvError::Closed) => break,
},
e = relayed.recv() => match e {
Ok(RelayEvent::Message(msg)) => {
let _ = events.send(QueryEvent::Chat(msg));
}
Ok(RelayEvent::Closed { channel, reason }) => {
let _ = events.send(QueryEvent::Error(format!("relay for channel {channel} closed: {reason}")));
}
Err(_) => {}
},
cmd = commands.recv() => match cmd {
None | Some(QueryCmd::Stop) => break,
Some(QueryCmd::OpenChat(target)) => {
if let ChatTarget::Channel(cid) = target
&& let Err(e) = relays.open(cid).await
{
let _ = events.send(QueryEvent::Error(e.to_string()));
continue;
}
open.insert(target);
}
Some(QueryCmd::CloseChat(target)) => {
if let ChatTarget::Channel(cid) = target {
relays.close(cid).await;
}
open.remove(&target);
}
Some(QueryCmd::SendChat { target, nick, text }) => {
let result = match &target {
ChatTarget::Channel(cid) => relays.send(*cid, &nick, &text).await,
_ => Err(voelin_query::Error::Unsupported("only channel chat is relayed with own credentials")),
};
if let Err(e) = result {
let _ = events.send(QueryEvent::Error(e.to_string()));
}
}
},
}
}
relays.close_all().await;
let _ = events.send(QueryEvent::Disconnected);
}