Skip to content

Commit

Permalink
Remove registration type for now as it is unused
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnius committed Sep 24, 2023
1 parent 37afa5a commit 10b5549
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 43 deletions.
44 changes: 14 additions & 30 deletions src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ impl Handler {
log::info!("Setting up the manager2");
let registration = if config_store.is_registered() {
log::info!("Registered, starting the manager");
// TODO do not hard code
tokio::task::spawn_local(async move {
let manager = ManagerThread::new(
config_store.clone(),
Expand All @@ -102,7 +101,7 @@ impl Handler {
}
});

Registration::Chosen(registration::Type::Primary, State::Registered)
Registration::Chosen(State::Registered)
} else {
log::info!("Not yet registered.");
Registration::Unregistered
Expand Down Expand Up @@ -311,9 +310,7 @@ impl Handler {
"sendCode" => {
if self.handle_send_code_message(axolotl_request.data).await? {
self.send_registration_confirmation().await;
// TODO don't hard code the type
self.registration =
Registration::Chosen(registration::Type::Primary, State::Registered);
self.registration = Registration::Chosen(State::Registered);
true
} else {
false
Expand Down Expand Up @@ -342,7 +339,7 @@ impl Handler {
);
false
} else {
self.registration = Registration::Chosen(registration::Type::Primary, State::Started);
self.registration = Registration::Chosen(State::Started);
self.get_phone_number().await;
true
}
Expand All @@ -357,7 +354,7 @@ impl Handler {
);
return Ok(false);
}
self.registration = Registration::Chosen(registration::Type::Secondary, State::Started);
self.registration = Registration::Chosen(State::Started);

loop {
log::debug!("Registering secondary device");
Expand All @@ -376,22 +373,18 @@ impl Handler {
match e {
Some(u) => {
log::error!("Error registering secondary device: {}", u);
Some(u)
}
None => {
tokio::time::sleep(time::Duration::from_secs(1)).await;
continue;
}
};
}
if error_reciever.try_recv().is_err() {
log::debug!("Break out of loop, because error channel is closed");
match Handler::check_registration().await {
Ok(_) => {
// TODO how to set to registered? Don't we need the manager?
//self.is_registered = Some(true);
//break;
todo!();
self.registration = Registration::Chosen(State::Registered);
break;
}
Err(e) => {
log::debug!("Error checking registration: {}", e);
Expand Down Expand Up @@ -450,14 +443,16 @@ impl Handler {
return Ok(true);
};

let Registration::Chosen(device, State::Confirming(_)) = &self.registration else {
return Err(ApplicationError::RegistrationError("Got unexpected registration confirmation code.".to_string()));
let Registration::Chosen(State::Confirming(_)) = &self.registration else {
return Err(ApplicationError::RegistrationError(
"Got unexpected registration confirmation code.".to_string(),
));
};

let mut new_state = Registration::Chosen(*device, State::Registered);
let mut new_state = Registration::Chosen(State::Registered);
std::mem::swap(&mut self.registration, &mut new_state);

if let Registration::Chosen(_, State::Confirming(manager)) = new_state {
if let Registration::Chosen(State::Confirming(manager)) = new_state {
log::info!("Going to send verification code: {code}");
let result = self.send_verification_code(manager, &code).await;
if let Err(e) = result {
Expand Down Expand Up @@ -616,15 +611,7 @@ impl Handler {
}
};

// TODO arbitrarily set to primary for testing
let new_state =
Registration::Chosen(registration::Type::Primary, State::Confirming(manager));
//let new_state = match self.registration {
// Registration::Chosen(device, State::Started) => {
// Registration::Chosen(device, State::Confirming(manager))
// }
// _ => todo!(), // TODO how to avoid this branch in the first place?
//};
let new_state = Registration::Chosen(State::Confirming(manager));
self.registration = new_state;

Ok(())
Expand Down Expand Up @@ -1511,10 +1498,7 @@ impl Handler {
}

fn is_registered(&self) -> bool {
matches!(
self.registration,
Registration::Chosen(_, State::Registered)
)
matches!(self.registration, Registration::Chosen(State::Registered))
}
}

Expand Down
20 changes: 7 additions & 13 deletions src/handlers/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,23 @@ pub enum State {
Registered,
}

#[derive(Debug, Clone, Copy)]
pub enum Type {
Primary,
Secondary,
}

pub enum Registration {
Unregistered,
Chosen(Type, State),
Chosen(State),
}

impl Registration {
pub fn explain_for_log(&self) -> String {
match self {
Self::Unregistered => "No registration started yet.".to_string(),
Self::Chosen(device, State::Started) => {
format!("Registration as {device:?} device started.")
Self::Chosen(State::Started) => {
format!("Registration started.")
}
Self::Chosen(device, State::Confirming(_)) => {
format!("{device:?} device registration is waiting for confirmation.")
Self::Chosen(State::Confirming(_)) => {
format!("Registration is waiting for confirmation.")
}
Self::Chosen(device, State::Registered) => {
format!("Registered as {device:?}.")
Self::Chosen(State::Registered) => {
format!("Registered.")
}
}
}
Expand Down

0 comments on commit 10b5549

Please sign in to comment.