wip: refactor
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
use gpui::*;
|
use gpui::{AssetSource, Result, SharedString};
|
||||||
use rust_embed::RustEmbed;
|
use rust_embed::RustEmbed;
|
||||||
|
|
||||||
#[derive(RustEmbed)]
|
#[derive(RustEmbed)]
|
||||||
|
|||||||
@@ -3,5 +3,5 @@ pub const APP_NAME: &str = "Coop";
|
|||||||
pub const FAKE_SIG: &str = "f9e79d141c004977192d05a86f81ec7c585179c371f7350a5412d33575a2a356433f58e405c2296ed273e2fe0aafa25b641e39cc4e1f3f261ebf55bce0cbac83";
|
pub const FAKE_SIG: &str = "f9e79d141c004977192d05a86f81ec7c585179c371f7350a5412d33575a2a356433f58e405c2296ed273e2fe0aafa25b641e39cc4e1f3f261ebf55bce0cbac83";
|
||||||
pub const NEW_MESSAGE_SUB_ID: &str = "listen_new_giftwrap";
|
pub const NEW_MESSAGE_SUB_ID: &str = "listen_new_giftwrap";
|
||||||
pub const ALL_MESSAGES_SUB_ID: &str = "listen_all_giftwraps";
|
pub const ALL_MESSAGES_SUB_ID: &str = "listen_all_giftwraps";
|
||||||
pub const METADATA_DELAY: u64 = 150;
|
pub const METADATA_DELAY: u64 = 100;
|
||||||
pub const IMAGE_SERVICE: &str = "https://wsrv.nl";
|
pub const IMAGE_SERVICE: &str = "https://wsrv.nl";
|
||||||
|
|||||||
@@ -1,7 +1,14 @@
|
|||||||
use asset::Assets;
|
use asset::Assets;
|
||||||
|
use constants::{
|
||||||
|
ALL_MESSAGES_SUB_ID, APP_NAME, FAKE_SIG, KEYRING_SERVICE, METADATA_DELAY, NEW_MESSAGE_SUB_ID,
|
||||||
|
};
|
||||||
use dirs::config_dir;
|
use dirs::config_dir;
|
||||||
use gpui::*;
|
use gpui::{
|
||||||
|
actions, point, px, size, App, AppContext, Bounds, SharedString, TitlebarOptions,
|
||||||
|
VisualContext, WindowBounds, WindowDecorations, WindowKind, WindowOptions,
|
||||||
|
};
|
||||||
use nostr_sdk::prelude::*;
|
use nostr_sdk::prelude::*;
|
||||||
|
use states::{account::AccountRegistry, chat::ChatRegistry};
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashSet,
|
collections::HashSet,
|
||||||
fs,
|
fs,
|
||||||
@@ -13,20 +20,9 @@ use tokio::{
|
|||||||
sync::{mpsc, Mutex},
|
sync::{mpsc, Mutex},
|
||||||
time::sleep,
|
time::sleep,
|
||||||
};
|
};
|
||||||
|
|
||||||
use constants::{
|
|
||||||
ALL_MESSAGES_SUB_ID, APP_NAME, FAKE_SIG, KEYRING_SERVICE, METADATA_DELAY, NEW_MESSAGE_SUB_ID,
|
|
||||||
};
|
|
||||||
use ui::Root;
|
use ui::Root;
|
||||||
use views::app::AppView;
|
use views::app::AppView;
|
||||||
|
|
||||||
use states::{
|
|
||||||
account::AccountRegistry,
|
|
||||||
chat::ChatRegistry,
|
|
||||||
metadata::MetadataRegistry,
|
|
||||||
signal::{Signal, SignalRegistry},
|
|
||||||
};
|
|
||||||
|
|
||||||
mod asset;
|
mod asset;
|
||||||
mod constants;
|
mod constants;
|
||||||
mod states;
|
mod states;
|
||||||
@@ -38,6 +34,14 @@ actions!(app, [ReloadMetadata]);
|
|||||||
|
|
||||||
static CLIENT: OnceLock<Client> = OnceLock::new();
|
static CLIENT: OnceLock<Client> = OnceLock::new();
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub enum Signal {
|
||||||
|
/// Receive event
|
||||||
|
Event(Event),
|
||||||
|
/// Receive EOSE
|
||||||
|
Eose,
|
||||||
|
}
|
||||||
|
|
||||||
pub fn initialize_client() {
|
pub fn initialize_client() {
|
||||||
// Setup app data folder
|
// Setup app data folder
|
||||||
let config_dir = config_dir().expect("Config directory not found");
|
let config_dir = config_dir().expect("Config directory not found");
|
||||||
@@ -84,14 +88,11 @@ async fn main() {
|
|||||||
_ = client.connect().await;
|
_ = client.connect().await;
|
||||||
|
|
||||||
// Signal
|
// Signal
|
||||||
let (signal_tx, mut signal_rx) = mpsc::channel::<Signal>(4096); // TODO: adjust?
|
let (signal_tx, mut signal_rx) = mpsc::channel::<Signal>(4096);
|
||||||
let (mta_tx, mut mta_rx) = mpsc::unbounded_channel::<PublicKey>();
|
let (mta_tx, mut mta_rx) = mpsc::channel::<PublicKey>(4096);
|
||||||
|
|
||||||
// Re use sender
|
|
||||||
let mta_tx_clone = mta_tx.clone();
|
|
||||||
|
|
||||||
// Handle notification from Relays
|
// Handle notification from Relays
|
||||||
// Send notfiy back to GPUI
|
// Send notify back to GPUI
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
let sig = Signature::from_str(FAKE_SIG).unwrap();
|
let sig = Signature::from_str(FAKE_SIG).unwrap();
|
||||||
let new_message = SubscriptionId::new(NEW_MESSAGE_SUB_ID);
|
let new_message = SubscriptionId::new(NEW_MESSAGE_SUB_ID);
|
||||||
@@ -107,20 +108,23 @@ async fn main() {
|
|||||||
{
|
{
|
||||||
if event.kind == Kind::GiftWrap {
|
if event.kind == Kind::GiftWrap {
|
||||||
match client.unwrap_gift_wrap(&event).await {
|
match client.unwrap_gift_wrap(&event).await {
|
||||||
Ok(UnwrappedGift { rumor, .. }) => {
|
Ok(UnwrappedGift { mut rumor, sender }) => {
|
||||||
let mut rumor_clone = rumor.clone();
|
// Request metadata
|
||||||
|
if let Err(e) = mta_tx.send(sender).await {
|
||||||
|
println!("Send error: {}", e)
|
||||||
|
};
|
||||||
|
|
||||||
// Compute event id if not exist
|
// Compute event id if not exist
|
||||||
rumor_clone.ensure_id();
|
rumor.ensure_id();
|
||||||
|
|
||||||
if let Some(id) = rumor_clone.id {
|
if let Some(id) = rumor.id {
|
||||||
let ev = Event::new(
|
let ev = Event::new(
|
||||||
id,
|
id,
|
||||||
rumor_clone.pubkey,
|
rumor.pubkey,
|
||||||
rumor_clone.created_at,
|
rumor.created_at,
|
||||||
rumor_clone.kind,
|
rumor.kind,
|
||||||
rumor_clone.tags,
|
rumor.tags,
|
||||||
rumor_clone.content,
|
rumor.content,
|
||||||
sig,
|
sig,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -139,10 +143,6 @@ async fn main() {
|
|||||||
}
|
}
|
||||||
Err(e) => println!("Unwrap error: {}", e),
|
Err(e) => println!("Unwrap error: {}", e),
|
||||||
}
|
}
|
||||||
} else if event.kind == Kind::Metadata {
|
|
||||||
if let Err(e) = signal_tx.send(Signal::Metadata(event.pubkey)).await {
|
|
||||||
println!("Send error: {}", e)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if let RelayMessage::EndOfStoredEvents(subscription_id) = message {
|
} else if let RelayMessage::EndOfStoredEvents(subscription_id) = message {
|
||||||
if subscription_id == all_messages {
|
if subscription_id == all_messages {
|
||||||
@@ -183,10 +183,7 @@ async fn main() {
|
|||||||
.kind(Kind::Metadata)
|
.kind(Kind::Metadata)
|
||||||
.limit(total);
|
.limit(total);
|
||||||
|
|
||||||
let opts = SubscribeAutoCloseOptions::default()
|
if let Err(e) = client.sync(filter, &SyncOptions::default()).await {
|
||||||
.exit_policy(ReqExitPolicy::WaitDurationAfterEOSE(Duration::from_secs(2)));
|
|
||||||
|
|
||||||
if let Err(e) = client.subscribe(vec![filter], Some(opts)).await {
|
|
||||||
println!("Error: {}", e);
|
println!("Error: {}", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -200,12 +197,8 @@ async fn main() {
|
|||||||
.run(move |cx| {
|
.run(move |cx| {
|
||||||
// Account state
|
// Account state
|
||||||
AccountRegistry::set_global(cx);
|
AccountRegistry::set_global(cx);
|
||||||
// Metadata state
|
|
||||||
MetadataRegistry::set_global(cx);
|
|
||||||
// Chat state
|
// Chat state
|
||||||
ChatRegistry::set_global(cx);
|
ChatRegistry::set_global(cx);
|
||||||
// Signal state
|
|
||||||
SignalRegistry::set_global(cx, mta_tx_clone);
|
|
||||||
|
|
||||||
// Initialize components
|
// Initialize components
|
||||||
ui::init(cx);
|
ui::init(cx);
|
||||||
@@ -224,6 +217,7 @@ async fn main() {
|
|||||||
let keys = Keys::parse(&hex).unwrap();
|
let keys = Keys::parse(&hex).unwrap();
|
||||||
|
|
||||||
_ = client.set_signer(keys).await;
|
_ = client.set_signer(keys).await;
|
||||||
|
|
||||||
// Update global state
|
// Update global state
|
||||||
_ = async_cx.update_global::<AccountRegistry, _>(|state, _cx| {
|
_ = async_cx.update_global::<AccountRegistry, _>(|state, _cx| {
|
||||||
state.set_user(Some(public_key));
|
state.set_user(Some(public_key));
|
||||||
@@ -260,8 +254,8 @@ async fn main() {
|
|||||||
while let Ok(signal) = rx.recv().await {
|
while let Ok(signal) = rx.recv().await {
|
||||||
match signal {
|
match signal {
|
||||||
Signal::Eose => {
|
Signal::Eose => {
|
||||||
_ = async_cx.update_global::<ChatRegistry, _>(|state, _| {
|
_ = async_cx.update_global::<ChatRegistry, _>(|state, cx| {
|
||||||
state.update();
|
state.init(cx);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Signal::Event(event) => {
|
Signal::Event(event) => {
|
||||||
@@ -277,23 +271,7 @@ async fn main() {
|
|||||||
.await;
|
.await;
|
||||||
|
|
||||||
_ = async_cx.update_global::<ChatRegistry, _>(|state, _cx| {
|
_ = async_cx.update_global::<ChatRegistry, _>(|state, _cx| {
|
||||||
state.push(event, metadata);
|
state.new_message(event, metadata)
|
||||||
});
|
|
||||||
}
|
|
||||||
Signal::Metadata(public_key) => {
|
|
||||||
let metadata = async_cx
|
|
||||||
.background_executor()
|
|
||||||
.spawn(async move {
|
|
||||||
client
|
|
||||||
.database()
|
|
||||||
.metadata(public_key)
|
|
||||||
.await
|
|
||||||
.unwrap_or_default()
|
|
||||||
})
|
|
||||||
.await;
|
|
||||||
|
|
||||||
_ = async_cx.update_global::<MetadataRegistry, _>(|state, _cx| {
|
|
||||||
state.seen(public_key, metadata);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,15 @@
|
|||||||
use gpui::*;
|
use crate::get_client;
|
||||||
|
use crate::utils::get_room_id;
|
||||||
|
use gpui::{AppContext, Context, Global, Model, SharedString};
|
||||||
|
use itertools::Itertools;
|
||||||
use nostr_sdk::prelude::*;
|
use nostr_sdk::prelude::*;
|
||||||
use rnglib::{Language, RNG};
|
use rnglib::{Language, RNG};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::sync::{Arc, RwLock};
|
use std::{
|
||||||
|
cmp::Reverse,
|
||||||
use super::metadata::MetadataRegistry;
|
collections::HashMap,
|
||||||
use crate::utils::get_room_id;
|
sync::{Arc, RwLock},
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Deserialize)]
|
#[derive(Clone, PartialEq, Eq, Deserialize)]
|
||||||
pub struct Room {
|
pub struct Room {
|
||||||
@@ -15,23 +19,17 @@ pub struct Room {
|
|||||||
pub last_seen: Timestamp,
|
pub last_seen: Timestamp,
|
||||||
pub title: Option<SharedString>,
|
pub title: Option<SharedString>,
|
||||||
pub metadata: Option<Metadata>,
|
pub metadata: Option<Metadata>,
|
||||||
is_initialized: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Room {
|
impl Room {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
|
id: SharedString,
|
||||||
owner: PublicKey,
|
owner: PublicKey,
|
||||||
members: Vec<PublicKey>,
|
members: Vec<PublicKey>,
|
||||||
last_seen: Timestamp,
|
last_seen: Timestamp,
|
||||||
title: Option<SharedString>,
|
title: Option<SharedString>,
|
||||||
cx: &mut WindowContext<'_>,
|
metadata: Option<Metadata>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
// Get unique id based on members
|
|
||||||
let id = get_room_id(&owner, &members).into();
|
|
||||||
|
|
||||||
// Get metadata for all members if exists
|
|
||||||
let metadata = cx.global::<MetadataRegistry>().get(&owner);
|
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
id,
|
id,
|
||||||
title,
|
title,
|
||||||
@@ -39,16 +37,17 @@ impl Room {
|
|||||||
last_seen,
|
last_seen,
|
||||||
owner,
|
owner,
|
||||||
metadata,
|
metadata,
|
||||||
is_initialized: false,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse(event: &Event, cx: &mut WindowContext<'_>) -> Self {
|
pub fn parse(event: &Event, metadata: Option<Metadata>) -> Self {
|
||||||
let owner = event.pubkey;
|
let owner = event.pubkey;
|
||||||
let last_seen = event.created_at;
|
let last_seen = event.created_at;
|
||||||
|
let id = SharedString::from(get_room_id(&owner, &event.tags));
|
||||||
|
|
||||||
// Get all members from event's tag
|
// Get all members from event's tag
|
||||||
let members: Vec<PublicKey> = event.tags.public_keys().copied().collect();
|
let mut members: Vec<PublicKey> = event.tags.public_keys().copied().collect();
|
||||||
|
members.push(owner);
|
||||||
|
|
||||||
// Get title from event's tag
|
// Get title from event's tag
|
||||||
let title = if let Some(tag) = event.tags.find(TagKind::Title) {
|
let title = if let Some(tag) = event.tags.find(TagKind::Title) {
|
||||||
@@ -60,20 +59,26 @@ impl Room {
|
|||||||
Some(name.into())
|
Some(name.into())
|
||||||
};
|
};
|
||||||
|
|
||||||
Self::new(owner, members, last_seen, title, cx)
|
Self::new(id, owner, members, last_seen, title, metadata)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Message {
|
pub struct Message {
|
||||||
pub room_id: SharedString,
|
|
||||||
pub event: Event,
|
pub event: Event,
|
||||||
pub metadata: Option<Metadata>,
|
pub metadata: Option<Metadata>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Message {
|
||||||
|
pub fn new(event: Event, metadata: Option<Metadata>) -> Self {
|
||||||
|
// TODO: parse event's content
|
||||||
|
Self { event, metadata }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct ChatRegistry {
|
pub struct ChatRegistry {
|
||||||
pub new_messages: Arc<RwLock<Vec<Message>>>,
|
pub messages: RwLock<HashMap<SharedString, Arc<RwLock<Vec<Message>>>>>,
|
||||||
pub reload: bool,
|
pub rooms: Model<Vec<Event>>,
|
||||||
pub is_initialized: bool,
|
pub is_initialized: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,34 +86,90 @@ impl Global for ChatRegistry {}
|
|||||||
|
|
||||||
impl ChatRegistry {
|
impl ChatRegistry {
|
||||||
pub fn set_global(cx: &mut AppContext) {
|
pub fn set_global(cx: &mut AppContext) {
|
||||||
cx.set_global(Self::new());
|
let rooms = cx.new_model(|_| Vec::new());
|
||||||
}
|
let messages = RwLock::new(HashMap::new());
|
||||||
|
|
||||||
pub fn update(&mut self) {
|
cx.set_global(Self {
|
||||||
if !self.is_initialized {
|
messages,
|
||||||
self.is_initialized = true;
|
rooms,
|
||||||
} else {
|
|
||||||
self.reload = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn push(&mut self, event: Event, metadata: Option<Metadata>) {
|
|
||||||
let pubkeys: Vec<PublicKey> = event.tags.public_keys().copied().collect();
|
|
||||||
let room_id = get_room_id(&event.pubkey, &pubkeys);
|
|
||||||
let message = Message {
|
|
||||||
room_id: room_id.into(),
|
|
||||||
event,
|
|
||||||
metadata,
|
|
||||||
};
|
|
||||||
|
|
||||||
self.new_messages.write().unwrap().push(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn new() -> Self {
|
|
||||||
Self {
|
|
||||||
new_messages: Arc::new(RwLock::new(Vec::new())),
|
|
||||||
reload: false,
|
|
||||||
is_initialized: false,
|
is_initialized: false,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn init(&mut self, cx: &mut AppContext) {
|
||||||
|
if self.is_initialized {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let async_cx = cx.to_async();
|
||||||
|
// Get all current room's ids
|
||||||
|
let ids: Vec<String> = self
|
||||||
|
.rooms
|
||||||
|
.read(cx)
|
||||||
|
.iter()
|
||||||
|
.map(|ev| get_room_id(&ev.pubkey, &ev.tags))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
cx.foreground_executor()
|
||||||
|
.spawn(async move {
|
||||||
|
let client = get_client();
|
||||||
|
let signer = client.signer().await.unwrap();
|
||||||
|
let public_key = signer.get_public_key().await.unwrap();
|
||||||
|
|
||||||
|
let filter = Filter::new()
|
||||||
|
.kind(Kind::PrivateDirectMessage)
|
||||||
|
.pubkey(public_key);
|
||||||
|
|
||||||
|
let events = async_cx
|
||||||
|
.background_executor()
|
||||||
|
.spawn(async move {
|
||||||
|
if let Ok(events) = client.database().query(vec![filter]).await {
|
||||||
|
events
|
||||||
|
.into_iter()
|
||||||
|
.filter(|ev| ev.pubkey != public_key)
|
||||||
|
.filter(|ev| {
|
||||||
|
let new_id = get_room_id(&ev.pubkey, &ev.tags);
|
||||||
|
// Get new events only
|
||||||
|
!ids.iter().any(|id| id == &new_id)
|
||||||
|
}) // Filter all messages from current user
|
||||||
|
.unique_by(|ev| ev.pubkey)
|
||||||
|
.sorted_by_key(|ev| Reverse(ev.created_at))
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
} else {
|
||||||
|
Vec::new()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
|
_ = async_cx.update_global::<Self, _>(|state, cx| {
|
||||||
|
state.rooms.update(cx, |model, cx| {
|
||||||
|
model.extend(events);
|
||||||
|
cx.notify();
|
||||||
|
});
|
||||||
|
|
||||||
|
state.is_initialized = true;
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_message(&mut self, event: Event, metadata: Option<Metadata>) {
|
||||||
|
// Get room id
|
||||||
|
let room_id = SharedString::from(get_room_id(&event.pubkey, &event.tags));
|
||||||
|
// Create message
|
||||||
|
let message = Message::new(event, metadata);
|
||||||
|
|
||||||
|
self.messages
|
||||||
|
.write()
|
||||||
|
.unwrap()
|
||||||
|
.entry(room_id)
|
||||||
|
.or_insert(Arc::new(RwLock::new(Vec::new())))
|
||||||
|
.write()
|
||||||
|
.unwrap()
|
||||||
|
.push(message)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_messages(&self, id: &SharedString) -> Option<Arc<RwLock<Vec<Message>>>> {
|
||||||
|
self.messages.read().unwrap().get(id).cloned()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,43 +0,0 @@
|
|||||||
use gpui::*;
|
|
||||||
use nostr_sdk::prelude::*;
|
|
||||||
use std::{
|
|
||||||
collections::HashMap,
|
|
||||||
sync::{Arc, Mutex, RwLock},
|
|
||||||
};
|
|
||||||
|
|
||||||
pub struct MetadataRegistry {
|
|
||||||
seens: Arc<Mutex<Vec<PublicKey>>>,
|
|
||||||
profiles: Arc<RwLock<HashMap<PublicKey, Metadata>>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Global for MetadataRegistry {}
|
|
||||||
|
|
||||||
impl MetadataRegistry {
|
|
||||||
pub fn set_global(cx: &mut AppContext) {
|
|
||||||
cx.set_global(Self::new());
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn seen(&mut self, public_key: PublicKey, metadata: Option<Metadata>) {
|
|
||||||
let mut seens = self.seens.lock().unwrap();
|
|
||||||
|
|
||||||
if !seens.contains(&public_key) {
|
|
||||||
seens.push(public_key);
|
|
||||||
drop(seens);
|
|
||||||
|
|
||||||
if let Some(metadata) = metadata {
|
|
||||||
self.profiles.write().unwrap().insert(public_key, metadata);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get(&self, public_key: &PublicKey) -> Option<Metadata> {
|
|
||||||
self.profiles.read().unwrap().get(public_key).cloned()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn new() -> Self {
|
|
||||||
let seens = Arc::new(Mutex::new(Vec::new()));
|
|
||||||
let profiles = Arc::new(RwLock::new(HashMap::new()));
|
|
||||||
|
|
||||||
Self { seens, profiles }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,2 @@
|
|||||||
pub mod account;
|
pub mod account;
|
||||||
pub mod chat;
|
pub mod chat;
|
||||||
pub mod metadata;
|
|
||||||
pub mod signal;
|
|
||||||
|
|||||||
@@ -1,30 +0,0 @@
|
|||||||
use gpui::*;
|
|
||||||
use nostr_sdk::prelude::*;
|
|
||||||
use std::sync::Arc;
|
|
||||||
use tokio::sync::mpsc::UnboundedSender;
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub enum Signal {
|
|
||||||
/// Receive metadata
|
|
||||||
Metadata(PublicKey),
|
|
||||||
/// Receive event
|
|
||||||
Event(Event),
|
|
||||||
/// Receive EOSE
|
|
||||||
Eose,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct SignalRegistry {
|
|
||||||
pub tx: Arc<UnboundedSender<PublicKey>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Global for SignalRegistry {}
|
|
||||||
|
|
||||||
impl SignalRegistry {
|
|
||||||
pub fn set_global(cx: &mut AppContext, tx: UnboundedSender<PublicKey>) {
|
|
||||||
cx.set_global(Self::new(tx));
|
|
||||||
}
|
|
||||||
|
|
||||||
fn new(tx: UnboundedSender<PublicKey>) -> Self {
|
|
||||||
Self { tx: Arc::new(tx) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +1,13 @@
|
|||||||
use chrono::{Duration, Local, TimeZone};
|
use chrono::{Duration, Local, TimeZone};
|
||||||
use nostr_sdk::prelude::*;
|
use nostr_sdk::prelude::*;
|
||||||
|
|
||||||
pub fn get_room_id(owner: &PublicKey, public_keys: &[PublicKey]) -> String {
|
pub fn get_room_id(author: &PublicKey, tags: &Tags) -> String {
|
||||||
let hex: Vec<String> = public_keys
|
// Get all public keys
|
||||||
|
let mut pubkeys: Vec<PublicKey> = tags.public_keys().copied().collect();
|
||||||
|
// Add author to public keys list
|
||||||
|
pubkeys.insert(0, *author);
|
||||||
|
|
||||||
|
let hex: Vec<String> = pubkeys
|
||||||
.iter()
|
.iter()
|
||||||
.map(|m| {
|
.map(|m| {
|
||||||
let hex = m.to_hex();
|
let hex = m.to_hex();
|
||||||
@@ -11,9 +16,8 @@ pub fn get_room_id(owner: &PublicKey, public_keys: &[PublicKey]) -> String {
|
|||||||
split.to_owned()
|
split.to_owned()
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
let mems = hex.join("-");
|
|
||||||
|
|
||||||
format!("{}-{}", &owner.to_hex()[..6], mems)
|
hex.join("-")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn show_npub(public_key: PublicKey, len: usize) -> String {
|
pub fn show_npub(public_key: PublicKey, len: usize) -> String {
|
||||||
|
|||||||
@@ -1,21 +1,21 @@
|
|||||||
use gpui::*;
|
use gpui::prelude::FluentBuilder;
|
||||||
|
use gpui::{
|
||||||
|
actions, img, Context, IntoElement, Model, ObjectFit, ParentElement, Render, Styled,
|
||||||
|
StyledImage, ViewContext,
|
||||||
|
};
|
||||||
use nostr_sdk::prelude::*;
|
use nostr_sdk::prelude::*;
|
||||||
use prelude::FluentBuilder;
|
|
||||||
use ui::{
|
use ui::{
|
||||||
button::{Button, ButtonVariants},
|
button::{Button, ButtonVariants},
|
||||||
popup_menu::PopupMenuExt,
|
popup_menu::PopupMenuExt,
|
||||||
Icon, IconName, Sizable,
|
Icon, IconName, Sizable,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{constants::IMAGE_SERVICE, get_client};
|
||||||
constants::IMAGE_SERVICE,
|
|
||||||
get_client,
|
|
||||||
states::{metadata::MetadataRegistry, signal::SignalRegistry},
|
|
||||||
};
|
|
||||||
|
|
||||||
actions!(account, [ToDo]);
|
actions!(account, [ToDo]);
|
||||||
|
|
||||||
pub struct Account {
|
pub struct Account {
|
||||||
|
#[allow(dead_code)]
|
||||||
public_key: PublicKey,
|
public_key: PublicKey,
|
||||||
metadata: Model<Option<Metadata>>,
|
metadata: Model<Option<Metadata>>,
|
||||||
}
|
}
|
||||||
@@ -23,25 +23,8 @@ pub struct Account {
|
|||||||
impl Account {
|
impl Account {
|
||||||
pub fn new(public_key: PublicKey, cx: &mut ViewContext<'_, Self>) -> Self {
|
pub fn new(public_key: PublicKey, cx: &mut ViewContext<'_, Self>) -> Self {
|
||||||
let metadata = cx.new_model(|_| None);
|
let metadata = cx.new_model(|_| None);
|
||||||
|
let async_metadata = metadata.clone();
|
||||||
|
|
||||||
// Request metadata
|
|
||||||
_ = cx.global::<SignalRegistry>().tx.send(public_key);
|
|
||||||
|
|
||||||
// Reload when received metadata
|
|
||||||
cx.observe_global::<MetadataRegistry>(|chat, cx| {
|
|
||||||
chat.load_metadata(cx);
|
|
||||||
})
|
|
||||||
.detach();
|
|
||||||
|
|
||||||
Self {
|
|
||||||
public_key,
|
|
||||||
metadata,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn load_metadata(&mut self, cx: &mut ViewContext<Self>) {
|
|
||||||
let public_key = self.public_key;
|
|
||||||
let async_metadata = self.metadata.clone();
|
|
||||||
let mut async_cx = cx.to_async();
|
let mut async_cx = cx.to_async();
|
||||||
|
|
||||||
cx.foreground_executor()
|
cx.foreground_executor()
|
||||||
@@ -60,6 +43,11 @@ impl Account {
|
|||||||
};
|
};
|
||||||
})
|
})
|
||||||
.detach();
|
.detach();
|
||||||
|
|
||||||
|
Self {
|
||||||
|
public_key,
|
||||||
|
metadata,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,13 @@
|
|||||||
use gpui::*;
|
use super::{
|
||||||
use prelude::FluentBuilder;
|
account::Account, chat::ChatPanel, onboarding::Onboarding, sidebar::Sidebar,
|
||||||
|
welcome::WelcomePanel,
|
||||||
|
};
|
||||||
|
use crate::states::{account::AccountRegistry, chat::Room};
|
||||||
|
use gpui::prelude::FluentBuilder;
|
||||||
|
use gpui::{
|
||||||
|
div, impl_actions, px, Axis, Context, Edges, InteractiveElement, IntoElement, Model,
|
||||||
|
ParentElement, Render, Styled, View, ViewContext, VisualContext, WeakView, WindowContext,
|
||||||
|
};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use ui::{
|
use ui::{
|
||||||
@@ -9,16 +17,9 @@ use ui::{
|
|||||||
Root, Sizable, TitleBar,
|
Root, Sizable, TitleBar,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{
|
|
||||||
account::Account, chat::ChatPanel, contact::ContactPanel, onboarding::Onboarding,
|
|
||||||
sidebar::Sidebar, welcome::WelcomePanel,
|
|
||||||
};
|
|
||||||
use crate::states::{account::AccountRegistry, chat::Room};
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Deserialize)]
|
#[derive(Clone, PartialEq, Eq, Deserialize)]
|
||||||
pub enum PanelKind {
|
pub enum PanelKind {
|
||||||
Room(Arc<Room>),
|
Room(Arc<Room>),
|
||||||
Contact,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Deserialize)]
|
#[derive(Clone, PartialEq, Eq, Deserialize)]
|
||||||
@@ -125,13 +126,6 @@ impl AppView {
|
|||||||
PanelKind::Room(room) => {
|
PanelKind::Room(room) => {
|
||||||
let panel = Arc::new(ChatPanel::new(room, cx));
|
let panel = Arc::new(ChatPanel::new(room, cx));
|
||||||
|
|
||||||
self.dock.update(cx, |dock_area, cx| {
|
|
||||||
dock_area.add_panel(panel, action.position, cx);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
PanelKind::Contact => {
|
|
||||||
let panel = Arc::new(ContactPanel::new(cx));
|
|
||||||
|
|
||||||
self.dock.update(cx, |dock_area, cx| {
|
self.dock.update(cx, |dock_area, cx| {
|
||||||
dock_area.add_panel(panel, action.position, cx);
|
dock_area.add_panel(panel, action.position, cx);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
use gpui::*;
|
use gpui::{
|
||||||
|
div, img, prelude::FluentBuilder, InteractiveElement, IntoElement, ParentElement, RenderOnce,
|
||||||
|
SharedString, Styled, WindowContext,
|
||||||
|
};
|
||||||
use nostr_sdk::prelude::*;
|
use nostr_sdk::prelude::*;
|
||||||
use prelude::FluentBuilder;
|
|
||||||
use ui::{theme::ActiveTheme, StyledExt};
|
use ui::{theme::ActiveTheme, StyledExt};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
use std::sync::Arc;
|
use gpui::{
|
||||||
|
div, AnyElement, AppContext, EventEmitter, FocusHandle, FocusableView, IntoElement,
|
||||||
use gpui::*;
|
ParentElement, Render, SharedString, Styled, View, VisualContext, WindowContext,
|
||||||
|
};
|
||||||
use nostr_sdk::prelude::*;
|
use nostr_sdk::prelude::*;
|
||||||
use room::RoomPanel;
|
use room::RoomPanel;
|
||||||
|
use std::sync::Arc;
|
||||||
use ui::{
|
use ui::{
|
||||||
button::Button,
|
button::Button,
|
||||||
dock::{Panel, PanelEvent, PanelState},
|
dock::{Panel, PanelEvent, PanelState},
|
||||||
@@ -91,7 +93,7 @@ impl Panel for ChatPanel {
|
|||||||
impl EventEmitter<PanelEvent> for ChatPanel {}
|
impl EventEmitter<PanelEvent> for ChatPanel {}
|
||||||
|
|
||||||
impl FocusableView for ChatPanel {
|
impl FocusableView for ChatPanel {
|
||||||
fn focus_handle(&self, _: &AppContext) -> gpui::FocusHandle {
|
fn focus_handle(&self, _: &AppContext) -> FocusHandle {
|
||||||
self.focus_handle.clone()
|
self.focus_handle.clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
use gpui::*;
|
use gpui::{
|
||||||
|
div, list, px, Context, Flatten, IntoElement, ListAlignment, ListState, Model, ParentElement,
|
||||||
|
PathPromptOptions, Pixels, Render, SharedString, Styled, View, ViewContext, VisualContext,
|
||||||
|
};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use nostr_sdk::prelude::*;
|
use nostr_sdk::prelude::*;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
@@ -15,7 +18,6 @@ use crate::{
|
|||||||
states::{
|
states::{
|
||||||
account::AccountRegistry,
|
account::AccountRegistry,
|
||||||
chat::{ChatRegistry, Room},
|
chat::{ChatRegistry, Room},
|
||||||
metadata::MetadataRegistry,
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -122,21 +124,34 @@ impl RoomPanel {
|
|||||||
.await;
|
.await;
|
||||||
|
|
||||||
if let Ok(events) = events {
|
if let Ok(events) = events {
|
||||||
let items: Vec<RoomMessage> = events
|
let mut items: Vec<RoomMessage> = Vec::new();
|
||||||
.into_iter()
|
|
||||||
.sorted_by_key(|ev| ev.created_at)
|
|
||||||
.map(|ev| {
|
|
||||||
// Get user's metadata
|
|
||||||
let metadata = async_cx
|
|
||||||
.read_global::<MetadataRegistry, _>(|state, _cx| {
|
|
||||||
state.get(&ev.pubkey)
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// Return message item
|
for event in events.into_iter().sorted_by_key(|ev| ev.created_at) {
|
||||||
RoomMessage::new(ev.pubkey, metadata, ev.content, ev.created_at)
|
let metadata = async_cx
|
||||||
})
|
.background_executor()
|
||||||
.collect();
|
.spawn(
|
||||||
|
async move { client.database().metadata(event.pubkey).await },
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let message = if let Ok(metadata) = metadata {
|
||||||
|
RoomMessage::new(
|
||||||
|
event.pubkey,
|
||||||
|
metadata,
|
||||||
|
event.content,
|
||||||
|
event.created_at,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
RoomMessage::new(
|
||||||
|
event.pubkey,
|
||||||
|
None,
|
||||||
|
event.content,
|
||||||
|
event.created_at,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
items.push(message);
|
||||||
|
}
|
||||||
|
|
||||||
let total = items.len();
|
let total = items.len();
|
||||||
|
|
||||||
@@ -154,38 +169,38 @@ impl RoomPanel {
|
|||||||
pub fn subscribe(&self, cx: &mut ViewContext<Self>) {
|
pub fn subscribe(&self, cx: &mut ViewContext<Self>) {
|
||||||
let room_id = self.id.clone();
|
let room_id = self.id.clone();
|
||||||
let messages = self.messages.clone();
|
let messages = self.messages.clone();
|
||||||
let current_user = cx.global::<AccountRegistry>().get().unwrap();
|
|
||||||
|
|
||||||
cx.observe_global::<ChatRegistry>(move |_, cx| {
|
cx.observe_global::<ChatRegistry>(move |_, cx| {
|
||||||
let state = cx.global::<ChatRegistry>();
|
let state = cx.global::<ChatRegistry>();
|
||||||
let new_messages = state.new_messages.read().unwrap().clone();
|
let new_messages = state.get_messages(&room_id);
|
||||||
let filter = new_messages
|
|
||||||
.into_iter()
|
|
||||||
.filter(|m| m.room_id == room_id && m.event.pubkey != current_user)
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
let items: Vec<RoomMessage> = filter
|
if let Some(new_messages) = new_messages {
|
||||||
.into_iter()
|
let items: Vec<RoomMessage> = new_messages
|
||||||
.map(|m| {
|
.read()
|
||||||
RoomMessage::new(
|
.unwrap()
|
||||||
m.event.pubkey,
|
.clone()
|
||||||
m.metadata,
|
.into_iter()
|
||||||
m.event.content,
|
.map(|m| {
|
||||||
m.event.created_at,
|
RoomMessage::new(
|
||||||
)
|
m.event.pubkey,
|
||||||
})
|
m.metadata,
|
||||||
.collect();
|
m.event.content,
|
||||||
|
m.event.created_at,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
cx.update_model(&messages, |model, cx| {
|
cx.update_model(&messages, |model, cx| {
|
||||||
model.items.extend(items);
|
model.items.extend(items);
|
||||||
model.count = model.items.len();
|
model.count = model.items.len();
|
||||||
cx.notify();
|
cx.notify();
|
||||||
});
|
});
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.detach();
|
.detach();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send_message(&mut self, cx: &mut ViewContext<Self>) {
|
fn send_message(&mut self, cx: &mut ViewContext<Self>) {
|
||||||
let owner = self.owner;
|
let owner = self.owner;
|
||||||
let current_user = cx.global::<AccountRegistry>().get().unwrap();
|
let current_user = cx.global::<AccountRegistry>().get().unwrap();
|
||||||
let content = self.input.read(cx).text().to_string();
|
let content = self.input.read(cx).text().to_string();
|
||||||
@@ -252,7 +267,7 @@ impl RoomPanel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Render for RoomPanel {
|
impl Render for RoomPanel {
|
||||||
fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> impl IntoElement {
|
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||||
v_flex()
|
v_flex()
|
||||||
.size_full()
|
.size_full()
|
||||||
.child(list(self.list.clone()).flex_1())
|
.child(list(self.list.clone()).flex_1())
|
||||||
|
|||||||
@@ -1,113 +0,0 @@
|
|||||||
use gpui::*;
|
|
||||||
use nostr_sdk::prelude::*;
|
|
||||||
use prelude::FluentBuilder;
|
|
||||||
use ui::theme::ActiveTheme;
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
constants::IMAGE_SERVICE,
|
|
||||||
get_client,
|
|
||||||
states::{metadata::MetadataRegistry, signal::SignalRegistry},
|
|
||||||
utils::show_npub,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub struct ContactListItem {
|
|
||||||
public_key: PublicKey,
|
|
||||||
metadata: Model<Option<Metadata>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ContactListItem {
|
|
||||||
pub fn new(public_key: PublicKey, cx: &mut ViewContext<'_, Self>) -> Self {
|
|
||||||
let metadata = cx.new_model(|_| None);
|
|
||||||
|
|
||||||
// Request metadata
|
|
||||||
_ = cx.global::<SignalRegistry>().tx.send(public_key);
|
|
||||||
|
|
||||||
// Reload when received metadata
|
|
||||||
cx.observe_global::<MetadataRegistry>(|item, cx| {
|
|
||||||
item.load_metadata(cx);
|
|
||||||
})
|
|
||||||
.detach();
|
|
||||||
|
|
||||||
Self {
|
|
||||||
public_key,
|
|
||||||
metadata,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn load_metadata(&mut self, cx: &mut ViewContext<Self>) {
|
|
||||||
let public_key = self.public_key;
|
|
||||||
let async_metadata = self.metadata.clone();
|
|
||||||
let mut async_cx = cx.to_async();
|
|
||||||
|
|
||||||
cx.foreground_executor()
|
|
||||||
.spawn({
|
|
||||||
let client = get_client();
|
|
||||||
|
|
||||||
async move {
|
|
||||||
let query = async_cx
|
|
||||||
.background_executor()
|
|
||||||
.spawn(async move { client.database().metadata(public_key).await })
|
|
||||||
.await;
|
|
||||||
|
|
||||||
if let Ok(metadata) = query {
|
|
||||||
_ = async_cx.update_model(&async_metadata, |a, b| {
|
|
||||||
*a = metadata;
|
|
||||||
b.notify();
|
|
||||||
});
|
|
||||||
};
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.detach();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for ContactListItem {
|
|
||||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
|
||||||
let fallback = show_npub(self.public_key, 16);
|
|
||||||
let mut content = div()
|
|
||||||
.w_full()
|
|
||||||
.h_10()
|
|
||||||
.px_2()
|
|
||||||
.flex()
|
|
||||||
.items_center()
|
|
||||||
.gap_2()
|
|
||||||
.text_sm();
|
|
||||||
|
|
||||||
if let Some(metadata) = self.metadata.read(cx).as_ref() {
|
|
||||||
content = content
|
|
||||||
.map(|this| {
|
|
||||||
if let Some(picture) = metadata.picture.clone() {
|
|
||||||
this.flex_shrink_0().child(
|
|
||||||
img(format!(
|
|
||||||
"{}/?url={}&w=72&h=72&fit=cover&mask=circle&n=-1",
|
|
||||||
IMAGE_SERVICE, picture
|
|
||||||
))
|
|
||||||
.size_8(),
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
this.flex_shrink_0()
|
|
||||||
.child(img("brand/avatar.png").size_8().rounded_full())
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.map(|this| {
|
|
||||||
if let Some(display_name) = metadata.display_name.clone() {
|
|
||||||
this.flex_1().child(display_name)
|
|
||||||
} else {
|
|
||||||
this.flex_1().child(fallback)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
content = content
|
|
||||||
.child(img("brand/avatar.png").size_8().rounded_full())
|
|
||||||
.child(fallback)
|
|
||||||
}
|
|
||||||
|
|
||||||
div()
|
|
||||||
.rounded_md()
|
|
||||||
.hover(|this| {
|
|
||||||
this.bg(cx.theme().muted)
|
|
||||||
.text_color(cx.theme().muted_foreground)
|
|
||||||
})
|
|
||||||
.child(content)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,146 +0,0 @@
|
|||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
use gpui::*;
|
|
||||||
use item::ContactListItem;
|
|
||||||
use prelude::FluentBuilder;
|
|
||||||
use ui::{
|
|
||||||
button::Button,
|
|
||||||
dock::{Panel, PanelEvent, PanelState},
|
|
||||||
indicator::Indicator,
|
|
||||||
popup_menu::PopupMenu,
|
|
||||||
scroll::ScrollbarAxis,
|
|
||||||
theme::ActiveTheme,
|
|
||||||
v_flex, Sizable, StyledExt,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::get_client;
|
|
||||||
|
|
||||||
mod item;
|
|
||||||
|
|
||||||
pub struct ContactPanel {
|
|
||||||
name: SharedString,
|
|
||||||
closeable: bool,
|
|
||||||
zoomable: bool,
|
|
||||||
focus_handle: FocusHandle,
|
|
||||||
// Contacts
|
|
||||||
view_id: EntityId,
|
|
||||||
contacts: Model<Option<Vec<View<ContactListItem>>>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ContactPanel {
|
|
||||||
pub fn new(cx: &mut WindowContext) -> View<Self> {
|
|
||||||
cx.new_view(Self::view)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn view(cx: &mut ViewContext<Self>) -> Self {
|
|
||||||
let contacts = cx.new_model(|_| None);
|
|
||||||
let async_contacts = contacts.clone();
|
|
||||||
|
|
||||||
let mut async_cx = cx.to_async();
|
|
||||||
|
|
||||||
cx.foreground_executor()
|
|
||||||
.spawn({
|
|
||||||
let client = get_client();
|
|
||||||
|
|
||||||
async move {
|
|
||||||
if let Ok(contacts) = async_cx
|
|
||||||
.background_executor()
|
|
||||||
.spawn(async move { client.get_contact_list(Duration::from_secs(3)).await })
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
let views: Vec<View<ContactListItem>> = contacts
|
|
||||||
.into_iter()
|
|
||||||
.map(|contact| {
|
|
||||||
async_cx
|
|
||||||
.new_view(|cx| ContactListItem::new(contact.public_key, cx))
|
|
||||||
.unwrap()
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
_ = async_cx.update_model(&async_contacts, |model, cx| {
|
|
||||||
*model = Some(views);
|
|
||||||
cx.notify();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.detach();
|
|
||||||
|
|
||||||
Self {
|
|
||||||
name: "Contacts".into(),
|
|
||||||
closeable: true,
|
|
||||||
zoomable: true,
|
|
||||||
focus_handle: cx.focus_handle(),
|
|
||||||
view_id: cx.entity_id(),
|
|
||||||
contacts,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Panel for ContactPanel {
|
|
||||||
fn panel_id(&self) -> SharedString {
|
|
||||||
"Contact".into()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn title(&self, _cx: &WindowContext) -> AnyElement {
|
|
||||||
self.name.clone().into_any_element()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn closeable(&self, _cx: &WindowContext) -> bool {
|
|
||||||
self.closeable
|
|
||||||
}
|
|
||||||
|
|
||||||
fn zoomable(&self, _cx: &WindowContext) -> bool {
|
|
||||||
self.zoomable
|
|
||||||
}
|
|
||||||
|
|
||||||
fn popup_menu(&self, menu: PopupMenu, _cx: &WindowContext) -> PopupMenu {
|
|
||||||
menu.track_focus(&self.focus_handle)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn toolbar_buttons(&self, _cx: &WindowContext) -> Vec<Button> {
|
|
||||||
vec![]
|
|
||||||
}
|
|
||||||
|
|
||||||
fn dump(&self, _cx: &AppContext) -> PanelState {
|
|
||||||
PanelState::new(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl EventEmitter<PanelEvent> for ContactPanel {}
|
|
||||||
|
|
||||||
impl FocusableView for ContactPanel {
|
|
||||||
fn focus_handle(&self, _: &AppContext) -> gpui::FocusHandle {
|
|
||||||
self.focus_handle.clone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for ContactPanel {
|
|
||||||
fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> impl IntoElement {
|
|
||||||
v_flex()
|
|
||||||
.scrollable(self.view_id, ScrollbarAxis::Vertical)
|
|
||||||
.w_full()
|
|
||||||
.gap_1()
|
|
||||||
.p_2()
|
|
||||||
.map(|this| {
|
|
||||||
if let Some(contacts) = self.contacts.read(cx).as_ref() {
|
|
||||||
this.children(contacts.clone())
|
|
||||||
} else {
|
|
||||||
this.w_full()
|
|
||||||
.h_40()
|
|
||||||
.flex()
|
|
||||||
.items_center()
|
|
||||||
.justify_center()
|
|
||||||
.child(
|
|
||||||
div()
|
|
||||||
.flex()
|
|
||||||
.items_center()
|
|
||||||
.gap_1p5()
|
|
||||||
.text_color(cx.theme().muted_foreground)
|
|
||||||
.child(Indicator::new().small())
|
|
||||||
.child(div().text_xs().child("Loading")),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,153 +0,0 @@
|
|||||||
use gpui::*;
|
|
||||||
use nostr_sdk::prelude::*;
|
|
||||||
use prelude::FluentBuilder;
|
|
||||||
use std::sync::Arc;
|
|
||||||
use ui::{theme::ActiveTheme, StyledExt};
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
constants::IMAGE_SERVICE,
|
|
||||||
get_client,
|
|
||||||
states::{chat::Room, metadata::MetadataRegistry, signal::SignalRegistry},
|
|
||||||
utils::{ago, get_room_id, show_npub},
|
|
||||||
views::app::{AddPanel, PanelKind},
|
|
||||||
};
|
|
||||||
|
|
||||||
pub struct InboxListItem {
|
|
||||||
id: SharedString,
|
|
||||||
event: Event,
|
|
||||||
metadata: Model<Option<Metadata>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl InboxListItem {
|
|
||||||
pub fn new(event: Event, cx: &mut ViewContext<'_, Self>) -> Self {
|
|
||||||
let pubkeys: Vec<PublicKey> = event.tags.public_keys().copied().collect();
|
|
||||||
let id = get_room_id(&event.pubkey, &pubkeys).into();
|
|
||||||
let metadata = cx.new_model(|_| None);
|
|
||||||
|
|
||||||
// Reload when received metadata
|
|
||||||
cx.observe_global::<MetadataRegistry>(|chat, cx| {
|
|
||||||
chat.load_metadata(cx);
|
|
||||||
})
|
|
||||||
.detach();
|
|
||||||
|
|
||||||
Self {
|
|
||||||
id,
|
|
||||||
event,
|
|
||||||
metadata,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn request_metadata(&mut self, cx: &mut ViewContext<Self>) {
|
|
||||||
_ = cx.global::<SignalRegistry>().tx.send(self.event.pubkey);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn load_metadata(&mut self, cx: &mut ViewContext<Self>) {
|
|
||||||
let public_key = self.event.pubkey;
|
|
||||||
let async_metadata = self.metadata.clone();
|
|
||||||
let mut async_cx = cx.to_async();
|
|
||||||
|
|
||||||
cx.foreground_executor()
|
|
||||||
.spawn(async move {
|
|
||||||
let client = get_client();
|
|
||||||
let query = async_cx
|
|
||||||
.background_executor()
|
|
||||||
.spawn(async move { client.database().metadata(public_key).await })
|
|
||||||
.await;
|
|
||||||
|
|
||||||
if let Ok(metadata) = query {
|
|
||||||
_ = async_cx.update_model(&async_metadata, |a, b| {
|
|
||||||
*a = metadata;
|
|
||||||
b.notify();
|
|
||||||
});
|
|
||||||
};
|
|
||||||
})
|
|
||||||
.detach();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn id(&self) -> String {
|
|
||||||
self.id.clone().into()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn action(&self, cx: &mut WindowContext<'_>) {
|
|
||||||
let room = Arc::new(Room::parse(&self.event, cx));
|
|
||||||
|
|
||||||
cx.dispatch_action(Box::new(AddPanel {
|
|
||||||
panel: PanelKind::Room(room),
|
|
||||||
position: ui::dock::DockPlacement::Center,
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for InboxListItem {
|
|
||||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
|
||||||
let ago = ago(self.event.created_at.as_u64());
|
|
||||||
let fallback_name = show_npub(self.event.pubkey, 16);
|
|
||||||
|
|
||||||
let mut content = div()
|
|
||||||
.font_medium()
|
|
||||||
.text_color(cx.theme().sidebar_accent_foreground);
|
|
||||||
|
|
||||||
if let Some(metadata) = self.metadata.read(cx).as_ref() {
|
|
||||||
content = content
|
|
||||||
.flex()
|
|
||||||
.items_center()
|
|
||||||
.gap_2()
|
|
||||||
.map(|this| {
|
|
||||||
if let Some(picture) = metadata.picture.clone() {
|
|
||||||
this.flex_shrink_0().child(
|
|
||||||
img(format!(
|
|
||||||
"{}/?url={}&w=72&h=72&fit=cover&mask=circle&n=-1",
|
|
||||||
IMAGE_SERVICE, picture
|
|
||||||
))
|
|
||||||
.size_6(),
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
this.flex_shrink_0()
|
|
||||||
.child(img("brand/avatar.png").size_6().rounded_full())
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.map(|this| {
|
|
||||||
if let Some(display_name) = metadata.display_name.clone() {
|
|
||||||
this.child(display_name)
|
|
||||||
} else {
|
|
||||||
this.child(fallback_name)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
content = content
|
|
||||||
.flex()
|
|
||||||
.items_center()
|
|
||||||
.gap_2()
|
|
||||||
.child(
|
|
||||||
img("brand/avatar.png")
|
|
||||||
.flex_shrink_0()
|
|
||||||
.size_6()
|
|
||||||
.rounded_full(),
|
|
||||||
)
|
|
||||||
.child(fallback_name)
|
|
||||||
}
|
|
||||||
|
|
||||||
div()
|
|
||||||
.id(self.id.clone())
|
|
||||||
.h_8()
|
|
||||||
.px_1()
|
|
||||||
.flex()
|
|
||||||
.items_center()
|
|
||||||
.justify_between()
|
|
||||||
.text_xs()
|
|
||||||
.rounded_md()
|
|
||||||
.hover(|this| {
|
|
||||||
this.bg(cx.theme().sidebar_accent)
|
|
||||||
.text_color(cx.theme().sidebar_accent_foreground)
|
|
||||||
})
|
|
||||||
.child(content)
|
|
||||||
.child(
|
|
||||||
div()
|
|
||||||
.child(ago)
|
|
||||||
.text_color(cx.theme().sidebar_accent_foreground.opacity(0.7)),
|
|
||||||
)
|
|
||||||
.on_click(cx.listener(|this, _, cx| {
|
|
||||||
this.action(cx);
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,216 +0,0 @@
|
|||||||
use gpui::*;
|
|
||||||
use itertools::Itertools;
|
|
||||||
use nostr_sdk::prelude::*;
|
|
||||||
use prelude::FluentBuilder;
|
|
||||||
use std::cmp::Reverse;
|
|
||||||
use ui::{skeleton::Skeleton, theme::ActiveTheme, v_flex, Collapsible, Icon, IconName, StyledExt};
|
|
||||||
|
|
||||||
use super::inbox::item::InboxListItem;
|
|
||||||
use crate::{get_client, states::chat::ChatRegistry, utils::get_room_id};
|
|
||||||
|
|
||||||
pub mod item;
|
|
||||||
|
|
||||||
pub struct Inbox {
|
|
||||||
label: SharedString,
|
|
||||||
items: Model<Option<Vec<View<InboxListItem>>>>,
|
|
||||||
is_loading: bool,
|
|
||||||
is_collapsed: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Inbox {
|
|
||||||
pub fn new(cx: &mut ViewContext<'_, Self>) -> Self {
|
|
||||||
let items = cx.new_model(|_| None);
|
|
||||||
|
|
||||||
cx.observe_global::<ChatRegistry>(|this, cx| {
|
|
||||||
let state = cx.global::<ChatRegistry>();
|
|
||||||
let empty_messages = state.new_messages.read().unwrap().is_empty();
|
|
||||||
|
|
||||||
if state.reload || (state.is_initialized && empty_messages) {
|
|
||||||
this.load(cx);
|
|
||||||
} else {
|
|
||||||
#[allow(clippy::collapsible_if)]
|
|
||||||
if let Some(items) = this.items.read(cx).as_ref() {
|
|
||||||
// Get all current chats
|
|
||||||
let current_rooms: Vec<String> =
|
|
||||||
items.iter().map(|item| item.model.read(cx).id()).collect();
|
|
||||||
|
|
||||||
// Get all new messages
|
|
||||||
let messages = state
|
|
||||||
.new_messages
|
|
||||||
.read()
|
|
||||||
.unwrap()
|
|
||||||
.clone()
|
|
||||||
.into_iter()
|
|
||||||
.filter(|m| {
|
|
||||||
let keys = m.event.tags.public_keys().copied().collect::<Vec<_>>();
|
|
||||||
let new_id = get_room_id(&m.event.pubkey, &keys);
|
|
||||||
|
|
||||||
!current_rooms.iter().any(|id| id == &new_id)
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
// Create view for new chats only
|
|
||||||
let new = messages
|
|
||||||
.into_iter()
|
|
||||||
.map(|m| cx.new_view(|cx| InboxListItem::new(m.event, cx)))
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
cx.update_model(&this.items, |a, b| {
|
|
||||||
if let Some(items) = a {
|
|
||||||
items.extend(new);
|
|
||||||
b.notify();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.detach();
|
|
||||||
|
|
||||||
cx.observe_new_views::<InboxListItem>(|item, cx| {
|
|
||||||
item.request_metadata(cx);
|
|
||||||
item.load_metadata(cx);
|
|
||||||
})
|
|
||||||
.detach();
|
|
||||||
|
|
||||||
Self {
|
|
||||||
items,
|
|
||||||
label: "Inbox".into(),
|
|
||||||
is_loading: true,
|
|
||||||
is_collapsed: false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn load(&mut self, cx: &mut ViewContext<Self>) {
|
|
||||||
// Hide loading indicator
|
|
||||||
self.set_loading(cx);
|
|
||||||
|
|
||||||
let items = self.items.read(cx).as_ref();
|
|
||||||
// Get all current rooms id
|
|
||||||
let current_rooms: Vec<String> = if let Some(items) = items {
|
|
||||||
items.iter().map(|item| item.model.read(cx).id()).collect()
|
|
||||||
} else {
|
|
||||||
Vec::new()
|
|
||||||
};
|
|
||||||
|
|
||||||
let async_items = self.items.clone();
|
|
||||||
let mut async_cx = cx.to_async();
|
|
||||||
|
|
||||||
cx.foreground_executor()
|
|
||||||
.spawn(async move {
|
|
||||||
let client = get_client();
|
|
||||||
let signer = client.signer().await.unwrap();
|
|
||||||
let public_key = signer.get_public_key().await.unwrap();
|
|
||||||
|
|
||||||
let filter = Filter::new()
|
|
||||||
.kind(Kind::PrivateDirectMessage)
|
|
||||||
.pubkey(public_key);
|
|
||||||
|
|
||||||
let events = async_cx
|
|
||||||
.background_executor()
|
|
||||||
.spawn(async move {
|
|
||||||
if let Ok(events) = client.database().query(vec![filter]).await {
|
|
||||||
events
|
|
||||||
.into_iter()
|
|
||||||
.filter(|ev| ev.pubkey != public_key) // Filter all messages from current user
|
|
||||||
.unique_by(|ev| ev.pubkey)
|
|
||||||
.sorted_by_key(|ev| Reverse(ev.created_at))
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
} else {
|
|
||||||
Vec::new()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.await;
|
|
||||||
|
|
||||||
let views: Vec<View<InboxListItem>> = events
|
|
||||||
.into_iter()
|
|
||||||
.filter(|ev| {
|
|
||||||
let keys = ev.tags.public_keys().copied().collect::<Vec<_>>();
|
|
||||||
let new_id = get_room_id(&ev.pubkey, &keys);
|
|
||||||
|
|
||||||
!current_rooms.iter().any(|id| id == &new_id)
|
|
||||||
})
|
|
||||||
.map(|ev| async_cx.new_view(|cx| InboxListItem::new(ev, cx)).unwrap())
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
async_cx.update_model(&async_items, |model, cx| {
|
|
||||||
if let Some(items) = model {
|
|
||||||
items.extend(views);
|
|
||||||
} else {
|
|
||||||
*model = Some(views);
|
|
||||||
}
|
|
||||||
|
|
||||||
cx.notify();
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.detach();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn set_loading(&mut self, cx: &mut ViewContext<Self>) {
|
|
||||||
self.is_loading = false;
|
|
||||||
cx.notify();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Collapsible for Inbox {
|
|
||||||
fn is_collapsed(&self) -> bool {
|
|
||||||
self.is_collapsed
|
|
||||||
}
|
|
||||||
|
|
||||||
fn collapsed(mut self, collapsed: bool) -> Self {
|
|
||||||
self.is_collapsed = collapsed;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for Inbox {
|
|
||||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
|
||||||
let mut content = div();
|
|
||||||
|
|
||||||
if self.is_loading {
|
|
||||||
content = content.children((0..5).map(|_| {
|
|
||||||
div()
|
|
||||||
.h_8()
|
|
||||||
.px_1()
|
|
||||||
.flex()
|
|
||||||
.items_center()
|
|
||||||
.gap_2()
|
|
||||||
.child(Skeleton::new().flex_shrink_0().size_6().rounded_full())
|
|
||||||
.child(Skeleton::new().w_20().h_3().rounded_sm())
|
|
||||||
}))
|
|
||||||
} else if let Some(items) = self.items.read(cx).as_ref() {
|
|
||||||
content = content.children(items.clone())
|
|
||||||
} else {
|
|
||||||
// TODO: handle error
|
|
||||||
}
|
|
||||||
|
|
||||||
v_flex()
|
|
||||||
.px_2()
|
|
||||||
.gap_1()
|
|
||||||
.child(
|
|
||||||
div()
|
|
||||||
.id("inbox")
|
|
||||||
.h_7()
|
|
||||||
.px_1()
|
|
||||||
.flex()
|
|
||||||
.items_center()
|
|
||||||
.rounded_md()
|
|
||||||
.text_xs()
|
|
||||||
.font_semibold()
|
|
||||||
.text_color(cx.theme().sidebar_foreground.opacity(0.7))
|
|
||||||
.hover(|this| this.bg(cx.theme().sidebar_accent.opacity(0.7)))
|
|
||||||
.on_click(cx.listener(move |view, _event, cx| {
|
|
||||||
view.is_collapsed = !view.is_collapsed;
|
|
||||||
cx.notify();
|
|
||||||
}))
|
|
||||||
.child(
|
|
||||||
Icon::new(IconName::ChevronDown)
|
|
||||||
.size_6()
|
|
||||||
.when(self.is_collapsed, |this| {
|
|
||||||
this.rotate(percentage(270. / 360.))
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
.child(self.label.clone()),
|
|
||||||
)
|
|
||||||
.when(!self.is_collapsed, |this| this.child(content))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +1,7 @@
|
|||||||
pub mod app;
|
|
||||||
|
|
||||||
mod account;
|
mod account;
|
||||||
mod chat;
|
mod chat;
|
||||||
mod contact;
|
|
||||||
mod inbox;
|
|
||||||
mod onboarding;
|
mod onboarding;
|
||||||
mod sidebar;
|
mod sidebar;
|
||||||
mod welcome;
|
mod welcome;
|
||||||
|
|
||||||
|
pub mod app;
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
use gpui::*;
|
use gpui::{
|
||||||
|
div, IntoElement,
|
||||||
|
ParentElement, Render, Styled, View, ViewContext, VisualContext,
|
||||||
|
};
|
||||||
use nostr_sdk::prelude::*;
|
use nostr_sdk::prelude::*;
|
||||||
use ui::{
|
use ui::{
|
||||||
input::{InputEvent, TextInput},
|
input::{InputEvent, TextInput},
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
use std::collections::{BTreeSet, HashSet};
|
use crate::{
|
||||||
|
constants::IMAGE_SERVICE, get_client, states::account::AccountRegistry, utils::show_npub,
|
||||||
|
};
|
||||||
use gpui::{
|
use gpui::{
|
||||||
div, img, impl_actions, list, px, Context, ElementId, FocusHandle, InteractiveElement,
|
div, img, impl_actions, list, px, Context, ElementId, FocusHandle, InteractiveElement,
|
||||||
IntoElement, ListAlignment, ListState, Model, ParentElement, Pixels, Render, RenderOnce,
|
IntoElement, ListAlignment, ListState, Model, ParentElement, Pixels, Render, RenderOnce,
|
||||||
@@ -7,16 +8,13 @@ use gpui::{
|
|||||||
};
|
};
|
||||||
use nostr_sdk::prelude::*;
|
use nostr_sdk::prelude::*;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
use std::collections::{BTreeSet, HashSet};
|
||||||
use ui::{
|
use ui::{
|
||||||
prelude::FluentBuilder,
|
prelude::FluentBuilder,
|
||||||
theme::{ActiveTheme, Colorize},
|
theme::{ActiveTheme, Colorize},
|
||||||
Icon, IconName, Selectable, StyledExt,
|
Icon, IconName, Selectable, StyledExt,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
|
||||||
constants::IMAGE_SERVICE, get_client, states::account::AccountRegistry, utils::show_npub,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Deserialize)]
|
#[derive(Clone, PartialEq, Eq, Deserialize)]
|
||||||
struct SelectContact(PublicKey);
|
struct SelectContact(PublicKey);
|
||||||
|
|
||||||
|
|||||||
255
crates/app/src/views/sidebar/inbox.rs
Normal file
255
crates/app/src/views/sidebar/inbox.rs
Normal file
@@ -0,0 +1,255 @@
|
|||||||
|
use crate::{
|
||||||
|
constants::IMAGE_SERVICE,
|
||||||
|
get_client,
|
||||||
|
states::chat::ChatRegistry,
|
||||||
|
states::chat::Room,
|
||||||
|
utils::get_room_id,
|
||||||
|
utils::{ago, show_npub},
|
||||||
|
views::app::{AddPanel, PanelKind},
|
||||||
|
};
|
||||||
|
use gpui::prelude::FluentBuilder;
|
||||||
|
use gpui::{
|
||||||
|
div, img, percentage, Context, InteractiveElement, IntoElement, Model, ParentElement, Render,
|
||||||
|
SharedString, StatefulInteractiveElement, Styled, View, ViewContext, VisualContext,
|
||||||
|
WindowContext,
|
||||||
|
};
|
||||||
|
use nostr_sdk::prelude::*;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use ui::{skeleton::Skeleton, theme::ActiveTheme, v_flex, Collapsible, Icon, IconName, StyledExt};
|
||||||
|
|
||||||
|
struct InboxListItem {
|
||||||
|
id: SharedString,
|
||||||
|
event: Event,
|
||||||
|
metadata: Option<Metadata>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl InboxListItem {
|
||||||
|
pub fn new(event: Event, metadata: Option<Metadata>, _cx: &mut ViewContext<'_, Self>) -> Self {
|
||||||
|
let id = SharedString::from(get_room_id(&event.pubkey, &event.tags));
|
||||||
|
|
||||||
|
Self {
|
||||||
|
id,
|
||||||
|
event,
|
||||||
|
metadata,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn action(&self, cx: &mut WindowContext<'_>) {
|
||||||
|
let room = Arc::new(Room::parse(&self.event, self.metadata.clone()));
|
||||||
|
|
||||||
|
cx.dispatch_action(Box::new(AddPanel {
|
||||||
|
panel: PanelKind::Room(room),
|
||||||
|
position: ui::dock::DockPlacement::Center,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Render for InboxListItem {
|
||||||
|
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||||
|
let ago = ago(self.event.created_at.as_u64());
|
||||||
|
let fallback_name = show_npub(self.event.pubkey, 16);
|
||||||
|
|
||||||
|
let mut content = div()
|
||||||
|
.font_medium()
|
||||||
|
.text_color(cx.theme().sidebar_accent_foreground);
|
||||||
|
|
||||||
|
if let Some(metadata) = self.metadata.clone() {
|
||||||
|
content = content
|
||||||
|
.flex()
|
||||||
|
.items_center()
|
||||||
|
.gap_2()
|
||||||
|
.map(|this| {
|
||||||
|
if let Some(picture) = metadata.picture.clone() {
|
||||||
|
this.flex_shrink_0().child(
|
||||||
|
img(format!(
|
||||||
|
"{}/?url={}&w=72&h=72&fit=cover&mask=circle&n=-1",
|
||||||
|
IMAGE_SERVICE, picture
|
||||||
|
))
|
||||||
|
.size_6(),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
this.flex_shrink_0()
|
||||||
|
.child(img("brand/avatar.png").size_6().rounded_full())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.map(|this| {
|
||||||
|
if let Some(display_name) = metadata.display_name.clone() {
|
||||||
|
this.child(display_name)
|
||||||
|
} else {
|
||||||
|
this.child(fallback_name)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
content = content
|
||||||
|
.flex()
|
||||||
|
.items_center()
|
||||||
|
.gap_2()
|
||||||
|
.child(
|
||||||
|
img("brand/avatar.png")
|
||||||
|
.flex_shrink_0()
|
||||||
|
.size_6()
|
||||||
|
.rounded_full(),
|
||||||
|
)
|
||||||
|
.child(fallback_name)
|
||||||
|
}
|
||||||
|
|
||||||
|
div()
|
||||||
|
.id(self.id.clone())
|
||||||
|
.h_8()
|
||||||
|
.px_1()
|
||||||
|
.flex()
|
||||||
|
.items_center()
|
||||||
|
.justify_between()
|
||||||
|
.text_xs()
|
||||||
|
.rounded_md()
|
||||||
|
.hover(|this| {
|
||||||
|
this.bg(cx.theme().sidebar_accent)
|
||||||
|
.text_color(cx.theme().sidebar_accent_foreground)
|
||||||
|
})
|
||||||
|
.child(content)
|
||||||
|
.child(
|
||||||
|
div()
|
||||||
|
.child(ago)
|
||||||
|
.text_color(cx.theme().sidebar_accent_foreground.opacity(0.7)),
|
||||||
|
)
|
||||||
|
.on_click(cx.listener(|this, _, cx| {
|
||||||
|
this.action(cx);
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Inbox {
|
||||||
|
label: SharedString,
|
||||||
|
items: Model<Option<Vec<View<InboxListItem>>>>,
|
||||||
|
is_loading: bool,
|
||||||
|
is_collapsed: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Inbox {
|
||||||
|
pub fn new(cx: &mut ViewContext<'_, Self>) -> Self {
|
||||||
|
let items = cx.new_model(|_| None);
|
||||||
|
|
||||||
|
cx.observe_global::<ChatRegistry>(|this, cx| {
|
||||||
|
if cx.global::<ChatRegistry>().is_initialized {
|
||||||
|
this.load(cx)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
|
||||||
|
Self {
|
||||||
|
items,
|
||||||
|
label: "Inbox".into(),
|
||||||
|
is_loading: true,
|
||||||
|
is_collapsed: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load(&mut self, cx: &mut ViewContext<Self>) {
|
||||||
|
// Hide loading indicator
|
||||||
|
self.set_loading(cx);
|
||||||
|
|
||||||
|
// Get all room's events
|
||||||
|
let events: Vec<Event> = cx.global::<ChatRegistry>().rooms.read(cx).clone();
|
||||||
|
|
||||||
|
cx.spawn(|view, mut async_cx| async move {
|
||||||
|
let client = get_client();
|
||||||
|
let mut views = Vec::new();
|
||||||
|
|
||||||
|
for event in events.into_iter() {
|
||||||
|
let metadata = async_cx
|
||||||
|
.background_executor()
|
||||||
|
.spawn(async move { client.database().metadata(event.pubkey).await })
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let item = async_cx
|
||||||
|
.new_view(|cx| {
|
||||||
|
if let Ok(metadata) = metadata {
|
||||||
|
InboxListItem::new(event, metadata, cx)
|
||||||
|
} else {
|
||||||
|
InboxListItem::new(event, None, cx)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
views.push(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = view.update(&mut async_cx, |this, cx| {
|
||||||
|
this.items.update(cx, |model, cx| {
|
||||||
|
*model = Some(views);
|
||||||
|
cx.notify()
|
||||||
|
});
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_loading(&mut self, cx: &mut ViewContext<Self>) {
|
||||||
|
self.is_loading = false;
|
||||||
|
cx.notify();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Collapsible for Inbox {
|
||||||
|
fn collapsed(mut self, collapsed: bool) -> Self {
|
||||||
|
self.is_collapsed = collapsed;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_collapsed(&self) -> bool {
|
||||||
|
self.is_collapsed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Render for Inbox {
|
||||||
|
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||||
|
let mut content = div();
|
||||||
|
|
||||||
|
if self.is_loading {
|
||||||
|
content = content.children((0..5).map(|_| {
|
||||||
|
div()
|
||||||
|
.h_8()
|
||||||
|
.px_1()
|
||||||
|
.flex()
|
||||||
|
.items_center()
|
||||||
|
.gap_2()
|
||||||
|
.child(Skeleton::new().flex_shrink_0().size_6().rounded_full())
|
||||||
|
.child(Skeleton::new().w_20().h_3().rounded_sm())
|
||||||
|
}))
|
||||||
|
} else if let Some(items) = self.items.read(cx).as_ref() {
|
||||||
|
content = content.children(items.clone())
|
||||||
|
} else {
|
||||||
|
// TODO: handle error
|
||||||
|
}
|
||||||
|
|
||||||
|
v_flex()
|
||||||
|
.px_2()
|
||||||
|
.gap_1()
|
||||||
|
.child(
|
||||||
|
div()
|
||||||
|
.id("inbox")
|
||||||
|
.h_7()
|
||||||
|
.px_1()
|
||||||
|
.flex()
|
||||||
|
.items_center()
|
||||||
|
.rounded_md()
|
||||||
|
.text_xs()
|
||||||
|
.font_semibold()
|
||||||
|
.text_color(cx.theme().sidebar_foreground.opacity(0.7))
|
||||||
|
.hover(|this| this.bg(cx.theme().sidebar_accent.opacity(0.7)))
|
||||||
|
.on_click(cx.listener(move |view, _event, cx| {
|
||||||
|
view.is_collapsed = !view.is_collapsed;
|
||||||
|
cx.notify();
|
||||||
|
}))
|
||||||
|
.child(
|
||||||
|
Icon::new(IconName::ChevronDown)
|
||||||
|
.size_6()
|
||||||
|
.when(self.is_collapsed, |this| {
|
||||||
|
this.rotate(percentage(270. / 360.))
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.child(self.label.clone()),
|
||||||
|
)
|
||||||
|
.when(!self.is_collapsed, |this| this.child(content))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,9 +1,10 @@
|
|||||||
use std::sync::Arc;
|
use crate::views::sidebar::inbox::Inbox;
|
||||||
|
|
||||||
use contact_list::ContactList;
|
use contact_list::ContactList;
|
||||||
use gpui::*;
|
use gpui::{
|
||||||
use nostr_sdk::Timestamp;
|
div, AnyElement, AppContext, Entity, EntityId, EventEmitter, FocusHandle, FocusableView,
|
||||||
use rnglib::{Language, RNG};
|
IntoElement, ParentElement, Render, SharedString, Styled, View, ViewContext, VisualContext,
|
||||||
|
WindowContext,
|
||||||
|
};
|
||||||
use ui::{
|
use ui::{
|
||||||
button::{Button, ButtonRounded, ButtonVariants},
|
button::{Button, ButtonRounded, ButtonVariants},
|
||||||
dock::{Panel, PanelEvent, PanelState},
|
dock::{Panel, PanelEvent, PanelState},
|
||||||
@@ -12,14 +13,8 @@ use ui::{
|
|||||||
v_flex, ContextModal, Icon, IconName, Sizable, StyledExt,
|
v_flex, ContextModal, Icon, IconName, Sizable, StyledExt,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::states::{account::AccountRegistry, chat::Room};
|
|
||||||
|
|
||||||
use super::{
|
|
||||||
app::{AddPanel, PanelKind},
|
|
||||||
inbox::Inbox,
|
|
||||||
};
|
|
||||||
|
|
||||||
mod contact_list;
|
mod contact_list;
|
||||||
|
mod inbox;
|
||||||
|
|
||||||
pub struct Sidebar {
|
pub struct Sidebar {
|
||||||
// Panel
|
// Panel
|
||||||
@@ -64,23 +59,7 @@ impl Sidebar {
|
|||||||
.on_click({
|
.on_click({
|
||||||
let contact_list = contact_list.clone();
|
let contact_list = contact_list.clone();
|
||||||
move |_, cx| {
|
move |_, cx| {
|
||||||
let members = contact_list.model.read(cx).selected();
|
// TODO: open room
|
||||||
let owner = cx.global::<AccountRegistry>().get().unwrap();
|
|
||||||
let rng = RNG::from(&Language::Roman);
|
|
||||||
let name = rng.generate_names(2, true).join("-").to_lowercase();
|
|
||||||
|
|
||||||
let room = Arc::new(Room::new(
|
|
||||||
owner,
|
|
||||||
members,
|
|
||||||
Timestamp::now(),
|
|
||||||
Some(name.into()),
|
|
||||||
cx,
|
|
||||||
));
|
|
||||||
|
|
||||||
cx.dispatch_action(Box::new(AddPanel {
|
|
||||||
panel: PanelKind::Room(room),
|
|
||||||
position: ui::dock::DockPlacement::Center,
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
use gpui::*;
|
use gpui::{
|
||||||
|
div, AnyElement, AppContext, EventEmitter, FocusHandle, FocusableView, IntoElement,
|
||||||
|
ParentElement, Render, SharedString, Styled, View, ViewContext, VisualContext, WindowContext,
|
||||||
|
};
|
||||||
use ui::{
|
use ui::{
|
||||||
button::Button,
|
button::Button,
|
||||||
dock::{Panel, PanelEvent, PanelState},
|
dock::{Panel, PanelEvent, PanelState},
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
use gpui::*;
|
|
||||||
use nostr_sdk::prelude::*;
|
|
||||||
use prelude::FluentBuilder as _;
|
|
||||||
|
|
||||||
use crate::theme::ActiveTheme;
|
use crate::theme::ActiveTheme;
|
||||||
use crate::Selectable;
|
use crate::Selectable;
|
||||||
|
use gpui::prelude::FluentBuilder;
|
||||||
|
use gpui::*;
|
||||||
|
use nostr_sdk::prelude::*;
|
||||||
|
|
||||||
#[derive(IntoElement)]
|
#[derive(IntoElement)]
|
||||||
pub struct Tab {
|
pub struct Tab {
|
||||||
|
|||||||
Reference in New Issue
Block a user