Skip to content

Commit

Permalink
refactor: Move conversation logic into a single task and save list to…
Browse files Browse the repository at this point in the history
… a single id
  • Loading branch information
dariusc93 committed Oct 17, 2023
1 parent eeb7d14 commit 8b8b55c
Show file tree
Hide file tree
Showing 4 changed files with 510 additions and 486 deletions.
52 changes: 18 additions & 34 deletions extensions/warp-ipfs/src/store/conversation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,20 +301,20 @@ impl ConversationDocument {
ipfs: &Ipfs,
list: BTreeSet<MessageDocument>,
) -> Result<(), Error> {
let old_cid = self.messages;
// let old_cid = self.messages;
let cid = list.to_cid(ipfs).await?;
if !ipfs.is_pinned(&cid).await? {
ipfs.insert_pin(&cid, false).await?;
}

if let Some(old_cid) = old_cid {
if old_cid != cid {
if ipfs.is_pinned(&old_cid).await? {
ipfs.remove_pin(&old_cid, false).await?;
}
ipfs.remove_block(old_cid).await?;
}
}
// if let Some(old_cid) = old_cid {
// if old_cid != cid {
// if ipfs.is_pinned(&old_cid).await? {
// ipfs.remove_pin(&old_cid, false).await?;
// }
// ipfs.remove_block(old_cid).await?;
// }
// }

self.messages = Some(cid);

Expand Down Expand Up @@ -524,7 +524,7 @@ impl ConversationDocument {
.ok_or(Error::MessageNotFound)?;
messages.remove(&document);
self.set_message_list(ipfs, messages).await?;
document.remove(ipfs).await
Ok(())
}

pub async fn delete_all_message(&mut self, ipfs: Ipfs) -> Result<(), Error> {
Expand Down Expand Up @@ -632,10 +632,6 @@ impl MessageDocument {

let message = data.to_cid(ipfs).await?;

if !ipfs.is_pinned(&message).await? {
ipfs.insert_pin(&message, false).await?;
}

let sender = DIDEd25519Reference::from_did(&sender);

let document = MessageDocument {
Expand All @@ -649,15 +645,15 @@ impl MessageDocument {
Ok(document)
}

pub async fn remove(&self, ipfs: &Ipfs) -> Result<(), Error> {
let cid = self.message;
if ipfs.is_pinned(&cid).await? {
ipfs.remove_pin(&cid, false).await?;
}
ipfs.remove_block(cid).await?;
// pub async fn remove(&self, ipfs: &Ipfs) -> Result<(), Error> {
// let cid = self.message;
// if ipfs.is_pinned(&cid).await? {
// ipfs.remove_pin(&cid, false).await?;
// }
// ipfs.remove_block(cid).await?;

Ok(())
}
// Ok(())
// }

pub async fn update(
&mut self,
Expand All @@ -668,7 +664,6 @@ impl MessageDocument {
) -> Result<(), Error> {
info!("Updating message {} for {}", self.id, self.conversation_id);
let old_message = self.resolve(ipfs, did, keystore).await?;
let old_document = self.message;

if old_message.id() != message.id()
|| old_message.conversation_id() != message.conversation_id()
Expand All @@ -690,20 +685,9 @@ impl MessageDocument {

let message_cid = data.to_cid(ipfs).await?;

if !ipfs.is_pinned(&message_cid).await? {
ipfs.insert_pin(&message_cid, false).await?;
}

info!("Setting Message to document");
self.message = message_cid;
info!("Message is updated");
if old_document != message_cid {
if ipfs.is_pinned(&old_document).await? {
info!("Removing pin for {old_document}");
ipfs.remove_pin(&old_document, false).await?;
}
ipfs.remove_block(old_document).await?;
}
Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions extensions/warp-ipfs/src/store/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod cache;
pub mod identity;
pub mod root;
pub mod utils;
pub mod conversation;

use chrono::{DateTime, Utc};
use futures::TryFutureExt;
Expand Down
Loading

0 comments on commit 8b8b55c

Please sign in to comment.