Sort PGP keys by email and id

This commit is contained in:
the-djmaze 2024-02-26 19:21:40 +01:00
parent 6141ca7936
commit 091895df38
2 changed files with 22 additions and 17 deletions

View file

@ -101,9 +101,13 @@ export const GnuPGUserStore = new class {
}; };
key.view = () => key.fetch(() => showScreenPopup(OpenPgpKeyPopupView, [key])); key.view = () => key.fetch(() => showScreenPopup(OpenPgpKeyPopupView, [key]));
return key; return key;
}; },
this.publicKeys(oData.Result.public.map(key => initKey(key, 0))); collator = new Intl.Collator(undefined, {sensitivity: 'base'}),
this.privateKeys(oData.Result.private.map(key => initKey(key, 1))); sort = keys => keys.sort(
(a, b) => collator.compare(a.emails[0], b.emails[0]) || collator.compare(a.id, b.id)
);
this.publicKeys(sort(oData.Result.public.map(key => initKey(key, 0))));
this.privateKeys(sort(oData.Result.private.map(key => initKey(key, 1))));
console.log('gnupg ready'); console.log('gnupg ready');
} }
} }

View file

@ -39,6 +39,15 @@ const
} }
}, },
collator = new Intl.Collator(undefined, {sensitivity: 'base'}),
sort = keys => keys.sort(
// (a, b) => collator.compare(a.emails[0], b.emails[0]) || collator.compare(a.fingerprint, b.fingerprint)
(a, b) => collator.compare(a.emails[0], b.emails[0]) || collator.compare(a.id, b.id)
),
dedup = keys => sort((keys || [])
.filter((v, i, a) => a.findIndex(entry => entry.fingerprint == v.fingerprint) === i)
),
/** /**
* OpenPGP.js v5 removed the localStorage (keyring) * OpenPGP.js v5 removed the localStorage (keyring)
* This should be compatible with the old OpenPGP.js v2 * This should be compatible with the old OpenPGP.js v2
@ -114,10 +123,6 @@ export const OpenPGPUserStore = new class {
loadKeyrings() { loadKeyrings() {
if (window.openpgp) { if (window.openpgp) {
const collator = new Intl.Collator(undefined, {numeric: true, sensitivity: 'base'}),
dedup = keys => (keys || [])
.filter((v, i, a) => a.findIndex(entry => entry.fingerprint == v.fingerprint) === i)
.sort((a, b) => collator.compare(a.emails[0], b.emails[0]));
loadOpenPgpKeys(publicKeysItem).then(keys => { loadOpenPgpKeys(publicKeysItem).then(keys => {
this.publicKeys(dedup(keys)); this.publicKeys(dedup(keys));
console.log('openpgp.js public keys loaded'); console.log('openpgp.js public keys loaded');
@ -140,16 +145,12 @@ export const OpenPGPUserStore = new class {
window.openpgp && openpgp.readKey({armoredKey:armoredKey}).then(key => { window.openpgp && openpgp.readKey({armoredKey:armoredKey}).then(key => {
if (!key.err) { if (!key.err) {
key = new OpenPgpKeyModel(armoredKey, key); key = new OpenPgpKeyModel(armoredKey, key);
if (key.key.isPrivate()) { const isPrivate = key.key.isPrivate(),
if (!this.privateKeys.find(entry => entry.fingerprint == key.fingerprint)) { keys = isPrivate ? this.privateKeys : this.publicKeys;
this.privateKeys.push(key); if (!keys.find(entry => entry.fingerprint == key.fingerprint)) {
storeOpenPgpKeys(this.privateKeys, privateKeysItem); keys.push(key);
} keys(sort(keys));
} else { storeOpenPgpKeys(keys, isPrivate ? privateKeysItem : publicKeysItem);
if (!this.publicKeys.find(entry => entry.fingerprint == key.fingerprint)) {
this.publicKeys.push(key);
storeOpenPgpKeys(this.publicKeys, publicKeysItem);
}
} }
} }
}); });