snappymail/dev/Model/OpenPgpKey.js

75 lines
1.5 KiB
JavaScript
Raw Normal View History

2016-07-07 05:03:30 +08:00
import ko from 'ko';
2021-07-22 03:34:17 +08:00
import { arrayLength } from 'Common/Utils';
2019-07-05 03:19:24 +08:00
import { AbstractModel } from 'Knoin/AbstractModel';
import { PgpUserStore } from 'Stores/User/Pgp';
2016-07-07 05:03:30 +08:00
2021-01-22 23:32:08 +08:00
export class OpenPgpKeyModel extends AbstractModel {
2016-07-07 05:03:30 +08:00
/**
* @param {string} index
* @param {string} guID
* @param {string} ID
* @param {array} IDs
* @param {array} userIDs
* @param {array} emails
* @param {boolean} isPrivate
* @param {string} armor
* @param {string} userID
*/
2019-07-05 03:19:24 +08:00
constructor(index, guID, ID, IDs, userIDs, emails, isPrivate, armor, userID) {
2020-10-19 01:19:45 +08:00
super();
2016-07-07 05:03:30 +08:00
this.index = index;
this.id = ID;
2021-07-22 03:34:17 +08:00
this.ids = arrayLength(IDs) ? IDs : [ID];
2016-07-07 05:03:30 +08:00
this.guid = guID;
this.user = '';
this.users = userIDs;
this.email = '';
this.emails = emails;
this.armor = armor;
this.isPrivate = !!isPrivate;
this.selectUser(userID);
this.deleteAccess = ko.observable(false);
}
getNativeKey() {
let key = null;
2019-07-05 03:19:24 +08:00
try {
key = PgpUserStore.openpgp.key.readArmored(this.armor);
2019-07-05 03:19:24 +08:00
if (key && !key.err && key.keys && key.keys[0]) {
2016-07-07 05:03:30 +08:00
return key;
}
2019-07-05 03:19:24 +08:00
} catch (e) {
console.log(e);
2016-07-07 05:03:30 +08:00
}
return null;
}
getNativeKeys() {
const key = this.getNativeKey();
return key && key.keys ? key.keys : null;
}
select(pattern, property) {
2019-07-05 03:19:24 +08:00
if (this[property]) {
2016-07-07 05:03:30 +08:00
const index = this[property].indexOf(pattern);
2019-07-05 03:19:24 +08:00
if (-1 !== index) {
2016-07-07 05:03:30 +08:00
this.user = this.users[index];
this.email = this.emails[index];
}
}
}
selectUser(user) {
this.select(user, 'users');
}
selectEmail(email) {
this.select(email, 'emails');
}
}