Skip to content

Commit

Permalink
Add Profile
Browse files Browse the repository at this point in the history
  • Loading branch information
akiomik committed Jan 13, 2024
1 parent dada62b commit 61ed045
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/nostr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ mod event;
mod metadata;
pub mod nip10;
pub mod nip27;
mod profile;

pub use connection::Connection;
pub use connection_process::ConnectionProcess;
pub use event::SortableEvent;
pub use metadata::Metadata;
pub use profile::Profile;
2 changes: 1 addition & 1 deletion src/nostr/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};

use nostr_sdk::{JsonUtil, Metadata as NostrMetadata};
use nostr_sdk::prelude::{Metadata as NostrMetadata, *};

#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Metadata {
Expand Down
33 changes: 33 additions & 0 deletions src/nostr/profile.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use nostr_sdk::prelude::*;

use crate::nostr::Metadata;
use crate::text::shorten_hex;

pub struct Profile {
pub pubkey: XOnlyPublicKey,
pub created_at: Timestamp,
pub metadata: Metadata,
}

impl Profile {
pub fn new(pubkey: XOnlyPublicKey, created_at: Timestamp, metadata: Metadata) -> Self {
Self {
pubkey,
created_at,
metadata,
}
}

pub fn name(&self) -> String {
match (
self.metadata.display_name.clone(),
self.metadata.name.clone(),
self.pubkey.to_bech32(),
) {
(Some(display_name), _, _) if !display_name.is_empty() => display_name,
(_, Some(name), _) if !name.is_empty() => format!("@{name}"),
(_, _, Ok(npub)) => npub,
_ => shorten_hex(&self.pubkey.to_string()),
}
}
}

0 comments on commit 61ed045

Please sign in to comment.