Skip to content

Commit

Permalink
chore: Implement conversation timestamp (#343)
Browse files Browse the repository at this point in the history
  • Loading branch information
dariusc93 authored Oct 22, 2023
1 parent 2360aab commit 4247e1b
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 90 deletions.
6 changes: 4 additions & 2 deletions extensions/warp-ipfs/examples/messenger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,15 +439,17 @@ async fn main() -> anyhow::Result<()> {
}
Some("/list-conversations") => {
let mut table = Table::new();
table.set_header(vec!["Name", "ID", "Recipients"]);
table.set_header(vec!["Name", "ID", "Created", "Updated", "Recipients"]);
let list = chat.list_conversations().await?;
for convo in list.iter() {
let mut recipients = vec![];
for recipient in convo.recipients() {
let username = get_username(new_account.clone(), recipient.clone()).await.unwrap_or_else(|_| recipient.to_string());
recipients.push(username);
}
table.add_row(vec![convo.name().unwrap_or_default(), convo.id().to_string(), recipients.join(",").to_string()]);
let created = convo.created();
let modified = convo.modified();
table.add_row(vec![convo.name().unwrap_or_default(), convo.id().to_string(), created.to_string(), modified.to_string(), recipients.join(",").to_string()]);
}
writeln!(stdout, "{table}")?;
},
Expand Down
28 changes: 20 additions & 8 deletions extensions/warp-ipfs/src/store/conversation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ pub struct ConversationDocument {
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub creator: Option<DID>,
pub created: DateTime<Utc>,
pub modified: DateTime<Utc>,
pub conversation_type: ConversationType,
pub recipients: Vec<DID>,
pub excluded: HashMap<DID, String>,
Expand All @@ -57,7 +59,8 @@ impl From<&Conversation> for ConversationDocument {
id: conversation.id(),
name: conversation.name(),
creator: conversation.creator(),

created: conversation.created(),
modified: conversation.modified(),
conversation_type: conversation.conversation_type(),
recipients: conversation.recipients(),
excluded: Default::default(),
Expand Down Expand Up @@ -130,12 +133,15 @@ impl ConversationDocument {
}

impl ConversationDocument {
#[allow(clippy::too_many_arguments)]
pub fn new(
did: &DID,
name: Option<String>,
mut recipients: Vec<DID>,
id: Option<Uuid>,
conversation_type: ConversationType,
created: Option<DateTime<Utc>>,
modified: Option<DateTime<Utc>>,
creator: Option<DID>,
signature: Option<String>,
) -> Result<Self, Error> {
Expand All @@ -152,11 +158,16 @@ impl ConversationDocument {
let messages = None;
let excluded = Default::default();

let created = created.unwrap_or(Utc::now());
let modified = modified.unwrap_or(created);

let mut document = Self {
id,
name,
recipients,
creator,
created,
modified,
conversation_type,
excluded,
messages,
Expand Down Expand Up @@ -196,6 +207,8 @@ impl ConversationDocument {
ConversationType::Direct,
None,
None,
None,
None,
)
}

Expand All @@ -207,6 +220,8 @@ impl ConversationDocument {
recipients.to_vec(),
conversation_id,
ConversationType::Group,
None,
None,
Some(did.clone()),
None,
)
Expand Down Expand Up @@ -301,6 +316,7 @@ impl ConversationDocument {
ipfs: &Ipfs,
list: BTreeSet<MessageDocument>,
) -> Result<(), Error> {
self.modified = Utc::now();
let cid = list.to_cid(ipfs).await?;
self.messages = Some(cid);
Ok(())
Expand Down Expand Up @@ -534,13 +550,7 @@ impl ConversationDocument {

impl From<ConversationDocument> for Conversation {
fn from(document: ConversationDocument) -> Self {
let mut conversation = Conversation::default();
conversation.set_id(document.id);
conversation.set_name(document.name());
conversation.set_creator(document.creator.clone());
conversation.set_conversation_type(document.conversation_type);
conversation.set_recipients(document.recipients());
conversation
Conversation::from(&document)
}
}

Expand All @@ -552,6 +562,8 @@ impl From<&ConversationDocument> for Conversation {
conversation.set_creator(document.creator.clone());
conversation.set_conversation_type(document.conversation_type);
conversation.set_recipients(document.recipients());
conversation.set_created(document.created);
conversation.set_modified(document.modified);
conversation
}
}
Expand Down
Loading

0 comments on commit 4247e1b

Please sign in to comment.