mirror of
https://github.com/Foundry376/Mailspring.git
synced 2024-11-12 04:25:31 +08:00
ed749e0f51
- Handles sync errors in a single place. For now, if error is not a socket error, will treat as a permanent error, save the error to the account object, and prevent any other syncing until the error is cleared from the account object - Adds a NylasError class that can be extended and serialized. Adds it to global namespace on all packages and replaces all uses of regular Error
62 lines
1.9 KiB
JavaScript
62 lines
1.9 KiB
JavaScript
const crypto = require('crypto');
|
|
const {JSONType} = require('../../database-types');
|
|
|
|
const {DB_ENCRYPTION_ALGORITHM, DB_ENCRYPTION_PASSWORD} = process.env;
|
|
|
|
module.exports = (sequelize, Sequelize) => {
|
|
const Account = sequelize.define('Account', {
|
|
name: Sequelize.STRING,
|
|
provider: Sequelize.STRING,
|
|
emailAddress: Sequelize.STRING,
|
|
connectionSettings: JSONType('connectionSettings'),
|
|
connectionCredentials: Sequelize.STRING,
|
|
syncPolicy: JSONType('syncPolicy'),
|
|
syncError: JSONType('syncError', {defaultValue: null}),
|
|
}, {
|
|
classMethods: {
|
|
associate: ({AccountToken}) => {
|
|
Account.hasMany(AccountToken, {as: 'tokens'})
|
|
},
|
|
},
|
|
instanceMethods: {
|
|
toJSON: function toJSON() {
|
|
return {
|
|
id: this.id,
|
|
email_address: this.emailAddress,
|
|
connection_settings: this.connectionSettings,
|
|
sync_policy: this.syncPolicy,
|
|
sync_error: this.syncError,
|
|
}
|
|
},
|
|
|
|
errored: function errored() {
|
|
return this.syncError != null
|
|
},
|
|
|
|
setCredentials: function setCredentials(json) {
|
|
if (!(json instanceof Object)) {
|
|
throw new NylasError("Call setCredentials with JSON!")
|
|
}
|
|
const cipher = crypto.createCipher(DB_ENCRYPTION_ALGORITHM, DB_ENCRYPTION_PASSWORD)
|
|
let crypted = cipher.update(JSON.stringify(json), 'utf8', 'hex')
|
|
crypted += cipher.final('hex');
|
|
|
|
this.connectionCredentials = crypted;
|
|
},
|
|
|
|
decryptedCredentials: function decryptedCredentials() {
|
|
const decipher = crypto.createDecipher(DB_ENCRYPTION_ALGORITHM, DB_ENCRYPTION_PASSWORD)
|
|
let dec = decipher.update(this.connectionCredentials, 'hex', 'utf8')
|
|
dec += decipher.final('utf8');
|
|
|
|
try {
|
|
return JSON.parse(dec);
|
|
} catch (err) {
|
|
return null;
|
|
}
|
|
},
|
|
},
|
|
});
|
|
|
|
return Account;
|
|
};
|