Changeset View
Changeset View
Standalone View
Standalone View
services/identity/src/database.rs
Show First 20 Lines • Show All 138 Lines • ▼ Show 20 Lines | impl DatabaseClient { | ||||
async fn add_user_to_users_table( | async fn add_user_to_users_table( | ||||
&self, | &self, | ||||
flattened_device_key_upload: FlattenedDeviceKeyUpload, | flattened_device_key_upload: FlattenedDeviceKeyUpload, | ||||
username_and_password_file: Option<(String, Blob)>, | username_and_password_file: Option<(String, Blob)>, | ||||
wallet_address: Option<String>, | wallet_address: Option<String>, | ||||
social_proof: Option<String>, | social_proof: Option<String>, | ||||
) -> Result<String, Error> { | ) -> Result<String, Error> { | ||||
let user_id = generate_uuid(); | let user_id = generate_uuid(); | ||||
let mut device_info = HashMap::from([ | let device_info = | ||||
( | create_device_info(flattened_device_key_upload.clone(), social_proof); | ||||
USERS_TABLE_DEVICES_MAP_DEVICE_TYPE_ATTRIBUTE_NAME.to_string(), | |||||
AttributeValue::S(Device::Client.to_string()), | |||||
), | |||||
( | |||||
USERS_TABLE_DEVICES_MAP_KEY_PAYLOAD_ATTRIBUTE_NAME.to_string(), | |||||
AttributeValue::S(flattened_device_key_upload.key_payload), | |||||
), | |||||
( | |||||
USERS_TABLE_DEVICES_MAP_KEY_PAYLOAD_SIGNATURE_ATTRIBUTE_NAME | |||||
.to_string(), | |||||
AttributeValue::S(flattened_device_key_upload.key_payload_signature), | |||||
), | |||||
( | |||||
USERS_TABLE_DEVICES_MAP_IDENTITY_PREKEY_ATTRIBUTE_NAME.to_string(), | |||||
AttributeValue::S(flattened_device_key_upload.identity_prekey), | |||||
), | |||||
( | |||||
USERS_TABLE_DEVICES_MAP_IDENTITY_PREKEY_SIGNATURE_ATTRIBUTE_NAME | |||||
.to_string(), | |||||
AttributeValue::S( | |||||
flattened_device_key_upload.identity_prekey_signature, | |||||
), | |||||
), | |||||
( | |||||
USERS_TABLE_DEVICES_MAP_IDENTITY_ONETIME_KEYS_ATTRIBUTE_NAME | |||||
.to_string(), | |||||
AttributeValue::L( | |||||
flattened_device_key_upload | |||||
.identity_onetime_keys | |||||
.into_iter() | |||||
.map(AttributeValue::S) | |||||
.collect(), | |||||
), | |||||
), | |||||
( | |||||
USERS_TABLE_DEVICES_MAP_NOTIF_PREKEY_ATTRIBUTE_NAME.to_string(), | |||||
AttributeValue::S(flattened_device_key_upload.notif_prekey), | |||||
), | |||||
( | |||||
USERS_TABLE_DEVICES_MAP_NOTIF_PREKEY_SIGNATURE_ATTRIBUTE_NAME | |||||
.to_string(), | |||||
AttributeValue::S(flattened_device_key_upload.notif_prekey_signature), | |||||
), | |||||
( | |||||
USERS_TABLE_DEVICES_MAP_NOTIF_ONETIME_KEYS_ATTRIBUTE_NAME.to_string(), | |||||
AttributeValue::L( | |||||
flattened_device_key_upload | |||||
.notif_onetime_keys | |||||
.into_iter() | |||||
.map(AttributeValue::S) | |||||
.collect(), | |||||
), | |||||
), | |||||
]); | |||||
if let Some(social_proof) = social_proof { | |||||
device_info.insert( | |||||
USERS_TABLE_DEVICES_MAP_SOCIAL_PROOF_ATTRIBUTE_NAME.to_string(), | |||||
AttributeValue::S(social_proof), | |||||
); | |||||
} | |||||
let devices = HashMap::from([( | let devices = HashMap::from([( | ||||
flattened_device_key_upload.device_id_key, | flattened_device_key_upload.device_id_key, | ||||
AttributeValue::M(device_info), | AttributeValue::M(device_info), | ||||
)]); | )]); | ||||
let mut user = HashMap::from([ | let mut user = HashMap::from([ | ||||
( | ( | ||||
USERS_TABLE_PARTITION_KEY.to_string(), | USERS_TABLE_PARTITION_KEY.to_string(), | ||||
AttributeValue::S(user_id.clone()), | AttributeValue::S(user_id.clone()), | ||||
▲ Show 20 Lines • Show All 60 Lines • ▼ Show 20 Lines | impl DatabaseClient { | ||||
} | } | ||||
async fn add_device_to_users_table( | async fn add_device_to_users_table( | ||||
&self, | &self, | ||||
user_id: String, | user_id: String, | ||||
flattened_device_key_upload: FlattenedDeviceKeyUpload, | flattened_device_key_upload: FlattenedDeviceKeyUpload, | ||||
social_proof: Option<String>, | social_proof: Option<String>, | ||||
) -> Result<(), Error> { | ) -> Result<(), Error> { | ||||
let mut device_info = HashMap::from([ | let device_info = | ||||
( | create_device_info(flattened_device_key_upload.clone(), social_proof); | ||||
USERS_TABLE_DEVICES_MAP_DEVICE_TYPE_ATTRIBUTE_NAME.to_string(), | |||||
AttributeValue::S(Device::Client.to_string()), | |||||
), | |||||
( | |||||
USERS_TABLE_DEVICES_MAP_KEY_PAYLOAD_ATTRIBUTE_NAME.to_string(), | |||||
AttributeValue::S(flattened_device_key_upload.key_payload), | |||||
), | |||||
( | |||||
USERS_TABLE_DEVICES_MAP_KEY_PAYLOAD_SIGNATURE_ATTRIBUTE_NAME | |||||
.to_string(), | |||||
AttributeValue::S(flattened_device_key_upload.key_payload_signature), | |||||
), | |||||
( | |||||
USERS_TABLE_DEVICES_MAP_IDENTITY_PREKEY_ATTRIBUTE_NAME.to_string(), | |||||
AttributeValue::S(flattened_device_key_upload.identity_prekey), | |||||
), | |||||
( | |||||
USERS_TABLE_DEVICES_MAP_IDENTITY_PREKEY_SIGNATURE_ATTRIBUTE_NAME | |||||
.to_string(), | |||||
AttributeValue::S( | |||||
flattened_device_key_upload.identity_prekey_signature, | |||||
), | |||||
), | |||||
( | |||||
USERS_TABLE_DEVICES_MAP_IDENTITY_ONETIME_KEYS_ATTRIBUTE_NAME | |||||
.to_string(), | |||||
AttributeValue::L( | |||||
flattened_device_key_upload | |||||
.identity_onetime_keys | |||||
.into_iter() | |||||
.map(AttributeValue::S) | |||||
.collect(), | |||||
), | |||||
), | |||||
( | |||||
USERS_TABLE_DEVICES_MAP_NOTIF_PREKEY_ATTRIBUTE_NAME.to_string(), | |||||
AttributeValue::S(flattened_device_key_upload.notif_prekey), | |||||
), | |||||
( | |||||
USERS_TABLE_DEVICES_MAP_NOTIF_PREKEY_SIGNATURE_ATTRIBUTE_NAME | |||||
.to_string(), | |||||
AttributeValue::S(flattened_device_key_upload.notif_prekey_signature), | |||||
), | |||||
( | |||||
USERS_TABLE_DEVICES_MAP_NOTIF_ONETIME_KEYS_ATTRIBUTE_NAME.to_string(), | |||||
AttributeValue::L( | |||||
flattened_device_key_upload | |||||
.notif_onetime_keys | |||||
.into_iter() | |||||
.map(AttributeValue::S) | |||||
.collect(), | |||||
), | |||||
), | |||||
]); | |||||
if let Some(social_proof) = social_proof { | |||||
device_info.insert( | |||||
USERS_TABLE_DEVICES_MAP_SOCIAL_PROOF_ATTRIBUTE_NAME.to_string(), | |||||
AttributeValue::S(social_proof), | |||||
); | |||||
} | |||||
let update_expression = | let update_expression = | ||||
format!("SET {}.#{} = :v", USERS_TABLE_DEVICES_ATTRIBUTE, "deviceID",); | format!("SET {}.#{} = :v", USERS_TABLE_DEVICES_ATTRIBUTE, "deviceID",); | ||||
let expression_attribute_names = HashMap::from([( | let expression_attribute_names = HashMap::from([( | ||||
format!("#{}", "deviceID"), | format!("#{}", "deviceID"), | ||||
flattened_device_key_upload.device_id_key, | flattened_device_key_upload.device_id_key, | ||||
)]); | )]); | ||||
let expression_attribute_values = | let expression_attribute_values = | ||||
HashMap::from([(":v".to_string(), AttributeValue::M(device_info))]); | HashMap::from([(":v".to_string(), AttributeValue::M(device_info))]); | ||||
▲ Show 20 Lines • Show All 536 Lines • ▼ Show 20 Lines | match attribute_value { | ||||
None => Err(DBItemError::new( | None => Err(DBItemError::new( | ||||
attribute_name, | attribute_name, | ||||
attribute_value, | attribute_value, | ||||
DBItemAttributeError::Missing, | DBItemAttributeError::Missing, | ||||
)), | )), | ||||
} | } | ||||
} | } | ||||
fn create_device_info( | |||||
flattened_device_key_upload: FlattenedDeviceKeyUpload, | |||||
social_proof: Option<String>, | |||||
) -> HashMap<String, AttributeValue> { | |||||
let mut device_info = HashMap::from([ | |||||
( | |||||
USERS_TABLE_DEVICES_MAP_DEVICE_TYPE_ATTRIBUTE_NAME.to_string(), | |||||
AttributeValue::S(Device::Client.to_string()), | |||||
), | |||||
( | |||||
USERS_TABLE_DEVICES_MAP_KEY_PAYLOAD_ATTRIBUTE_NAME.to_string(), | |||||
AttributeValue::S(flattened_device_key_upload.key_payload), | |||||
), | |||||
( | |||||
USERS_TABLE_DEVICES_MAP_KEY_PAYLOAD_SIGNATURE_ATTRIBUTE_NAME.to_string(), | |||||
AttributeValue::S(flattened_device_key_upload.key_payload_signature), | |||||
), | |||||
( | |||||
USERS_TABLE_DEVICES_MAP_IDENTITY_PREKEY_ATTRIBUTE_NAME.to_string(), | |||||
AttributeValue::S(flattened_device_key_upload.identity_prekey), | |||||
), | |||||
( | |||||
USERS_TABLE_DEVICES_MAP_IDENTITY_PREKEY_SIGNATURE_ATTRIBUTE_NAME | |||||
.to_string(), | |||||
AttributeValue::S(flattened_device_key_upload.identity_prekey_signature), | |||||
), | |||||
( | |||||
USERS_TABLE_DEVICES_MAP_IDENTITY_ONETIME_KEYS_ATTRIBUTE_NAME.to_string(), | |||||
AttributeValue::L( | |||||
flattened_device_key_upload | |||||
.identity_onetime_keys | |||||
.into_iter() | |||||
.map(AttributeValue::S) | |||||
.collect(), | |||||
), | |||||
), | |||||
( | |||||
USERS_TABLE_DEVICES_MAP_NOTIF_PREKEY_ATTRIBUTE_NAME.to_string(), | |||||
AttributeValue::S(flattened_device_key_upload.notif_prekey), | |||||
), | |||||
( | |||||
USERS_TABLE_DEVICES_MAP_NOTIF_PREKEY_SIGNATURE_ATTRIBUTE_NAME.to_string(), | |||||
AttributeValue::S(flattened_device_key_upload.notif_prekey_signature), | |||||
), | |||||
( | |||||
USERS_TABLE_DEVICES_MAP_NOTIF_ONETIME_KEYS_ATTRIBUTE_NAME.to_string(), | |||||
AttributeValue::L( | |||||
flattened_device_key_upload | |||||
.notif_onetime_keys | |||||
.into_iter() | |||||
.map(AttributeValue::S) | |||||
.collect(), | |||||
), | |||||
), | |||||
]); | |||||
if let Some(social_proof) = social_proof { | |||||
device_info.insert( | |||||
USERS_TABLE_DEVICES_MAP_SOCIAL_PROOF_ATTRIBUTE_NAME.to_string(), | |||||
AttributeValue::S(social_proof), | |||||
); | |||||
} | |||||
device_info | |||||
} | |||||
#[cfg(test)] | #[cfg(test)] | ||||
mod tests { | mod tests { | ||||
use super::*; | use super::*; | ||||
#[test] | #[test] | ||||
fn test_create_simple_primary_key() { | fn test_create_simple_primary_key() { | ||||
let partition_key_name = "userID".to_string(); | let partition_key_name = "userID".to_string(); | ||||
let partition_key_value = "12345".to_string(); | let partition_key_value = "12345".to_string(); | ||||
▲ Show 20 Lines • Show All 45 Lines • Show Last 20 Lines |