2016-06-29 09:01:43 +08:00
|
|
|
const {JSONARRAYType} = require('../../database-types');
|
|
|
|
|
2016-06-19 18:02:32 +08:00
|
|
|
module.exports = (sequelize, Sequelize) => {
|
2016-06-30 01:36:32 +08:00
|
|
|
const Thread = sequelize.define('thread', {
|
|
|
|
accountId: { type: Sequelize.STRING, allowNull: false },
|
2016-06-30 02:22:38 +08:00
|
|
|
version: Sequelize.INTEGER,
|
2016-06-23 02:49:53 +08:00
|
|
|
threadId: Sequelize.STRING,
|
|
|
|
subject: Sequelize.STRING,
|
2016-06-29 09:01:43 +08:00
|
|
|
snippet: Sequelize.STRING,
|
2016-06-25 07:14:04 +08:00
|
|
|
unreadCount: Sequelize.INTEGER,
|
2016-06-28 01:15:05 +08:00
|
|
|
starredCount: Sequelize.INTEGER,
|
2016-06-29 09:01:43 +08:00
|
|
|
firstMessageDate: Sequelize.DATE,
|
|
|
|
lastMessageDate: Sequelize.DATE,
|
|
|
|
lastMessageReceivedDate: Sequelize.DATE,
|
|
|
|
lastMessageSentDate: Sequelize.DATE,
|
|
|
|
participants: JSONARRAYType('participants'),
|
2016-06-19 18:02:32 +08:00
|
|
|
}, {
|
2016-06-30 07:10:39 +08:00
|
|
|
indexes: [
|
|
|
|
{ fields: ['subject'] },
|
|
|
|
{ fields: ['threadId'] },
|
|
|
|
],
|
2016-06-19 18:02:32 +08:00
|
|
|
classMethods: {
|
2016-06-29 06:01:41 +08:00
|
|
|
associate: ({Category, Message, ThreadCategory}) => {
|
|
|
|
Thread.belongsToMany(Category, {through: ThreadCategory})
|
2016-06-23 02:49:53 +08:00
|
|
|
Thread.hasMany(Message, {as: 'messages'})
|
2016-06-19 18:02:32 +08:00
|
|
|
},
|
|
|
|
},
|
2016-06-29 09:01:43 +08:00
|
|
|
instanceMethods: {
|
|
|
|
toJSON: function toJSON() {
|
2016-06-30 03:02:31 +08:00
|
|
|
if (!(this.categories instanceof Array)) {
|
2016-06-30 02:22:38 +08:00
|
|
|
throw new Error("Thread.toJSON called on a thread where categories were not eagerly loaded.")
|
|
|
|
}
|
2016-06-30 03:02:31 +08:00
|
|
|
if (!(this.messages instanceof Array)) {
|
|
|
|
throw new Error("Thread.toJSON called on a thread where messages were not eagerly loaded. (Only need the IDs!)")
|
|
|
|
}
|
|
|
|
|
|
|
|
const response = {
|
2016-06-29 09:01:43 +08:00
|
|
|
id: this.id,
|
2016-06-29 09:14:54 +08:00
|
|
|
object: 'thread',
|
2016-06-30 03:02:31 +08:00
|
|
|
folders: this.categories.filter(c => c.type === 'folder'),
|
|
|
|
labels: this.categories.filter(c => c.type === 'label'),
|
2016-06-30 01:36:32 +08:00
|
|
|
account_id: this.accountId,
|
2016-06-29 09:01:43 +08:00
|
|
|
participants: this.participants,
|
|
|
|
subject: this.subject,
|
|
|
|
snippet: this.snippet,
|
|
|
|
unread: this.unreadCount > 0,
|
|
|
|
starred: this.starredCount > 0,
|
|
|
|
last_message_timestamp: this.lastMessageDate ? this.lastMessageDate.getTime() / 1000.0 : null,
|
|
|
|
last_message_sent_timestamp: this.lastMessageSentDate ? this.lastMessageSentDate.getTime() / 1000.0 : null,
|
|
|
|
last_message_received_timestamp: this.lastMessageReceivedDate ? this.lastMessageReceivedDate.getTime() / 1000.0 : null,
|
|
|
|
};
|
2016-06-30 03:02:31 +08:00
|
|
|
|
|
|
|
const expanded = this.messages[0] ? !!this.messages[0].accountId : false;
|
|
|
|
if (expanded) {
|
|
|
|
response.messages = this.messages;
|
|
|
|
} else {
|
|
|
|
response.message_ids = this.messages.map(m => m.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
2016-06-29 09:01:43 +08:00
|
|
|
},
|
|
|
|
},
|
2016-06-19 18:02:32 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
return Thread;
|
|
|
|
};
|