migrate accounts to be referenced through pks instead of indices
Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use enostr::FullKeypair;
|
||||
use enostr::{FullKeypair, Pubkey};
|
||||
use nostrdb::{Ndb, Transaction};
|
||||
|
||||
use notedeck::{Accounts, Images, SingleUnkIdAction, UnknownIds};
|
||||
@@ -35,11 +35,11 @@ pub struct SwitchAccountAction {
|
||||
pub source_column: usize,
|
||||
|
||||
/// The account to switch to
|
||||
pub switch_to: usize,
|
||||
pub switch_to: Pubkey,
|
||||
}
|
||||
|
||||
impl SwitchAccountAction {
|
||||
pub fn new(source_column: usize, switch_to: usize) -> Self {
|
||||
pub fn new(source_column: usize, switch_to: Pubkey) -> Self {
|
||||
SwitchAccountAction {
|
||||
source_column,
|
||||
switch_to,
|
||||
@@ -50,7 +50,7 @@ impl SwitchAccountAction {
|
||||
#[derive(Debug)]
|
||||
pub enum AccountsAction {
|
||||
Switch(SwitchAccountAction),
|
||||
Remove(usize),
|
||||
Remove(Pubkey),
|
||||
}
|
||||
|
||||
#[must_use = "You must call process_login_action on this to handle unknown ids"]
|
||||
@@ -120,24 +120,24 @@ pub fn process_accounts_view_response(
|
||||
let router = get_active_columns_mut(accounts, decks)
|
||||
.column_mut(col)
|
||||
.router_mut();
|
||||
let mut selection = None;
|
||||
let mut action = None;
|
||||
match response {
|
||||
AccountsViewResponse::RemoveAccount(index) => {
|
||||
let acc_sel = AccountsAction::Remove(index);
|
||||
info!("account selection: {:?}", acc_sel);
|
||||
selection = Some(acc_sel);
|
||||
AccountsViewResponse::RemoveAccount(pk_to_remove) => {
|
||||
let cur_action = AccountsAction::Remove(pk_to_remove);
|
||||
info!("account selection: {:?}", action);
|
||||
action = Some(cur_action);
|
||||
}
|
||||
AccountsViewResponse::SelectAccount(index) => {
|
||||
let acc_sel = AccountsAction::Switch(SwitchAccountAction::new(col, index));
|
||||
AccountsViewResponse::SelectAccount(new_pk) => {
|
||||
let acc_sel = AccountsAction::Switch(SwitchAccountAction::new(col, new_pk));
|
||||
info!("account selection: {:?}", acc_sel);
|
||||
selection = Some(acc_sel);
|
||||
action = Some(acc_sel);
|
||||
}
|
||||
AccountsViewResponse::RouteToLogin => {
|
||||
router.route_to(Route::add_account());
|
||||
}
|
||||
}
|
||||
accounts.needs_relay_config();
|
||||
selection
|
||||
action
|
||||
}
|
||||
|
||||
pub fn process_login_view_response(
|
||||
@@ -160,13 +160,13 @@ pub fn process_login_view_response(
|
||||
|
||||
decks.add_deck_default(pubkey);
|
||||
|
||||
if let Some(resp) = r {
|
||||
if let Some(action) = r {
|
||||
AddAccountAction {
|
||||
accounts_action: Some(AccountsAction::Switch(SwitchAccountAction {
|
||||
source_column: col,
|
||||
switch_to: resp.switch_to,
|
||||
switch_to: action.switch_to,
|
||||
})),
|
||||
unk_id_action: resp.unk_id_action,
|
||||
unk_id_action: action.unk_id_action,
|
||||
}
|
||||
} else {
|
||||
AddAccountAction {
|
||||
|
||||
@@ -97,10 +97,8 @@ fn execute_note_action(
|
||||
NoteAction::Quote(note_id) => {
|
||||
router_action = Some(RouterAction::route_to(Route::quote(note_id)));
|
||||
}
|
||||
NoteAction::Zap(zap_action) => 's: {
|
||||
let Some(cur_acc) = accounts.get_selected_account_mut() else {
|
||||
break 's;
|
||||
};
|
||||
NoteAction::Zap(zap_action) => {
|
||||
let cur_acc = accounts.get_selected_account_mut();
|
||||
|
||||
let sender = cur_acc.key.pubkey;
|
||||
|
||||
|
||||
@@ -428,8 +428,8 @@ impl Damus {
|
||||
} else {
|
||||
info!("DecksCache: creating new with demo configuration");
|
||||
let mut cache = DecksCache::new_with_demo_config(&mut timeline_cache, ctx);
|
||||
for account in ctx.accounts.get_accounts() {
|
||||
cache.add_deck_default(account.key.pubkey);
|
||||
for (pk, _) in &ctx.accounts.cache {
|
||||
cache.add_deck_default(*pk);
|
||||
}
|
||||
set_demo(&mut cache, ctx.ndb, ctx.accounts, ctx.unknown_ids);
|
||||
|
||||
@@ -445,8 +445,6 @@ impl Damus {
|
||||
|
||||
let jobs = JobsCache::default();
|
||||
|
||||
ctx.accounts.with_fallback(FALLBACK_PUBKEY());
|
||||
|
||||
let threads = Threads::default();
|
||||
|
||||
Self {
|
||||
@@ -770,13 +768,14 @@ pub fn set_demo(
|
||||
accounts: &mut Accounts,
|
||||
unk_ids: &mut UnknownIds,
|
||||
) {
|
||||
let fallback = decks_cache.get_fallback_pubkey();
|
||||
let txn = Transaction::new(ndb).expect("txn");
|
||||
if let Some(resp) =
|
||||
accounts.add_account(Keypair::only_pubkey(*decks_cache.get_fallback_pubkey()))
|
||||
{
|
||||
resp.unk_id_action.process_action(unk_ids, ndb, &txn);
|
||||
}
|
||||
accounts.select_account(accounts.num_accounts() - 1);
|
||||
accounts.select_account(fallback);
|
||||
}
|
||||
|
||||
fn columns_to_decks_cache(cols: Columns, key: &[u8; 32]) -> DecksCache {
|
||||
|
||||
@@ -78,14 +78,14 @@ impl SwitchingAction {
|
||||
match &self {
|
||||
SwitchingAction::Accounts(account_action) => match account_action {
|
||||
AccountsAction::Switch(switch_action) => {
|
||||
ctx.accounts.select_account(switch_action.switch_to);
|
||||
ctx.accounts.select_account(&switch_action.switch_to);
|
||||
// pop nav after switch
|
||||
get_active_columns_mut(ctx.accounts, decks_cache)
|
||||
.column_mut(switch_action.source_column)
|
||||
.router_mut()
|
||||
.go_back();
|
||||
}
|
||||
AccountsAction::Remove(index) => ctx.accounts.remove_account(*index),
|
||||
AccountsAction::Remove(to_remove) => ctx.accounts.remove_account(to_remove),
|
||||
},
|
||||
SwitchingAction::Columns(columns_action) => match *columns_action {
|
||||
ColumnsAction::Remove(index) => {
|
||||
@@ -481,7 +481,7 @@ fn render_nav_body(
|
||||
};
|
||||
|
||||
let id = egui::Id::new(("post", col, note.key().unwrap()));
|
||||
let poster = ctx.accounts.selected_or_first_nsec()?;
|
||||
let poster = ctx.accounts.selected_filled()?;
|
||||
|
||||
let action = {
|
||||
let draft = app.drafts.reply_mut(note.id());
|
||||
@@ -519,7 +519,7 @@ fn render_nav_body(
|
||||
|
||||
let id = egui::Id::new(("post", col, note.key().unwrap()));
|
||||
|
||||
let poster = ctx.accounts.selected_or_first_nsec()?;
|
||||
let poster = ctx.accounts.selected_filled()?;
|
||||
let draft = app.drafts.quote_mut(note.id());
|
||||
|
||||
let response = egui::ScrollArea::vertical()
|
||||
@@ -656,7 +656,7 @@ fn render_nav_body(
|
||||
}
|
||||
Route::EditProfile(pubkey) => {
|
||||
let mut action = None;
|
||||
if let Some(kp) = ctx.accounts.get_full(pubkey.bytes()) {
|
||||
if let Some(kp) = ctx.accounts.get_full(pubkey) {
|
||||
let state = app
|
||||
.view_state
|
||||
.pubkey_to_profile_state
|
||||
@@ -686,15 +686,16 @@ fn render_nav_body(
|
||||
Route::Wallet(wallet_type) => {
|
||||
let state = match wallet_type {
|
||||
notedeck::WalletType::Auto => 's: {
|
||||
if let Some(cur_acc) = ctx.accounts.get_selected_account_mut() {
|
||||
if let Some(wallet) = &mut cur_acc.wallet {
|
||||
let default_zap_state = get_default_zap_state(&mut wallet.default_zap);
|
||||
break 's WalletState::Wallet {
|
||||
wallet: &mut wallet.wallet,
|
||||
default_zap_state,
|
||||
can_create_local_wallet: false,
|
||||
};
|
||||
}
|
||||
if let Some(cur_acc_wallet) =
|
||||
&mut ctx.accounts.get_selected_account_mut().wallet
|
||||
{
|
||||
let default_zap_state =
|
||||
get_default_zap_state(&mut cur_acc_wallet.default_zap);
|
||||
break 's WalletState::Wallet {
|
||||
wallet: &mut cur_acc_wallet.wallet,
|
||||
default_zap_state,
|
||||
can_create_local_wallet: false,
|
||||
};
|
||||
}
|
||||
|
||||
let Some(wallet) = &mut ctx.global_wallet.wallet else {
|
||||
@@ -712,12 +713,7 @@ fn render_nav_body(
|
||||
}
|
||||
}
|
||||
notedeck::WalletType::Local => 's: {
|
||||
let Some(cur_acc) = ctx.accounts.get_selected_account_mut() else {
|
||||
break 's WalletState::NoWallet {
|
||||
state: &mut ctx.global_wallet.ui_state,
|
||||
show_local_only: false,
|
||||
};
|
||||
};
|
||||
let cur_acc = ctx.accounts.get_selected_account_mut();
|
||||
let Some(wallet) = &mut cur_acc.wallet else {
|
||||
break 's WalletState::NoWallet {
|
||||
state: &mut ctx.global_wallet.ui_state,
|
||||
|
||||
@@ -131,7 +131,7 @@ pub fn render_profile_route(
|
||||
if let Some(action) = action {
|
||||
match action {
|
||||
ui::profile::ProfileViewAction::EditProfile => accounts
|
||||
.get_full(pubkey.bytes())
|
||||
.get_full(pubkey)
|
||||
.map(|kp| RenderNavAction::ProfileAction(ProfileAction::Edit(kp.to_full()))),
|
||||
ui::profile::ProfileViewAction::Note(note_action) => {
|
||||
Some(RenderNavAction::NoteAction(note_action))
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use egui::{
|
||||
Align, Button, Frame, InnerResponse, Layout, RichText, ScrollArea, Ui, UiBuilder, Vec2,
|
||||
};
|
||||
use enostr::Pubkey;
|
||||
use nostrdb::{Ndb, Transaction};
|
||||
use notedeck::{Accounts, Images};
|
||||
use notedeck_ui::colors::PINK;
|
||||
@@ -16,8 +17,8 @@ pub struct AccountsView<'a> {
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum AccountsViewResponse {
|
||||
SelectAccount(usize),
|
||||
RemoveAccount(usize),
|
||||
SelectAccount(Pubkey),
|
||||
RemoveAccount(Pubkey),
|
||||
RouteToLogin,
|
||||
}
|
||||
|
||||
@@ -68,19 +69,11 @@ impl<'a> AccountsView<'a> {
|
||||
return;
|
||||
};
|
||||
|
||||
for i in 0..accounts.num_accounts() {
|
||||
let (account_pubkey, has_nsec) = match accounts.get_account(i) {
|
||||
Some(acc) => (acc.key.pubkey.bytes(), acc.key.secret_key.is_some()),
|
||||
None => continue,
|
||||
};
|
||||
|
||||
let profile = ndb.get_profile_by_pubkey(&txn, account_pubkey).ok();
|
||||
let is_selected = if let Some(selected) = accounts.get_selected_account_index()
|
||||
{
|
||||
i == selected
|
||||
} else {
|
||||
false
|
||||
};
|
||||
let selected = accounts.cache.selected();
|
||||
for (pk, account) in &accounts.cache {
|
||||
let profile = ndb.get_profile_by_pubkey(&txn, pk).ok();
|
||||
let is_selected = *pk == selected.key.pubkey;
|
||||
let has_nsec = account.key.secret_key.is_some();
|
||||
|
||||
let profile_peview_view = {
|
||||
let max_size = egui::vec2(ui.available_width(), 77.0);
|
||||
@@ -96,10 +89,10 @@ impl<'a> AccountsView<'a> {
|
||||
if let Some(op) = profile_peview_view {
|
||||
return_op = Some(match op {
|
||||
ProfilePreviewAction::SwitchTo => {
|
||||
AccountsViewResponse::SelectAccount(i)
|
||||
AccountsViewResponse::SelectAccount(*pk)
|
||||
}
|
||||
ProfilePreviewAction::RemoveAccount => {
|
||||
AccountsViewResponse::RemoveAccount(i)
|
||||
AccountsViewResponse::RemoveAccount(*pk)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ impl WalletAction {
|
||||
let ui_state = &mut global_wallet.ui_state;
|
||||
if ui_state.for_local_only {
|
||||
ui_state.for_local_only = false;
|
||||
let cur_acc = accounts.get_selected_account_mut()?;
|
||||
let cur_acc = accounts.get_selected_account_mut();
|
||||
|
||||
if cur_acc.wallet.is_some() {
|
||||
return None;
|
||||
|
||||
Reference in New Issue
Block a user