Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Drop unknown FCM users #197

Merged
merged 7 commits into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions autoendpoint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ validator_derive = "0.10.0"
yup-oauth2 = "4.1.2"

[dev-dependencies]
mockall = "0.7.1"
mockito = "0.26.0"
tempfile = "3.1.0"
tokio = { version = "0.2.12", features = ["macros"] }
71 changes: 51 additions & 20 deletions autoendpoint/src/db/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::db::retry::{
retry_policy, retryable_delete_error, retryable_getitem_error, retryable_putitem_error,
retryable_updateitem_error,
};
use async_trait::async_trait;
use autopush_common::db::{DynamoDbNotification, DynamoDbUser};
use autopush_common::notification::Notification;
use autopush_common::{ddb_item, hashmap, val};
Expand All @@ -18,15 +19,46 @@ use std::env;
use uuid::Uuid;

/// Provides high-level operations over the DynamoDB database
#[async_trait]
pub trait DbClient: Send + Sync {
/// Read a user from the database
async fn get_user(&self, uaid: Uuid) -> DbResult<Option<DynamoDbUser>>;

/// Delete a user from the router table
async fn remove_user(&self, uaid: Uuid) -> DbResult<()>;

/// Get the set of channel IDs for a user
async fn get_channels(&self, uaid: Uuid) -> DbResult<HashSet<Uuid>>;

/// Remove the node ID from a user in the router table.
/// The node ID will only be cleared if `connected_at` matches up with the
/// item's `connected_at`.
async fn remove_node_id(&self, uaid: Uuid, node_id: String, connected_at: u64) -> DbResult<()>;

/// Save a message to the message table
async fn save_message(&self, uaid: Uuid, message: Notification) -> DbResult<()>;

/// Get the message table name
fn message_table(&self) -> &str;

fn box_clone(&self) -> Box<dyn DbClient>;
}

impl Clone for Box<dyn DbClient> {
fn clone(&self) -> Self {
self.box_clone()
}
}

#[derive(Clone)]
pub struct DbClient {
pub struct DbClientImpl {
ddb: DynamoDbClient,
metrics: StatsdClient,
router_table: String,
pub message_table: String,
message_table: String,
}

impl DbClient {
impl DbClientImpl {
pub fn new(
metrics: StatsdClient,
router_table: String,
Expand All @@ -52,9 +84,11 @@ impl DbClient {
message_table,
})
}
}

/// Read a user from the database
pub async fn get_user(&self, uaid: Uuid) -> DbResult<Option<DynamoDbUser>> {
#[async_trait]
impl DbClient for DbClientImpl {
async fn get_user(&self, uaid: Uuid) -> DbResult<Option<DynamoDbUser>> {
let input = GetItemInput {
table_name: self.router_table.clone(),
consistent_read: Some(true),
Expand All @@ -74,8 +108,7 @@ impl DbClient {
.map_err(DbError::from)
}

/// Delete a user from the router table
pub async fn drop_user(&self, uaid: Uuid) -> DbResult<()> {
async fn remove_user(&self, uaid: Uuid) -> DbResult<()> {
let input = DeleteItemInput {
table_name: self.router_table.clone(),
key: ddb_item! { uaid: s => uaid.to_simple().to_string() },
Expand All @@ -91,8 +124,7 @@ impl DbClient {
Ok(())
}

/// Get the set of channel IDs for a user
pub async fn get_user_channels(&self, uaid: Uuid) -> DbResult<HashSet<Uuid>> {
async fn get_channels(&self, uaid: Uuid) -> DbResult<HashSet<Uuid>> {
// Channel IDs are stored in a special row in the message table, where
// chidmessageid = " "
let input = GetItemInput {
Expand Down Expand Up @@ -132,15 +164,7 @@ impl DbClient {
Ok(channels)
}

/// Remove the node ID from a user in the router table.
/// The node ID will only be cleared if `connected_at` matches up with the
/// item's `connected_at`.
pub async fn remove_node_id(
&self,
uaid: Uuid,
node_id: String,
connected_at: u64,
) -> DbResult<()> {
async fn remove_node_id(&self, uaid: Uuid, node_id: String, connected_at: u64) -> DbResult<()> {
let update_item = UpdateItemInput {
key: ddb_item! { uaid: s => uaid.to_simple().to_string() },
update_expression: Some("REMOVE node_id".to_string()),
Expand All @@ -163,8 +187,7 @@ impl DbClient {
Ok(())
}

/// Store a single message
pub async fn store_message(&self, uaid: Uuid, message: Notification) -> DbResult<()> {
async fn save_message(&self, uaid: Uuid, message: Notification) -> DbResult<()> {
let put_item = PutItemInput {
item: serde_dynamodb::to_hashmap(&DynamoDbNotification::from_notif(&uaid, message))?,
table_name: self.message_table.clone(),
Expand All @@ -180,4 +203,12 @@ impl DbClient {

Ok(())
}

fn message_table(&self) -> &str {
&self.message_table
}

fn box_clone(&self) -> Box<dyn DbClient> {
Box::new(self.clone())
}
}
Loading