Skip to content

Commit e9c3596

Browse files
kernelkindjb55
authored andcommitted
AccountManagementView
View used to add and remove accounts from the app Signed-off-by: kernelkind <[email protected]> Signed-off-by: William Casarin <[email protected]>
1 parent 93800e0 commit e9c3596

File tree

9 files changed

+555
-3
lines changed

9 files changed

+555
-3
lines changed

src/account_manager.rs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
use nostr_sdk::Keys;
2+
use nostrdb::{Ndb, Transaction};
3+
4+
pub use crate::user_account::UserAccount;
5+
use crate::{
6+
imgcache::ImageCache, key_storage::KeyStorage, relay_generation::RelayGenerator,
7+
ui::profile::preview::SimpleProfilePreview,
8+
};
9+
10+
pub struct SimpleProfilePreviewController<'a> {
11+
ndb: &'a Ndb,
12+
img_cache: &'a mut ImageCache,
13+
}
14+
15+
impl<'a> SimpleProfilePreviewController<'a> {
16+
pub fn new(ndb: &'a Ndb, img_cache: &'a mut ImageCache) -> Self {
17+
SimpleProfilePreviewController { ndb, img_cache }
18+
}
19+
20+
pub fn set_profile_previews(
21+
&mut self,
22+
account_manager: &AccountManager<'a>,
23+
ui: &mut egui::Ui,
24+
edit_mode: bool,
25+
add_preview_ui: fn(
26+
ui: &mut egui::Ui,
27+
preview: SimpleProfilePreview,
28+
edit_mode: bool,
29+
) -> bool,
30+
) -> Option<Vec<usize>> {
31+
let mut to_remove: Option<Vec<usize>> = None;
32+
33+
for i in 0..account_manager.num_accounts() {
34+
if let Some(account) = account_manager.get_account(i) {
35+
if let Ok(txn) = Transaction::new(self.ndb) {
36+
let profile = self
37+
.ndb
38+
.get_profile_by_pubkey(&txn, &account.key.public_key().to_bytes());
39+
40+
if let Ok(profile) = profile {
41+
let preview = SimpleProfilePreview::new(&profile, self.img_cache);
42+
43+
if add_preview_ui(ui, preview, edit_mode) {
44+
if to_remove.is_none() {
45+
to_remove = Some(Vec::new());
46+
}
47+
to_remove.as_mut().unwrap().push(i);
48+
}
49+
};
50+
}
51+
}
52+
}
53+
54+
to_remove
55+
}
56+
}
57+
58+
/// The interface for managing the user's accounts.
59+
/// Represents all user-facing operations related to account management.
60+
pub struct AccountManager<'a> {
61+
accounts: &'a mut Vec<UserAccount>,
62+
key_store: KeyStorage,
63+
relay_generator: RelayGenerator,
64+
}
65+
66+
impl<'a> AccountManager<'a> {
67+
pub fn new(
68+
accounts: &'a mut Vec<UserAccount>,
69+
key_store: KeyStorage,
70+
relay_generator: RelayGenerator,
71+
) -> Self {
72+
AccountManager {
73+
accounts,
74+
key_store,
75+
relay_generator,
76+
}
77+
}
78+
79+
pub fn get_accounts(&'a self) -> &'a Vec<UserAccount> {
80+
self.accounts
81+
}
82+
83+
pub fn get_account(&'a self, index: usize) -> Option<&'a UserAccount> {
84+
self.accounts.get(index)
85+
}
86+
87+
pub fn remove_account(&mut self, index: usize) {
88+
if let Some(account) = self.accounts.get(index) {
89+
self.key_store.remove_key(&account.key);
90+
}
91+
if index < self.accounts.len() {
92+
self.accounts.remove(index);
93+
}
94+
}
95+
96+
pub fn add_account(&'a mut self, key: Keys, ctx: &egui::Context) {
97+
self.key_store.add_key(&key);
98+
let relays = self.relay_generator.generate_relays_for(&key, ctx);
99+
let account = UserAccount { key, relays };
100+
101+
self.accounts.push(account)
102+
}
103+
104+
pub fn num_accounts(&self) -> usize {
105+
self.accounts.len()
106+
}
107+
}

src/key_storage.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use nostr_sdk::Keys;
2+
3+
pub enum KeyStorage {
4+
None,
5+
// TODO:
6+
// Linux,
7+
// Windows,
8+
// Android,
9+
}
10+
11+
impl KeyStorage {
12+
pub fn get_keys(&self) -> Result<Vec<Keys>, KeyStorageError> {
13+
match self {
14+
Self::None => Ok(Vec::new()),
15+
}
16+
}
17+
18+
pub fn add_key(&self, key: &Keys) -> Result<(), KeyStorageError> {
19+
match self {
20+
Self::None => Ok(()),
21+
}
22+
}
23+
24+
pub fn remove_key(&self, key: &Keys) -> Result<(), KeyStorageError> {
25+
match self {
26+
Self::None => Ok(()),
27+
}
28+
}
29+
}
30+
31+
#[derive(Debug, PartialEq)]
32+
pub enum KeyStorageError<'a> {
33+
Retrieval,
34+
Addition(&'a Keys),
35+
Removal(&'a Keys),
36+
}
37+
38+
impl std::fmt::Display for KeyStorageError<'_> {
39+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
40+
match self {
41+
Self::Retrieval => write!(f, "Failed to retrieve keys."),
42+
Self::Addition(key) => write!(f, "Failed to add key: {:?}", key.public_key()),
43+
Self::Removal(key) => write!(f, "Failed to remove key: {:?}", key.public_key()),
44+
}
45+
}
46+
}
47+
48+
impl std::error::Error for KeyStorageError<'_> {}

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod error;
44
//mod note;
55
//mod block;
66
mod abbrev;
7+
pub mod account_manager;
78
pub mod app_creation;
89
mod app_style;
910
mod colors;
@@ -13,16 +14,19 @@ mod frame_history;
1314
mod images;
1415
mod imgcache;
1516
mod key_parsing;
17+
mod key_storage;
1618
pub mod login_manager;
1719
mod notecache;
1820
mod profile;
21+
mod relay_generation;
1922
pub mod relay_pool_manager;
2023
mod result;
2124
mod test_data;
2225
mod time;
2326
mod timecache;
2427
mod timeline;
2528
pub mod ui;
29+
mod user_account;
2630

2731
#[cfg(test)]
2832
#[macro_use]

src/relay_generation.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use crate::relay_pool_manager::create_wakeup;
2+
use enostr::RelayPool;
3+
use nostr_sdk::Keys;
4+
use tracing::error;
5+
6+
pub enum RelayGenerator {
7+
GossipModel,
8+
Nip65,
9+
Constant,
10+
}
11+
12+
impl RelayGenerator {
13+
pub fn generate_relays_for(&self, key: &Keys, ctx: &egui::Context) -> RelayPool {
14+
match self {
15+
Self::GossipModel => generate_relays_gossip(key, ctx),
16+
Self::Nip65 => generate_relays_nip65(key, ctx),
17+
Self::Constant => generate_constant_relays(ctx),
18+
}
19+
}
20+
}
21+
22+
fn generate_relays_gossip(key: &Keys, ctx: &egui::Context) -> RelayPool {
23+
todo!()
24+
}
25+
26+
fn generate_relays_nip65(key: &Keys, ctx: &egui::Context) -> RelayPool {
27+
todo!()
28+
}
29+
30+
fn generate_constant_relays(ctx: &egui::Context) -> RelayPool {
31+
let mut pool = RelayPool::new();
32+
let wakeup = create_wakeup(ctx);
33+
34+
if let Err(e) = pool.add_url("ws://localhost:8080".to_string(), wakeup.clone()) {
35+
error!("{:?}", e)
36+
}
37+
if let Err(e) = pool.add_url("wss://relay.damus.io".to_string(), wakeup.clone()) {
38+
error!("{:?}", e)
39+
}
40+
if let Err(e) = pool.add_url("wss://pyramid.fiatjaf.com".to_string(), wakeup.clone()) {
41+
error!("{:?}", e)
42+
}
43+
if let Err(e) = pool.add_url("wss://nos.lol".to_string(), wakeup.clone()) {
44+
error!("{:?}", e)
45+
}
46+
if let Err(e) = pool.add_url("wss://nostr.wine".to_string(), wakeup.clone()) {
47+
error!("{:?}", e)
48+
}
49+
if let Err(e) = pool.add_url("wss://purplepag.es".to_string(), wakeup) {
50+
error!("{:?}", e)
51+
}
52+
53+
pool
54+
}

src/relay_pool_manager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl<'a> RelayPoolManager<'a> {
4646
}
4747
}
4848

49-
fn create_wakeup(ctx: &egui::Context) -> impl Fn() + Send + Sync + Clone + 'static {
49+
pub fn create_wakeup(ctx: &egui::Context) -> impl Fn() + Send + Sync + Clone + 'static {
5050
let ctx = ctx.clone();
5151
move || {
5252
ctx.request_repaint();

0 commit comments

Comments
 (0)