use std::sync::Arc;
use anyhow::Result;
use futures::future::BoxFuture;
use tracing::debug;
use super::{Channel, OutboundMessage};
use crate::ws::{ConnRegistry, types::EventFrame};
pub struct DesktopChannel {
conns: Arc<ConnRegistry>,
}
impl DesktopChannel {
pub fn new(conns: Arc<ConnRegistry>) -> Self {
Self { conns }
}
}
impl Channel for DesktopChannel {
fn name(&self) -> &str {
"desktop"
}
fn send(&self, msg: OutboundMessage) -> BoxFuture<'_, Result<()>> {
Box::pin(async move {
let payload = serde_json::json!({
"type": "notification",
"channel": "desktop",
"to": msg.target_id,
"text": msg.text,
});
let frame = EventFrame::new("notification", payload, 0);
debug!(target_id = %msg.target_id, text_len = msg.text.len(), "desktop: broadcasting notification");
self.conns.broadcast_all(frame).await;
Ok(())
})
}
fn run(self: Arc<Self>) -> BoxFuture<'static, Result<()>> {
Box::pin(async { Ok(()) })
}
}