Skip to content

Commit

Permalink
abstract timestamp check into a function
Browse files Browse the repository at this point in the history
  • Loading branch information
borngraced committed Oct 16, 2024
1 parent 3ea808b commit 91cb115
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 27 deletions.
1 change: 0 additions & 1 deletion pairing_api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
mod pairing;
mod uri;

pub use {pairing::*, uri::Methods};

Check failure on line 3 in pairing_api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy

unresolved import `pairing`

Check failure on line 3 in pairing_api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Tests

unresolved import `pairing`

Check failure on line 3 in pairing_api/src/lib.rs

View workflow job for this annotation

GitHub Actions / Documentation Tests

unresolved import `pairing`
45 changes: 19 additions & 26 deletions pairing_api/src/pairing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ impl PairingClient {
Self::default()
}

fn expiry(&self) -> Result<u64, PairingClientError> {
let expiry = Utc::now().timestamp();
if expiry < 0 {
return Err(PairingClientError::TimeError(
"Negative timestamp".to_string(),
));
}

Ok(expiry as u64)
}

/// Attempts to generate a new pairing, stores it in the client's pairing
/// list, subscribes to the pairing topic, and returns the necessary
/// information to establish a connection.
Expand All @@ -130,13 +141,7 @@ impl PairingClient {
metadata: Metadata,
methods: Option<Methods>,
) -> Result<(Topic, String), PairingClientError> {
let expiry = Utc::now().timestamp();
if expiry < 0 {
return Err(PairingClientError::TimeError(
"Negative timestamp".to_string(),
));
}

let expiry = self.expiry()?;
let topic = Topic::generate();
let relay = Relay {
protocol: RELAY_PROTOCOL.to_owned(),
Expand All @@ -146,7 +151,7 @@ impl PairingClient {
let pairing_info = PairingInfo {
active: false,
methods: methods.unwrap_or(Methods(vec![])),
expiry: expiry as u64 + EXPIRY_5_MINS,
expiry: expiry + EXPIRY_5_MINS,
relay,
topic: topic.clone().to_string(),
peer_metadata: Some(metadata),
Expand Down Expand Up @@ -183,15 +188,10 @@ impl PairingClient {

// Reactivate the pairing if needed
if activate {
let expiry = Utc::now().timestamp();
if expiry < 0 {
return Err(PairingClientError::TimeError(
"Negative timestamp".to_string(),
));
}
let expiry = self.expiry()?;

existing_pairing.pairing.active = true;
existing_pairing.pairing.expiry = expiry as u64 + EXPIRY_30_DAYS;
existing_pairing.pairing.expiry = expiry + EXPIRY_30_DAYS;
}

return Ok(topic.into());
Expand Down Expand Up @@ -225,22 +225,15 @@ impl PairingClient {

/// for either to activate a previously created pairing
pub async fn activate(&self, topic: &str) -> Result<(), PairingClientError> {
let expiry = Utc::now().timestamp();
if expiry < 0 {
return Err(PairingClientError::TimeError(
"Negative timestamp".to_string(),
));
};
let expiry = self.expiry()?;

let mut pairings = self.pairings.lock().await;
if let Some(pairing) = pairings.get_mut(topic) {
pairing.pairing.active = true;
pairing.pairing.expiry = expiry as u64 + EXPIRY_30_DAYS;

Ok(())
} else {
Err(PairingClientError::PairingNotFound)
pairing.pairing.expiry = expiry + EXPIRY_30_DAYS;
}

Ok(())
}

/// for either to update the expiry of an existing pairing.
Expand Down

0 comments on commit 91cb115

Please sign in to comment.