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 @@ -140,15 +140,20 @@ registration_state: UserRegistrationInfo, password_file: Vec, ) -> Result { - self + let device_key_upload = registration_state.flattened_device_key_upload; + let user_id = self .add_user_to_users_table( - registration_state.flattened_device_key_upload, + device_key_upload.clone(), Some((registration_state.username, Blob::new(password_file))), None, None, registration_state.user_id, ) - .await + .await?; + + device_list::add_device(self, &user_id, device_key_upload, None).await?; + + Ok(user_id) } pub async fn add_wallet_user_to_users_table( @@ -158,15 +163,26 @@ social_proof: String, user_id: Option, ) -> Result { - self + let social_proof = Some(social_proof); + let user_id = self .add_user_to_users_table( - flattened_device_key_upload, + flattened_device_key_upload.clone(), None, Some(wallet_address), - Some(social_proof), + social_proof.clone(), user_id, ) - .await + .await?; + + device_list::add_device( + self, + &user_id, + flattened_device_key_upload, + social_proof, + ) + .await?; + + Ok(user_id) } async fn add_user_to_users_table( @@ -241,8 +257,28 @@ user_id: String, flattened_device_key_upload: FlattenedDeviceKeyUpload, ) -> Result<(), Error> { + // add device to the legacy device list self - .add_device_to_users_table(user_id, flattened_device_key_upload, None) + .add_device_to_users_table( + user_id.clone(), + flattened_device_key_upload.clone(), + None, + ) + .await?; + + // add device to the new device list if not exists + let device_exists = device_list::device_exists( + self, + user_id.clone(), + flattened_device_key_upload.device_id_key.clone(), + ) + .await?; + + if device_exists { + return Ok(()); + } + + device_list::add_device(self, user_id, flattened_device_key_upload, None) .await } @@ -252,13 +288,35 @@ flattened_device_key_upload: FlattenedDeviceKeyUpload, social_proof: String, ) -> Result<(), Error> { + // add device to the legacy device list self .add_device_to_users_table( - user_id, - flattened_device_key_upload, - Some(social_proof), + user_id.clone(), + flattened_device_key_upload.clone(), + Some(social_proof.clone()), ) - .await + .await?; + + // add device to the new device list if not exists + let device_exists = device_list::device_exists( + self, + user_id.clone(), + flattened_device_key_upload.device_id_key.clone(), + ) + .await?; + + if device_exists { + return Ok(()); + } + + // add device to the new device list + device_list::add_device( + self, + user_id, + flattened_device_key_upload, + Some(social_proof), + ) + .await } pub async fn get_keyserver_keys_for_user(