Skip to content

Commit

Permalink
refactor: Implement timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
dariusc93 committed Sep 24, 2023
1 parent 50ac9e0 commit 35c8088
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
42 changes: 38 additions & 4 deletions extensions/warp-ipfs/src/store/document.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -73,6 +74,8 @@ where
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
pub struct ExtractedRootDocument {
pub identity: Identity,
pub created: DateTime<Utc>,
pub modified: DateTime<Utc>,
pub friends: Vec<DID>,
pub block_list: Vec<DID>,
pub block_by_list: Vec<DID>,
Expand All @@ -98,6 +101,13 @@ impl ExtractedRootDocument {
pub struct RootDocument {
/// Own Identity
pub identity: Cid,

#[serde(skip_serializing_if = "Option::is_none")]
pub created: Option<DateTime<Utc>>,

#[serde(skip_serializing_if = "Option::is_none")]
pub modified: Option<DateTime<Utc>>,

/// array of friends (DID)
#[serde(skip_serializing_if = "Option::is_none")]
pub friends: Option<Cid>,
Expand Down Expand Up @@ -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)?;
Expand All @@ -150,7 +160,18 @@ impl RootDocument {
pub async fn resolve(
&self,
ipfs: &Ipfs,
) -> Result<(Identity, Vec<DID>, Vec<DID>, Vec<DID>, Vec<Request>), Error> {
) -> Result<
(
Identity,
Option<DateTime<Utc>>,
Option<DateTime<Utc>>,
Vec<DID>,
Vec<DID>,
Vec<DID>,
Vec<Request>,
),
Error,
> {
let identity = match ipfs
.dag()
.get(IpfsPath::from(self.identity), &[], true)
Expand Down Expand Up @@ -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<Self, Error> {
Expand Down Expand Up @@ -218,6 +247,8 @@ impl RootDocument {

let mut root_document = RootDocument {
identity,
created: Some(data.created),
modified: Some(data.modified),
friends,
blocks,
block_by,
Expand All @@ -231,10 +262,13 @@ impl RootDocument {
}

pub async fn export(&self, ipfs: &Ipfs) -> Result<ExtractedRootDocument, Error> {
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,
Expand Down
13 changes: 13 additions & 0 deletions extensions/warp-ipfs/src/store/document/identity.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use chrono::{DateTime, Utc};
use futures::StreamExt;
use libipld::Cid;
use rust_ipfs::{Ipfs, IpfsPath};
Expand All @@ -17,6 +18,12 @@ pub struct IdentityDocument {

pub did: DID,

#[serde(default)]
pub created: DateTime<Utc>,

#[serde(default)]
pub modified: DateTime<Utc>,

#[serde(skip_serializing_if = "Option::is_none")]
pub status_message: Option<String>,

Expand All @@ -42,11 +49,16 @@ impl From<Identity> 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,
Expand Down Expand Up @@ -114,6 +126,7 @@ impl IdentityDocument {

pub fn sign(mut self, did: &DID) -> Result<Self, Error> {
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);
Expand Down
3 changes: 3 additions & 0 deletions extensions/warp-ipfs/src/store/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
23 changes: 23 additions & 0 deletions warp/src/multipass/identity.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -160,6 +161,12 @@ pub struct Identity {
/// Public key for the identity
did_key: DID,

/// Timestamp when the identity was created
created: DateTime<Utc>,

/// Timestamp when the identity was last modified or updated
modified: DateTime<Utc>,

/// Status message
status_message: Option<String>,
}
Expand All @@ -180,6 +187,14 @@ impl Identity {
pub fn set_status_message(&mut self, message: Option<String>) {
self.status_message = message
}

pub fn set_created(&mut self, time: DateTime<Utc>) {
self.created = time;
}

pub fn set_modified(&mut self, time: DateTime<Utc>) {
self.modified = time;
}
}

impl Identity {
Expand All @@ -198,6 +213,14 @@ impl Identity {
pub fn status_message(&self) -> Option<String> {
self.status_message.clone()
}

pub fn created(&self) -> DateTime<Utc> {
self.created
}

pub fn modified(&self) -> DateTime<Utc> {
self.modified
}
}

#[derive(Debug, Clone, FFIFree)]
Expand Down

0 comments on commit 35c8088

Please sign in to comment.