Skip to content

Commit ce5697c

Browse files
authored
feat: add account ordering functionality (#6993)
New public API `set_accounts_order` allows setting the order of accounts. The account order is stored as a list of account IDs in `accounts.toml` under a new `accounts_order: Vec<u32>` field.
1 parent 22258f7 commit ce5697c

File tree

2 files changed

+66
-3
lines changed

2 files changed

+66
-3
lines changed

deltachat-jsonrpc/src/api.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,14 @@ impl CommandApi {
224224
self.accounts.read().await.get_selected_account_id()
225225
}
226226

227+
/// Set the order of accounts.
228+
/// The provided list should contain all account IDs in the desired order.
229+
/// If an account ID is missing from the list, it will be appended at the end.
230+
/// If the list contains non-existent account IDs, they will be ignored.
231+
async fn set_accounts_order(&self, order: Vec<u32>) -> Result<()> {
232+
self.accounts.write().await.set_accounts_order(order).await
233+
}
234+
227235
/// Get a list of all configured accounts.
228236
async fn get_all_accounts(&self) -> Result<Vec<Account>> {
229237
let mut accounts = Vec::new();

src/accounts.rs

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! # Account manager module.
22
3-
use std::collections::BTreeMap;
3+
use std::collections::{BTreeMap, BTreeSet};
44
use std::future::Future;
55
use std::path::{Path, PathBuf};
66

@@ -270,9 +270,51 @@ impl Accounts {
270270
}
271271
}
272272

273-
/// Get a list of all account ids.
273+
/// Gets a list of all account ids in the user-configured order.
274274
pub fn get_all(&self) -> Vec<u32> {
275-
self.accounts.keys().copied().collect()
275+
let mut ordered_ids = Vec::new();
276+
let mut all_ids: BTreeSet<u32> = self.accounts.keys().copied().collect();
277+
278+
// First, add accounts in the configured order
279+
for &id in &self.config.inner.accounts_order {
280+
if all_ids.remove(&id) {
281+
ordered_ids.push(id);
282+
}
283+
}
284+
285+
// Then add any accounts not in the order list (newly added accounts)
286+
for id in all_ids {
287+
ordered_ids.push(id);
288+
}
289+
290+
ordered_ids
291+
}
292+
293+
/// Sets the order of accounts.
294+
///
295+
/// The provided list should contain all account IDs in the desired order.
296+
/// If an account ID is missing from the list, it will be appended at the end.
297+
/// If the list contains non-existent account IDs, they will be ignored.
298+
pub async fn set_accounts_order(&mut self, order: Vec<u32>) -> Result<()> {
299+
let existing_ids: BTreeSet<u32> = self.accounts.keys().copied().collect();
300+
301+
// Filter out non-existent account IDs
302+
let mut filtered_order: Vec<u32> = order
303+
.into_iter()
304+
.filter(|id| existing_ids.contains(id))
305+
.collect();
306+
307+
// Add any missing account IDs at the end
308+
for &id in &existing_ids {
309+
if !filtered_order.contains(&id) {
310+
filtered_order.push(id);
311+
}
312+
}
313+
314+
self.config.inner.accounts_order = filtered_order;
315+
self.config.sync().await?;
316+
self.emit_event(EventType::AccountsChanged);
317+
Ok(())
276318
}
277319

278320
/// Starts background tasks such as IMAP and SMTP loops for all accounts.
@@ -415,6 +457,10 @@ struct InnerConfig {
415457
pub selected_account: u32,
416458
pub next_id: u32,
417459
pub accounts: Vec<AccountConfig>,
460+
/// Ordered list of account IDs, representing the user's preferred order.
461+
/// If an account ID is not in this list, it will be appended at the end.
462+
#[serde(default)]
463+
pub accounts_order: Vec<u32>,
418464
}
419465

420466
impl Drop for Config {
@@ -481,6 +527,7 @@ impl Config {
481527
accounts: Vec::new(),
482528
selected_account: 0,
483529
next_id: 1,
530+
accounts_order: Vec::new(),
484531
};
485532
if !lock {
486533
let cfg = Self {
@@ -613,6 +660,10 @@ impl Config {
613660
uuid,
614661
});
615662
self.inner.next_id += 1;
663+
664+
// Add new account to the end of the order list
665+
self.inner.accounts_order.push(id);
666+
616667
id
617668
};
618669

@@ -634,6 +685,10 @@ impl Config {
634685
// remove account from the configs
635686
self.inner.accounts.remove(idx);
636687
}
688+
689+
// Remove from order list as well
690+
self.inner.accounts_order.retain(|&x| x != id);
691+
637692
if self.inner.selected_account == id {
638693
// reset selected account
639694
self.inner.selected_account = self

0 commit comments

Comments
 (0)