relocate sigchat service environment argument to lib.rs

temporarily default to Staging
This commit is contained in:
nworbnhoj 2023-12-28 09:49:23 +11:00
parent 59895ac525
commit 3d5030f142
3 changed files with 26 additions and 15 deletions

View file

@ -2,7 +2,7 @@ mod service_environment;
use crate::manager::libsignal::{DeviceNameUtil, ProvisionMessage};
use pddb::Pddb;
use service_environment::ServiceEnvironment;
pub use service_environment::ServiceEnvironment;
use std::io::{Error, ErrorKind, Read, Write};
use std::str::FromStr;
@ -24,7 +24,7 @@ pub struct Account {
pni_service_id: Option<String>,
profile_key: Option<String>,
registered: bool,
service_environment: ServiceEnvironment,
service_environment: Option<ServiceEnvironment>,
storage_key: Option<String>,
store_last_receive_timestamp: i64,
store_manifest_version: i64,
@ -86,12 +86,7 @@ impl Account {
set(&pddb, pddb_dict, PNI_SERVICE_ID_KEY, None)?;
set(&pddb, pddb_dict, PROFILE_KEY_KEY, None)?;
set(&pddb, pddb_dict, REGISTERED_KEY, Some(&false.to_string()))?;
set(
&pddb,
pddb_dict,
SERVICE_ENVIRONMENT_KEY,
Some(&ServiceEnvironment::Staging.to_string()),
)?;
set(&pddb, pddb_dict, SERVICE_ENVIRONMENT_KEY, None)?;
set(&pddb, pddb_dict, STORAGE_KEY_KEY, None)?;
set(
&pddb,
@ -152,7 +147,7 @@ impl Account {
Ok(pni_service_id),
Ok(profile_key),
Ok(Some(registered)),
Ok(Some(service_environment)),
Ok(service_environment),
Ok(storage_key),
Ok(Some(store_last_receive_timestamp)),
Ok(Some(store_manifest_version)),
@ -174,7 +169,8 @@ impl Account {
pni_service_id: pni_service_id,
profile_key: profile_key,
registered: registered.parse().unwrap(),
service_environment: ServiceEnvironment::from_str(&service_environment).unwrap(),
service_environment: service_environment
.map(|se| ServiceEnvironment::from_str(&se).unwrap()),
storage_key: storage_key,
store_last_receive_timestamp: store_last_receive_timestamp.parse().unwrap(),
store_manifest_version: store_manifest_version.parse().unwrap(),
@ -220,6 +216,7 @@ impl Account {
pub fn link(
&mut self,
device_name: &str,
service_environment: &str,
provisioning_msg: ProvisionMessage,
) -> Result<bool, Error> {
// TODO complete link pre-checks
@ -257,6 +254,7 @@ impl Account {
),
)?;
self.set(REGISTERED_KEY, Some(&false.to_string()))?;
self.set(SERVICE_ENVIRONMENT_KEY, Some(service_environment))?;
self.set(STORAGE_KEY_KEY, None)?;
self.set(STORE_LAST_RECEIVE_TIMESTAMP_KEY, Some("0"))?;
self.set(STORE_MANIFEST_VERSION_KEY, Some("-1"))?;
@ -330,7 +328,7 @@ impl Account {
PROFILE_KEY_KEY => Ok(self.profile_key = owned_value),
REGISTERED_KEY => Ok(self.registered = owned_value.unwrap().parse().unwrap()),
SERVICE_ENVIRONMENT_KEY => Ok(self.service_environment =
ServiceEnvironment::from_str(&value.unwrap()).unwrap()),
Some(ServiceEnvironment::from_str(&value.unwrap()).unwrap())),
STORAGE_KEY_KEY => Ok(self.storage_key = owned_value),
_ => {
log::warn!("invalid key: {key}");

View file

@ -2,7 +2,7 @@ mod account;
pub mod api;
mod manager;
use crate::account::Account;
use crate::account::{Account, ServiceEnvironment};
use crate::manager::{Manager, TrustMode};
pub use api::*;
use chat::Chat;
@ -218,7 +218,11 @@ impl<'a> SigChat<'a> {
self.chat
.set_status_text(t!("sigchat.status.connecting", locales::LANG));
self.chat.set_busy_state(true);
match manager.link(Some(&name), Some(&host)) {
match manager.link(
Some(&name),
Some(&host),
Some(&ServiceEnvironment::Staging.to_string()),
) {
Ok(true) => {
log::info!("Linked Signal Account");
self.chat.set_busy_state(false);

View file

@ -4,7 +4,7 @@ mod link_state;
mod signal_ws;
mod trust_mode;
use crate::{Account, DEFAULT_DEVICE_NAME, DEFAULT_HOST_NAME};
use crate::{Account, ServiceEnvironment, DEFAULT_DEVICE_NAME, DEFAULT_HOST_NAME};
use group_permission::GroupPermission;
use link_state::LinkState;
use locales::t;
@ -143,12 +143,20 @@ impl Manager {
/// # Arguments
/// * `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").
/// * `service_environment` - Optionally specify a service_environment to connect to (defaults to "Live").
///
#[allow(dead_code)]
pub fn link(&mut self, name: Option<&str>, host: Option<&str>) -> Result<bool, Error> {
pub fn link(
&mut self,
name: Option<&str>,
host: Option<&str>,
service_environment: Option<&str>,
) -> Result<bool, Error> {
// insert default values as required
let name = name.unwrap_or(DEFAULT_DEVICE_NAME);
let host = host.unwrap_or(DEFAULT_HOST_NAME);
let live_service_environment = ServiceEnvironment::Live.to_string();
let service_environment = service_environment.unwrap_or(&live_service_environment);
let link_err = Error::new(ErrorKind::Other, "failed to link device");
match SignalWS::new_provision(&host) {
Ok(mut ws) => {
@ -180,6 +188,7 @@ impl Manager {
ws.close();
match self.account.link(
name,
service_environment,
libsignal::ProvisionMessage::decode(
identity_key_pair,
registration,