Skip to content

Commit

Permalink
adds list-devices to presage-cli (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
rlking committed Jul 16, 2024
1 parent 8f45330 commit 093c3bf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
28 changes: 28 additions & 0 deletions presage-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ enum Cmd {
#[clap(long, help = "Force to register again if already registered")]
force: bool,
},
#[clap(about = "Create QR code and wait until new device is linked")]
LinkDevice {
/// Possible values: staging, production
#[clap(long, short = 's', default_value = "production")]
Expand All @@ -89,6 +90,8 @@ enum Cmd {
)]
device_name: String,
},
#[clap(about = "List all linked devices")]
ListDevices,
#[clap(about = "Get information on the registered user")]
Whoami,
#[clap(about = "Retrieve the user profile")]
Expand Down Expand Up @@ -533,6 +536,31 @@ async fn run<S: Store>(subcommand: Cmd, config_store: S) -> anyhow::Result<()> {
}
}
}
Cmd::ListDevices => {
let manager = Manager::load_registered(config_store).await?;
let devices = manager.devices().await?;
let current_device_id = manager.device_id() as i64;

for device in devices {
let device_name = device
.name
.unwrap_or_else(|| "(no device name)".to_string());
let current_marker = if device.id == current_device_id {
"(this device)"
} else {
""
};

println!(
"- Device {} {}\n Name: {}\n Created: {}\n Last seen: {}",
device.id,
current_marker,
device_name,
device.created.to_string(),
device.last_seen.to_string()
);
}
}
Cmd::Receive { notifications } => {
let mut manager = Manager::load_registered(config_store).await?;
receive(&mut manager, notifications).await?;
Expand Down
11 changes: 5 additions & 6 deletions presage/src/manager/registered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,10 @@ impl<S: Store> Manager<S, Registered> {
Ok(self.identified_push_service().whoami().await?)
}

pub fn device_id(&self) -> u32 {
return self.state.device_id();
}

/// Fetches the profile (name, about, status emoji) of the registered user.
pub async fn retrieve_profile(&mut self) -> Result<Profile, Error<S::Error>> {
self.retrieve_profile_by_uuid(self.state.data.service_ids.aci, self.state.data.profile_key)
Expand Down Expand Up @@ -1269,13 +1273,8 @@ impl<S: Store> Manager<S, Registered> {
Ok(())
}

/// As a primary device, list all the devices (uncluding the current device).
/// As a primary device, list all the devices (including the current device).
pub async fn devices(&self) -> Result<Vec<DeviceInfo>, Error<S::Error>> {
// XXX: What happens if secondary device? Possible to use static typing to make this method call impossible in that case?
if self.registration_type() != RegistrationType::Primary {
return Err(Error::<S::Error>::NotPrimaryDevice);
}

let aci_protocol_store = self.store.aci_protocol_store();
let mut account_manager = AccountManager::new(
self.identified_push_service(),
Expand Down

0 comments on commit 093c3bf

Please sign in to comment.