wip: refactor

This commit is contained in:
2024-12-17 18:51:06 +07:00
parent 377f169420
commit 0682612d42
27 changed files with 486 additions and 259 deletions

View File

@@ -58,6 +58,10 @@ impl AccountRegistry {
.detach();
}
pub fn get(&self) -> Option<PublicKey> {
self.public_key
}
pub fn set_user(&mut self, public_key: Option<PublicKey>) {
self.public_key = public_key
}

View File

@@ -1,75 +1,92 @@
use flume::Receiver;
use gpui::*;
use itertools::Itertools;
use nostr_sdk::prelude::*;
use std::cmp::Reverse;
use crate::get_client;
pub struct ChatRegistry {
events: Model<Option<Vec<Event>>>,
chats: Model<Option<Vec<Event>>>,
is_initialized: bool,
// Use for receive new message
pub(crate) receiver: Receiver<Event>,
}
impl Global for ChatRegistry {}
impl ChatRegistry {
pub fn set_global(cx: &mut AppContext) {
let events = cx.new_model(|_| None);
pub fn set_global(cx: &mut AppContext, receiver: Receiver<Event>) {
let chats = cx.new_model(|_| None);
cx.set_global(Self::new(events));
cx.set_global(Self::new(chats, receiver));
}
pub fn load(&self, cx: &mut AppContext) {
pub fn load(&mut self, cx: &mut AppContext) {
let mut async_cx = cx.to_async();
let async_events = self.events.clone();
let async_chats = self.chats.clone();
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();
if !self.is_initialized {
self.is_initialized = true;
let filter = Filter::new()
.kind(Kind::PrivateDirectMessage)
.pubkey(public_key);
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 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 messages from current user
.unique_by(|ev| ev.pubkey) // Get unique list
.sorted_by_key(|ev| Reverse(ev.created_at)) // Sort by created at
.collect::<Vec<_>>()
} else {
Vec::new()
}
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();
})
.await;
async_cx.update_model(&async_events, |a, b| {
*a = Some(events);
b.notify();
})
})
.detach();
.detach();
}
}
pub fn push(&self, event: Event, cx: &mut AppContext) {
cx.update_model(&self.events, |a, b| {
if let Some(events) = a {
events.push(event);
b.notify();
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 get(&self, cx: &WindowContext) -> Option<Vec<Event>> {
self.events.read(cx).clone()
self.chats.read(cx).clone()
}
fn new(events: Model<Option<Vec<Event>>>) -> Self {
Self { events }
fn new(chats: Model<Option<Vec<Event>>>, receiver: Receiver<Event>) -> Self {
Self {
chats,
receiver,
is_initialized: false,
}
}
}

View File

@@ -0,0 +1,41 @@
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 contains(&self, public_key: PublicKey) -> bool {
self.seens.contains(&public_key)
}
pub fn seen(&mut self, public_key: PublicKey) {
if !self.seens.contains(&public_key) {
self.seens.push(public_key);
}
}
fn new(reqs: Sender<Signal>) -> Self {
Self {
seens: Vec::new(),
reqs,
}
}
}

View File

@@ -1,3 +1,3 @@
pub mod account;
pub mod chat;
pub mod signal;
pub mod metadata;

View File

@@ -1,38 +0,0 @@
use gpui::*;
use nostr_sdk::prelude::*;
use std::sync::Arc;
use tokio::sync::broadcast::Sender;
pub struct SignalRegistry {
public_keys: Vec<PublicKey>,
pub queue: Arc<Sender<PublicKey>>,
}
impl Global for SignalRegistry {}
impl SignalRegistry {
pub fn set_global(cx: &mut AppContext, queue: Arc<Sender<PublicKey>>) {
cx.set_global(Self::new(queue));
}
pub fn contains(&self, public_key: PublicKey) -> bool {
self.public_keys.contains(&public_key)
}
pub fn push(&mut self, public_key: PublicKey) {
self.public_keys.push(public_key);
}
pub fn add_to_queue(&mut self, public_key: PublicKey) {
if let Err(e) = self.queue.send(public_key) {
println!("Dropped: {}", e)
}
}
fn new(queue: Arc<Sender<PublicKey>>) -> Self {
Self {
public_keys: Vec::new(),
queue,
}
}
}