Skip to content

Commit

Permalink
Fix outgoing messages stored in wrong thread
Browse files Browse the repository at this point in the history
Outgoing messages in 1-to-1 chats were stored in the note-to-self chat.
This was broken in 6125534. This will be fixed by this MR.

This also fixes the timestamp of identity verification messages, which
are inserted into the store by presage. By convention, all timestamps
used are in milliseconds, this one was in seconds. This was wrong since whisperfish#206.
  • Loading branch information
Schmiddiii committed Nov 28, 2023
1 parent 23c157b commit 2ee8cba
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
34 changes: 25 additions & 9 deletions presage/src/manager/registered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ impl<S: Store> Manager<S, Registered> {
&mut state.store,
&mut state.push_service,
content.clone(),
None,
)
.await
{
Expand Down Expand Up @@ -708,7 +709,13 @@ impl<S: Store> Manager<S, Registered> {
};

let mut push_service = self.identified_push_service();
save_message(&mut self.store, &mut push_service, content).await?;
save_message(
&mut self.store,
&mut push_service,
content,
Some(Thread::Contact(recipient.uuid)),
)
.await?;

Ok(())
}
Expand Down Expand Up @@ -740,6 +747,9 @@ impl<S: Store> Manager<S, Registered> {
timestamp: u64,
) -> Result<(), Error<S::Error>> {
let mut content_body = message.into();
let master_key_bytes = master_key_bytes
.try_into()
.expect("Master key bytes to be of size 32.");

// Only update the expiration timer if it is not set.
match content_body {
Expand All @@ -750,11 +760,7 @@ impl<S: Store> Manager<S, Registered> {
// Set the expire timer to None for errors.
let store_expire_timer = self
.store
.expire_timer(&Thread::Group(
master_key_bytes
.try_into()
.expect("Master key bytes to be of size 32."),
))
.expire_timer(&Thread::Group(master_key_bytes))
.unwrap_or_default();

*timer = store_expire_timer;
Expand All @@ -765,7 +771,7 @@ impl<S: Store> Manager<S, Registered> {

let mut groups_manager = self.groups_manager()?;
let Some(group) =
upsert_group(&self.store, &mut groups_manager, master_key_bytes, &0).await?
upsert_group(&self.store, &mut groups_manager, &master_key_bytes, &0).await?
else {
return Err(Error::UnknownGroup);
};
Expand Down Expand Up @@ -807,7 +813,13 @@ impl<S: Store> Manager<S, Registered> {
};

let mut push_service = self.identified_push_service();
save_message(&mut self.store, &mut push_service, content).await?;
save_message(
&mut self.store,
&mut push_service,
content,
Some(Thread::Group(master_key_bytes)),
)
.await?;

Ok(())
}
Expand Down Expand Up @@ -1015,13 +1027,17 @@ async fn upsert_group<S: Store>(
Ok(store.group(master_key_bytes.try_into()?)?)
}

/// Save a message into the store.
/// Note that `override_thread` can be used to specify the thread the message will be stored in.
/// This is required when storing outgoing messages, as in this case the appropriate storage place cannot be derived from the message itself.
async fn save_message<S: Store>(
store: &mut S,
push_service: &mut HyperPushService,
message: Content,
override_thread: Option<Thread>,
) -> Result<(), Error<S::Error>> {
// derive the thread from the message type
let thread = Thread::try_from(&message)?;
let thread = override_thread.unwrap_or(Thread::try_from(&message)?);

// only save DataMessage and SynchronizeMessage (sent)
let message = match message.body {
Expand Down
2 changes: 1 addition & 1 deletion presage/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub trait ContentsStore {
timestamp: SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap_or_default()
.as_secs(),
.as_millis() as u64,
needs_receipt: false,
unidentified_sender: false,
},
Expand Down

0 comments on commit 2ee8cba

Please sign in to comment.