2016-06-21 05:44:02 +08:00
|
|
|
const crypto = require('crypto');
|
2016-06-23 01:59:22 +08:00
|
|
|
const {JSONType, JSONARRAYType} = require('../../database-types');
|
2016-06-21 05:44:02 +08:00
|
|
|
|
2016-06-19 18:02:32 +08:00
|
|
|
module.exports = (sequelize, Sequelize) => {
|
|
|
|
const Message = sequelize.define('Message', {
|
2016-06-23 01:59:22 +08:00
|
|
|
rawBody: Sequelize.STRING,
|
|
|
|
rawHeaders: Sequelize.STRING,
|
2016-06-22 05:58:20 +08:00
|
|
|
messageId: Sequelize.STRING,
|
|
|
|
body: Sequelize.STRING,
|
2016-06-22 08:25:25 +08:00
|
|
|
headers: JSONType('headers'),
|
2016-06-19 18:02:32 +08:00
|
|
|
subject: Sequelize.STRING,
|
|
|
|
snippet: Sequelize.STRING,
|
2016-06-21 05:57:54 +08:00
|
|
|
hash: Sequelize.STRING,
|
2016-06-19 18:02:32 +08:00
|
|
|
date: Sequelize.DATE,
|
|
|
|
unread: Sequelize.BOOLEAN,
|
|
|
|
starred: Sequelize.BOOLEAN,
|
2016-06-22 05:58:20 +08:00
|
|
|
processed: Sequelize.INTEGER,
|
2016-06-23 01:59:22 +08:00
|
|
|
to: JSONARRAYType('to'),
|
|
|
|
from: JSONARRAYType('from'),
|
|
|
|
cc: JSONARRAYType('cc'),
|
|
|
|
bcc: JSONARRAYType('bcc'),
|
2016-06-22 05:58:20 +08:00
|
|
|
CategoryUID: { type: Sequelize.STRING, allowNull: true},
|
2016-06-19 18:02:32 +08:00
|
|
|
}, {
|
2016-06-22 05:58:20 +08:00
|
|
|
indexes: [
|
|
|
|
{
|
|
|
|
unique: true,
|
|
|
|
fields: ['hash'],
|
|
|
|
},
|
|
|
|
],
|
2016-06-19 18:02:32 +08:00
|
|
|
classMethods: {
|
2016-06-22 05:58:20 +08:00
|
|
|
associate: ({Category}) => {
|
|
|
|
Message.belongsTo(Category)
|
2016-06-19 18:02:32 +08:00
|
|
|
},
|
2016-06-21 05:44:02 +08:00
|
|
|
hashForHeaders: (headers) => {
|
|
|
|
return crypto.createHash('sha256').update(headers, 'utf8').digest('hex');
|
|
|
|
},
|
2016-06-19 18:02:32 +08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return Message;
|
|
|
|
};
|