refactor
This commit is contained in:
@@ -9,7 +9,7 @@ use gpui::{
|
||||
};
|
||||
use instant::Duration;
|
||||
use nostr_sdk::prelude::*;
|
||||
use person::{Directory, Person, PersonRegistry, WebOfTrust, shorten_pubkey};
|
||||
use person::{Directory, Person, PersonRegistry, shorten_pubkey};
|
||||
use settings::AppSettings;
|
||||
use state::{NostrRegistry, upload};
|
||||
use theme::{ActiveTheme, TextStyle, Typeset as _, button_radius};
|
||||
@@ -460,7 +460,7 @@ impl Render for ProfileDialog {
|
||||
.map(SharedString::from);
|
||||
|
||||
let score = {
|
||||
let wot = WebOfTrust::global(cx).read(cx);
|
||||
let wot = PersonRegistry::global(cx).read(cx).wot().read(cx);
|
||||
wot.is_loaded().then(|| wot.score(&self.public_key))
|
||||
}
|
||||
.map(|score| {
|
||||
|
||||
@@ -9,7 +9,7 @@ use gpui::{
|
||||
};
|
||||
use instant::Duration;
|
||||
use nostr_sdk::prelude::*;
|
||||
use person::{Directory, Person, WebOfTrust, shorten_pubkey};
|
||||
use person::{Directory, Person, PersonRegistry, shorten_pubkey};
|
||||
use smallvec::{SmallVec, smallvec};
|
||||
use state::{BOOTSTRAP_RELAYS, NostrAddress, NostrRegistry, TIMEOUT};
|
||||
use theme::{ActiveTheme, TextStyle, Typeset as _, button_radius};
|
||||
@@ -33,7 +33,7 @@ pub struct Screening {
|
||||
last_active: Option<Timestamp>,
|
||||
/// All mutual contacts of the person being screened.
|
||||
mutual_contacts: Vec<PublicKey>,
|
||||
/// Whether the database fallback for mutual contacts has run.
|
||||
/// Whether the fallback lookup for mutual contacts has run.
|
||||
mutuals_fallback: bool,
|
||||
/// Person directory for the person and their mutual contacts
|
||||
directory: Entity<Directory>,
|
||||
@@ -43,7 +43,7 @@ pub struct Screening {
|
||||
|
||||
impl Screening {
|
||||
pub fn new(public_key: PublicKey, window: &mut Window, cx: &mut Context<Self>) -> Self {
|
||||
let wot = WebOfTrust::global(cx);
|
||||
let wot = PersonRegistry::global(cx).read(cx).wot();
|
||||
let directory = Directory::new(cx);
|
||||
|
||||
let mut subscriptions = smallvec![];
|
||||
@@ -53,8 +53,8 @@ impl Screening {
|
||||
window.close_all_dialogs(cx);
|
||||
}));
|
||||
|
||||
subscriptions.push(cx.subscribe(&wot, |this, _wot, _event, cx| {
|
||||
this.check_wot(cx);
|
||||
subscriptions.push(cx.subscribe(&wot, |_this, _wot, _event, cx| {
|
||||
cx.notify();
|
||||
}));
|
||||
|
||||
directory.update(cx, |directory, cx| {
|
||||
@@ -63,7 +63,7 @@ impl Screening {
|
||||
|
||||
cx.defer_in(window, |this, _window, cx| {
|
||||
this.check_contact(cx);
|
||||
this.check_wot(cx);
|
||||
this.fetch_fallback(cx);
|
||||
this.check_last_activity(cx);
|
||||
this.verify_identifier(cx);
|
||||
});
|
||||
@@ -121,27 +121,17 @@ impl Screening {
|
||||
}));
|
||||
}
|
||||
|
||||
/// Check mutual contacts through the web of trust,
|
||||
/// falling back to the database while it loads
|
||||
fn check_wot(&mut self, cx: &mut Context<Self>) {
|
||||
let trusted = {
|
||||
let wot = WebOfTrust::global(cx).read(cx);
|
||||
wot.is_loaded().then(|| wot.mutuals(&self.public_key))
|
||||
};
|
||||
/// Fetch mutual contacts from the database while the web of trust loads
|
||||
fn fetch_fallback(&mut self, cx: &mut Context<Self>) {
|
||||
let persons = PersonRegistry::global(cx);
|
||||
let nostr = NostrRegistry::global(cx);
|
||||
let client = nostr.read(cx).client();
|
||||
|
||||
if let Some(contacts) = trusted {
|
||||
self.mutual_contacts = contacts;
|
||||
cx.notify();
|
||||
return;
|
||||
}
|
||||
|
||||
if self.mutuals_fallback {
|
||||
if self.mutuals_fallback || persons.read(cx).wot().read(cx).is_loaded() {
|
||||
return;
|
||||
}
|
||||
self.mutuals_fallback = true;
|
||||
|
||||
let nostr = NostrRegistry::global(cx);
|
||||
let client = nostr.read(cx).client();
|
||||
let public_key = self.public_key;
|
||||
|
||||
let Some(current_user) = nostr.read(cx).current_user() else {
|
||||
@@ -166,11 +156,9 @@ impl Screening {
|
||||
let contacts = task.await;
|
||||
|
||||
this.update(cx, |this, cx| {
|
||||
// The web of trust wins once its load has finished
|
||||
if WebOfTrust::global(cx).read(cx).is_loaded() {
|
||||
if persons.read(cx).wot().read(cx).is_loaded() {
|
||||
return;
|
||||
}
|
||||
|
||||
this.mutual_contacts = contacts;
|
||||
cx.notify();
|
||||
})
|
||||
@@ -292,8 +280,12 @@ impl Screening {
|
||||
}));
|
||||
}
|
||||
|
||||
fn mutual_contacts(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||
let contacts = self.mutual_contacts.clone();
|
||||
fn mutual_contacts(
|
||||
&mut self,
|
||||
contacts: Vec<PublicKey>,
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
let directory = self.directory.clone();
|
||||
|
||||
window.open_dialog(cx, move |this, _window, _cx| {
|
||||
@@ -346,11 +338,23 @@ impl Render for Screening {
|
||||
const NIP05_NOT_MATCH: &str = "The address does not match the user's public key.";
|
||||
const NO_NIP05: &str = "This person has not set up their friendly address";
|
||||
|
||||
let persons = PersonRegistry::global(cx);
|
||||
let profile = self.profile(cx);
|
||||
let shorten_pubkey = shorten_pubkey(self.public_key, 8);
|
||||
|
||||
let last_active = self.last_active.map(|_| true);
|
||||
let mutuals = self.mutual_contacts.len();
|
||||
|
||||
// Pull from the web of trust once loaded, else use the database fallback
|
||||
let mutual_contacts = {
|
||||
let wot = persons.read(cx).wot().read(cx);
|
||||
if wot.is_loaded() {
|
||||
wot.mutuals(&self.public_key)
|
||||
} else {
|
||||
self.mutual_contacts.clone()
|
||||
}
|
||||
};
|
||||
|
||||
let mutuals = mutual_contacts.len();
|
||||
let mutuals_str = format!("You have {} mutual contacts with this person.", mutuals);
|
||||
|
||||
v_flex()
|
||||
@@ -543,7 +547,11 @@ impl Render for Screening {
|
||||
.rounded()
|
||||
.disabled(mutuals == 0)
|
||||
.on_click(cx.listener(move |this, _, window, cx| {
|
||||
this.mutual_contacts(window, cx);
|
||||
this.mutual_contacts(
|
||||
mutual_contacts.clone(),
|
||||
window,
|
||||
cx,
|
||||
);
|
||||
})),
|
||||
),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user