| 1 | use common::{ |
| 2 | call::{FollowStats, GetServerStats, PushStats}, |
| 3 | event::Event, |
| 4 | stats::Stats, |
| 5 | }; |
| 6 | |
| 7 | use crate::{ |
| 8 | call::{Cx, Handle, Me}, |
| 9 | error::ServerError, |
| 10 | publish::To, |
| 11 | }; |
| 12 | |
| 13 | impl Handle for FollowStats { |
| 14 | async fn handle(self, _: Me, cx: &mut Cx<'_>) -> Result<Option<Stats>, ServerError> { |
| 15 | let change = cx.sh.services.stats.follow(cx.client.id, self.user_id); |
| 16 | if let Some(user_id) = change.stopped { |
| 17 | cx.emit(To::User(user_id), Event::StatsWanted(false)); |
| 18 | } |
| 19 | if let Some(user_id) = change.started { |
| 20 | cx.emit(To::User(user_id), Event::StatsWanted(true)); |
| 21 | } |
| 22 | |
| 23 | Ok(change.latest) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | impl Handle for PushStats { |
| 28 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<(), ServerError> { |
| 29 | let user_id = me.user()?.id; |
| 30 | for conn in cx.sh.services.stats.push(user_id, self.stats.clone()) { |
| 31 | cx.emit( |
| 32 | To::Conn(conn), |
| 33 | Event::StatsOf { |
| 34 | user_id, |
| 35 | stats: self.stats.clone(), |
| 36 | }, |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | Ok(()) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | impl Handle for GetServerStats { |
| 45 | async fn handle(self, _: Me, cx: &mut Cx<'_>) -> Result<Stats, ServerError> { |
| 46 | let services = &cx.sh.services; |
| 47 | Ok(services.machine.sample(services.hub.bytes())) |
| 48 | } |
| 49 | } |