Sort the app list by approval time, newest first

list_for now orders an identity's apps by approved_at descending, so the
Apps page shows the app just paired at the top instead of wherever it
landed in apps.json. Whole-second timestamps tie, so the stable sort is
reversed to keep the later pairing first among equals; entries written
before approved_at existed carry 0 and sink to the bottom.

apps.json still stores insertion order — this is a view rule, and every
consumer of list_for (the Apps page, Forget all, the Connect dialog's
count, the identities window's revocation list) wants the same order.
This commit is contained in:
raucao committed 2026-11-28 21:06:49 +04:00
1 parent d4bb9d5978
commit a9d51db400
2 files changed
+77 -3

No files matched your search

+5
View File
@@ -321,6 +321,11 @@ data/ desktop, metainfo, gschema (all `.in` templates), icons, re
- The Apps page shows one `AdwExpanderRow` per app, with a per-app `view-more`
menu as its only suffix (*Rename…* and *Revoke*), one *Remove* row per stored
permission (kind for `sign_event`, method for encryption), and *Forget all*.
The list is **newest approval first** (`list_for` sorts by `approved_at`,
descending, ties in reverse insertion order), so a re-pairing moves an app back
to the top and pre-`approved_at` entries (timestamp 0) sit at the bottom.
`apps.json` still stores them in insertion order — the sort is a view rule, not
a storage one.
The title is the app's **display name**: the user's own `label` first, then
what the client declared, else the shortened npub. The collapsed byline is the
**host** the app declares (`declared_host`), falling back to the shortened npub
+72 -3
View File
@@ -346,14 +346,24 @@ impl AppRegistry {
Ok(())
}
/// Applications bound to one identity.
/// Applications bound to one identity, newest approval first.
///
/// Re-pairing an app sets `approved_at` again, so it moves back to the top.
/// `approved_at` is whole seconds, so pairings in the same second tie: the
/// ascending sort is stable and the reverse then puts the later insertion
/// first, which is what "newest" means here. Entries written before
/// `approved_at` existed carry 0 and sort to the bottom.
pub fn list_for(&self, identity: PublicKey) -> Vec<App> {
let identity = identity.to_hex();
self.apps
let mut apps: Vec<App> = self
.apps
.iter()
.filter(|app| app.identity == identity)
.cloned()
.collect()
.collect();
apps.sort_by_key(|app| app.approved_at);
apps.reverse();
apps
}
/// Forget every application bound to one identity.
@@ -564,6 +574,65 @@ mod tests {
);
}
fn app(identity: PublicKey, approved_at: i64) -> App {
App {
public_key: Keys::generate().public_key().to_hex(),
name: Some(format!("app-{approved_at}")),
label: None,
url: None,
image: None,
approved_at,
identity: identity.to_hex(),
policy: Policy::default(),
}
}
/// The Apps page shows the most recently paired application first, scoped to
/// one identity.
#[test]
fn apps_are_listed_newest_first() {
let identity = Keys::generate().public_key();
let other = Keys::generate().public_key();
let mut registry = AppRegistry::default();
registry.apps.push(app(identity, 100));
registry.apps.push(app(identity, 300));
registry.apps.push(app(other, 200));
registry.apps.push(app(identity, 200));
let names: Vec<String> = registry
.list_for(identity)
.iter()
.map(|app| app.display_name().unwrap().to_string())
.collect();
assert_eq!(names, ["app-300", "app-200", "app-100"]);
// `approved_at` is whole seconds, so pairings in the same second tie;
// the later insertion is the newer one.
let earlier = app(identity, 400);
let later = app(identity, 400);
registry.apps.push(earlier.clone());
registry.apps.push(later.clone());
let listed = registry.list_for(identity);
assert_eq!(listed[0].public_key, later.public_key);
assert_eq!(listed[1].public_key, earlier.public_key);
// Re-pairing rewrites the timestamp, so the app comes back to the top.
let keys = Keys::generate();
registry.apps.push(App {
public_key: keys.public_key().to_hex(),
approved_at: 0,
..app(identity, 0)
});
assert_eq!(registry.list_for(identity).last().unwrap().approved_at, 0);
registry
.approve(&client(keys.public_key()), identity, None)
.unwrap();
assert_eq!(
registry.list_for(identity)[0].public_key,
keys.public_key().to_hex()
);
}
#[test]
fn legacy_apps_are_bound_once() {
let identity = Keys::generate().public_key();