wip: refactor
This commit is contained in:
@@ -1,91 +1,35 @@
|
||||
use flume::Receiver;
|
||||
use gpui::*;
|
||||
use itertools::Itertools;
|
||||
use nostr_sdk::prelude::*;
|
||||
|
||||
use crate::get_client;
|
||||
|
||||
pub struct ChatRegistry {
|
||||
chats: Model<Option<Vec<Event>>>,
|
||||
is_initialized: bool,
|
||||
// Use for receive new message
|
||||
pub(crate) receiver: Receiver<Event>,
|
||||
pub new_messages: Vec<Event>,
|
||||
pub reload: bool,
|
||||
pub is_initialized: bool,
|
||||
}
|
||||
|
||||
impl Global for ChatRegistry {}
|
||||
|
||||
impl ChatRegistry {
|
||||
pub fn set_global(cx: &mut AppContext, receiver: Receiver<Event>) {
|
||||
let chats = cx.new_model(|_| None);
|
||||
|
||||
cx.set_global(Self::new(chats, receiver));
|
||||
pub fn set_global(cx: &mut AppContext) {
|
||||
cx.set_global(Self::new());
|
||||
}
|
||||
|
||||
pub fn load(&mut self, cx: &mut AppContext) {
|
||||
let mut async_cx = cx.to_async();
|
||||
let async_chats = self.chats.clone();
|
||||
|
||||
if !self.is_initialized {
|
||||
self.is_initialized = true;
|
||||
|
||||
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) // Get unique list
|
||||
.collect::<Vec<_>>()
|
||||
} else {
|
||||
Vec::new()
|
||||
}
|
||||
})
|
||||
.await;
|
||||
|
||||
async_cx.update_model(&async_chats, |a, b| {
|
||||
*a = Some(events);
|
||||
b.notify();
|
||||
})
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
pub fn set_init(&mut self) {
|
||||
self.is_initialized = true;
|
||||
}
|
||||
|
||||
pub fn push(&self, event: Event, cx: &mut AppContext) {
|
||||
cx.update_model(&self.chats, |a, b| {
|
||||
if let Some(chats) = a {
|
||||
if let Some(index) = chats.iter().position(|c| c.pubkey == event.pubkey) {
|
||||
chats.swap_remove(index);
|
||||
chats.push(event);
|
||||
|
||||
b.notify();
|
||||
} else {
|
||||
chats.push(event);
|
||||
b.notify();
|
||||
}
|
||||
}
|
||||
})
|
||||
pub fn set_reload(&mut self) {
|
||||
self.reload = true;
|
||||
}
|
||||
|
||||
pub fn get(&self, cx: &WindowContext) -> Option<Vec<Event>> {
|
||||
self.chats.read(cx).clone()
|
||||
pub fn push(&mut self, event: Event) {
|
||||
self.new_messages.push(event);
|
||||
}
|
||||
|
||||
fn new(chats: Model<Option<Vec<Event>>>, receiver: Receiver<Event>) -> Self {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
chats,
|
||||
receiver,
|
||||
new_messages: Vec::new(),
|
||||
reload: false,
|
||||
is_initialized: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,25 +1,15 @@
|
||||
use gpui::*;
|
||||
use nostr_sdk::prelude::*;
|
||||
use tokio::sync::mpsc::Sender;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum Signal {
|
||||
/// Send
|
||||
DONE(PublicKey),
|
||||
/// Receive
|
||||
REQ(PublicKey),
|
||||
}
|
||||
|
||||
pub struct MetadataRegistry {
|
||||
seens: Vec<PublicKey>,
|
||||
pub reqs: Sender<Signal>,
|
||||
}
|
||||
|
||||
impl Global for MetadataRegistry {}
|
||||
|
||||
impl MetadataRegistry {
|
||||
pub fn set_global(cx: &mut AppContext, reqs: Sender<Signal>) {
|
||||
cx.set_global(Self::new(reqs));
|
||||
pub fn set_global(cx: &mut AppContext) {
|
||||
cx.set_global(Self::new());
|
||||
}
|
||||
|
||||
pub fn contains(&self, public_key: PublicKey) -> bool {
|
||||
@@ -32,10 +22,7 @@ impl MetadataRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
fn new(reqs: Sender<Signal>) -> Self {
|
||||
Self {
|
||||
seens: Vec::new(),
|
||||
reqs,
|
||||
}
|
||||
fn new() -> Self {
|
||||
Self { seens: Vec::new() }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
pub mod account;
|
||||
pub mod chat;
|
||||
pub mod metadata;
|
||||
pub mod signal;
|
||||
|
||||
32
crates/app/src/states/signal.rs
Normal file
32
crates/app/src/states/signal.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
use gpui::*;
|
||||
use nostr_sdk::prelude::*;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::mpsc::UnboundedSender;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum Signal {
|
||||
/// Request metadata
|
||||
ReqMetadata(PublicKey),
|
||||
/// Receive metadata
|
||||
RecvMetadata(PublicKey),
|
||||
/// Receive EOSE
|
||||
RecvEose(SubscriptionId),
|
||||
/// Receive event
|
||||
RecvEvent(Event),
|
||||
}
|
||||
|
||||
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) }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user