use common::{ call::{FollowStats, GetServerStats, PushStats}, event::Event, model::User, stats::Stats, }; use crate::{ call::{Cx, Handle}, error::ServerError, publish::To, }; impl Handle for FollowStats { async fn handle(self, _: User, cx: &mut Cx<'_>) -> Result, ServerError> { let change = cx.sh.services.stats.follow(cx.client.id, self.user_id); if let Some(user_id) = change.stopped { cx.emit(To::User(user_id), Event::StatsWanted(false)); } if let Some(user_id) = change.started { cx.emit(To::User(user_id), Event::StatsWanted(true)); } Ok(change.latest) } } impl Handle for PushStats { async fn handle(self, me: User, cx: &mut Cx<'_>) -> Result<(), ServerError> { let user_id = me.id; for conn in cx.sh.services.stats.push(user_id, self.stats.clone()) { cx.emit( To::Conn(conn), Event::StatsOf { user_id, stats: self.stats.clone(), }, ); } Ok(()) } } impl Handle for GetServerStats { async fn handle(self, _: User, cx: &mut Cx<'_>) -> Result { let services = &cx.sh.services; Ok(services.machine.sample(services.hub.bytes())) } }