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.
pull/16/head
ClaraCrazy 2024-10-04 23:32:21 +02:00
parent d992ec2470
commit 0770ebdfc2
No known key found for this signature in database
GPG Key ID: EBBC896ACB497011
6 changed files with 59 additions and 26 deletions

View File

@ -1,13 +1,13 @@
const config = { const config = {
email: { email: { // Email configuration
domains: process.env.EMAIL_DOMAINS, // List object of domains domains: process.env.EMAIL_DOMAINS, // List object of domains
purgeTime: process.env.EMAIL_PURGE_TIME || { 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 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 user: process.env.IMAP_USER, // imap user
password: process.env.IMAP_PASSWORD, // imap password password: process.env.IMAP_PASSWORD, // imap password
host: process.env.IMAP_SERVER, // imap server host: process.env.IMAP_SERVER, // imap server
@ -16,12 +16,12 @@ const config = {
authTimeout: process.env.IMAP_AUTHTIMEOUT || 3000, // timeout for auth authTimeout: process.env.IMAP_AUTHTIMEOUT || 3000, // timeout for auth
refreshIntervalSeconds: process.env.IMAP_REFRESH_INTERVAL_SECONDS || 60 // refresh interval refreshIntervalSeconds: process.env.IMAP_REFRESH_INTERVAL_SECONDS || 60 // refresh interval
}, },
http: { http: { // HTTP configuration
port: normalizePort(process.env.HTTP_PORT || 3000), // http port port: normalizePort(process.env.HTTP_PORT || 3000), // http port to listen on
branding: process.env.HTTP_BRANDING || ["48hr.email", "CrazyCo", "https://crazyco.xyz"], // branding branding: process.env.HTTP_BRANDING || ["48hr.email", "CrazyCo", "https://crazyco.xyz"], // branding
examples: process.env.HTTP_EXAMPLES || { examples: process.env.HTTP_EXAMPLES || { // Examples to use to demonstrate the service
email: "example@48hr.email", // example email email: "example@48hr.email", // example email to keep clean, besides the IDs specified below
ids: [1, 2, 3] // example ids ids: [1, 2, 3] // example ids to keep
} }
}, },
} }

View File

@ -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 {Number} time
* @param {String} unit * @param {String} unit
* @returns {String} * @returns {String}
*/ */
convertUp(time, unit) { convertAndRound(time, unit) {
let convertedTime = time; let convertedTime = time;
let convertedUnit = unit; let convertedUnit = unit;
let rounded = false;
if (convertedUnit === 'minutes') { if (convertedUnit === 'minutes') {
if (convertedTime > 120 && Number.isSafeInteger(convertedTime / 60)) { if (convertedTime > 60) {
convertedTime = convertedTime / 60; convertedTime = convertedTime / 60
convertedUnit = 'hours'; convertedUnit = 'hours';
} }}
}
if (convertedUnit === 'hours') { if (convertedUnit === 'hours') {
if (convertedTime > 48 && Number.isSafeInteger(convertedTime / 24)) { if (convertedTime > 24) {
convertedTime = convertedTime / 24; convertedTime = convertedTime / 24;
convertedUnit = 'days'; convertedUnit = 'days';
} }
} }
if (!convertedTime == Number.isSafeInteger(convertedTime)) {
convertedTime = Math.round(convertedTime);
rounded = true;
}
if (rounded) {
convertedTime = `~${convertedTime}`;
}
return `${convertedTime} ${convertedUnit}`; 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 = `<label title="${Tooltip}">
<h4 style="display: inline;"><u><i>${time}</i></u></h4>
</Label>`
return footer
}
} }
module.exports = Helper module.exports = Helper

View File

@ -140,3 +140,7 @@ input, textarea, select, select:active, select:focus, select:hover {
width: 80%; width: 80%;
padding-left:10% padding-left:10%
} }
label {
display: inline;
}

View File

@ -1,13 +1,12 @@
const express = require('express')
const router = new express.Router() const router = new express.Router()
const express = require('express')
const {param} = require('express-validator') const {param} = require('express-validator')
const config = require('../../../application/config') const config = require('../../../application/config')
const Helper = require('../../../application/helper') const Helper = require('../../../application/helper')
const helper = new(Helper) const helper = new(Helper)
const purgeTime = config.email.purgeTime.convert ? helper.convertUp(config.email.purgeTime.time, config.email.purgeTime.unit) const purgeTime = helper.purgeTimeElemetBuilder()
: config.email.purgeTime.time +` ${config.email.purgeTime.unit}`;
const sanitizeAddress = param('address').customSanitizer( const sanitizeAddress = param('address').customSanitizer(
(value, {req}) => { (value, {req}) => {

View File

@ -1,14 +1,13 @@
const express = require('express')
const router = new express.Router() const router = new express.Router()
const randomWord = require('random-word') const express = require('express')
const {check, validationResult} = require('express-validator') const {check, validationResult} = require('express-validator')
const randomWord = require('random-word')
const config = require('../../../application/config') const config = require('../../../application/config')
const Helper = require('../../../application/helper') const Helper = require('../../../application/helper')
const helper = new(Helper) const helper = new(Helper)
const purgeTime = config.email.purgeTime.convert ? helper.convertUp(config.email.purgeTime.time, config.email.purgeTime.unit) const purgeTime = helper.purgeTimeElemetBuilder()
: config.email.purgeTime.time +` ${config.email.purgeTime.unit}`;
router.get('/', (req, res, _next) => { router.get('/', (req, res, _next) => {
res.render('login', { res.render('login', {

View File

@ -28,7 +28,7 @@
{% block footer %} {% block footer %}
<section class="container footer"> <section class="container footer">
<hr> <hr>
<h4>{{ branding[0] }} offered by <a href="{{ branding[2] }}" style="text-decoration:underline" target="_blank">{{ branding[1] }}</a> | All Emails will be deleted after {{ purgeTime }} | This project is <a href="https://github.com/crazyco-xyz/48hr.email" style="text-decoration:underline" target="_blank">open-source ♥</a></h4> <h4>{{ branding[0] }} offered by <a href="{{ branding[2] }}" style="text-decoration:underline" target="_blank">{{ branding[1] }}</a> | All Emails will be deleted after {{ purgeTime | raw }} | This project is <a href="https://github.com/crazyco-xyz/48hr.email" style="text-decoration:underline" target="_blank">open-source ♥</a></h4>
</section> </section>
{% endblock %} {% endblock %}