From 0770ebdfc21ece92ff55a8dce7b90765620d6e79 Mon Sep 17 00:00:00 2001 From: ClaraCrazy Date: Fri, 4 Oct 2024 23:32:21 +0200 Subject: [PATCH] Update conversion function and footer - Conversion function updates: - Enabling conversion now rounds to the nearest max. value where `i > 1`, and if rounding was needed it will create a Tooltip displaying the config value on hover. - If rounding wasnt needed, or conversion is disabled in the config then no tooltip will be created, to avoid confusion. - Updated footer: - Created new function called footerbuilder() - The config value is now always underlined and italic to make it more obvious you can (if rounded) "interact" with it. --- application/config.sample.js | 18 ++++---- application/helper.js | 45 ++++++++++++++++--- .../web/public/stylesheets/custom.css | 4 ++ infrastructure/web/routes/inbox.js | 7 ++- infrastructure/web/routes/login.js | 9 ++-- infrastructure/web/views/layout.twig | 2 +- 6 files changed, 59 insertions(+), 26 deletions(-) diff --git a/application/config.sample.js b/application/config.sample.js index 4618778..97d30fd 100644 --- a/application/config.sample.js +++ b/application/config.sample.js @@ -1,13 +1,13 @@ const config = { - email: { + email: { // Email configuration domains: process.env.EMAIL_DOMAINS, // List object of domains purgeTime: process.env.EMAIL_PURGE_TIME || { - time: 48, // Time value to purge + time: 48, // Time value for when to purge unit: 'hours', // minutes, hours, days - convert: true, // Convert to highest sensible unit + convert: true, // Convert to highest sensible unit (and round) } }, - imap: { + imap: { // IMAP configuration user: process.env.IMAP_USER, // imap user password: process.env.IMAP_PASSWORD, // imap password host: process.env.IMAP_SERVER, // imap server @@ -16,12 +16,12 @@ const config = { authTimeout: process.env.IMAP_AUTHTIMEOUT || 3000, // timeout for auth refreshIntervalSeconds: process.env.IMAP_REFRESH_INTERVAL_SECONDS || 60 // refresh interval }, - http: { - port: normalizePort(process.env.HTTP_PORT || 3000), // http port + http: { // HTTP configuration + port: normalizePort(process.env.HTTP_PORT || 3000), // http port to listen on branding: process.env.HTTP_BRANDING || ["48hr.email", "CrazyCo", "https://crazyco.xyz"], // branding - examples: process.env.HTTP_EXAMPLES || { - email: "example@48hr.email", // example email - ids: [1, 2, 3] // example ids + examples: process.env.HTTP_EXAMPLES || { // Examples to use to demonstrate the service + email: "example@48hr.email", // example email to keep clean, besides the IDs specified below + ids: [1, 2, 3] // example ids to keep } }, } diff --git a/application/helper.js b/application/helper.js index 4bb2b80..c925a65 100644 --- a/application/helper.js +++ b/application/helper.js @@ -29,30 +29,61 @@ class Helper { } /** - * Convert time to highest possible unit (minutes, hours, days) where `time > 2` and `Number.isSafeInteger(time)` (whole number) + * Convert time to highest possible unit (minutes, hours, days) where `time > 1` and `Number.isSafeInteger(time)` (whole number) * @param {Number} time * @param {String} unit * @returns {String} */ - convertUp(time, unit) { + convertAndRound(time, unit) { let convertedTime = time; let convertedUnit = unit; + let rounded = false; if (convertedUnit === 'minutes') { - if (convertedTime > 120 && Number.isSafeInteger(convertedTime / 60)) { - convertedTime = convertedTime / 60; + if (convertedTime > 60) { + convertedTime = convertedTime / 60 convertedUnit = 'hours'; - } - } + }} if (convertedUnit === 'hours') { - if (convertedTime > 48 && Number.isSafeInteger(convertedTime / 24)) { + if (convertedTime > 24) { convertedTime = convertedTime / 24; convertedUnit = 'days'; } } + + if (!convertedTime == Number.isSafeInteger(convertedTime)) { + convertedTime = Math.round(convertedTime); + rounded = true; + } + + if (rounded) { + convertedTime = `~${convertedTime}`; + } + return `${convertedTime} ${convertedUnit}`; } + + /** + * Build a purgeTime html element for the page to keep the clutter outside of the twig template + * @returns {String} + */ + purgeTimeElemetBuilder() { + let time = `${config.email.purgeTime.time} ${config.email.purgeTime.unit}` + let Tooltip = '' + if (config.email.purgeTime.convert) { + time = this.convertAndRound(config.email.purgeTime.time, config.email.purgeTime.unit) + if (time !== `${config.email.purgeTime.time} ${config.email.purgeTime.unit}`) { + Tooltip = `Config: ${config.email.purgeTime.time} ${config.email.purgeTime.unit}` + } + } + + const footer = `` + + return footer + } } module.exports = Helper diff --git a/infrastructure/web/public/stylesheets/custom.css b/infrastructure/web/public/stylesheets/custom.css index 3314227..7483505 100644 --- a/infrastructure/web/public/stylesheets/custom.css +++ b/infrastructure/web/public/stylesheets/custom.css @@ -140,3 +140,7 @@ input, textarea, select, select:active, select:focus, select:hover { width: 80%; padding-left:10% } + +label { + display: inline; +} diff --git a/infrastructure/web/routes/inbox.js b/infrastructure/web/routes/inbox.js index ca6cf40..742460d 100644 --- a/infrastructure/web/routes/inbox.js +++ b/infrastructure/web/routes/inbox.js @@ -1,13 +1,12 @@ -const express = require('express') - const router = new express.Router() +const express = require('express') const {param} = require('express-validator') + const config = require('../../../application/config') const Helper = require('../../../application/helper') const helper = new(Helper) -const purgeTime = config.email.purgeTime.convert ? helper.convertUp(config.email.purgeTime.time, config.email.purgeTime.unit) - : config.email.purgeTime.time +` ${config.email.purgeTime.unit}`; +const purgeTime = helper.purgeTimeElemetBuilder() const sanitizeAddress = param('address').customSanitizer( (value, {req}) => { diff --git a/infrastructure/web/routes/login.js b/infrastructure/web/routes/login.js index 06db523..284955d 100644 --- a/infrastructure/web/routes/login.js +++ b/infrastructure/web/routes/login.js @@ -1,14 +1,13 @@ -const express = require('express') - const router = new express.Router() -const randomWord = require('random-word') +const express = require('express') const {check, validationResult} = require('express-validator') + +const randomWord = require('random-word') const config = require('../../../application/config') const Helper = require('../../../application/helper') const helper = new(Helper) -const purgeTime = config.email.purgeTime.convert ? helper.convertUp(config.email.purgeTime.time, config.email.purgeTime.unit) - : config.email.purgeTime.time +` ${config.email.purgeTime.unit}`; +const purgeTime = helper.purgeTimeElemetBuilder() router.get('/', (req, res, _next) => { res.render('login', { diff --git a/infrastructure/web/views/layout.twig b/infrastructure/web/views/layout.twig index 0210f95..79268d4 100644 --- a/infrastructure/web/views/layout.twig +++ b/infrastructure/web/views/layout.twig @@ -28,7 +28,7 @@ {% block footer %} {% endblock %}