Respec of get_people_in_list()

This commit is contained in:
Mike Dilger 2023-11-29 10:29:15 +13:00
parent 5cdaa91c8b
commit a0ffd4b573
9 changed files with 77 additions and 62 deletions

View File

@ -470,25 +470,34 @@ pub fn print_relays(_cmd: Command) -> Result<(), Error> {
}
pub fn print_followed(_cmd: Command) -> Result<(), Error> {
let pubkeys = GLOBALS
.storage
.get_people_in_list(PersonList::Followed, None)?;
for pk in &pubkeys {
let members = GLOBALS.storage.get_people_in_list(PersonList::Followed)?;
for (pk, public) in &members {
if let Some(person) = GLOBALS.storage.read_person(pk)? {
println!("{} {}", pk.as_hex_string(), person.best_name());
println!(
"{} {} {}",
if *public { "pub" } else { "prv" },
pk.as_hex_string(),
person.best_name()
);
} else {
println!("{}", pk.as_hex_string());
println!(
"{} {}",
if *public { "pub" } else { "prv" },
pk.as_hex_string()
);
}
}
Ok(())
}
pub fn print_muted(_cmd: Command) -> Result<(), Error> {
let pubkeys = GLOBALS
.storage
.get_people_in_list(PersonList::Muted, None)?;
for pk in &pubkeys {
println!("{}", pk.as_hex_string());
let members = GLOBALS.storage.get_people_in_list(PersonList::Muted)?;
for (pk, public) in &members {
println!(
"{} {}",
if *public { "pub" } else { "prv" },
pk.as_hex_string()
);
}
Ok(())
}
@ -497,20 +506,21 @@ pub fn print_person_lists(_cmd: Command) -> Result<(), Error> {
let lists = PersonList::all_lists();
for (list, name) in lists.iter() {
println!("LIST {}: {}", u8::from(*list), name);
let pubkeys = GLOBALS.storage.get_people_in_list(*list, Some(true))?;
for pk in &pubkeys {
let members = GLOBALS.storage.get_people_in_list(*list)?;
for (pk, public) in &members {
if let Some(person) = GLOBALS.storage.read_person(pk)? {
println!("public: {} {}", pk.as_hex_string(), person.best_name());
println!(
"{} {} {}",
if *public { "pub" } else { "prv" },
pk.as_hex_string(),
person.best_name()
);
} else {
println!("public: {}", pk.as_hex_string());
}
}
let pubkeys = GLOBALS.storage.get_people_in_list(*list, Some(false))?;
for pk in &pubkeys {
if let Some(person) = GLOBALS.storage.read_person(pk)? {
println!("private: {} {}", pk.as_hex_string(), person.best_name());
} else {
println!("private: {}", pk.as_hex_string());
println!(
"{} {}",
if *public { "pub" } else { "prv" },
pk.as_hex_string()
);
}
}
println!();

View File

@ -4,12 +4,16 @@ use eframe::egui;
use egui::{Context, RichText, Ui};
use gossip_lib::comms::ToOverlordMessage;
use gossip_lib::{Person, PersonList, GLOBALS};
use nostr_types::PublicKey;
pub(super) fn update(app: &mut GossipUi, ctx: &Context, _frame: &mut eframe::Frame, ui: &mut Ui) {
let followed_pubkeys = GLOBALS
let followed_pubkeys: Vec<PublicKey> = GLOBALS
.storage
.get_people_in_list(PersonList::Followed, None)
.unwrap_or_default();
.get_people_in_list(PersonList::Followed)
.unwrap_or_default()
.drain(..)
.map(|(pk, _)| pk)
.collect();
let mut people: Vec<Person> = Vec::new();
for pk in &followed_pubkeys {
if let Ok(Some(person)) = GLOBALS.storage.read_person(pk) {

View File

@ -4,13 +4,16 @@ use eframe::egui;
use egui::{Context, RichText, Ui};
use gossip_lib::comms::ToOverlordMessage;
use gossip_lib::{Person, PersonList, GLOBALS};
use nostr_types::PublicKey;
pub(super) fn update(app: &mut GossipUi, ctx: &Context, _frame: &mut eframe::Frame, ui: &mut Ui) {
let muted_pubkeys = GLOBALS
let muted_pubkeys: Vec<PublicKey> = GLOBALS
.storage
.get_people_in_list(PersonList::Muted, None)
.unwrap_or_default();
.get_people_in_list(PersonList::Muted)
.unwrap_or_default()
.drain(..)
.map(|(pk, _)| pk)
.collect();
let mut people: Vec<Person> = Vec::new();
for pk in &muted_pubkeys {
if let Ok(Some(person)) = GLOBALS.storage.read_person(pk) {

View File

@ -81,8 +81,11 @@ impl WizardState {
self.followed = GLOBALS
.storage
.get_people_in_list(PersonList::Followed, None)
.unwrap_or_default();
.get_people_in_list(PersonList::Followed)
.unwrap_or_default()
.drain(..)
.map(|(pk, _)| pk)
.collect();
if self.need_discovery_relays() {
let purplepages = RelayUrl::try_from_str("wss://purplepag.es/").unwrap();

View File

@ -305,7 +305,13 @@ impl Feed {
let current_feed_kind = self.current_feed_kind.read().to_owned();
match current_feed_kind {
FeedKind::List(list, with_replies) => {
let pubkeys: Vec<PublicKey> = GLOBALS.storage.get_people_in_list(list, None)?;
let pubkeys: Vec<PublicKey> = GLOBALS
.storage
.get_people_in_list(list)?
.drain(..)
.map(|(pk, _)| pk)
.collect();
let since = now - Duration::from_secs(GLOBALS.storage.read_setting_feed_chunk());
// FIXME we don't include delegated events. We should look for all events

View File

@ -591,12 +591,7 @@ impl People {
let my_pubkey = GLOBALS.signer.public_key().unwrap();
// Read the person list in two parts
let public_people = GLOBALS
.storage
.get_people_in_list(person_list, Some(true))?;
let private_people = GLOBALS
.storage
.get_people_in_list(person_list, Some(false))?;
let people = GLOBALS.storage.get_people_in_list(person_list)?;
// Determine the event kind
let kind = match person_list {
@ -607,7 +602,11 @@ impl People {
// Build public p-tags
let mut tags: Vec<Tag> = Vec::new();
for pubkey in public_people.iter() {
for (pubkey, public) in people.iter() {
if !*public {
continue;
}
// Only include petnames in the ContactList (which is only public people)
let petname = if kind == EventKind::ContactList {
if let Some(person) = GLOBALS.storage.read_person(pubkey)? {
@ -656,7 +655,11 @@ impl People {
} else {
// Build private p-tags (except for ContactList)
let mut private_p_tags: Vec<Tag> = Vec::new();
for pubkey in private_people.iter() {
for (pubkey, public) in people.iter() {
if *public {
continue;
}
private_p_tags.push(Tag::Pubkey {
pubkey: pubkey.into(),
recommended_relay_url: None,

View File

@ -2436,12 +2436,8 @@ impl Storage {
}
/// Get people in a person list
pub fn get_people_in_list(
&self,
list: PersonList,
public: Option<bool>,
) -> Result<Vec<PublicKey>, Error> {
self.get_people_in_list2(list, public)
pub fn get_people_in_list(&self, list: PersonList) -> Result<Vec<(PublicKey, bool)>, Error> {
self.get_people_in_list2(list)
}
pub fn get_people_in_all_followed_lists(&self) -> Result<Vec<PublicKey>, Error> {

View File

@ -100,28 +100,18 @@ impl Storage {
pub(crate) fn get_people_in_list2(
&self,
list: PersonList1,
public: Option<bool>,
) -> Result<Vec<PublicKey>, Error> {
) -> Result<Vec<(PublicKey, bool)>, Error> {
let txn = self.env.read_txn()?;
let mut pubkeys: Vec<PublicKey> = Vec::new();
let mut output: Vec<(PublicKey, bool)> = Vec::new();
for result in self.db_person_lists2()?.iter(&txn)? {
let (key, val) = result?;
let pubkey = PublicKey::from_bytes(key, true)?;
let map = HashMap::<PersonList1, bool>::read_from_buffer(val)?;
if let Some(actual_public) = map.get(&list) {
match public {
Some(requested_public) => {
if requested_public == *actual_public {
pubkeys.push(pubkey);
}
}
None => {
pubkeys.push(pubkey);
}
}
output.push((pubkey, *actual_public));
}
}
Ok(pubkeys)
Ok(output)
}
pub(crate) fn clear_person_list2<'a>(

View File

@ -104,7 +104,7 @@ impl PersonList1 {
/// Deallocate this PersonList1
pub fn deallocate(&self, txn: Option<&mut RwTxn<'_>>) -> Result<(), Error> {
if !GLOBALS.storage.get_people_in_list(*self, None)?.is_empty() {
if !GLOBALS.storage.get_people_in_list(*self)?.is_empty() {
Err(ErrorKind::ListIsNotEmpty.into())
} else {
if let PersonList1::Custom(i) = self {