Code refactoring

This commit is contained in:
RainLoop Team 2015-02-22 02:00:51 +04:00
parent bf56821c2d
commit 31860420cb
32 changed files with 1342 additions and 1282 deletions

View file

@ -3,7 +3,10 @@
'use strict';
var
ko = require('ko')
_ = require('_'),
ko = require('ko'),
Utils = require('Common/Utils')
;
/**
@ -35,6 +38,84 @@
return !!this.openpgp;
};
PgpUserStore.prototype.findPublicKeyByHex = function (sHash)
{
return _.find(this.openpgpkeysPublic(), function (oItem) {
return oItem && sHash === oItem.id;
});
};
PgpUserStore.prototype.findPublicKeysByEmail = function (sEmail)
{
var self = this;
return _.compact(_.map(this.openpgpkeysPublic(), function (oItem) {
var oKey = null;
if (oItem && sEmail === oItem.email)
{
try
{
oKey = self.openpgp.key.readArmored(oItem.armor);
if (oKey && !oKey.err && oKey.keys && oKey.keys[0])
{
return oKey.keys[0];
}
}
catch (e) {}
}
return null;
}));
};
/**
* @param {string} sEmail
* @param {string=} sPassword
* @returns {?}
*/
PgpUserStore.prototype.findPrivateKeyByEmail = function (sEmail, sPassword)
{
var
oPrivateKey = null,
oKey = _.find(this.openpgpkeysPrivate(), function (oItem) {
return oItem && sEmail === oItem.email;
})
;
if (oKey)
{
try
{
oPrivateKey = this.openpgp.key.readArmored(oKey.armor);
if (oPrivateKey && !oPrivateKey.err && oPrivateKey.keys && oPrivateKey.keys[0])
{
oPrivateKey = oPrivateKey.keys[0];
oPrivateKey.decrypt(Utils.pString(sPassword));
}
else
{
oPrivateKey = null;
}
}
catch (e)
{
oPrivateKey = null;
}
}
return oPrivateKey;
};
/**
* @param {string=} sPassword
* @returns {?}
*/
PgpUserStore.prototype.findSelfPrivateKey = function (sPassword)
{
return this.findPrivateKeyByEmail(require('Stores/User/Account').email(), sPassword);
};
module.exports = new PgpUserStore();
}());