diff --git a/services/identity/src/database.rs b/services/identity/src/database.rs --- a/services/identity/src/database.rs +++ b/services/identity/src/database.rs @@ -219,22 +219,10 @@ user_id: Option, ) -> Result { let user_id = user_id.unwrap_or_else(generate_uuid); - let device_info = - create_device_info(flattened_device_key_upload.clone(), social_proof); - let devices = HashMap::from([( - flattened_device_key_upload.device_id_key.clone(), - AttributeValue::M(device_info), + let mut user = HashMap::from([( + USERS_TABLE_PARTITION_KEY.to_string(), + AttributeValue::S(user_id.clone()), )]); - let mut user = HashMap::from([ - ( - USERS_TABLE_PARTITION_KEY.to_string(), - AttributeValue::S(user_id.clone()), - ), - ( - USERS_TABLE_DEVICES_ATTRIBUTE.to_string(), - AttributeValue::M(devices), - ), - ]); if let Some((username, password_file)) = username_and_password_file { user.insert( @@ -284,16 +272,12 @@ code_version: u64, access_token_creation_time: DateTime, ) -> Result<(), Error> { - // add device to the legacy device list - self - .add_device_to_users_table( - user_id.clone(), - flattened_device_key_upload.clone(), - None, - ) - .await?; + let content_one_time_keys = + flattened_device_key_upload.content_one_time_keys.clone(); + let notif_one_time_keys = + flattened_device_key_upload.notif_one_time_keys.clone(); - // add device to the new device list if not exists + // add device to the device list if not exists let device_id = flattened_device_key_upload.device_id_key.clone(); let device_exists = self .device_exists(user_id.clone(), device_id.clone()) @@ -318,7 +302,17 @@ code_version, access_token_creation_time, ) - .await + .await?; + + self + .append_one_time_prekeys( + device_id, + content_one_time_keys, + notif_one_time_keys, + ) + .await?; + + Ok(()) } pub async fn add_wallet_user_device_to_users_table( @@ -329,16 +323,12 @@ code_version: u64, access_token_creation_time: DateTime, ) -> Result<(), Error> { - // add device to the legacy device list - self - .add_device_to_users_table( - user_id.clone(), - flattened_device_key_upload.clone(), - Some(social_proof.clone()), - ) - .await?; + let content_one_time_keys = + flattened_device_key_upload.content_one_time_keys.clone(); + let notif_one_time_keys = + flattened_device_key_upload.notif_one_time_keys.clone(); - // add device to the new device list if not exists + // add device to the device list if not exists let device_id = flattened_device_key_upload.device_id_key.clone(); let device_exists = self .device_exists(user_id.clone(), device_id.clone()) @@ -364,7 +354,17 @@ code_version, access_token_creation_time, ) - .await + .await?; + + self + .append_one_time_prekeys( + device_id, + content_one_time_keys, + notif_one_time_keys, + ) + .await?; + + Ok(()) } pub async fn get_keyserver_keys_for_user( @@ -734,51 +734,6 @@ Ok(()) } - async fn add_device_to_users_table( - &self, - user_id: String, - flattened_device_key_upload: FlattenedDeviceKeyUpload, - social_proof: Option, - ) -> Result<(), Error> { - // Avoid borrowing from lifetime of flattened_device_key_upload - let device_id = flattened_device_key_upload.device_id_key.clone(); - let content_one_time_keys = - flattened_device_key_upload.content_one_time_keys.clone(); - let notif_one_time_keys = - flattened_device_key_upload.notif_one_time_keys.clone(); - - let device_info = - create_device_info(flattened_device_key_upload, social_proof); - let update_expression = - format!("SET {}.#{} = :v", USERS_TABLE_DEVICES_ATTRIBUTE, "deviceID",); - let expression_attribute_names = - HashMap::from([(format!("#{}", "deviceID"), device_id.clone())]); - let expression_attribute_values = - HashMap::from([(":v".to_string(), AttributeValue::M(device_info))]); - - self - .client - .update_item() - .table_name(USERS_TABLE) - .key(USERS_TABLE_PARTITION_KEY, AttributeValue::S(user_id)) - .update_expression(update_expression) - .set_expression_attribute_names(Some(expression_attribute_names)) - .set_expression_attribute_values(Some(expression_attribute_values)) - .send() - .await - .map_err(|e| Error::AwsSdk(e.into()))?; - - self - .append_one_time_prekeys( - device_id, - content_one_time_keys, - notif_one_time_keys, - ) - .await?; - - Ok(()) - } - pub async fn remove_device_from_users_table( &self, user_id: String, @@ -1622,52 +1577,6 @@ } } -fn create_device_info( - flattened_device_key_upload: FlattenedDeviceKeyUpload, - social_proof: Option, -) -> AttributeMap { - let mut device_info = HashMap::from([ - ( - USERS_TABLE_DEVICES_MAP_DEVICE_TYPE_ATTRIBUTE_NAME.to_string(), - AttributeValue::S(flattened_device_key_upload.device_type.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_CONTENT_PREKEY_ATTRIBUTE_NAME.to_string(), - AttributeValue::S(flattened_device_key_upload.content_prekey), - ), - ( - USERS_TABLE_DEVICES_MAP_CONTENT_PREKEY_SIGNATURE_ATTRIBUTE_NAME - .to_string(), - AttributeValue::S(flattened_device_key_upload.content_prekey_signature), - ), - ( - 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), - ), - ]); - - 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)] mod tests { use super::*;