Skip to content

Commit

Permalink
fix: fixed tz issue, inherit server logger for IMAP/SMTP
Browse files Browse the repository at this point in the history
  • Loading branch information
titanism committed Oct 16, 2023
1 parent d549c3d commit 3c952d9
Show file tree
Hide file tree
Showing 25 changed files with 129 additions and 122 deletions.
2 changes: 1 addition & 1 deletion app/views/layout.pug
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ html.h-100.no-js(

if ['/', '/resources', '/about', '/terms', '/about', '/faq', '/private-business-email'].includes(ctx.pathWithoutLocale) || ctx.pathWithoutLocale.startsWith('/docs') || ctx.pathWithoutLocale.startsWith('/guides')
//- in order for snapshots to be consistent we need the same date to be used
- const now = config.env === "test" ? dayjs("1/1/23", "M/D/YY").toDate() : new Date();
- const now = config.env === "test" ? dayjs.tz("2023-01-01", "America/Los_Angeles").toDate() : new Date();
meta(
property="og:updated_time",
content=dayjs(now).startOf("day").toDate().toISOString()
Expand Down
4 changes: 3 additions & 1 deletion config/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ const { developerDocs, nsProviders, platforms } = require('#config/utilities');
module.exports = function (config) {
// in order for snapshots to be consistent we need the same date to be used
const now =
config.env === 'test' ? dayjs('1/1/23', 'M/D/YY').toDate() : new Date();
config.env === 'test'
? dayjs.tz('2023-01-01', 'America/Los_Angeles').toDate()
: new Date();

// currently we cannot use the `|` pipe character due to this issue
// <https://github.com/mashpie/i18n-node/issues/274>
Expand Down
7 changes: 6 additions & 1 deletion helpers/imap-notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@

const { EventEmitter } = require('node:events');

const Axe = require('axe');
const _ = require('lodash');
const safeStringify = require('fast-safe-stringify');

const IMAPError = require('#helpers/imap-error');
const Journals = require('#models/journals');
const Mailboxes = require('#models/mailboxes');
const Messages = require('#models/messages');
const config = require('#config');
const helperLogger = require('#helpers/logger');
const i18n = require('#helpers/i18n');
const logger = require('#helpers/logger');

const logger =
config.env === 'development' ? helperLogger : new Axe({ silent: true });

const IMAP_REDIS_CHANNEL_NAME = 'imap_events';

Expand Down
15 changes: 7 additions & 8 deletions helpers/imap/on-append.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ const Messages = require('#models/messages');
const Threads = require('#models/threads');
const Mailboxes = require('#models/mailboxes');
const i18n = require('#helpers/i18n');
const logger = require('#helpers/logger');
const refineAndLogError = require('#helpers/refine-and-log-error');

// eslint-disable-next-line max-params
async function onAppend(path, flags, date, raw, session, fn) {
logger.debug('APPEND', { path, flags, date, raw, session });
this.logger.debug('APPEND', { path, flags, date, raw, session });

let thread;
let storageUsed = 0;
Expand Down Expand Up @@ -232,7 +231,7 @@ async function onAppend(path, flags, date, raw, session, fn) {

// store the message
const message = await Messages.create(data);
logger.debug('message created', {
this.logger.debug('message created', {
message,
path,
flags,
Expand All @@ -257,7 +256,7 @@ async function onAppend(path, flags, date, raw, session, fn) {
});
this.server.notifier.fire(alias.id);
} catch (err) {
logger.fatal(err, { path, flags, date, raw, session });
this.logger.fatal(err, { path, flags, date, raw, session });
}

const response = {
Expand All @@ -270,7 +269,7 @@ async function onAppend(path, flags, date, raw, session, fn) {
status: 'new'
};

logger.debug('command response', { response });
this.logger.debug('command response', { response });

fn(null, true, response);
} catch (err) {
Expand All @@ -285,7 +284,7 @@ async function onAppend(path, flags, date, raw, session, fn) {
this.attachmentStorage
.deleteManyPromise(attachmentIds, maildata.magic)
.then()
.catch((err) => logger.fatal(err, { storageUsed, session }));
.catch((err) => this.logger.fatal(err, { storageUsed, session }));

// rollback storage if there was an error and storage was consumed
if (storageUsed > 0) {
Expand All @@ -301,15 +300,15 @@ async function onAppend(path, flags, date, raw, session, fn) {
}
)
.then()
.catch((err) => logger.fatal(err, { storageUsed, session }));
.catch((err) => this.logger.fatal(err, { storageUsed, session }));
}

// handle mongodb error
if (err.code === 1100) err.imapResponse = 'ALREADYEXISTS';

// NOTE: wildduck uses `imapResponse` so we are keeping it consistent
if (err.imapResponse) {
logger.error(err, { path, flags, date, raw, session });
this.logger.error(err, { path, flags, date, raw, session });
return fn(null, err.imapResponse);
}

Expand Down
17 changes: 8 additions & 9 deletions helpers/imap/on-copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ const Messages = require('#models/messages');
const ServerShutdownError = require('#helpers/server-shutdown-error');
const SocketError = require('#helpers/socket-error');
const i18n = require('#helpers/i18n');
const logger = require('#helpers/logger');
const refineAndLogError = require('#helpers/refine-and-log-error');

// eslint-disable-next-line max-params
async function onCopy(connection, mailboxId, update, session, fn) {
logger.debug('COPY', { connection, mailboxId, update, session });
this.logger.debug('COPY', { connection, mailboxId, update, session });

try {
const { alias } = await this.refreshSession(session, 'COPY');
Expand Down Expand Up @@ -90,7 +89,7 @@ async function onCopy(connection, mailboxId, update, session, fn) {
.maxTimeMS(ms('2m'))
.lean()
.cursor()) {
logger.debug('copying message', { message });
this.logger.debug('copying message', { message });

// check if server is in the process of shutting down
if (this.server._closeTimeout) throw new ServerShutdownError();
Expand Down Expand Up @@ -172,7 +171,7 @@ async function onCopy(connection, mailboxId, update, session, fn) {
newMessage.magic
);
} catch (err) {
logger.fatal(err, { mailboxId, update, session });
this.logger.fatal(err, { mailboxId, update, session });
}
}

Expand All @@ -193,7 +192,7 @@ async function onCopy(connection, mailboxId, update, session, fn) {
junk: newMessage.junk
});
} catch (err) {
logger.fatal(err, { mailboxId, update, session });
this.logger.fatal(err, { mailboxId, update, session });
}
}

Expand All @@ -216,11 +215,11 @@ async function onCopy(connection, mailboxId, update, session, fn) {
isCodeBug: true // admins will get an email/sms alert
}
);
logger.fatal(err, { mailboxId, update, session });
this.logger.fatal(err, { mailboxId, update, session });
}
})
.catch((err) =>
logger.fatal(err, {
this.logger.fatal(err, {
copiedStorage,
connection,
mailboxId,
Expand All @@ -237,7 +236,7 @@ async function onCopy(connection, mailboxId, update, session, fn) {
})
.then()
.catch((err) =>
logger.fatal(err, {
this.logger.fatal(err, {
copiedStorage,
connection,
mailboxId,
Expand All @@ -255,7 +254,7 @@ async function onCopy(connection, mailboxId, update, session, fn) {
} catch (err) {
// NOTE: wildduck uses `imapResponse` so we are keeping it consistent
if (err.imapResponse) {
logger.error(err, { connection, mailboxId, update, session });
this.logger.error(err, { connection, mailboxId, update, session });
return fn(null, err.imapResponse);
}

Expand Down
7 changes: 3 additions & 4 deletions helpers/imap/on-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ const IMAPError = require('#helpers/imap-error');
const Mailboxes = require('#models/mailboxes');
const config = require('#config');
const i18n = require('#helpers/i18n');
const logger = require('#helpers/logger');
const refineAndLogError = require('#helpers/refine-and-log-error');

async function onCreate(path, session, fn) {
logger.debug('CREATE', { path, session });
this.logger.debug('CREATE', { path, session });

try {
const { alias } = await this.refreshSession(session, 'CREATE');
Expand Down Expand Up @@ -73,15 +72,15 @@ async function onCreate(path, session, fn) {
});
this.server.notifier.fire(alias.id);
} catch (err) {
logger.fatal(err, { path, session });
this.logger.fatal(err, { path, session });
}

fn(null, true, mailbox._id);
} catch (err) {
if (err.code === 11000) err.imapResponse = 'ALREADYEXISTS';
// NOTE: wildduck uses `imapResponse` so we are keeping it consistent
if (err.imapResponse) {
logger.error(err, { path, session });
this.logger.error(err, { path, session });
return fn(null, err.imapResponse);
}

Expand Down
9 changes: 4 additions & 5 deletions helpers/imap/on-delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ const IMAPError = require('#helpers/imap-error');
const Mailboxes = require('#models/mailboxes');
const Messages = require('#models/messages');
const i18n = require('#helpers/i18n');
const logger = require('#helpers/logger');
const refineAndLogError = require('#helpers/refine-and-log-error');

async function onDelete(path, session, fn) {
logger.debug('DELETE', { path, session });
this.logger.debug('DELETE', { path, session });

try {
const { alias } = await this.refreshSession(session, 'DELETE');
Expand Down Expand Up @@ -52,7 +51,7 @@ async function onDelete(path, session, fn) {
_id: mailbox._id
});

logger.debug('deleted', { results, path, session });
this.logger.debug('deleted', { results, path, session });

// results.deletedCount is mainly for publish/notifier
if (results.deletedCount > 0) {
Expand All @@ -63,7 +62,7 @@ async function onDelete(path, session, fn) {
});
this.server.notifier.fire(alias.id);
} catch (err) {
logger.fatal(err, { path, session });
this.logger.fatal(err, { path, session });
}
}

Expand All @@ -89,7 +88,7 @@ async function onDelete(path, session, fn) {
} catch (err) {
// NOTE: wildduck uses `imapResponse` so we are keeping it consistent
if (err.imapResponse) {
logger.error(err, { path, session });
this.logger.error(err, { path, session });
return fn(null, err.imapResponse);
}

Expand Down
27 changes: 15 additions & 12 deletions helpers/imap/on-expunge.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ const IMAPError = require('#helpers/imap-error');
const Mailboxes = require('#models/mailboxes');
const Messages = require('#models/messages');
const i18n = require('#helpers/i18n');
const logger = require('#helpers/logger');
const refineAndLogError = require('#helpers/refine-and-log-error');

// eslint-disable-next-line complexity
async function onExpunge(mailboxId, update, session, fn) {
logger.debug('EXPUNGE', { mailboxId, update, session });
this.logger.debug('EXPUNGE', { mailboxId, update, session });

try {
const { alias } = await this.refreshSession(session, 'EXPUNGE');
Expand Down Expand Up @@ -72,13 +71,13 @@ async function onExpunge(mailboxId, update, session, fn) {
.lean()
.cursor();

logger.debug('expunge query', { query });
this.logger.debug('expunge query', { query });

let err;

try {
for await (const message of cursor) {
logger.debug('expunge message', {
this.logger.debug('expunge message', {
message,
mailboxId,
update,
Expand All @@ -89,7 +88,11 @@ async function onExpunge(mailboxId, update, session, fn) {
// TODO: will edge cases like this in cursor() usage ever occur (?)
//
if (!message) {
logger.fatal('message not expunged', { mailboxId, update, session });
this.logger.fatal('message not expunged', {
mailboxId,
update,
session
});
// write to stream
if (
!update.silent &&
Expand Down Expand Up @@ -122,7 +125,7 @@ async function onExpunge(mailboxId, update, session, fn) {
} catch (err) {
// duplicate error (already archived)
if (err.code === 11000) {
logger.fatal(err, { message. mailboxId, update, session });
this.logger.fatal(err, { message. mailboxId, update, session });
} else {
throw err;
}
Expand Down Expand Up @@ -153,7 +156,7 @@ async function onExpunge(mailboxId, update, session, fn) {
.deleteManyPromise(attachmentIds, message.magic)
.then()
.catch((err) =>
logger.fatal(err, { mailboxId, update, session })
this.logger.fatal(err, { mailboxId, update, session })
);

// write to socket we've expunged message
Expand Down Expand Up @@ -182,7 +185,7 @@ async function onExpunge(mailboxId, update, session, fn) {
});
this.server.notifier.fire(alias.id);
} catch (err) {
logger.fatal(err, { mailboxId, update, session });
this.logger.fatal(err, { mailboxId, update, session });
}
}
}
Expand All @@ -194,14 +197,14 @@ async function onExpunge(mailboxId, update, session, fn) {
try {
await cursor.close();
} catch (err) {
logger.fatal(err, { mailboxId, update, session });
this.logger.fatal(err, { mailboxId, update, session });
}

// release lock
try {
await this.server.lock.releaseLock(lock);
} catch (err) {
logger.fatal(err, { mailboxId, update, session });
this.logger.fatal(err, { mailboxId, update, session });
}

// update storage quota
Expand All @@ -218,7 +221,7 @@ async function onExpunge(mailboxId, update, session, fn) {
)
.then()
.catch((err) =>
logger.fatal(err, { storageUsed, mailboxId, update, session })
this.logger.fatal(err, { storageUsed, mailboxId, update, session })
);

// throw error
Expand All @@ -228,7 +231,7 @@ async function onExpunge(mailboxId, update, session, fn) {
} catch (err) {
// NOTE: wildduck uses `imapResponse` so we are keeping it consistent
if (err.imapResponse) {
logger.error(err, { mailboxId, update, session });
this.logger.error(err, { mailboxId, update, session });
return fn(null, err.imapResponse);
}

Expand Down
Loading

0 comments on commit 3c952d9

Please sign in to comment.