Skip to content

Commit

Permalink
Only save DataMessage and SyncMessage (sent) in store (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
gferon authored Mar 19, 2023
1 parent 9f717c7 commit c5f23d4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Only `DataMessage` that are sent, received, or sent from another device are saved in the local store (#137).
This is a breaking change because clients previously had to introduce calls to filter internal messages themselves.

- Changed (and fixed) the behaviour of the iterator returned by `SledStore::messages` (#119)
* The iterator yields elements in chronological order (used to be reversed)
* The iterator now implements `DoubleEndedIterator` which means you it can be reversed or consumed from the end
Expand Down
41 changes: 30 additions & 11 deletions src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -809,13 +809,10 @@ impl<C: Store> Manager<C, Registered> {
}
}

if let Ok(thread) = Thread::try_from(&content) {
// TODO: handle reactions here, we should update the original message?
if let Err(e) =
state.config_store.save_message(&thread, content.clone())
{
log::error!("Error saving message to store: {}", e);
}
if let Err(e) =
save_message(&mut state.config_store, content.clone())
{
log::error!("Error saving message to store: {}", e);
}

return Some((content, state));
Expand Down Expand Up @@ -859,7 +856,6 @@ impl<C: Store> Manager<C, Registered> {
.await?;

// save the message
let thread = Thread::Contact(recipient.uuid);
let content = Content {
metadata: Metadata {
sender: self.state.uuid.into(),
Expand All @@ -871,7 +867,7 @@ impl<C: Store> Manager<C, Registered> {
body: content_body,
};

self.config_store.save_message(&thread, content)?;
save_message(&mut self.config_store, content)?;

Ok(())
}
Expand Down Expand Up @@ -923,8 +919,8 @@ impl<C: Store> Manager<C, Registered> {
},
body: message.into(),
};
let thread = Thread::try_from(&content)?;
self.config_store.save_message(&thread, content)?;

save_message(&mut self.config_store, content)?;

Ok(())
}
Expand Down Expand Up @@ -1094,3 +1090,26 @@ async fn upsert_group<C: Store>(

config_store.group(master_key_bytes)
}

fn save_message<C: Store>(config_store: &mut C, message: Content) -> Result<(), Error> {
let thread = Thread::try_from(&message)?;
// only save DataMessage and SynchronizeMessage (sent)
match message.body {
ContentBody::DataMessage(_)
| ContentBody::SynchronizeMessage(SyncMessage { sent: Some(_), .. })
| ContentBody::SynchronizeMessage(SyncMessage {
call_event: Some(_),
..
}) => {
config_store.save_message(&thread, message)?;
}
ContentBody::SynchronizeMessage(_) => {
debug!("skipping saving sync message without interesting fields")
}
ContentBody::CallMessage(_) => debug!("skipping saving call message"),
ContentBody::ReceiptMessage(_) => debug!("skipping saving receipt message"),
ContentBody::TypingMessage(_) => debug!("skipping saving typing message"),
}

Ok(())
}
1 change: 0 additions & 1 deletion src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ pub trait MessageStore {
fn clear_messages(&mut self) -> Result<(), Error>;

/// Save a message in a [Thread] identified by a timestamp.
/// TODO: deriving the thread happens from the content, so we can also ditch the first parameter
fn save_message(&mut self, thread: &Thread, message: Content) -> Result<(), Error>;

/// Delete a single message, identified by its received timestamp from a thread.
Expand Down
2 changes: 1 addition & 1 deletion src/store/sled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ impl MessageStore for SledStore {
message.metadata.timestamp,
);

let tree = self.messages_thread_tree_name(thread);
let tree = self.messages_thread_tree_name(&thread);
let key = message.metadata.timestamp.to_be_bytes();

let proto: ContentProto = message.into();
Expand Down

0 comments on commit c5f23d4

Please sign in to comment.