feat: resend messages after authentication (#137)
* resend failed message * update settings
This commit is contained in:
@@ -10,9 +10,9 @@ use common::display::ReadableProfile;
|
||||
use common::event::EventUtils;
|
||||
use global::constants::{
|
||||
ACCOUNT_IDENTIFIER, BOOTSTRAP_RELAYS, DEFAULT_SIDEBAR_WIDTH, METADATA_BATCH_LIMIT,
|
||||
METADATA_BATCH_TIMEOUT, SEARCH_RELAYS, TOTAL_RETRY, WAIT_FOR_FINISH,
|
||||
METADATA_BATCH_TIMEOUT, RELAY_RETRY, SEARCH_RELAYS, WAIT_FOR_FINISH,
|
||||
};
|
||||
use global::{ingester, nostr_client, sent_ids, starting_time, AuthReq, IngesterSignal, Notice};
|
||||
use global::{css, ingester, nostr_client, AuthRequest, IngesterSignal, Notice};
|
||||
use gpui::prelude::FluentBuilder;
|
||||
use gpui::{
|
||||
div, px, rems, App, AppContext, AsyncWindowContext, Axis, Context, Entity, InteractiveElement,
|
||||
@@ -218,14 +218,14 @@ impl ChatSpace {
|
||||
break;
|
||||
} else {
|
||||
retry += 1;
|
||||
if retry == TOTAL_RETRY {
|
||||
if retry == RELAY_RETRY {
|
||||
ingester.send(IngesterSignal::DmRelayNotFound).await;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
nip65_retry += 1;
|
||||
if nip65_retry == TOTAL_RETRY {
|
||||
if nip65_retry == RELAY_RETRY {
|
||||
ingester.send(IngesterSignal::DmRelayNotFound).await;
|
||||
break;
|
||||
}
|
||||
@@ -358,12 +358,12 @@ impl ChatSpace {
|
||||
) -> Result<(), Error> {
|
||||
let client = nostr_client();
|
||||
let ingester = ingester();
|
||||
let sent_ids = sent_ids();
|
||||
let css = css();
|
||||
let auto_close =
|
||||
SubscribeAutoCloseOptions::default().exit_policy(ReqExitPolicy::ExitOnEOSE);
|
||||
|
||||
let mut processed_events: HashSet<EventId> = HashSet::new();
|
||||
let mut auth_requests: HashMap<RelayUrl, Cow<'_, str>> = HashMap::new();
|
||||
let mut challenges: HashSet<Cow<'_, str>> = HashSet::new();
|
||||
let mut notifications = client.notifications();
|
||||
|
||||
while let Ok(notification) = notifications.recv().await {
|
||||
@@ -457,21 +457,28 @@ impl ChatSpace {
|
||||
}
|
||||
}
|
||||
RelayMessage::Auth { challenge } => {
|
||||
// Prevent duplicate auth requests
|
||||
if auth_requests
|
||||
.insert(relay_url.clone(), challenge.clone())
|
||||
.is_none()
|
||||
{
|
||||
let auth_req = AuthReq::new(challenge, relay_url);
|
||||
ingester.send(IngesterSignal::Auth(auth_req)).await;
|
||||
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(IngesterSignal::Auth(req)).await;
|
||||
}
|
||||
}
|
||||
RelayMessage::Ok { event_id, .. } => {
|
||||
RelayMessage::Ok {
|
||||
event_id, message, ..
|
||||
} => {
|
||||
// Keep track of events sent by Coop
|
||||
sent_ids.write().await.push(event_id);
|
||||
}
|
||||
RelayMessage::Notice(msg) => {
|
||||
log::info!("Notice: {msg} - {relay_url}");
|
||||
css.sent_ids.write().await.insert(event_id);
|
||||
|
||||
// Keep track of events that need to be resent
|
||||
match MachineReadablePrefix::parse(&message) {
|
||||
Some(MachineReadablePrefix::AuthRequired) => {
|
||||
if client.has_signer().await {
|
||||
css.resend_queue.write().await.insert(event_id, relay_url);
|
||||
};
|
||||
}
|
||||
Some(_) => {}
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
@@ -488,6 +495,7 @@ impl ChatSpace {
|
||||
while let Ok(signal) = signals.recv().await {
|
||||
cx.update(|window, cx| {
|
||||
let registry = Registry::global(cx);
|
||||
let settings = AppSettings::global(cx);
|
||||
|
||||
match signal {
|
||||
IngesterSignal::SignerSet(public_key) => {
|
||||
@@ -499,6 +507,11 @@ impl ChatSpace {
|
||||
})
|
||||
.ok();
|
||||
|
||||
// Load user's settings
|
||||
settings.update(cx, |this, cx| {
|
||||
this.load_settings(cx);
|
||||
});
|
||||
|
||||
// Load all chat rooms
|
||||
registry.update(cx, |this, cx| {
|
||||
this.set_identity(public_key, cx);
|
||||
@@ -520,13 +533,14 @@ impl ChatSpace {
|
||||
IngesterSignal::Auth(req) => {
|
||||
let relay_url = &req.url;
|
||||
let challenge = &req.challenge;
|
||||
let auth_auth = AppSettings::get_auto_auth(cx);
|
||||
let auth_relays = AppSettings::read_global(cx).auth_relays();
|
||||
let auto_auth = AppSettings::get_auto_auth(cx);
|
||||
let is_authenticated_relays =
|
||||
AppSettings::read_global(cx).is_authenticated_relays(relay_url);
|
||||
|
||||
view.update(cx, |this, cx| {
|
||||
this.push_auth_request(challenge, relay_url, cx);
|
||||
|
||||
if auth_auth && auth_relays.contains(relay_url) {
|
||||
if auto_auth && is_authenticated_relays {
|
||||
// Automatically authenticate if the relay is authenticated before
|
||||
this.auth(challenge, relay_url, window, cx);
|
||||
} else {
|
||||
@@ -708,6 +722,7 @@ impl ChatSpace {
|
||||
async fn unwrap_gift_wrap_event(gift: &Event, pubkey_tx: &Sender<PublicKey>) -> bool {
|
||||
let client = nostr_client();
|
||||
let ingester = ingester();
|
||||
let css = css();
|
||||
let mut is_cached = false;
|
||||
|
||||
let event = match Self::get_unwrapped_event(gift.id).await {
|
||||
@@ -745,7 +760,7 @@ impl ChatSpace {
|
||||
}
|
||||
|
||||
// Send a notify to GPUI if this is a new message
|
||||
if &event.created_at >= starting_time() {
|
||||
if event.created_at >= css.init_at {
|
||||
ingester
|
||||
.send(IngesterSignal::GiftWrap((gift.id, event)))
|
||||
.await;
|
||||
@@ -800,6 +815,7 @@ impl ChatSpace {
|
||||
|
||||
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
|
||||
let client = nostr_client();
|
||||
let css = css();
|
||||
let signer = client.signer().await?;
|
||||
|
||||
// Construct event
|
||||
@@ -827,16 +843,38 @@ impl ChatSpace {
|
||||
} => {
|
||||
if id == event_id {
|
||||
// Re-subscribe to previous subscription
|
||||
match relay.resubscribe().await {
|
||||
Ok(_) => {
|
||||
log::info!("{relay_url} - re-subscribe");
|
||||
relay.resubscribe().await?;
|
||||
|
||||
// Get all failed events that need to be resent
|
||||
let mut queue = css.resend_queue.write().await;
|
||||
|
||||
let ids: Vec<EventId> = queue
|
||||
.iter()
|
||||
.filter(|(_, url)| relay_url == *url)
|
||||
.map(|(id, _)| *id)
|
||||
.collect();
|
||||
|
||||
for id in ids.into_iter() {
|
||||
if let Some(relay_url) = queue.remove(&id) {
|
||||
if let Some(event) = client.database().event_by_id(&id).await? {
|
||||
let event_id = relay.send_event(&event).await?;
|
||||
|
||||
let output = Output {
|
||||
val: event_id,
|
||||
failed: HashMap::new(),
|
||||
success: HashSet::from([relay_url]),
|
||||
};
|
||||
|
||||
css.sent_ids.write().await.insert(event_id);
|
||||
css.resent_ids.write().await.push(output);
|
||||
}
|
||||
}
|
||||
Err(e) => return Err(e.into()),
|
||||
}
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
RelayNotification::AuthenticationFailed => break,
|
||||
RelayNotification::Shutdown => break,
|
||||
_ => {}
|
||||
}
|
||||
@@ -854,7 +892,7 @@ impl ChatSpace {
|
||||
|
||||
// Save the authenticated relay to automatically authenticate future requests
|
||||
settings.update(cx, |this, cx| {
|
||||
this.push_auth_relay(url.clone(), cx);
|
||||
this.push_relay(&url, cx);
|
||||
});
|
||||
|
||||
// Clear the current notification
|
||||
|
||||
@@ -2,7 +2,7 @@ use std::sync::Arc;
|
||||
|
||||
use assets::Assets;
|
||||
use global::constants::{APP_ID, APP_NAME};
|
||||
use global::{ingester, nostr_client, sent_ids, starting_time};
|
||||
use global::{css, ingester, nostr_client};
|
||||
use gpui::{
|
||||
point, px, size, AppContext, Application, Bounds, KeyBinding, Menu, MenuItem, SharedString,
|
||||
TitlebarOptions, WindowBackgroundAppearance, WindowBounds, WindowDecorations, WindowKind,
|
||||
@@ -29,11 +29,8 @@ fn main() {
|
||||
// Initialize the ingester
|
||||
let _ingester = ingester();
|
||||
|
||||
// Initialize the starting time
|
||||
let _starting_time = starting_time();
|
||||
|
||||
// Initialize the sent IDs storage
|
||||
let _sent_ids = sent_ids();
|
||||
// Initialize the coop simple storage
|
||||
let _css = css();
|
||||
|
||||
// Initialize the Application
|
||||
let app = Application::new()
|
||||
|
||||
@@ -3,7 +3,7 @@ use std::collections::{HashMap, HashSet};
|
||||
use anyhow::anyhow;
|
||||
use common::display::{ReadableProfile, ReadableTimestamp};
|
||||
use common::nip96::nip96_upload;
|
||||
use global::{nostr_client, sent_ids};
|
||||
use global::{css, nostr_client};
|
||||
use gpui::prelude::FluentBuilder;
|
||||
use gpui::{
|
||||
div, img, list, px, red, relative, rems, svg, white, Action, AnyElement, App, AppContext,
|
||||
@@ -220,8 +220,7 @@ impl Chat {
|
||||
|
||||
/// Check if the event is sent by Coop
|
||||
fn is_sent_by_coop(&self, gift_wrap_id: &EventId) -> bool {
|
||||
let sent_ids = sent_ids();
|
||||
sent_ids.read_blocking().contains(gift_wrap_id)
|
||||
css().sent_ids.read_blocking().contains(gift_wrap_id)
|
||||
}
|
||||
|
||||
/// Set the sending state of the chat panel
|
||||
|
||||
Reference in New Issue
Block a user