.
This commit is contained in:
@@ -63,11 +63,17 @@ pub struct ChatRegistry {
|
|||||||
/// Loading status of the registry
|
/// Loading status of the registry
|
||||||
loading: bool,
|
loading: bool,
|
||||||
|
|
||||||
|
/// Tracking the status of unwrapping gift wrap events.
|
||||||
|
tracking_flag: Arc<AtomicBool>,
|
||||||
|
|
||||||
|
/// Channel's sender for communication between nostr and gpui
|
||||||
|
sender: Sender<NostrEvent>,
|
||||||
|
|
||||||
/// Handle notifications asynchronous task
|
/// Handle notifications asynchronous task
|
||||||
notifications: Option<Task<Result<(), Error>>>,
|
notifications: Option<Task<Result<(), Error>>>,
|
||||||
|
|
||||||
/// Tasks for asynchronous operations
|
/// Tasks for asynchronous operations
|
||||||
tasks: SmallVec<[Task<()>; 3]>,
|
tasks: Vec<Task<()>>,
|
||||||
|
|
||||||
/// Subscriptions
|
/// Subscriptions
|
||||||
_subscriptions: SmallVec<[Subscription; 1]>,
|
_subscriptions: SmallVec<[Subscription; 1]>,
|
||||||
@@ -89,68 +95,41 @@ impl ChatRegistry {
|
|||||||
/// Create a new chat registry instance
|
/// Create a new chat registry instance
|
||||||
fn new(cx: &mut Context<Self>) -> Self {
|
fn new(cx: &mut Context<Self>) -> Self {
|
||||||
let nostr = NostrRegistry::global(cx);
|
let nostr = NostrRegistry::global(cx);
|
||||||
let client = nostr.read(cx).client();
|
let identity = nostr.read(cx).identity();
|
||||||
let device_signer = nostr.read(cx).device_signer();
|
let device_signer = nostr.read(cx).device_signer();
|
||||||
|
|
||||||
// A flag to indicate if the registry is loading
|
// A flag to indicate if the registry is loading
|
||||||
let status = Arc::new(AtomicBool::new(true));
|
let tracking_flag = Arc::new(AtomicBool::new(true));
|
||||||
|
|
||||||
// Channel for communication between nostr and gpui
|
// Channel for communication between nostr and gpui
|
||||||
let (tx, rx) = flume::bounded::<NostrEvent>(2048);
|
let (tx, rx) = flume::bounded::<NostrEvent>(2048);
|
||||||
|
|
||||||
let mut tasks = smallvec![];
|
let mut tasks = vec![];
|
||||||
let mut subscriptions = smallvec![];
|
let mut subscriptions = smallvec![];
|
||||||
|
|
||||||
let notifications =
|
subscriptions.push(
|
||||||
Some(
|
// Observe the identity
|
||||||
cx.background_spawn({
|
cx.observe(&identity, |this, state, cx| {
|
||||||
let client = client.clone();
|
if state.read(cx).has_public_key() {
|
||||||
let device_signer = device_signer.read(cx).clone();
|
// Handle nostr notifications
|
||||||
|
this.handle_notifications(cx);
|
||||||
let loading = Arc::clone(&status);
|
// Track unwrapping progress
|
||||||
let tx = tx.clone();
|
this.tracking(cx);
|
||||||
|
|
||||||
async move {
|
|
||||||
Self::handle_notifications(&client, &device_signer, &loading, &tx).await
|
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
subscriptions.push(
|
subscriptions.push(
|
||||||
// Observe the device signer state
|
// Observe the device signer state
|
||||||
cx.observe(&device_signer, {
|
cx.observe(&device_signer, |this, state, cx| {
|
||||||
let loading = Arc::clone(&status);
|
|
||||||
let tx = tx.clone();
|
|
||||||
|
|
||||||
move |this, state, cx| {
|
|
||||||
if state.read(cx).is_some() {
|
if state.read(cx).is_some() {
|
||||||
this.notifications = Some(cx.background_spawn({
|
this.handle_notifications(cx);
|
||||||
let nostr = NostrRegistry::global(cx);
|
|
||||||
let client = nostr.read(cx).client();
|
|
||||||
let device_signer = state.read(cx).clone();
|
|
||||||
|
|
||||||
let loading = Arc::clone(&loading);
|
|
||||||
let tx = tx.clone();
|
|
||||||
|
|
||||||
async move {
|
|
||||||
Self::handle_notifications(&client, &device_signer, &loading, &tx)
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
tasks.push(
|
tasks.push(
|
||||||
// Handle unwrapping progress
|
// Update GPUI states
|
||||||
cx.background_spawn(
|
|
||||||
async move { Self::unwrapping_status(&client, &status, &tx).await },
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
tasks.push(
|
|
||||||
// Handle new messages
|
|
||||||
cx.spawn(async move |this, cx| {
|
cx.spawn(async move |this, cx| {
|
||||||
while let Ok(message) = rx.recv_async().await {
|
while let Ok(message) = rx.recv_async().await {
|
||||||
match message {
|
match message {
|
||||||
@@ -181,18 +160,24 @@ impl ChatRegistry {
|
|||||||
Self {
|
Self {
|
||||||
rooms: vec![],
|
rooms: vec![],
|
||||||
loading: true,
|
loading: true,
|
||||||
notifications,
|
tracking_flag,
|
||||||
|
sender: tx.clone(),
|
||||||
|
notifications: None,
|
||||||
tasks,
|
tasks,
|
||||||
_subscriptions: subscriptions,
|
_subscriptions: subscriptions,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_notifications(
|
/// Handle nostr notifications
|
||||||
client: &Client,
|
fn handle_notifications(&mut self, cx: &mut Context<Self>) {
|
||||||
device_signer: &Option<Arc<dyn NostrSigner>>,
|
let nostr = NostrRegistry::global(cx);
|
||||||
loading: &Arc<AtomicBool>,
|
let client = nostr.read(cx).client();
|
||||||
tx: &Sender<NostrEvent>,
|
let device_signer = nostr.read(cx).device_signer().read(cx).clone();
|
||||||
) -> Result<(), Error> {
|
|
||||||
|
let status = self.tracking_flag.clone();
|
||||||
|
let tx = self.sender.clone();
|
||||||
|
|
||||||
|
self.tasks.push(cx.background_spawn(async move {
|
||||||
let initialized_at = Timestamp::now();
|
let initialized_at = Timestamp::now();
|
||||||
let subscription_id = SubscriptionId::new(GIFTWRAP_SUBSCRIPTION);
|
let subscription_id = SubscriptionId::new(GIFTWRAP_SUBSCRIPTION);
|
||||||
|
|
||||||
@@ -218,7 +203,7 @@ impl ChatRegistry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Extract the rumor from the gift wrap event
|
// Extract the rumor from the gift wrap event
|
||||||
match Self::extract_rumor(client, device_signer, event.as_ref()).await {
|
match Self::extract_rumor(&client, &device_signer, event.as_ref()).await {
|
||||||
Ok(rumor) => match rumor.created_at >= initialized_at {
|
Ok(rumor) => match rumor.created_at >= initialized_at {
|
||||||
true => {
|
true => {
|
||||||
// Check if the event is sent by coop
|
// Check if the event is sent by coop
|
||||||
@@ -236,7 +221,7 @@ impl ChatRegistry {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
false => {
|
false => {
|
||||||
loading.store(true, Ordering::Release);
|
status.store(true, Ordering::Release);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
@@ -252,12 +237,20 @@ impl ChatRegistry {
|
|||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}));
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn unwrapping_status(client: &Client, status: &Arc<AtomicBool>, tx: &Sender<NostrEvent>) {
|
/// Tracking the status of unwrapping gift wrap events.
|
||||||
|
fn tracking(&mut self, cx: &mut Context<Self>) {
|
||||||
|
let nostr = NostrRegistry::global(cx);
|
||||||
|
let client = nostr.read(cx).client();
|
||||||
|
|
||||||
|
let status = self.tracking_flag.clone();
|
||||||
|
let tx = self.sender.clone();
|
||||||
|
|
||||||
|
self.notifications = Some(cx.background_spawn(async move {
|
||||||
let loop_duration = Duration::from_secs(12);
|
let loop_duration = Duration::from_secs(12);
|
||||||
|
|
||||||
let mut is_start_processing = false;
|
let mut is_start_processing = false;
|
||||||
let mut total_loops = 0;
|
let mut total_loops = 0;
|
||||||
|
|
||||||
@@ -268,7 +261,12 @@ impl ChatRegistry {
|
|||||||
if status.load(Ordering::Acquire) {
|
if status.load(Ordering::Acquire) {
|
||||||
is_start_processing = true;
|
is_start_processing = true;
|
||||||
// Reset gift wrap processing flag
|
// Reset gift wrap processing flag
|
||||||
_ = status.compare_exchange(true, false, Ordering::Release, Ordering::Relaxed);
|
_ = status.compare_exchange(
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
Ordering::Release,
|
||||||
|
Ordering::Relaxed,
|
||||||
|
);
|
||||||
|
|
||||||
tx.send_async(NostrEvent::Unwrapping(true)).await.ok();
|
tx.send_async(NostrEvent::Unwrapping(true)).await.ok();
|
||||||
} else {
|
} else {
|
||||||
@@ -285,6 +283,7 @@ impl ChatRegistry {
|
|||||||
}
|
}
|
||||||
smol::Timer::after(loop_duration).await;
|
smol::Timer::after(loop_duration).await;
|
||||||
}
|
}
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the loading status of the chat registry
|
/// Get the loading status of the chat registry
|
||||||
|
|||||||
Reference in New Issue
Block a user