wip: refactor

This commit is contained in:
2025-02-01 08:30:24 +07:00
parent 82f18fc478
commit a61fd27b9d
25 changed files with 547 additions and 507 deletions

View File

@@ -1,2 +1,3 @@
pub mod constants;
pub mod profile;
pub mod utils;

View File

@@ -0,0 +1,62 @@
use crate::{constants::IMAGE_SERVICE, utils::shorted_public_key};
use nostr_sdk::prelude::*;
#[derive(Debug, Clone)]
pub struct NostrProfile {
public_key: PublicKey,
metadata: Metadata,
}
impl PartialEq for NostrProfile {
fn eq(&self, other: &Self) -> bool {
self.public_key() == other.public_key()
}
}
impl NostrProfile {
pub fn new(public_key: PublicKey, metadata: Metadata) -> Self {
Self {
public_key,
metadata,
}
}
/// Get contact's public key
pub fn public_key(&self) -> PublicKey {
self.public_key
}
/// Set contact's metadata
pub fn metadata(&mut self, metadata: &Metadata) {
self.metadata = metadata.clone()
}
/// Get contact's avatar
pub fn avatar(&self) -> String {
if let Some(picture) = &self.metadata.picture {
format!(
"{}/?url={}&w=100&h=100&fit=cover&mask=circle&n=-1",
IMAGE_SERVICE, picture
)
} else {
"brand/avatar.png".into()
}
}
/// Get contact's name, fallback to public key as shorted format
pub fn name(&self) -> String {
if let Some(display_name) = &self.metadata.display_name {
if !display_name.is_empty() {
return display_name.clone();
}
}
if let Some(name) = &self.metadata.name {
if !name.is_empty() {
return name.clone();
}
}
shorted_public_key(self.public_key)
}
}