feat: improve chat state

This commit is contained in:
2025-02-10 09:03:29 +07:00
parent 6805ed8be5
commit c4573ef1da
13 changed files with 171 additions and 181 deletions

View File

@@ -1,4 +1,4 @@
use crate::{constants::IMAGE_SERVICE, utils::shorted_public_key};
use crate::constants::IMAGE_SERVICE;
use nostr_sdk::prelude::*;
#[derive(Debug, Clone)]
@@ -58,17 +58,18 @@ impl NostrProfile {
pub fn name(&self) -> String {
if let Some(display_name) = &self.metadata.display_name {
if !display_name.is_empty() {
return display_name.clone();
return display_name.to_owned();
}
}
if let Some(name) = &self.metadata.name {
if !name.is_empty() {
return name.clone();
return name.to_owned();
}
}
shorted_public_key(self.public_key)
let pubkey = self.public_key.to_string();
format!("{}:{}", &pubkey[0..4], &pubkey[pubkey.len() - 4..])
}
/// Get contact's metadata

View File

@@ -1,5 +1,4 @@
use crate::constants::NIP96_SERVER;
use chrono::{Datelike, Local, TimeZone};
use itertools::Itertools;
use nostr_sdk::prelude::*;
use rnglib::{Language, RNG};
@@ -25,13 +24,11 @@ pub async fn nip96_upload(client: &Client, file: Vec<u8>) -> anyhow::Result<Url,
Ok(url)
}
pub fn room_hash(tags: &Tags) -> u64 {
let pubkeys: Vec<&PublicKey> = tags.public_keys().unique_by(|&pubkey| pubkey).collect();
pub fn room_hash(event: &Event) -> u64 {
let pubkeys: Vec<&PublicKey> = event.tags.public_keys().unique().collect();
let mut hasher = DefaultHasher::new();
// Generate unique hash
pubkeys.hash(&mut hasher);
hasher.finish()
}
@@ -49,53 +46,3 @@ where
a == b
}
pub fn shorted_public_key(public_key: PublicKey) -> String {
let pk = public_key.to_string();
format!("{}:{}", &pk[0..4], &pk[pk.len() - 4..])
}
pub fn message_ago(time: Timestamp) -> String {
let now = Local::now();
let input_time = Local.timestamp_opt(time.as_u64() as i64, 0).unwrap();
let diff = (now - input_time).num_hours();
if diff < 24 {
let duration = now.signed_duration_since(input_time);
if duration.num_seconds() < 60 {
"now".to_string()
} else if duration.num_minutes() == 1 {
"1m".to_string()
} else if duration.num_minutes() < 60 {
format!("{}m", duration.num_minutes())
} else if duration.num_hours() == 1 {
"1h".to_string()
} else if duration.num_hours() < 24 {
format!("{}h", duration.num_hours())
} else if duration.num_days() == 1 {
"1d".to_string()
} else {
format!("{}d", duration.num_days())
}
} else {
input_time.format("%b %d").to_string()
}
}
pub fn message_time(time: Timestamp) -> String {
let now = Local::now();
let input_time = Local.timestamp_opt(time.as_u64() as i64, 0).unwrap();
if input_time.day() == now.day() {
format!("Today at {}", input_time.format("%H:%M %p"))
} else if input_time.day() == now.day() - 1 {
format!("Yesterday at {}", input_time.format("%H:%M %p"))
} else {
format!(
"{}, {}",
input_time.format("%d/%m/%y"),
input_time.format("%H:%M %p")
)
}
}