use rmux_proto::{PaneTargetRef, WebShareScope};
use crate::handles::Rmux;
use crate::transport::TransportClient;
use crate::Result;
use super::{
list_web_shares, lookup_summary, stop_all_web_shares, stop_web_share, token_from_url,
web_config, WebConfigInfo, WebShareSummary,
};
#[derive(Clone)]
pub struct WebShareHandle {
transport: TransportClient,
id: String,
scope: WebShareScope,
spectator_url: Option<String>,
operator_url: Option<String>,
expires_at_unix: Option<u64>,
operator_pairing_code: Option<String>,
spectator_pairing_code: Option<String>,
max_operators: Option<u16>,
max_spectators: Option<u16>,
operator: bool,
spectator: bool,
kill_session_on_expire: bool,
}
impl WebShareHandle {
pub(crate) fn new(
transport: TransportClient,
created: rmux_proto::WebShareCreatedResponse,
) -> Self {
Self {
transport,
id: created.share_id,
scope: created.scope,
spectator_url: created.spectator_url,
operator_url: created.operator_url,
expires_at_unix: created.expires_at_unix,
operator_pairing_code: created.operator_pairing_code,
spectator_pairing_code: created.spectator_pairing_code,
max_operators: created.max_operators,
max_spectators: created.max_spectators,
operator: created.operator,
spectator: created.spectator,
kill_session_on_expire: created.kill_session_on_expire,
}
}
#[must_use]
pub fn id(&self) -> &str {
&self.id
}
#[must_use]
pub const fn scope(&self) -> &WebShareScope {
&self.scope
}
#[must_use]
pub fn pane_target(&self) -> Option<&PaneTargetRef> {
match &self.scope {
WebShareScope::Pane(target) => Some(target),
WebShareScope::Session(_) => None,
}
}
#[must_use]
pub const fn operator(&self) -> bool {
self.operator
}
#[must_use]
pub const fn spectator(&self) -> bool {
self.spectator
}
#[must_use]
pub const fn kill_session_on_expire(&self) -> bool {
self.kill_session_on_expire
}
#[must_use]
pub fn spectator_url(&self) -> Option<&str> {
self.spectator_url.as_deref()
}
#[must_use]
pub fn spectator_token(&self) -> Option<&str> {
self.spectator_url.as_deref().and_then(token_from_url)
}
#[must_use]
pub fn operator_url(&self) -> Option<&str> {
self.operator_url.as_deref()
}
#[must_use]
pub fn operator_token(&self) -> Option<&str> {
self.operator_url.as_deref().and_then(token_from_url)
}
#[must_use]
pub fn operator_pairing_code(&self) -> Option<&str> {
self.operator_pairing_code.as_deref()
}
#[must_use]
pub fn spectator_pairing_code(&self) -> Option<&str> {
self.spectator_pairing_code.as_deref()
}
#[must_use]
pub const fn max_spectators(&self) -> Option<u16> {
self.max_spectators
}
#[must_use]
pub const fn max_operators(&self) -> Option<u16> {
self.max_operators
}
#[must_use]
pub const fn expires_at_unix(&self) -> Option<u64> {
self.expires_at_unix
}
pub async fn summary(&self) -> Result<WebShareSummary> {
lookup_summary(&self.transport, &self.id).await
}
pub async fn spectators_active(&self) -> Result<u16> {
Ok(self.summary().await?.active_spectators)
}
pub async fn operators_active(&self) -> Result<u16> {
Ok(self.summary().await?.active_operators)
}
pub async fn stop(self) -> Result<()> {
stop_web_share(&self.transport, &self.id).await.map(|_| ())
}
}
#[derive(Clone)]
pub struct WebShareLookup {
transport: TransportClient,
summary: WebShareSummary,
}
impl WebShareLookup {
pub(crate) fn new(transport: TransportClient, summary: WebShareSummary) -> Self {
Self { transport, summary }
}
#[must_use]
pub fn id(&self) -> &str {
&self.summary.id
}
#[must_use]
pub const fn scope(&self) -> &WebShareScope {
&self.summary.scope
}
#[must_use]
pub fn pane_target(&self) -> Option<&PaneTargetRef> {
self.summary.pane_target()
}
#[must_use]
pub const fn operator(&self) -> bool {
self.summary.operator
}
#[must_use]
pub const fn spectator(&self) -> bool {
self.summary.spectator
}
#[must_use]
pub fn spectator_url_redacted(&self) -> Option<&str> {
self.summary.spectator_url_redacted.as_deref()
}
#[must_use]
pub const fn cached_summary(&self) -> &WebShareSummary {
&self.summary
}
pub async fn summary(&self) -> Result<WebShareSummary> {
lookup_summary(&self.transport, &self.summary.id).await
}
pub async fn stop(self) -> Result<()> {
stop_web_share(&self.transport, &self.summary.id)
.await
.map(|_| ())
}
}
impl Rmux {
pub async fn list_web_shares(&self) -> Result<Vec<WebShareSummary>> {
let transport = self
.connect_transport_for_operation(self.resolved_timeout(None))
.await?;
list_web_shares(&transport).await
}
pub async fn stop_web_share(&self, id: &str) -> Result<bool> {
let transport = self
.connect_transport_for_operation(self.resolved_timeout(None))
.await?;
stop_web_share(&transport, id).await
}
pub async fn stop_all_web_shares(&self) -> Result<usize> {
let transport = self
.connect_transport_for_operation(self.resolved_timeout(None))
.await?;
stop_all_web_shares(&transport).await
}
pub async fn web_share_by_id(&self, id: &str) -> Result<WebShareLookup> {
let transport = self
.connect_transport_for_operation(self.resolved_timeout(None))
.await?;
let summary = lookup_summary(&transport, id).await?;
Ok(WebShareLookup::new(transport, summary))
}
pub async fn web_config(&self) -> Result<WebConfigInfo> {
let transport = self
.connect_transport_for_operation(self.resolved_timeout(None))
.await?;
web_config(&transport).await
}
}