chore: clean up
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
use std::sync::atomic::Ordering;
|
||||
|
||||
use global::constants::KEYRING_URL;
|
||||
use global::first_run;
|
||||
use global::css;
|
||||
use gpui::{App, AppContext, Context, Entity, Global, Subscription, Window};
|
||||
use nostr_sdk::prelude::*;
|
||||
use smallvec::{smallvec, SmallVec};
|
||||
@@ -59,6 +61,7 @@ impl ClientKeys {
|
||||
return;
|
||||
}
|
||||
|
||||
let css = css();
|
||||
let read_client_keys = cx.read_credentials(KEYRING_URL);
|
||||
|
||||
cx.spawn_in(window, async move |this, cx| {
|
||||
@@ -73,7 +76,7 @@ impl ClientKeys {
|
||||
this.set_keys(Some(keys), false, true, cx);
|
||||
})
|
||||
.ok();
|
||||
} else if *first_run() {
|
||||
} else if css.is_first_run.load(Ordering::Acquire) {
|
||||
// If this is the first run, generate new keys and use them for the client keys
|
||||
this.update(cx, |this, cx| {
|
||||
this.new_keys(cx);
|
||||
|
||||
@@ -9,12 +9,11 @@ use auto_update::AutoUpdater;
|
||||
use client_keys::ClientKeys;
|
||||
use common::display::ReadableProfile;
|
||||
use common::event::EventUtils;
|
||||
use flume::{Receiver, Sender};
|
||||
use global::constants::{
|
||||
ACCOUNT_IDENTIFIER, BOOTSTRAP_RELAYS, DEFAULT_SIDEBAR_WIDTH, METADATA_BATCH_LIMIT,
|
||||
METADATA_BATCH_TIMEOUT, SEARCH_RELAYS,
|
||||
};
|
||||
use global::{css, ingester, nostr_client, AuthRequest, Notice, Signal, UnwrappingStatus};
|
||||
use global::{css, nostr_client, AuthRequest, Notice, SignalKind, UnwrappingStatus};
|
||||
use gpui::prelude::FluentBuilder;
|
||||
use gpui::{
|
||||
div, px, rems, App, AppContext, AsyncWindowContext, Axis, Context, Entity, InteractiveElement,
|
||||
@@ -92,7 +91,6 @@ impl ChatSpace {
|
||||
let title_bar = cx.new(|_| TitleBar::new());
|
||||
let dock = cx.new(|cx| DockArea::new(window, cx));
|
||||
|
||||
let (pubkey_tx, pubkey_rx) = flume::bounded::<PublicKey>(1024);
|
||||
let mut subscriptions = smallvec![];
|
||||
let mut tasks = smallvec![];
|
||||
|
||||
@@ -147,7 +145,7 @@ impl ChatSpace {
|
||||
.await
|
||||
.expect("Failed connect the bootstrap relays. Please restart the application.");
|
||||
|
||||
Self::process_nostr_events(&pubkey_tx)
|
||||
Self::process_nostr_events()
|
||||
.await
|
||||
.expect("Failed to handle nostr events. Please restart the application.");
|
||||
}),
|
||||
@@ -171,7 +169,7 @@ impl ChatSpace {
|
||||
tasks.push(
|
||||
// Listen all metadata requests then batch them into single subscription
|
||||
cx.background_spawn(async move {
|
||||
Self::process_batching_metadata(&pubkey_rx).await;
|
||||
Self::process_batching_metadata().await;
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -215,7 +213,7 @@ impl ChatSpace {
|
||||
|
||||
async fn observe_signer() {
|
||||
let client = nostr_client();
|
||||
let ingester = ingester();
|
||||
let css = css();
|
||||
let stream_timeout = Duration::from_secs(5);
|
||||
let loop_duration = Duration::from_secs(1);
|
||||
|
||||
@@ -231,7 +229,7 @@ impl ChatSpace {
|
||||
};
|
||||
|
||||
// Notify the app that the signer has been set.
|
||||
ingester.send(Signal::SignerSet(public_key)).await;
|
||||
css.signal.send(SignalKind::SignerSet(public_key)).await;
|
||||
|
||||
// Subscribe to the NIP-65 relays for the public key.
|
||||
let filter = Filter::new()
|
||||
@@ -239,36 +237,49 @@ impl ChatSpace {
|
||||
.author(public_key)
|
||||
.limit(1);
|
||||
|
||||
let mut nip65_found = false;
|
||||
|
||||
match client
|
||||
.stream_events_from(BOOTSTRAP_RELAYS, filter, stream_timeout)
|
||||
.await
|
||||
{
|
||||
Ok(mut stream) => {
|
||||
let mut processed_ids = HashSet::new();
|
||||
|
||||
if let Some(event) = stream.next().await {
|
||||
if processed_ids.insert(event.id) {
|
||||
// Fetch user's metadata event
|
||||
Self::fetch_single_event(Kind::Metadata, event.pubkey).await;
|
||||
|
||||
// Fetch user's contact list event
|
||||
Self::fetch_single_event(Kind::ContactList, event.pubkey).await;
|
||||
|
||||
// Fetch user's inbox relays event
|
||||
Self::fetch_nip17_relays(event.pubkey).await;
|
||||
|
||||
break;
|
||||
}
|
||||
if stream.next().await.is_some() {
|
||||
nip65_found = true;
|
||||
} else {
|
||||
ingester.send(Signal::DmRelayNotFound).await;
|
||||
// Timeout
|
||||
css.signal.send(SignalKind::RelaysNotFound).await;
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("Error fetching NIP-17 Relay: {e:?}");
|
||||
ingester.send(Signal::DmRelayNotFound).await;
|
||||
log::error!("Error fetching NIP-65 Relay: {e:?}");
|
||||
css.signal.send(SignalKind::RelaysNotFound).await;
|
||||
}
|
||||
};
|
||||
|
||||
if nip65_found {
|
||||
// Subscribe to the NIP-17 relays for the public key.
|
||||
let filter = Filter::new()
|
||||
.kind(Kind::InboxRelays)
|
||||
.author(public_key)
|
||||
.limit(1);
|
||||
|
||||
match client.stream_events(filter, stream_timeout).await {
|
||||
Ok(mut stream) => {
|
||||
if stream.next().await.is_some() {
|
||||
break;
|
||||
} else {
|
||||
// Timeout
|
||||
css.signal.send(SignalKind::RelaysNotFound).await;
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("Error fetching NIP-17 Relay: {e:?}");
|
||||
css.signal.send(SignalKind::RelaysNotFound).await;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -276,7 +287,6 @@ impl ChatSpace {
|
||||
async fn observe_giftwrap() {
|
||||
let client = nostr_client();
|
||||
let css = css();
|
||||
let ingester = ingester();
|
||||
let loop_duration = Duration::from_secs(20);
|
||||
let mut is_start_processing = false;
|
||||
let mut total_loops = 0;
|
||||
@@ -296,14 +306,14 @@ impl ChatSpace {
|
||||
Ordering::Relaxed,
|
||||
);
|
||||
|
||||
let signal = Signal::GiftWrapProcess(UnwrappingStatus::Processing);
|
||||
ingester.send(signal).await;
|
||||
let signal = SignalKind::GiftWrapStatus(UnwrappingStatus::Processing);
|
||||
css.signal.send(signal).await;
|
||||
} else {
|
||||
// Only run further if we are already processing
|
||||
// Wait until after 2 loops to prevent exiting early while events are still being processed
|
||||
if is_start_processing && total_loops >= 2 {
|
||||
let signal = Signal::GiftWrapProcess(UnwrappingStatus::Complete);
|
||||
ingester.send(signal).await;
|
||||
let signal = SignalKind::GiftWrapStatus(UnwrappingStatus::Complete);
|
||||
css.signal.send(signal).await;
|
||||
|
||||
// Reset the counter
|
||||
is_start_processing = false;
|
||||
@@ -316,7 +326,8 @@ impl ChatSpace {
|
||||
}
|
||||
}
|
||||
|
||||
async fn process_batching_metadata(rx: &Receiver<PublicKey>) {
|
||||
async fn process_batching_metadata() {
|
||||
let css = css();
|
||||
let timeout = Duration::from_millis(METADATA_BATCH_TIMEOUT);
|
||||
let mut processed_pubkeys: HashSet<PublicKey> = HashSet::new();
|
||||
let mut batch: HashSet<PublicKey> = HashSet::new();
|
||||
@@ -331,7 +342,7 @@ impl ChatSpace {
|
||||
loop {
|
||||
let futs = smol::future::or(
|
||||
async move {
|
||||
if let Ok(public_key) = rx.recv_async().await {
|
||||
if let Ok(public_key) = css.ingester.receiver().recv_async().await {
|
||||
BatchEvent::PublicKey(public_key)
|
||||
} else {
|
||||
BatchEvent::Closed
|
||||
@@ -366,9 +377,8 @@ impl ChatSpace {
|
||||
}
|
||||
}
|
||||
|
||||
async fn process_nostr_events(pubkey_tx: &Sender<PublicKey>) -> Result<(), Error> {
|
||||
async fn process_nostr_events() -> Result<(), Error> {
|
||||
let client = nostr_client();
|
||||
let ingester = ingester();
|
||||
let css = css();
|
||||
|
||||
let mut processed_events: HashSet<EventId> = HashSet::new();
|
||||
@@ -396,6 +406,36 @@ impl ChatSpace {
|
||||
}
|
||||
|
||||
match event.kind {
|
||||
Kind::RelayList => {
|
||||
if let Ok(true) = Self::is_self_event(&event).await {
|
||||
// Fetch user's metadata event
|
||||
Self::fetch_single_event(Kind::Metadata, event.pubkey).await;
|
||||
|
||||
// Fetch user's contact list event
|
||||
Self::fetch_single_event(Kind::ContactList, event.pubkey).await;
|
||||
}
|
||||
}
|
||||
Kind::InboxRelays => {
|
||||
let relays = nip17::extract_relay_list(&event).collect_vec();
|
||||
|
||||
if !relays.is_empty() {
|
||||
for relay in relays.clone().into_iter() {
|
||||
if client.add_relay(relay).await.is_err() {
|
||||
let notice = Notice::RelayFailed(relay.clone());
|
||||
css.signal.send(SignalKind::Notice(notice)).await;
|
||||
}
|
||||
if client.connect_relay(relay).await.is_err() {
|
||||
let notice = Notice::RelayFailed(relay.clone());
|
||||
css.signal.send(SignalKind::Notice(notice)).await;
|
||||
}
|
||||
}
|
||||
|
||||
// Subscribe to gift wrap events only in the current user's NIP-17 relays
|
||||
Self::fetch_gift_wrap(relays, event.pubkey).await;
|
||||
} else {
|
||||
css.signal.send(SignalKind::RelaysNotFound).await;
|
||||
}
|
||||
}
|
||||
Kind::ContactList => {
|
||||
if let Ok(true) = Self::is_self_event(&event).await {
|
||||
let public_keys = event.tags.public_keys().copied().collect_vec();
|
||||
@@ -411,28 +451,28 @@ impl ChatSpace {
|
||||
}
|
||||
}
|
||||
Kind::Metadata => {
|
||||
if let Ok(metadata) = Metadata::from_json(&event.content) {
|
||||
let profile = Profile::new(event.pubkey, metadata);
|
||||
ingester.send(Signal::Metadata(profile)).await;
|
||||
}
|
||||
let metadata = Metadata::from_json(&event.content).unwrap_or_default();
|
||||
let profile = Profile::new(event.pubkey, metadata);
|
||||
|
||||
css.signal.send(SignalKind::NewProfile(profile)).await;
|
||||
}
|
||||
Kind::GiftWrap => {
|
||||
Self::unwrap_gift_wrap(&event, pubkey_tx).await;
|
||||
Self::unwrap_gift_wrap(&event).await;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
RelayMessage::EndOfStoredEvents(subscription_id) => {
|
||||
if *subscription_id == css.gift_wrap_sub_id {
|
||||
let signal = Signal::GiftWrapProcess(UnwrappingStatus::Processing);
|
||||
ingester.send(signal).await;
|
||||
let signal = SignalKind::GiftWrapStatus(UnwrappingStatus::Processing);
|
||||
css.signal.send(signal).await;
|
||||
}
|
||||
}
|
||||
RelayMessage::Auth { challenge } => {
|
||||
if challenges.insert(challenge.clone()) {
|
||||
let req = AuthRequest::new(challenge, relay_url);
|
||||
// Send a signal to the ingester to handle the auth request
|
||||
ingester.send(Signal::Auth(req)).await;
|
||||
css.signal.send(SignalKind::Auth(req)).await;
|
||||
}
|
||||
}
|
||||
RelayMessage::Ok {
|
||||
@@ -458,17 +498,16 @@ impl ChatSpace {
|
||||
}
|
||||
|
||||
async fn process_nostr_signals(view: WeakEntity<ChatSpace>, cx: &mut AsyncWindowContext) {
|
||||
let ingester = ingester();
|
||||
let signals = ingester.signals();
|
||||
let css = css();
|
||||
let mut is_open_proxy_modal = false;
|
||||
|
||||
while let Ok(signal) = signals.recv_async().await {
|
||||
while let Ok(signal) = css.signal.receiver().recv_async().await {
|
||||
cx.update(|window, cx| {
|
||||
let registry = Registry::global(cx);
|
||||
let settings = AppSettings::global(cx);
|
||||
|
||||
match signal {
|
||||
Signal::SignerSet(public_key) => {
|
||||
SignalKind::SignerSet(public_key) => {
|
||||
window.close_modal(cx);
|
||||
|
||||
// Setup the default layout for current workspace
|
||||
@@ -488,7 +527,7 @@ impl ChatSpace {
|
||||
this.load_rooms(window, cx);
|
||||
});
|
||||
}
|
||||
Signal::SignerUnset => {
|
||||
SignalKind::SignerUnset => {
|
||||
// Setup the onboarding layout for current workspace
|
||||
view.update(cx, |this, cx| {
|
||||
this.set_onboarding_layout(window, cx);
|
||||
@@ -500,7 +539,7 @@ impl ChatSpace {
|
||||
this.reset(cx);
|
||||
});
|
||||
}
|
||||
Signal::Auth(req) => {
|
||||
SignalKind::Auth(req) => {
|
||||
let url = &req.url;
|
||||
let auto_auth = AppSettings::get_auto_auth(cx);
|
||||
let is_authenticated = AppSettings::read_global(cx).is_authenticated(url);
|
||||
@@ -518,7 +557,7 @@ impl ChatSpace {
|
||||
})
|
||||
.ok();
|
||||
}
|
||||
Signal::ProxyDown => {
|
||||
SignalKind::ProxyDown => {
|
||||
if !is_open_proxy_modal {
|
||||
is_open_proxy_modal = true;
|
||||
|
||||
@@ -528,28 +567,28 @@ impl ChatSpace {
|
||||
.ok();
|
||||
}
|
||||
}
|
||||
Signal::GiftWrapProcess(status) => {
|
||||
SignalKind::GiftWrapStatus(status) => {
|
||||
registry.update(cx, |this, cx| {
|
||||
this.set_unwrapping_status(status, cx);
|
||||
});
|
||||
}
|
||||
Signal::Metadata(profile) => {
|
||||
SignalKind::NewProfile(profile) => {
|
||||
registry.update(cx, |this, cx| {
|
||||
this.insert_or_update_person(profile, cx);
|
||||
});
|
||||
}
|
||||
Signal::Message((gift_wrap_id, event)) => {
|
||||
SignalKind::NewMessage((gift_wrap_id, event)) => {
|
||||
registry.update(cx, |this, cx| {
|
||||
this.event_to_message(gift_wrap_id, event, window, cx);
|
||||
});
|
||||
}
|
||||
Signal::DmRelayNotFound => {
|
||||
SignalKind::RelaysNotFound => {
|
||||
view.update(cx, |this, cx| {
|
||||
this.set_required_relays(cx);
|
||||
})
|
||||
.ok();
|
||||
}
|
||||
Signal::Notice(msg) => {
|
||||
SignalKind::Notice(msg) => {
|
||||
window.push_notification(msg.as_str(), cx);
|
||||
}
|
||||
};
|
||||
@@ -578,49 +617,7 @@ impl ChatSpace {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn fetch_nip17_relays(public_key: PublicKey) {
|
||||
let client = nostr_client();
|
||||
let ingester = ingester();
|
||||
let filter = Filter::new()
|
||||
.kind(Kind::InboxRelays)
|
||||
.author(public_key)
|
||||
.limit(1);
|
||||
|
||||
match client.stream_events(filter, Duration::from_secs(5)).await {
|
||||
Ok(mut stream) => {
|
||||
let mut processed_ids = HashSet::new();
|
||||
|
||||
if let Some(event) = stream.next().await {
|
||||
if processed_ids.insert(event.id) {
|
||||
let relays = nip17::extract_relay_list(&event).collect_vec();
|
||||
|
||||
if !relays.is_empty() {
|
||||
for relay in relays.clone().into_iter() {
|
||||
if client.add_relay(relay).await.is_err() {
|
||||
let notice = Notice::RelayFailed(relay.clone());
|
||||
ingester.send(Signal::Notice(notice)).await;
|
||||
}
|
||||
if client.connect_relay(relay).await.is_err() {
|
||||
let notice = Notice::RelayFailed(relay.clone());
|
||||
ingester.send(Signal::Notice(notice)).await;
|
||||
}
|
||||
}
|
||||
|
||||
// Subscribe to gift wrap events only in the current user's NIP-17 relays
|
||||
Self::fetch_gift_wrap(relays, event.pubkey).await;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ingester.send(Signal::DmRelayNotFound).await;
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("Error fetching NIP-17 Relay: {e:?}");
|
||||
ingester.send(Signal::DmRelayNotFound).await;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Fetches gift wrap events for a given public key and relays
|
||||
pub async fn fetch_gift_wrap(relays: Vec<&RelayUrl>, public_key: PublicKey) {
|
||||
let client = nostr_client();
|
||||
let sub_id = css().gift_wrap_sub_id.clone();
|
||||
@@ -697,9 +694,8 @@ impl ChatSpace {
|
||||
}
|
||||
|
||||
/// Unwraps a gift-wrapped event and processes its contents.
|
||||
async fn unwrap_gift_wrap(target: &Event, pubkey_tx: &Sender<PublicKey>) {
|
||||
async fn unwrap_gift_wrap(target: &Event) {
|
||||
let client = nostr_client();
|
||||
let ingester = ingester();
|
||||
let css = css();
|
||||
let mut message: Option<Event> = None;
|
||||
|
||||
@@ -720,14 +716,17 @@ impl ChatSpace {
|
||||
if let Some(event) = message {
|
||||
// Send all pubkeys to the metadata batch to sync data
|
||||
for public_key in event.all_pubkeys() {
|
||||
pubkey_tx.send_async(public_key).await.ok();
|
||||
css.ingester.send(public_key).await;
|
||||
}
|
||||
|
||||
match event.created_at >= css.init_at {
|
||||
// New message: send a signal to notify the UI
|
||||
true => {
|
||||
// A small delay to prevent UI flickering
|
||||
smol::Timer::after(Duration::from_millis(200)).await;
|
||||
ingester.send(Signal::Message((target.id, event))).await;
|
||||
css.signal
|
||||
.send(SignalKind::NewMessage((target.id, event)))
|
||||
.await;
|
||||
}
|
||||
// Old message: Coop is probably processing the user's messages during initial load
|
||||
false => {
|
||||
@@ -1130,7 +1129,7 @@ impl ChatSpace {
|
||||
fn on_sign_out(&mut self, _e: &Logout, _window: &mut Window, cx: &mut Context<Self>) {
|
||||
cx.background_spawn(async move {
|
||||
let client = nostr_client();
|
||||
let ingester = ingester();
|
||||
let css = css();
|
||||
|
||||
let filter = Filter::new()
|
||||
.kind(Kind::ApplicationSpecificData)
|
||||
@@ -1143,7 +1142,7 @@ impl ChatSpace {
|
||||
client.reset().await;
|
||||
|
||||
// Notify the channel about the signer being unset
|
||||
ingester.send(Signal::SignerUnset).await;
|
||||
css.signal.send(SignalKind::SignerUnset).await;
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
@@ -1367,7 +1366,7 @@ impl ChatSpace {
|
||||
|
||||
this._tasks.push(cx.background_spawn(async move {
|
||||
let client = nostr_client();
|
||||
let ingester = ingester();
|
||||
let css = css();
|
||||
|
||||
if proxy.start().await.is_ok() {
|
||||
webbrowser::open(&url).ok();
|
||||
@@ -1398,7 +1397,7 @@ impl ChatSpace {
|
||||
|
||||
break;
|
||||
} else {
|
||||
ingester.send(Signal::ProxyDown).await;
|
||||
css.signal.send(SignalKind::ProxyDown).await;
|
||||
}
|
||||
smol::Timer::after(Duration::from_secs(1)).await;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use std::sync::Arc;
|
||||
|
||||
use assets::Assets;
|
||||
use global::constants::{APP_ID, APP_NAME};
|
||||
use global::{css, ingester, nostr_client};
|
||||
use global::{css, nostr_client};
|
||||
use gpui::{
|
||||
point, px, size, AppContext, Application, Bounds, KeyBinding, Menu, MenuItem, SharedString,
|
||||
TitlebarOptions, WindowBackgroundAppearance, WindowBounds, WindowDecorations, WindowKind,
|
||||
@@ -26,9 +26,6 @@ fn main() {
|
||||
// Initialize the Nostr client
|
||||
let _client = nostr_client();
|
||||
|
||||
// Initialize the ingester
|
||||
let _ingester = ingester();
|
||||
|
||||
// Initialize the coop simple storage
|
||||
let _css = css();
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ use client_keys::ClientKeys;
|
||||
use common::display::ReadableProfile;
|
||||
use common::handle_auth::CoopAuthUrlHandler;
|
||||
use global::constants::{ACCOUNT_IDENTIFIER, BUNKER_TIMEOUT};
|
||||
use global::{ingester, nostr_client, Signal};
|
||||
use global::{css, nostr_client, SignalKind};
|
||||
use gpui::prelude::FluentBuilder;
|
||||
use gpui::{
|
||||
div, relative, rems, svg, AnyElement, App, AppContext, Context, Entity, EventEmitter,
|
||||
@@ -248,7 +248,7 @@ impl Account {
|
||||
// Reset the nostr client in the background
|
||||
cx.background_spawn(async move {
|
||||
let client = nostr_client();
|
||||
let ingester = ingester();
|
||||
let css = css();
|
||||
|
||||
let filter = Filter::new()
|
||||
.kind(Kind::ApplicationSpecificData)
|
||||
@@ -261,7 +261,7 @@ impl Account {
|
||||
client.unset_signer().await;
|
||||
|
||||
// Notify the channel about the signer being unset
|
||||
ingester.send(Signal::SignerUnset).await;
|
||||
css.signal.send(SignalKind::SignerUnset).await;
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ pub enum UnwrappingStatus {
|
||||
|
||||
/// Signals sent through the global event channel to notify UI
|
||||
#[derive(Debug)]
|
||||
pub enum Signal {
|
||||
pub enum SignalKind {
|
||||
/// A signal to notify UI that the client's signer has been set
|
||||
SignerSet(PublicKey),
|
||||
|
||||
@@ -71,25 +71,54 @@ pub enum Signal {
|
||||
ProxyDown,
|
||||
|
||||
/// A signal to notify UI that a new profile has been received
|
||||
Metadata(Profile),
|
||||
NewProfile(Profile),
|
||||
|
||||
/// A signal to notify UI that a new gift wrap event has been received
|
||||
Message((EventId, Event)),
|
||||
NewMessage((EventId, Event)),
|
||||
|
||||
/// A signal to notify UI that gift wrap process status has changed
|
||||
GiftWrapProcess(UnwrappingStatus),
|
||||
/// A signal to notify UI that no DM relays for current user was found
|
||||
RelaysNotFound,
|
||||
|
||||
/// A signal to notify UI that no DM relay for current user was found
|
||||
DmRelayNotFound,
|
||||
/// A signal to notify UI that gift wrap status has changed
|
||||
GiftWrapStatus(UnwrappingStatus),
|
||||
|
||||
/// A signal to notify UI that there are errors or notices occurred
|
||||
Notice(Notice),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Signal {
|
||||
rx: Receiver<SignalKind>,
|
||||
tx: Sender<SignalKind>,
|
||||
}
|
||||
|
||||
impl Default for Signal {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Signal {
|
||||
pub fn new() -> Self {
|
||||
let (tx, rx) = flume::bounded::<SignalKind>(2048);
|
||||
Self { rx, tx }
|
||||
}
|
||||
|
||||
pub fn receiver(&self) -> &Receiver<SignalKind> {
|
||||
&self.rx
|
||||
}
|
||||
|
||||
pub async fn send(&self, kind: SignalKind) {
|
||||
if let Err(e) = self.tx.send_async(kind).await {
|
||||
log::error!("Failed to send signal: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Ingester {
|
||||
rx: Receiver<Signal>,
|
||||
tx: Sender<Signal>,
|
||||
rx: Receiver<PublicKey>,
|
||||
tx: Sender<PublicKey>,
|
||||
}
|
||||
|
||||
impl Default for Ingester {
|
||||
@@ -100,17 +129,17 @@ impl Default for Ingester {
|
||||
|
||||
impl Ingester {
|
||||
pub fn new() -> Self {
|
||||
let (tx, rx) = flume::bounded::<Signal>(2048);
|
||||
let (tx, rx) = flume::bounded::<PublicKey>(1024);
|
||||
Self { rx, tx }
|
||||
}
|
||||
|
||||
pub fn signals(&self) -> &Receiver<Signal> {
|
||||
pub fn receiver(&self) -> &Receiver<PublicKey> {
|
||||
&self.rx
|
||||
}
|
||||
|
||||
pub async fn send(&self, signal: Signal) {
|
||||
if let Err(e) = self.tx.send_async(signal).await {
|
||||
log::error!("Failed to send signal: {e}");
|
||||
pub async fn send(&self, public_key: PublicKey) {
|
||||
if let Err(e) = self.tx.send_async(public_key).await {
|
||||
log::error!("Failed to send public key: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -119,14 +148,28 @@ impl Ingester {
|
||||
#[derive(Debug)]
|
||||
pub struct CoopSimpleStorage {
|
||||
pub init_at: Timestamp,
|
||||
|
||||
pub last_used_at: Option<Timestamp>,
|
||||
|
||||
pub is_first_run: AtomicBool,
|
||||
|
||||
pub gift_wrap_sub_id: SubscriptionId,
|
||||
|
||||
pub gift_wrap_processing: AtomicBool,
|
||||
|
||||
pub auto_close_opts: Option<SubscribeAutoCloseOptions>,
|
||||
|
||||
pub seen_on_relays: RwLock<HashMap<EventId, HashSet<RelayUrl>>>,
|
||||
|
||||
pub sent_ids: RwLock<HashSet<EventId>>,
|
||||
|
||||
pub resent_ids: RwLock<Vec<Output<EventId>>>,
|
||||
|
||||
pub resend_queue: RwLock<HashMap<EventId, RelayUrl>>,
|
||||
|
||||
pub signal: Signal,
|
||||
|
||||
pub ingester: Ingester,
|
||||
}
|
||||
|
||||
impl Default for CoopSimpleStorage {
|
||||
@@ -137,14 +180,22 @@ impl Default for CoopSimpleStorage {
|
||||
|
||||
impl CoopSimpleStorage {
|
||||
pub fn new() -> Self {
|
||||
let init_at = Timestamp::now();
|
||||
let first_run = first_run();
|
||||
let opts = SubscribeAutoCloseOptions::default().exit_policy(ReqExitPolicy::ExitOnEOSE);
|
||||
|
||||
let signal = Signal::default();
|
||||
let ingester = Ingester::default();
|
||||
|
||||
Self {
|
||||
init_at: Timestamp::now(),
|
||||
init_at,
|
||||
signal,
|
||||
ingester,
|
||||
last_used_at: None,
|
||||
is_first_run: AtomicBool::new(first_run),
|
||||
gift_wrap_sub_id: SubscriptionId::new("inbox"),
|
||||
gift_wrap_processing: AtomicBool::new(false),
|
||||
auto_close_opts: Some(
|
||||
SubscribeAutoCloseOptions::default().exit_policy(ReqExitPolicy::ExitOnEOSE),
|
||||
),
|
||||
auto_close_opts: Some(opts),
|
||||
seen_on_relays: RwLock::new(HashMap::new()),
|
||||
sent_ids: RwLock::new(HashSet::new()),
|
||||
resent_ids: RwLock::new(Vec::new()),
|
||||
@@ -154,9 +205,7 @@ impl CoopSimpleStorage {
|
||||
}
|
||||
|
||||
static NOSTR_CLIENT: OnceLock<Client> = OnceLock::new();
|
||||
static INGESTER: OnceLock<Ingester> = OnceLock::new();
|
||||
static COOP_SIMPLE_STORAGE: OnceLock<CoopSimpleStorage> = OnceLock::new();
|
||||
static FIRST_RUN: OnceLock<bool> = OnceLock::new();
|
||||
|
||||
pub fn nostr_client() -> &'static Client {
|
||||
NOSTR_CLIENT.get_or_init(|| {
|
||||
@@ -181,25 +230,19 @@ pub fn nostr_client() -> &'static Client {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn ingester() -> &'static Ingester {
|
||||
INGESTER.get_or_init(Ingester::new)
|
||||
}
|
||||
|
||||
pub fn css() -> &'static CoopSimpleStorage {
|
||||
COOP_SIMPLE_STORAGE.get_or_init(CoopSimpleStorage::new)
|
||||
}
|
||||
|
||||
pub fn first_run() -> &'static bool {
|
||||
FIRST_RUN.get_or_init(|| {
|
||||
let flag = support_dir().join(format!(".{}-first_run", env!("CARGO_PKG_VERSION")));
|
||||
fn first_run() -> bool {
|
||||
let flag = support_dir().join(format!(".{}-first_run", env!("CARGO_PKG_VERSION")));
|
||||
|
||||
if !flag.exists() {
|
||||
if std::fs::write(&flag, "").is_err() {
|
||||
return false;
|
||||
}
|
||||
true // First run
|
||||
} else {
|
||||
false // Not first run
|
||||
if !flag.exists() {
|
||||
if std::fs::write(&flag, "").is_err() {
|
||||
return false;
|
||||
}
|
||||
})
|
||||
true // First run
|
||||
} else {
|
||||
false // Not first run
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user