Skip to content

Commit

Permalink
chore: Create compatibility for previous profiles without timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
dariusc93 committed Oct 12, 2023
1 parent 39e96f0 commit e487386
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
7 changes: 6 additions & 1 deletion extensions/warp-ipfs/examples/identity-interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
36 changes: 21 additions & 15 deletions extensions/warp-ipfs/src/store/document/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ pub struct IdentityDocument {

pub did: DID,

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

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

#[serde(skip_serializing_if = "Option::is_none")]
pub status_message: Option<String>,
Expand All @@ -49,8 +49,8 @@ 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();
let created = Some(identity.created());
let modified = Some(identity.modified());

IdentityDocument {
username,
Expand All @@ -70,11 +70,19 @@ impl From<Identity> for IdentityDocument {

impl From<IdentityDocument> 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
}
}
Expand Down Expand Up @@ -116,17 +124,15 @@ impl IdentityDocument {
impl IdentityDocument {
pub fn resolve(&self) -> Result<Identity, Error> {
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, Error> {
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);
Expand Down
6 changes: 4 additions & 2 deletions extensions/warp-ipfs/src/store/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit e487386

Please sign in to comment.