wip: refactor
This commit is contained in:
14
crates/common/Cargo.toml
Normal file
14
crates/common/Cargo.toml
Normal file
@@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "common"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
publish = false
|
||||
|
||||
[dependencies]
|
||||
gpui.workspace = true
|
||||
nostr-sdk.workspace = true
|
||||
anyhow.workspace = true
|
||||
itertools.workspace = true
|
||||
chrono.workspace = true
|
||||
|
||||
random_name_generator = "0.3.6"
|
||||
13
crates/common/src/constants.rs
Normal file
13
crates/common/src/constants.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
pub const KEYRING_SERVICE: &str = "Coop Safe Storage";
|
||||
pub const APP_NAME: &str = "Coop";
|
||||
pub const APP_ID: &str = "su.reya.coop";
|
||||
pub const FAKE_SIG: &str = "f9e79d141c004977192d05a86f81ec7c585179c371f7350a5412d33575a2a356433f58e405c2296ed273e2fe0aafa25b641e39cc4e1f3f261ebf55bce0cbac83";
|
||||
pub const NEW_MESSAGE_SUB_ID: &str = "listen_new_giftwrap";
|
||||
pub const ALL_MESSAGES_SUB_ID: &str = "listen_all_giftwraps";
|
||||
pub const METADATA_DELAY: u64 = 200;
|
||||
|
||||
/// Image Resizer Service
|
||||
pub const IMAGE_SERVICE: &str = "https://wsrv.nl";
|
||||
|
||||
/// NIP96 Media Server
|
||||
pub const NIP96_SERVER: &str = "https://nostrcheck.me";
|
||||
2
crates/common/src/lib.rs
Normal file
2
crates/common/src/lib.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod constants;
|
||||
pub mod utils;
|
||||
92
crates/common/src/utils.rs
Normal file
92
crates/common/src/utils.rs
Normal file
@@ -0,0 +1,92 @@
|
||||
use crate::constants::NIP96_SERVER;
|
||||
use chrono::{Datelike, Local, TimeZone};
|
||||
use nostr_sdk::prelude::*;
|
||||
use rnglib::{Language, RNG};
|
||||
use std::{
|
||||
collections::HashSet,
|
||||
hash::{DefaultHasher, Hash, Hasher},
|
||||
};
|
||||
|
||||
pub async fn nip96_upload(client: &Client, file: Vec<u8>) -> anyhow::Result<Url, anyhow::Error> {
|
||||
let signer = client.signer().await?;
|
||||
let server_url = Url::parse(NIP96_SERVER)?;
|
||||
|
||||
let config: ServerConfig = nip96::get_server_config(server_url, None).await?;
|
||||
let url = nip96::upload_data(&signer, &config, file, None, None).await?;
|
||||
|
||||
Ok(url)
|
||||
}
|
||||
|
||||
pub fn room_hash(tags: &Tags) -> u64 {
|
||||
let pubkeys: Vec<PublicKey> = tags.public_keys().copied().collect();
|
||||
let mut hasher = DefaultHasher::new();
|
||||
// Generate unique hash
|
||||
pubkeys.hash(&mut hasher);
|
||||
|
||||
hasher.finish()
|
||||
}
|
||||
|
||||
pub fn random_name(length: usize) -> String {
|
||||
let rng = RNG::from(&Language::Roman);
|
||||
rng.generate_names(length, true).join("-").to_lowercase()
|
||||
}
|
||||
|
||||
pub fn compare<T>(a: &[T], b: &[T]) -> bool
|
||||
where
|
||||
T: Eq + Hash,
|
||||
{
|
||||
let a: HashSet<_> = a.iter().collect();
|
||||
let b: HashSet<_> = b.iter().collect();
|
||||
|
||||
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")
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user