rebuild_wot(), and flag for data migration

This commit is contained in:
Mike Dilger 2024-09-21 11:34:11 +12:00
parent 0ff322b682
commit c4269d17e7
4 changed files with 42 additions and 2 deletions

View File

@ -229,6 +229,8 @@ pub fn init(rapid: bool) -> Result<(), Error> {
// If we need to rebuild relationships
if GLOBALS.db().get_flag_rebuild_relationships_needed()
|| GLOBALS.db().get_flag_rebuild_indexes_needed()
|| GLOBALS.db().get_flag_reprocess_relay_lists_needed()
|| GLOBALS.db().get_flag_rebuild_wot_needed()
{
GLOBALS.wait_for_login.store(true, Ordering::Relaxed);
GLOBALS

View File

@ -168,6 +168,12 @@ impl Overlord {
crate::process::reprocess_relay_lists()?;
}
// If we need to rebuild web of trust, do so now
if GLOBALS.db().get_flag_rebuild_wot_needed() {
tracing::info!("Rebuilding web of trust...");
GLOBALS.db().rebuild_wot(None)?;
}
// Data migrations complete
GLOBALS
.wait_for_data_migration

View File

@ -489,6 +489,7 @@ impl Storage {
b"reprocess_relay_lists_needed",
true
);
def_flag!(rebuild_wot_needed, b"rebuild_wot_needed", true);
// Settings ----------------------------------------------------------

View File

@ -1,8 +1,9 @@
use crate::error::Error;
use crate::storage::{RawDatabase, Storage};
use crate::globals::GLOBALS;
use crate::storage::{FollowingsTable, RawDatabase, Storage, Table};
use heed::types::Bytes;
use heed::RwTxn;
use nostr_types::PublicKey;
use nostr_types::{EventKind, Filter, PublicKey, PublicKeyHex};
use std::sync::Mutex;
// Pubkey -> u64
@ -117,4 +118,34 @@ impl Storage {
Ok(())
}
pub(crate) fn rebuild_wot<'a>(&'a self, rw_txn: Option<&mut RwTxn<'a>>) -> Result<(), Error> {
let mut local_txn = None;
let txn = maybe_local_txn!(self, rw_txn, local_txn);
// Clear WoT data
self.db_wot()?.clear(txn)?;
// Clear following lists
FollowingsTable::clear(Some(txn))?;
// Get the contact lists of each person we follow
let mut filter = Filter::new();
filter.add_event_kind(EventKind::ContactList);
for pubkey in GLOBALS.people.get_subscribed_pubkeys().iter() {
let pkh: PublicKeyHex = pubkey.into();
filter.add_author(&pkh);
}
let contact_lists = self.find_events_by_filter(&filter, |_| true)?;
for event in &contact_lists {
crate::process::update_followings_and_wot_from_contact_list(event, Some(txn))?;
}
self.set_flag_rebuild_wot_needed(false, Some(txn))?;
maybe_local_txn_commit!(local_txn);
Ok(())
}
}