fix(message-factory): Unlink circular dependency

This commit is contained in:
Jackie Luo 2016-12-08 18:10:17 -08:00
parent 6e111c073a
commit 4a11bfe977
4 changed files with 33 additions and 29 deletions

View file

@ -1,5 +1,6 @@
const Joi = require('joi');
const LocalDatabaseConnector = require('../../shared/local-database-connector');
const Errors = require('../../shared/errors');
const SendingUtils = require('../sending-utils');
const SendmailClient = require('../sendmail-client');
@ -128,7 +129,7 @@ module.exports = (server) => {
const {to, cc, bcc} = draft;
const recipients = [].concat(to, cc, bcc);
if (!recipients.find(contact => contact.email === sendTo.email)) {
throw new SendingUtils.HTTPError(
throw new Errors.HTTPError(
"Invalid sendTo, not present in message recipients",
400
);

View file

@ -1,26 +1,8 @@
const MessageFactory = require('../shared/message-factory')
const Errors = require('../shared/errors')
class HTTPError extends Error {
constructor(message, httpCode, logContext) {
super(message);
this.httpCode = httpCode;
this.logContext = logContext;
}
}
module.exports = {
HTTPError,
setReplyHeaders: (newMessage, prevMessage) => {
if (prevMessage.messageIdHeader) {
newMessage.inReplyTo = prevMessage.headerMessageId;
if (prevMessage.references) {
newMessage.references = prevMessage.references.concat(prevMessage.headerMessageId);
} else {
newMessage.references = [prevMessage.messageIdHeader];
}
}
},
findOrCreateMessageFromJSON: async (data, db) => {
const {Message} = db;
@ -34,10 +16,10 @@ module.exports = {
findMultiSendDraft: async (draftId, db) => {
const draft = await db.Message.findById(draftId)
if (!draft) {
throw new HTTPError(`Couldn't find multi-send draft ${draftId}`, 400);
throw new Errors.HTTPError(`Couldn't find multi-send draft ${draftId}`, 400);
}
if (draft.isSent || !draft.isSending) {
throw new HTTPError(`Message ${draftId} is not a multi-send draft`, 400);
throw new Errors.HTTPError(`Message ${draftId} is not a multi-send draft`, 400);
}
return draft;
},
@ -45,13 +27,13 @@ module.exports = {
const {to, cc, bcc} = draft;
const recipients = [].concat(to, cc, bcc);
if (recipients.length === 0) {
throw new HTTPError("No recipients specified", 400);
throw new Errors.HTTPError("No recipients specified", 400);
}
},
validateBase36: (value, name) => {
if (value == null) { return; }
if (isNaN(parseInt(value, 36))) {
throw new HTTPError(`${name} is not a base-36 integer`, 400)
throw new Errors.HTTPError(`${name} is not a base-36 integer`, 400)
}
},
}

View file

@ -0,0 +1,11 @@
class HTTPError extends Error {
constructor(message, httpCode, logContext) {
super(message);
this.httpCode = httpCode;
this.logContext = logContext;
}
}
module.exports = {
HTTPError,
}

View file

@ -5,8 +5,7 @@ const mimelib = require('mimelib');
const QuotedPrintable = require('quoted-printable');
const striptags = require('striptags');
const {Imap} = require('isomorphic-core');
const SendingUtils = require('../local-api/sending-utils');
const Errors = require('./errors');
const SNIPPET_SIZE = 100
@ -30,6 +29,17 @@ function hashForHeaders(headers) {
return cryptography.createHash('sha256').update(headers, 'utf8').digest('hex');
}
function setReplyHeaders(newMessage, prevMessage) {
if (prevMessage.messageIdHeader) {
newMessage.inReplyTo = prevMessage.headerMessageId;
if (prevMessage.references) {
newMessage.references = prevMessage.references.concat(prevMessage.headerMessageId);
} else {
newMessage.references = [prevMessage.messageIdHeader];
}
}
}
async function parseFromImap(imapMessage, desiredParts, {db, accountId, folder}) {
const {Label} = db
const body = {}
@ -151,7 +161,7 @@ async function associateFromJSON(data, db) {
if (replyToThread && replyToMessage) {
if (!replyToThread.messages.find((msg) => msg.id === replyToMessage.id)) {
throw new SendingUtils.HTTPError(
throw new Errors.HTTPError(
`Message ${replyToMessage.id} is not in thread ${replyToThread.id}`,
400
)
@ -160,14 +170,14 @@ async function associateFromJSON(data, db) {
let thread;
if (replyToMessage) {
SendingUtils.setReplyHeaders(message, replyToMessage);
setReplyHeaders(message, replyToMessage);
thread = await message.getThread();
} else if (replyToThread) {
thread = replyToThread;
const previousMessages = thread.messages.filter(msg => !msg.isDraft);
if (previousMessages.length > 0) {
const lastMessage = previousMessages[previousMessages.length - 1]
SendingUtils.setReplyHeaders(message, lastMessage);
setReplyHeaders(message, lastMessage);
}
} else {
thread = Thread.build({