implement Sigchat.account_link()

- create a new Account in the pddb
- create an Account Manager to coordinate the Link
- request a name for the device being linked (ie default 'xous')
- call Manager.link()
- on failure - delete the pddb Account record
This commit is contained in:
nworbnhoj 2023-12-10 19:19:56 +11:00
parent c9e3f549a4
commit 572dcd6bd1
4 changed files with 51 additions and 6 deletions

View file

@ -54,6 +54,18 @@ impl Account {
}
}
/// Delete this Account key/value from the pddb
///
/// While this Account struct will persist in memory, a subsequent Account.read() will fail
///
pub fn delete(pddb_dict: &str) -> Result<(), Error> {
let pddb = pddb::Pddb::new();
pddb.try_mount();
pddb.delete_dict(pddb_dict, None)?;
log::info!("deleted Signal Account from pddb");
Ok(())
}
#[allow(dead_code)]
pub fn number(&self) -> Option<&str> {
match &self.number {

View file

@ -1,5 +1,6 @@
#[allow(dead_code)]
pub(crate) const SERVER_NAME_SIGCHAT: &str = "_Signal chat_";
pub(crate) const DEFAULT_DEVICE_NAME: &str = "xous";
pub(crate) const DEFAULT_HOST_NAME: &str = "signal.org";
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]

View file

@ -178,9 +178,40 @@ impl<'a> SigChat<'a> {
/// Note that currently Signal allows up to three linked devices per primary.
///
pub fn account_link(&mut self, host: &str) -> Result<Account, Error> {
self.modals
.show_notification(&"sorry - linking is not implemented yet", None)
.expect("notification failed");
log::info!("Attempting to Link to an existing Signal Account");
match Account::new(SIGCHAT_ACCOUNT) {
Ok(account) => {
let mut manager = Manager::new(account, TrustMode::OnFirstUse);
let name = self.name_modal(
DEFAULT_DEVICE_NAME,
t!("sigchat.account.link.name", locales::LANG),
);
match manager.link(Some(&name), Some(&host)) {
Ok(true) => {
log::info!("Linked Signal Account");
}
Ok(false) => {
log::info!("failed to link Signal Account");
}
Err(e) => {
log::warn!("error while linking Signal Account: {e}");
Account::delete(SIGCHAT_ACCOUNT).unwrap_or_else(|e| {
log::warn!("failed to delete unregistered account from pddb: {e}")
});
self.modals
.show_notification(&format!("{}", e), None)
.expect("notification failed");
}
}
}
Err(_) => {
log::warn!("failed to create new Account in pddb: {}", SIGCHAT_ACCOUNT);
self.modals
.show_notification(t!("sigchat.account.failed", locales::LANG), None)
.expect("notification failed");
}
};
Ok(Account::read(SIGCHAT_ACCOUNT)?)
}
/// Prompt a name from the user
@ -210,7 +241,7 @@ impl<'a> SigChat<'a> {
/// The phone number must include the country calling code, i.e. the number must start with a "+" sign.
/// Warning: this will disable the authentication of your phone as a primary device.
///
pub fn account_register(&mut self, host: &str) -> Result<Account, Error> {
pub fn account_register(&mut self, _host: &str) -> Result<Account, Error> {
log::info!("Attempting to Register a new Signal Account");
self.modals
.show_notification(&"sorry - registration is not implemented yet", None)

View file

@ -123,10 +123,11 @@ impl Manager {
/// Link to an existing device, instead of registering a new number. This shows a "sgnl://linkdevice?uuid=…​" URI. If you want to connect to another signal-cli instance, you can just use this URI. If you want to link to an Android/iOS device, create a QR code with the URI (e.g. with qrencode) and scan that in the Signal app.
///
/// # Arguments
/// * `name` - Optionally specify a name to describe this new device. By default "cli" will be used.
/// * `name` - Optionally specify a name to describe this new device (defaults to "xous").
/// * `host` - Optionally specify a host to connect to (defaults to "signal.org").
///
#[allow(dead_code)]
pub fn link(_name: &str) -> Result<(), Error> {
pub fn link(&mut self, name: Option<&str>, host: Option<&str>) -> Result<bool, Error> {
todo!();
}