Skip to content

Commit

Permalink
Create thread metadat on receiving messages
Browse files Browse the repository at this point in the history
  • Loading branch information
nanu-c committed Apr 17, 2023
1 parent 2620a0d commit 677c2ea
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 8 deletions.
7 changes: 3 additions & 4 deletions presage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,19 @@ const USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "-rs-", env!("CARGO_PKG
// TODO: open a PR in libsignal and make sure the bytes can be read from `GroupMasterKey` instead of using this type
pub type GroupMasterKeyBytes = [u8; 32];

#[derive(Deserialize, Serialize, Debug)]
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct ThreadMetadata {
pub thread: Thread,
pub last_message: Option<ThreadMetadataMessageContent>,
pub unread_messages_count: usize,
pub contact: Option<prelude::Contact>,
pub title: Option<String>,
pub archived: bool,
pub muted: bool,
}

#[derive(Deserialize, Serialize, Debug)]
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct ThreadMetadataMessageContent {
pub sender: prelude::ServiceAddress,
pub sender: prelude::Uuid,
pub timestamp: u64,
pub message: Option<String>,

Expand Down
67 changes: 63 additions & 4 deletions presage/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -857,8 +857,24 @@ impl<C: Store> Manager<C, Registered> {
log::trace!("{group:?}");
}
}
let thread = Thread::try_from(&content).unwrap();

let store = &mut state.config_store;

match store.thread_metadata(&thread) {
Ok(metadata) => {
if metadata.is_none() {
// Create a new thread metadata and save it
create_thread_metadata(store, &thread).ok()?;
}
}
Err(e) => {
log::error!("Error getting thread metadata: {}", e);
}

};
if let Err(e) =
save_message(&mut state.config_store, content.clone())
save_message(store, content.clone())
{
log::error!("Error saving message to store: {}", e);
}
Expand Down Expand Up @@ -1175,9 +1191,9 @@ impl<C: Store> Manager<C, Registered> {
}

// Saves the metadata for a thread.
pub async fn save_thread_metadata(
&mut self,
metadata: ThreadMetadata
pub fn save_thread_metadata(
&mut self,
metadata: ThreadMetadata,
) -> Result<(), Error<C::Error>> {
self.config_store.save_thread_metadata(metadata)?;
Ok(())
Expand Down Expand Up @@ -1292,3 +1308,46 @@ fn save_message<C: Store>(config_store: &mut C, message: Content) -> Result<(),
let thread = Thread::try_from(&message)?;
save_message_with_thread(config_store, message, thread)
}

pub fn save_thread_metadata<C: Store>(
config_store: &mut C,
metadata: ThreadMetadata,
) -> Result<(), Error<C::Error>> {
config_store.save_thread_metadata(metadata)?;
Ok(())
}

pub fn create_thread_metadata<C: Store>(
config_store: &mut C,
thread: &Thread,
) -> Result<ThreadMetadata, Error<C::Error>> {
let title = match thread {
Thread::Contact(uuid) => {
let contact = match config_store.contact_by_id(*uuid) {
Ok(contact) => contact,
Err(e) => {
log::info!("Error getting contact by id: {}, {:?}", e, uuid);
None
}
};
match contact {
Some(contact) => contact.name,
None => uuid.to_string(),
}
}
Thread::Group(id) => match config_store.group(*id)? {
Some(group) => group.title,
None => "".to_string(),
},
};
let metadata = ThreadMetadata {
thread: thread.clone(),
unread_messages_count: 0,
last_message: None,
title: Some(title),
archived: false,
muted: false,
};
save_thread_metadata(config_store, metadata.clone())?;
Ok(metadata)
}

0 comments on commit 677c2ea

Please sign in to comment.