| 1 | use std::time::Duration; |
| 2 | |
| 3 | use chatsvc::{ActivityReporting, PresenceService, Realtime, RealtimeConfig, RealtimeEvent, StatusKind}; |
| 4 | use session::{DEFAULT_ENDPOINT, Session}; |
| 5 | |
| 6 | const CONNECT_TIMEOUT: Duration = Duration::from_secs(60); |
| 7 | const SETTLE: Duration = Duration::from_secs(10); |
| 8 | |
| 9 | #[tokio::main] |
| 10 | async fn main() { |
| 11 | let user_id = std::env::args().nth(1).expect("usage: presence_endpoint_probe <own user id GUID> [inactive wait seconds]"); |
| 12 | let inactive_wait = std::env::args() |
| 13 | .nth(2) |
| 14 | .and_then(|value| value.parse().ok()) |
| 15 | .map_or(SETTLE, Duration::from_secs); |
| 16 | let endpoint = std::env::var("CDP_ENDPOINT").unwrap_or_else(|_| DEFAULT_ENDPOINT.to_owned()); |
| 17 | let session = Session::connect(&endpoint).await.expect("browser"); |
| 18 | let presence = PresenceService::new(&session); |
| 19 | |
| 20 | let config = RealtimeConfig { |
| 21 | initially_active: true, |
| 22 | ..RealtimeConfig::default() |
| 23 | }; |
| 24 | let mut realtime = Realtime::start_with(&session, config).await.expect("realtime start"); |
| 25 | let endpoint_id = wait_for_endpoint(&mut realtime).await; |
| 26 | println!("# trouter connected"); |
| 27 | |
| 28 | println!("register: {}", outcome(presence.register_endpoint(&endpoint_id, ActivityReporting::Transport).await)); |
| 29 | realtime.resend_activity(); |
| 30 | println!("# sent active"); |
| 31 | tokio::time::sleep(SETTLE).await; |
| 32 | print_own_presence(&presence, &user_id).await; |
| 33 | |
| 34 | realtime.set_activity(false); |
| 35 | println!("# sent inactive"); |
| 36 | tokio::time::sleep(inactive_wait).await; |
| 37 | print_own_presence(&presence, &user_id).await; |
| 38 | |
| 39 | println!("http report inactive: {}", outcome(presence.report_activity(&endpoint_id, false).await)); |
| 40 | println!("unregister: {}", outcome(presence.unregister_endpoint(&endpoint_id).await)); |
| 41 | realtime.stop().await; |
| 42 | println!("# stopped"); |
| 43 | } |
| 44 | |
| 45 | async fn wait_for_endpoint(realtime: &mut Realtime) -> String { |
| 46 | let wait = async { |
| 47 | let mut endpoint_id = None; |
| 48 | let mut connected = false; |
| 49 | while let Some(event) = realtime.recv().await { |
| 50 | match event { |
| 51 | RealtimeEvent::Endpoint(endpoint) => endpoint_id = Some(endpoint.endpoint_id), |
| 52 | RealtimeEvent::Status(status) if status.kind == StatusKind::Connected => connected = true, |
| 53 | _ => {} |
| 54 | } |
| 55 | if connected && let Some(endpoint_id) = endpoint_id.take() { |
| 56 | return endpoint_id; |
| 57 | } |
| 58 | } |
| 59 | panic!("realtime closed before connecting"); |
| 60 | }; |
| 61 | tokio::time::timeout(CONNECT_TIMEOUT, wait).await.expect("trouter did not connect") |
| 62 | } |
| 63 | |
| 64 | async fn print_own_presence(presence: &PresenceService, user_id: &str) { |
| 65 | match presence.status(user_id).await { |
| 66 | Ok(status) => println!( |
| 67 | "own presence: availability={} activity={}", |
| 68 | status.availability, |
| 69 | status.activity.as_deref().unwrap_or("-") |
| 70 | ), |
| 71 | Err(error) => println!("own presence: failed {}", status_of(&error)), |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | fn outcome(result: chatsvc::Result<()>) -> String { |
| 76 | match result { |
| 77 | Ok(()) => "ok".to_owned(), |
| 78 | Err(error) => format!("failed {}", status_of(&error)), |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | fn status_of(error: &chatsvc::Error) -> String { |
| 83 | match error { |
| 84 | chatsvc::Error::Session(session::Error::Api { status, .. }) => format!("http {status}"), |
| 85 | _ => "no http status".to_owned(), |
| 86 | } |
| 87 | } |