Ability to hide relays

This commit is contained in:
Mike Dilger 2023-04-08 12:12:11 +12:00
parent ab78e4e073
commit 4641114a49
7 changed files with 53 additions and 5 deletions

View File

@ -16,6 +16,7 @@ pub enum ToOverlordMessage {
FollowNip05(String),
FollowNprofile(String),
GeneratePrivateKey(String),
HideOrShowRelay(RelayUrl, bool),
ImportPriv(String, String),
ImportPub(String),
Like(Id, PublicKey),

View File

@ -238,7 +238,7 @@ fn normalize_urls() -> Result<(), Error> {
Ok(())
}
const UPGRADE_SQL: [&str; 31] = [
const UPGRADE_SQL: [&str; 32] = [
include_str!("sql/schema1.sql"),
include_str!("sql/schema2.sql"),
include_str!("sql/schema3.sql"),
@ -270,4 +270,5 @@ const UPGRADE_SQL: [&str; 31] = [
include_str!("sql/schema29.sql"),
include_str!("sql/schema30.sql"),
include_str!("sql/schema31.sql"),
include_str!("sql/schema32.sql"),
];

View File

@ -14,6 +14,7 @@ pub struct DbRelay {
pub read: bool,
pub write: bool,
pub advertise: bool,
pub hidden: bool,
}
impl DbRelay {
@ -28,6 +29,7 @@ impl DbRelay {
read: false,
write: false,
advertise: false,
hidden: false,
}
}
@ -45,7 +47,7 @@ impl DbRelay {
pub async fn fetch(criteria: Option<&str>) -> Result<Vec<DbRelay>, Error> {
let sql = "SELECT url, success_count, failure_count, rank, last_connected_at, \
last_general_eose_at, read, write, advertise FROM relay"
last_general_eose_at, read, write, advertise, hidden FROM relay"
.to_owned();
let sql = match criteria {
None => sql,
@ -72,6 +74,7 @@ impl DbRelay {
read: row.get(6)?,
write: row.get(7)?,
advertise: row.get(8)?,
hidden: row.get(9)?,
});
}
}
@ -94,8 +97,8 @@ impl DbRelay {
pub async fn insert(relay: DbRelay) -> Result<(), Error> {
let sql = "INSERT OR IGNORE INTO relay (url, success_count, failure_count, rank, \
last_connected_at, last_general_eose_at, read, write, advertise) \
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)";
last_connected_at, last_general_eose_at, read, write, advertise, hidden) \
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)";
spawn_blocking(move || {
let db = GLOBALS.db.blocking_lock();
@ -111,6 +114,7 @@ impl DbRelay {
&relay.read,
&relay.write,
&relay.advertise,
&relay.hidden,
)));
Ok::<(), Error>(())
})
@ -121,7 +125,8 @@ impl DbRelay {
pub async fn update(relay: DbRelay) -> Result<(), Error> {
let sql = "UPDATE relay SET success_count=?, failure_count=?, rank=?, \
last_connected_at=?, last_general_eose_at=?, read=?, write=?, advertise=? WHERE url=?";
last_connected_at=?, last_general_eose_at=?, \
read=?, write=?, advertise=?, hidden=? WHERE url=?";
spawn_blocking(move || {
let db = GLOBALS.db.blocking_lock();
@ -136,6 +141,7 @@ impl DbRelay {
&relay.read,
&relay.write,
&relay.advertise,
&relay.hidden,
&relay.url.0,
)));
Ok::<(), Error>(())
@ -207,6 +213,19 @@ impl DbRelay {
Ok(())
}
pub async fn update_hidden(url: RelayUrl, hidden: bool) -> Result<(), Error> {
let sql = "UPDATE relay SET hidden = ? WHERE url = ?";
spawn_blocking(move || {
let db = GLOBALS.db.blocking_lock();
let mut stmt = db.prepare(sql)?;
rtry!(stmt.execute((&hidden, &url.0)));
Ok::<(), Error>(())
})
.await??;
Ok(())
}
/*
pub async fn delete(criteria: &str) -> Result<(), Error> {
let sql = format!("DELETE FROM relay WHERE {}", criteria);

1
src/db/sql/schema32.sql Normal file
View File

@ -0,0 +1 @@
ALTER TABLE relay ADD COLUMN hidden INTEGER DEFAULT 0;

View File

@ -513,6 +513,12 @@ impl Overlord {
password.zeroize();
GLOBALS.signer.save_through_settings().await?;
}
ToOverlordMessage::HideOrShowRelay(relay_url, hidden) => {
if let Some(mut relay) = GLOBALS.all_relays.get_mut(&relay_url) {
relay.value_mut().hidden = hidden;
}
DbRelay::update_hidden(relay_url, hidden).await?;
}
ToOverlordMessage::ImportPriv(mut import_priv, mut password) => {
if import_priv.starts_with("ncryptsec") {
let epk = EncryptedPrivateKey(import_priv);

View File

@ -181,6 +181,7 @@ struct GossipUi {
import_priv: String,
import_pub: String,
new_relay_url: String,
show_hidden_relays: bool,
search: String,
entering_search_page: bool,
@ -334,6 +335,7 @@ impl GossipUi {
import_priv: "".to_owned(),
import_pub: "".to_owned(),
new_relay_url: "".to_owned(),
show_hidden_relays: false,
search: "".to_owned(),
entering_search_page: false,
collapsed: vec![],

View File

@ -33,6 +33,10 @@ pub(super) fn update(app: &mut GossipUi, _ctx: &Context, _frame: &mut eframe::Fr
.to_overlord
.send(ToOverlordMessage::AdvertiseRelayList);
}
ui.checkbox(
&mut app.show_hidden_relays,
"Show hidden relays"
);
});
ui.add_space(10.0);
@ -45,6 +49,7 @@ pub(super) fn update(app: &mut GossipUi, _ctx: &Context, _frame: &mut eframe::Fr
.all_relays
.iter()
.map(|ri| ri.value().clone())
.filter(|ri| app.show_hidden_relays || !ri.hidden)
.collect();
relays.sort_by(|a, b| b.write.cmp(&a.write).then(a.url.cmp(&b.url)));
@ -70,6 +75,7 @@ fn relay_table(ui: &mut Ui, relays: &mut [DbRelay], id: &'static str) {
.column(Column::auto().resizable(true))
.column(Column::auto().resizable(true))
.column(Column::auto().resizable(true))
.column(Column::auto().resizable(true))
.column(Column::remainder())
.header(20.0, |mut header| {
header.col(|ui| {
@ -104,6 +110,10 @@ fn relay_table(ui: &mut Ui, relays: &mut [DbRelay], id: &'static str) {
ui.heading("Read rank")
.on_hover_text("How likely we will connect to relays to read other people's posts, from 0 (never) to 9 (highly). Default is 3.".to_string());
});
header.col(|ui| {
ui.heading("Hide")
.on_hover_text("Hide this relay.".to_string());
});
}).body(|body| {
body.rows(24.0, relays.len(), |row_index, mut row| {
let relay = relays.get_mut(row_index).unwrap();
@ -176,6 +186,14 @@ fn relay_table(ui: &mut Ui, relays: &mut [DbRelay], id: &'static str) {
}
});
});
row.col(|ui| {
let icon = if relay.hidden { "♻️" } else { "🗑️" };
if ui.button(icon).clicked() {
let _ = GLOBALS
.to_overlord
.send(ToOverlordMessage::HideOrShowRelay(relay.url.clone(), !relay.hidden));
}
});
})
});
});