Mailspring/packages/nylas-core/models/account/message.js
Juan Tejada caaef8d5b5 Add /messages/<id> enpoint + support for raw message/rfc822
- When message/rfc822 is requested, fetch ray message from IMAP
connection
2016-06-25 11:25:34 -07:00

79 lines
2.2 KiB
JavaScript

const crypto = require('crypto');
const IMAPConnection = require('../../imap-connection')
const {JSONType, JSONARRAYType} = require('../../database-types');
module.exports = (sequelize, Sequelize) => {
const Message = sequelize.define('Message', {
rawBody: Sequelize.STRING,
rawHeaders: Sequelize.STRING,
messageId: Sequelize.STRING,
body: Sequelize.STRING,
headers: JSONType('headers'),
subject: Sequelize.STRING,
snippet: Sequelize.STRING,
hash: Sequelize.STRING,
date: Sequelize.DATE,
unread: Sequelize.BOOLEAN,
starred: Sequelize.BOOLEAN,
processed: Sequelize.INTEGER,
to: JSONARRAYType('to'),
from: JSONARRAYType('from'),
cc: JSONARRAYType('cc'),
bcc: JSONARRAYType('bcc'),
CategoryUID: { type: Sequelize.STRING, allowNull: true},
}, {
indexes: [
{
unique: true,
fields: ['hash'],
},
],
classMethods: {
associate: ({Category, Thread}) => {
Message.belongsTo(Category)
Message.belongsTo(Thread)
},
hashForHeaders: (headers) => {
return crypto.createHash('sha256').update(headers, 'utf8').digest('hex');
},
},
instanceMethods: {
fetchRaw({account, db}) {
return this.getCategory()
.then((category) => {
const settings = Object.assign({}, account.connectionSettings, account.decryptedCredentials())
const conn = new IMAPConnection(db, settings)
return conn.connect()
.then(() => conn.openBox(category.name))
.then(() => {
return new Promise((resolve) => {
conn.fetchMessages([this.CategoryUID], (attributes, headers, body) => {
resolve(`${headers}${body}`)
})
})
})
.then((raw) => {
conn.end()
return raw
})
})
},
toJSON: function toJSON() {
return {
id: this.id,
body: this.body,
subject: this.subject,
snippet: this.snippet,
date: this.date.getTime() / 1000.0,
unread: this.unread,
starred: this.starred,
category_id: this.CategoryId,
};
},
},
});
return Message;
};