Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
refactor: remove/rearrange some clones
Browse files Browse the repository at this point in the history
and some minor changes

Issue #1238
  • Loading branch information
pjenvey committed May 17, 2018
1 parent e755bb4 commit f8c6e05
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 29 deletions.
8 changes: 4 additions & 4 deletions autopush_rs/src/db/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ pub fn lookup_user(
let response = response.and_then(move |data| -> MyFuture<_> {
let mut hello_response: HelloResponse = Default::default();
hello_response.message_month = cur_month.clone();
let user = _handle_user_result(
let user = handle_user_result(
&cur_month,
&messages_tables,
connected_at,
Expand All @@ -385,9 +385,9 @@ pub fn lookup_user(
Box::new(response)
}

// Helper function for determining if a returned user record is valid for use or
// if it should be dropped and a new one created.
fn _handle_user_result(
/// Helper function for determining if a returned user record is valid for use
/// or if it should be dropped and a new one created.
fn handle_user_result(
cur_month: &String,
messages_tables: &[String],
connected_at: u64,
Expand Down
39 changes: 17 additions & 22 deletions autopush_rs/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ const MAX_EXPIRY: u64 = 2_592_000;
const USER_RECORD_VERSION: u8 = 1;

/// Basic requirements for notification content to deliver to websocket client
// - channelID (the subscription website intended for)
// - version (only really utilized for notification acknowledgement in
// webpush, used to be the sole carrier of data, can now be anything)
// - data (encrypted content)
// - headers (hash of crypto headers: encoding, encrypption, crypto-key, encryption-key)
/// - channelID (the subscription website intended for)
/// - version (only really utilized for notification acknowledgement in
/// webpush, used to be the sole carrier of data, can now be anything)
/// - data (encrypted content)
/// - headers (hash of crypto headers: encoding, encrypption, crypto-key, encryption-key)
#[derive(Default, Clone)]
pub struct HelloResponse {
pub uaid: Option<Uuid>,
Expand Down Expand Up @@ -123,10 +123,9 @@ impl DynamoStorage {
metrics: &StatsdClient,
) -> MyFuture<HelloResponse> {
let router_table_name = router_table_name.to_string();
let ddb = self.ddb.clone();
let response: MyFuture<(HelloResponse, Option<DynamoDbUser>)> = if let Some(uaid) = uaid {
commands::lookup_user(
ddb,
self.ddb.clone(),
&uaid,
connected_at,
router_url,
Expand Down Expand Up @@ -208,8 +207,7 @@ impl DynamoStorage {
}

pub fn drop_uaid(&self, table_name: &str, uaid: &Uuid) -> MyFuture<()> {
let ddb = self.ddb.clone();
let response = commands::drop_user(ddb, uaid, table_name)
let response = commands::drop_user(self.ddb.clone(), uaid, table_name)
.and_then(move |_| -> MyFuture<_> { Box::new(future::ok(())) })
.chain_err(|| "Unable to drop user record");
Box::new(response)
Expand All @@ -223,10 +221,10 @@ impl DynamoStorage {
code: u32,
metrics: &StatsdClient,
) -> MyFuture<bool> {
let ddb = self.ddb.clone();
let response = commands::unregister_channel_id(ddb, uaid, channel_id, message_month)
.and_then(move |_| -> MyFuture<_> { Box::new(future::ok(true)) })
.or_else(move |_| -> MyFuture<_> { Box::new(future::ok(false)) });
let response =
commands::unregister_channel_id(self.ddb.clone(), uaid, channel_id, message_month)
.and_then(move |_| -> MyFuture<_> { Box::new(future::ok(true)) })
.or_else(move |_| -> MyFuture<_> { Box::new(future::ok(false)) });
metrics
.incr_with_tags("ua.command.unregister")
.with_tag("code", &code.to_string())
Expand All @@ -243,20 +241,18 @@ impl DynamoStorage {
current_message_month: &str,
router_table_name: &str,
) -> MyFuture<()> {
let uaid = *uaid;
let ddb = self.ddb.clone();
let ddb1 = self.ddb.clone();
let ddb2 = self.ddb.clone();
let uaid = *uaid;
let cur_month = current_message_month.to_string();
let cur_month1 = cur_month.clone();
let cur_month2 = cur_month.clone();
let router_table_name = router_table_name.to_string();
let response = commands::all_channels(ddb, &uaid, message_month)
let response = commands::all_channels(self.ddb.clone(), &uaid, message_month)
.and_then(move |channels| -> MyFuture<_> {
if channels.is_empty() {
Box::new(future::ok(()))
} else {
commands::save_channels(ddb1, &uaid, channels, &cur_month1)
commands::save_channels(ddb, &uaid, channels, &cur_month)
}
})
.and_then(move |_| -> MyFuture<_> {
Expand Down Expand Up @@ -343,15 +339,14 @@ impl DynamoStorage {
include_topic: bool,
timestamp: Option<u64>,
) -> MyFuture<CheckStorageResponse> {
let ddb = self.ddb.clone();
let response: MyFuture<FetchMessageResponse> = if include_topic {
commands::fetch_messages(ddb, table_name, uaid, 11 as u32)
commands::fetch_messages(self.ddb.clone(), table_name, uaid, 11 as u32)
} else {
Box::new(future::ok(Default::default()))
};
let uaid = *uaid;
let table_name = table_name.to_string();
let ddb2 = self.ddb.clone();
let ddb = self.ddb.clone();
let response = response.and_then(move |resp| -> MyFuture<_> {
// Return now from this future if we have messages
if !resp.messages.is_empty() {
Expand All @@ -371,7 +366,7 @@ impl DynamoStorage {
let next_query = {
if resp.messages.is_empty() || resp.timestamp.is_some() {
commands::fetch_timestamp_messages(
ddb2,
ddb,
table_name.as_ref(),
&uaid,
timestamp,
Expand Down
2 changes: 1 addition & 1 deletion autopush_rs/src/db/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ where
/// Direct representation of a DynamoDB Notification as we store it in the database
/// Most attributes are optional
#[derive(Default, Deserialize, PartialEq, Debug, Clone, Serialize)]
pub struct NotificationHeaders {
struct NotificationHeaders {
#[serde(skip_serializing_if = "Option::is_none")]
crypto_key: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down
4 changes: 2 additions & 2 deletions autopush_rs/src/db/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use rand::distributions::{IndependentSample, Range};
/// Generate a last_connect
///
/// This intentionally generates a limited set of keys for each month in a
// known sequence. For each month, there's 24 hours * 10 random numbers for
// a total of 240 keys per month depending on when the user migrates forward.
/// known sequence. For each month, there's 24 hours * 10 random numbers for
/// a total of 240 keys per month depending on when the user migrates forward.
pub fn generate_last_connect() -> u64 {
let today = Utc::now();
let mut rng = rand::thread_rng();
Expand Down

0 comments on commit f8c6e05

Please sign in to comment.