From 35c808876bac95484cbfd21b59b02f62cf78a34c Mon Sep 17 00:00:00 2001 From: Darius Date: Sun, 24 Sep 2023 10:47:00 -0400 Subject: [PATCH] refactor: Implement timestamp --- extensions/warp-ipfs/src/store/document.rs | 42 +++++++++++++++++-- .../warp-ipfs/src/store/document/identity.rs | 13 ++++++ extensions/warp-ipfs/src/store/identity.rs | 3 ++ warp/src/multipass/identity.rs | 23 ++++++++++ 4 files changed, 77 insertions(+), 4 deletions(-) diff --git a/extensions/warp-ipfs/src/store/document.rs b/extensions/warp-ipfs/src/store/document.rs index af8caad37..492adedf5 100644 --- a/extensions/warp-ipfs/src/store/document.rs +++ b/extensions/warp-ipfs/src/store/document.rs @@ -1,6 +1,7 @@ pub mod identity; pub mod utils; +use chrono::{DateTime, Utc}; use ipfs::{Ipfs, IpfsPath}; use libipld::{ serde::{from_ipld, to_ipld}, @@ -73,6 +74,8 @@ where #[derive(Debug, serde::Serialize, serde::Deserialize, Clone)] pub struct ExtractedRootDocument { pub identity: Identity, + pub created: DateTime, + pub modified: DateTime, pub friends: Vec, pub block_list: Vec, pub block_by_list: Vec, @@ -98,6 +101,13 @@ impl ExtractedRootDocument { pub struct RootDocument { /// Own Identity pub identity: Cid, + + #[serde(skip_serializing_if = "Option::is_none")] + pub created: Option>, + + #[serde(skip_serializing_if = "Option::is_none")] + pub modified: Option>, + /// array of friends (DID) #[serde(skip_serializing_if = "Option::is_none")] pub friends: Option, @@ -132,7 +142,7 @@ impl RootDocument { #[tracing::instrument(skip(self, ipfs))] pub async fn verify(&self, ipfs: &Ipfs) -> Result<(), Error> { - let (identity, _, _, _, _) = self.resolve(ipfs).await?; + let (identity, _, _, _, _, _, _) = self.resolve(ipfs).await?; let mut root_document = self.clone(); let signature = std::mem::take(&mut root_document.signature).ok_or(Error::InvalidSignature)?; @@ -150,7 +160,18 @@ impl RootDocument { pub async fn resolve( &self, ipfs: &Ipfs, - ) -> Result<(Identity, Vec, Vec, Vec, Vec), Error> { + ) -> Result< + ( + Identity, + Option>, + Option>, + Vec, + Vec, + Vec, + Vec, + ), + Error, + > { let identity = match ipfs .dag() .get(IpfsPath::from(self.identity), &[], true) @@ -184,7 +205,15 @@ impl RootDocument { request = document.get_local_dag(ipfs).await.unwrap_or_default(); } - Ok((identity, friends, block_list, block_by_list, request)) + Ok(( + identity, + self.created, + self.modified, + friends, + block_list, + block_by_list, + request, + )) } pub async fn import(ipfs: &Ipfs, data: ExtractedRootDocument) -> Result { @@ -218,6 +247,8 @@ impl RootDocument { let mut root_document = RootDocument { identity, + created: Some(data.created), + modified: Some(data.modified), friends, blocks, block_by, @@ -231,10 +262,13 @@ impl RootDocument { } pub async fn export(&self, ipfs: &Ipfs) -> Result { - let (identity, friends, block_list, block_by_list, request) = self.resolve(ipfs).await?; + let (identity, created, modified, friends, block_list, block_by_list, request) = + self.resolve(ipfs).await?; let mut exported = ExtractedRootDocument { identity, + created: created.unwrap_or_default(), + modified: modified.unwrap_or_default(), friends, block_list, block_by_list, diff --git a/extensions/warp-ipfs/src/store/document/identity.rs b/extensions/warp-ipfs/src/store/document/identity.rs index 2ccd7c7d4..f8418b207 100644 --- a/extensions/warp-ipfs/src/store/document/identity.rs +++ b/extensions/warp-ipfs/src/store/document/identity.rs @@ -1,3 +1,4 @@ +use chrono::{DateTime, Utc}; use futures::StreamExt; use libipld::Cid; use rust_ipfs::{Ipfs, IpfsPath}; @@ -17,6 +18,12 @@ pub struct IdentityDocument { pub did: DID, + #[serde(default)] + pub created: DateTime, + + #[serde(default)] + pub modified: DateTime, + #[serde(skip_serializing_if = "Option::is_none")] pub status_message: Option, @@ -42,11 +49,16 @@ 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(); + IdentityDocument { username, short_id, did, status_message, + created, + modified, profile_picture: None, profile_banner: None, platform: None, @@ -114,6 +126,7 @@ impl IdentityDocument { pub fn sign(mut self, did: &DID) -> Result { self.signature = None; + self.modified = 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 6680f4df5..1102d9445 100644 --- a/extensions/warp-ipfs/src/store/identity.rs +++ b/extensions/warp-ipfs/src/store/identity.rs @@ -5,6 +5,7 @@ use crate::{ config::{DefaultPfpFn, Discovery as DiscoveryConfig, UpdateEvents}, store::{did_to_libp2p_pub, discovery::Discovery, PeerIdExt, PeerTopic, VecExt}, }; +use chrono::Utc; use futures::{ channel::{mpsc, oneshot}, stream::BoxStream, @@ -1452,6 +1453,8 @@ impl IdentityStore { .try_into() .map_err(anyhow::Error::from)?, did: public_key.into(), + created: Utc::now(), + modified: Utc::now(), status_message: None, profile_banner: None, profile_picture: None, diff --git a/warp/src/multipass/identity.rs b/warp/src/multipass/identity.rs index c629fcefe..79c4b5a61 100644 --- a/warp/src/multipass/identity.rs +++ b/warp/src/multipass/identity.rs @@ -1,6 +1,7 @@ use std::fmt::Display; use crate::crypto::DID; +use chrono::{DateTime, Utc}; use derive_more::Display; use serde::{Deserialize, Serialize}; use warp_derive::FFIFree; @@ -160,6 +161,12 @@ pub struct Identity { /// Public key for the identity did_key: DID, + /// Timestamp when the identity was created + created: DateTime, + + /// Timestamp when the identity was last modified or updated + modified: DateTime, + /// Status message status_message: Option, } @@ -180,6 +187,14 @@ impl Identity { pub fn set_status_message(&mut self, message: Option) { self.status_message = message } + + pub fn set_created(&mut self, time: DateTime) { + self.created = time; + } + + pub fn set_modified(&mut self, time: DateTime) { + self.modified = time; + } } impl Identity { @@ -198,6 +213,14 @@ impl Identity { pub fn status_message(&self) -> Option { self.status_message.clone() } + + pub fn created(&self) -> DateTime { + self.created + } + + pub fn modified(&self) -> DateTime { + self.modified + } } #[derive(Debug, Clone, FFIFree)]