From e487386fec511967fc730eb16c3e958210289ad4 Mon Sep 17 00:00:00 2001 From: Darius Date: Thu, 12 Oct 2023 13:28:41 -0400 Subject: [PATCH] chore: Create compatibility for previous profiles without timestamp --- .../warp-ipfs/examples/identity-interface.rs | 7 +++- .../warp-ipfs/src/store/document/identity.rs | 36 +++++++++++-------- extensions/warp-ipfs/src/store/identity.rs | 6 ++-- 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/extensions/warp-ipfs/examples/identity-interface.rs b/extensions/warp-ipfs/examples/identity-interface.rs index 46f3b18c8..94d563061 100644 --- a/extensions/warp-ipfs/examples/identity-interface.rs +++ b/extensions/warp-ipfs/examples/identity-interface.rs @@ -750,15 +750,20 @@ async fn main() -> anyhow::Result<()> { } }; let mut table = Table::new(); - table.set_header(vec!["Username", "Public Key", "Status Message", "Banner", "Picture", "Platform", "Status"]); + table.set_header(vec!["Username", "Public Key", "Created", "Last Updated", "Status Message", "Banner", "Picture", "Platform", "Status"]); for identity in idents { let status = account.identity_status(&identity.did_key()).await.unwrap_or(IdentityStatus::Offline); let platform = account.identity_platform(&identity.did_key()).await.unwrap_or_default(); let profile_picture = account.identity_picture(&identity.did_key()).await.unwrap_or_default(); let profile_banner = account.identity_banner(&identity.did_key()).await.unwrap_or_default(); + let created = identity.created(); + let modified = identity.modified(); + table.add_row(vec![ identity.username(), identity.did_key().to_string(), + created.to_string(), + modified.to_string(), identity.status_message().unwrap_or_default(), (!profile_banner.is_empty()).to_string(), (!profile_picture.is_empty()).to_string(), diff --git a/extensions/warp-ipfs/src/store/document/identity.rs b/extensions/warp-ipfs/src/store/document/identity.rs index e022d1ad7..b2d397483 100644 --- a/extensions/warp-ipfs/src/store/document/identity.rs +++ b/extensions/warp-ipfs/src/store/document/identity.rs @@ -18,11 +18,11 @@ pub struct IdentityDocument { pub did: DID, - #[serde(default)] - pub created: DateTime, + #[serde(skip_serializing_if = "Option::is_none")] + pub created: Option>, - #[serde(default)] - pub modified: DateTime, + #[serde(skip_serializing_if = "Option::is_none")] + pub modified: Option>, #[serde(skip_serializing_if = "Option::is_none")] pub status_message: Option, @@ -49,8 +49,8 @@ impl From for IdentityDocument { let did = identity.did_key(); let short_id = *identity.short_id(); let status_message = identity.status_message(); - let created = identity.created(); - let modified = identity.modified(); + let created = Some(identity.created()); + let modified = Some(identity.modified()); IdentityDocument { username, @@ -70,11 +70,19 @@ impl From for IdentityDocument { impl From for Identity { fn from(document: IdentityDocument) -> Self { + Self::from(&document) + } +} + +impl From<&IdentityDocument> for Identity { + fn from(document: &IdentityDocument) -> Self { let mut identity = Identity::default(); - identity.set_did_key(document.did); + identity.set_did_key(document.did.clone()); identity.set_short_id(document.short_id); - identity.set_status_message(document.status_message); + identity.set_status_message(document.status_message.clone()); identity.set_username(&document.username); + identity.set_created(document.created.unwrap_or(Utc::now())); + identity.set_modified(document.modified.unwrap_or(Utc::now())); identity } } @@ -116,17 +124,15 @@ impl IdentityDocument { impl IdentityDocument { pub fn resolve(&self) -> Result { self.verify()?; - let mut identity = Identity::default(); - identity.set_username(&self.username); - identity.set_did_key(self.did.clone()); - identity.set_short_id(self.short_id); - identity.set_status_message(self.status_message.clone()); - Ok(identity) + Ok(self.into()) } pub fn sign(mut self, did: &DID) -> Result { self.signature = None; - self.modified = Utc::now(); + if self.created.is_none() { + self.created = Some(Utc::now()); + } + self.modified = Some(Utc::now()); let bytes = serde_json::to_vec(&self)?; let signature = bs58::encode(did.sign(&bytes)).into_string(); self.signature = Some(signature); diff --git a/extensions/warp-ipfs/src/store/identity.rs b/extensions/warp-ipfs/src/store/identity.rs index 457fc849f..10547ca74 100644 --- a/extensions/warp-ipfs/src/store/identity.rs +++ b/extensions/warp-ipfs/src/store/identity.rs @@ -1257,14 +1257,16 @@ impl IdentityStore { let fingerprint = public_key.fingerprint(); let bytes = fingerprint.as_bytes(); + let time = Utc::now(); + let identity = IdentityDocument { username, short_id: bytes[bytes.len() - SHORT_ID_SIZE..] .try_into() .map_err(anyhow::Error::from)?, did: public_key.into(), - created: Utc::now(), - modified: Utc::now(), + created: Some(time), + modified: Some(time), status_message: None, profile_banner: None, profile_picture: None,