Skip to content

Commit

Permalink
Upsert a contact on profile key change
Browse files Browse the repository at this point in the history
  • Loading branch information
gferon committed Nov 19, 2023
1 parent 7f90577 commit 142a865
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 26 deletions.
11 changes: 3 additions & 8 deletions presage-store-sled/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,14 +428,9 @@ impl ContentsStore for SledStore {
Ok(())
}

fn save_contacts(
&mut self,
contacts: impl Iterator<Item = Contact>,
) -> Result<(), SledStoreError> {
for contact in contacts {
self.insert(SLED_TREE_CONTACTS, contact.uuid, contact)?;
}
debug!("saved contacts");
fn save_contact(&mut self, contact: &Contact) -> Result<(), SledStoreError> {
self.insert(SLED_TREE_CONTACTS, contact.uuid, contact)?;
debug!("saved contact");
Ok(())
}

Expand Down
75 changes: 62 additions & 13 deletions presage/src/manager/registered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ use libsignal_service::messagepipe::{Incoming, MessagePipe, ServiceCredentials};
use libsignal_service::models::Contact;
use libsignal_service::prelude::phonenumber::PhoneNumber;
use libsignal_service::prelude::Uuid;
use libsignal_service::profile_cipher::ProfileCipher;
use libsignal_service::proto::data_message::Delete;
use libsignal_service::proto::{
sync_message, AttachmentPointer, DataMessage, EditMessage, GroupContextV2, NullMessage,
SyncMessage,
SyncMessage, Verified,
};
use libsignal_service::protocol::SenderCertificate;
use libsignal_service::protocol::{PrivateKey, PublicKey};
Expand Down Expand Up @@ -508,6 +509,7 @@ impl<S: Store> Manager<S, Registered> {
encrypted_messages: S,
message_receiver: MessageReceiver<HyperPushService>,
service_cipher: ServiceCipher<C>,
push_service: HyperPushService,
store: C,
groups_manager: GroupsManager<HyperPushService, InMemoryCredentialsCache>,
mode: ReceivingMode,
Expand All @@ -517,6 +519,7 @@ impl<S: Store> Manager<S, Registered> {
encrypted_messages: Box::pin(self.receive_messages_encrypted().await?),
message_receiver: MessageReceiver::new(self.identified_push_service()),
service_cipher: self.new_service_cipher()?,
push_service: self.identified_push_service(),
store: self.store.clone(),
groups_manager: self.groups_manager()?,
mode,
Expand All @@ -539,15 +542,11 @@ impl<S: Store> Manager<S, Registered> {
match state.message_receiver.retrieve_contacts(contacts).await {
Ok(contacts) => {
let _ = state.store.clear_contacts();
match state
.store
.save_contacts(contacts.filter_map(Result::ok))
{
Ok(()) => {
info!("saved contacts");
}
Err(e) => {
info!("saving contacts");
for contact in contacts.filter_map(Result::ok) {
if let Err(e) = state.store.save_contact(&contact) {
warn!("failed to save contacts: {e}");
break;
}
}
}
Expand Down Expand Up @@ -603,7 +602,13 @@ impl<S: Store> Manager<S, Registered> {
}
}

if let Err(e) = save_message(&mut state.store, content.clone()) {
if let Err(e) = save_message(
&mut state.store,
&mut state.push_service,
content.clone(),
)
.await
{
error!("Error saving message to store: {}", e);
}

Expand Down Expand Up @@ -704,7 +709,8 @@ impl<S: Store> Manager<S, Registered> {
body: content_body,
};

save_message(&mut self.store, content)?;
let mut push_service = self.identified_push_service();
save_message(&mut self.store, &mut push_service, content).await?;

Ok(())
}
Expand Down Expand Up @@ -802,7 +808,8 @@ impl<S: Store> Manager<S, Registered> {
body: content_body,
};

save_message(&mut self.store, content)?;
let mut push_service = self.identified_push_service();
save_message(&mut self.store, &mut push_service, content).await?;

Ok(())
}
Expand Down Expand Up @@ -1010,7 +1017,11 @@ async fn upsert_group<S: Store>(
Ok(store.group(master_key_bytes.try_into()?)?)
}

fn save_message<S: Store>(store: &mut S, message: Content) -> Result<(), Error<S::Error>> {
async fn save_message<S: Store>(
store: &mut S,
push_service: &mut HyperPushService,
message: Content,
) -> Result<(), Error<S::Error>> {
// derive the thread from the message type
let thread = Thread::try_from(&message)?;

Expand Down Expand Up @@ -1039,6 +1050,44 @@ fn save_message<S: Store>(store: &mut S, message: Content) -> Result<(), Error<S
let sender_uuid = message.metadata.sender.uuid;
let profile_key = ProfileKey::create(profile_key_bytes);
debug!("inserting profile key for {sender_uuid}");
// if we have never seen this profile key or it has changed
// fetch the contacts info
if !store
.profile_key(&sender_uuid)?
.is_some_and(|p| p.bytes == profile_key.bytes)
{
let encrypted_profile = push_service
.retrieve_profile_by_id(sender_uuid.into(), Some(profile_key))
.await?;
let profile_cipher = ProfileCipher::from(profile_key);
let decrypted_profile = encrypted_profile.decrypt(profile_cipher).unwrap();

let contact = Contact {
uuid: sender_uuid,
phone_number: None,
name: decrypted_profile
.name
// this assumes [firstname] [lastname]
.map(|pn| {
if let Some(family_name) = pn.family_name {
format!("{} {}", pn.given_name, family_name)
} else {
pn.given_name
}
})
.unwrap_or_default(),
profile_key: profile_key.bytes.to_vec(),
color: None,
blocked: false,
expire_timer: 0,
inbox_position: 0,
archived: false,
avatar: None,
verified: Verified::default(),
};

store.save_contact(&contact)?;
}
store.upsert_profile_key(&sender_uuid, profile_key)?;
}

Expand Down
7 changes: 2 additions & 5 deletions presage/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,8 @@ pub trait ContentsStore {
/// Clear all saved synchronized contact data
fn clear_contacts(&mut self) -> Result<(), Self::ContentsStoreError>;

/// Replace all contact data
fn save_contacts(
&mut self,
contacts: impl Iterator<Item = Contact>,
) -> Result<(), Self::ContentsStoreError>;
/// Save a contact
fn save_contact(&mut self, contacts: &Contact) -> Result<(), Self::ContentsStoreError>;

/// Get an iterator on all stored (synchronized) contacts
fn contacts(&self) -> Result<Self::ContactsIter, Self::ContentsStoreError>;
Expand Down

0 comments on commit 142a865

Please sign in to comment.