2024-10-03 01:22:10 +02:00
|
|
|
const config = require('./config')
|
|
|
|
const moment = require('moment')
|
|
|
|
|
|
|
|
class Helper {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Normalize our config into a proper timestamp, so we know what emails to purge
|
|
|
|
* @returns {Date}
|
|
|
|
*/
|
|
|
|
purgeTimeStamp() {
|
|
|
|
return moment()
|
|
|
|
.subtract(config.email.purgeTime.time, config.email.purgeTime.unit)
|
|
|
|
.toDate()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if time difference between now and purgeTimeStamp is more than one day
|
|
|
|
* @param {Date} now
|
|
|
|
* @param {Date} past
|
|
|
|
* @returns {Boolean}
|
|
|
|
*/
|
|
|
|
moreThanOneDay(now, past) {
|
|
|
|
const DAY_IN_MS = 24 * 60 * 60 * 1000;
|
|
|
|
if((now - past) / DAY_IN_MS >= 1){
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-10-04 23:32:21 +02:00
|
|
|
* Convert time to highest possible unit (minutes, hours, days) where `time > 1` and `Number.isSafeInteger(time)` (whole number)
|
2024-10-03 07:25:20 +02:00
|
|
|
* @param {Number} time
|
|
|
|
* @param {String} unit
|
|
|
|
* @returns {String}
|
2024-10-03 01:22:10 +02:00
|
|
|
*/
|
2024-10-04 23:32:21 +02:00
|
|
|
convertAndRound(time, unit) {
|
2024-10-03 07:25:20 +02:00
|
|
|
let convertedTime = time;
|
|
|
|
let convertedUnit = unit;
|
2024-10-04 23:32:21 +02:00
|
|
|
let rounded = false;
|
2024-10-03 07:25:20 +02:00
|
|
|
|
|
|
|
if (convertedUnit === 'minutes') {
|
2024-10-04 23:32:21 +02:00
|
|
|
if (convertedTime > 60) {
|
|
|
|
convertedTime = convertedTime / 60
|
2024-10-03 07:25:20 +02:00
|
|
|
convertedUnit = 'hours';
|
2024-10-04 23:32:21 +02:00
|
|
|
}}
|
2024-10-03 07:25:20 +02:00
|
|
|
|
|
|
|
if (convertedUnit === 'hours') {
|
2024-10-04 23:32:21 +02:00
|
|
|
if (convertedTime > 24) {
|
2024-10-03 07:25:20 +02:00
|
|
|
convertedTime = convertedTime / 24;
|
|
|
|
convertedUnit = 'days';
|
|
|
|
}
|
|
|
|
}
|
2024-10-04 23:32:21 +02:00
|
|
|
|
|
|
|
if (!convertedTime == Number.isSafeInteger(convertedTime)) {
|
|
|
|
convertedTime = Math.round(convertedTime);
|
|
|
|
rounded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rounded) {
|
|
|
|
convertedTime = `~${convertedTime}`;
|
|
|
|
}
|
|
|
|
|
2024-10-03 07:25:20 +02:00
|
|
|
return `${convertedTime} ${convertedUnit}`;
|
2024-10-03 01:22:10 +02:00
|
|
|
}
|
2024-10-04 23:32:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
}
|
2024-10-21 00:46:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shuffle an array using the Durstenfeld shuffle algorithm
|
|
|
|
* @param {Array} array
|
|
|
|
* @returns {Array}
|
|
|
|
*/
|
|
|
|
|
|
|
|
shuffleArray(array) {
|
|
|
|
for (let i = array.length - 1; i >= 0; i--) {
|
|
|
|
const j = Math.floor(Math.random() * (i + 1));
|
|
|
|
[array[i], array[j]] = [array[j], array[i]];
|
|
|
|
}
|
|
|
|
return array
|
|
|
|
}
|
2024-10-21 01:52:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shuffle first item of array, keeping original order afterwards
|
|
|
|
* @param {Array} array
|
|
|
|
* @returns {Array}
|
|
|
|
*/
|
|
|
|
|
|
|
|
shuffleFirstItem(array) {
|
|
|
|
let first = array[Math.floor(Math.random()*array.length)]
|
|
|
|
console.log(first)
|
|
|
|
array = array.filter((value)=>value!=first);
|
|
|
|
console.log(array)
|
|
|
|
array = [first].concat(array)
|
|
|
|
console.log(array)
|
|
|
|
return array
|
|
|
|
}
|
|
|
|
|
2024-10-21 02:21:03 +02:00
|
|
|
/**
|
|
|
|
* Hide other emails in the list and only show first (true) or show all (false)
|
|
|
|
* @param {Array} array
|
|
|
|
* @returns {Array}
|
|
|
|
*/
|
|
|
|
hideOther(array) {
|
|
|
|
if (config.http.hideOther) {
|
|
|
|
return array[0]
|
|
|
|
} else {
|
|
|
|
return array
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-21 01:52:33 +02:00
|
|
|
/**
|
|
|
|
* Get a domain list from config for use
|
|
|
|
* @returns {Array}
|
|
|
|
*/
|
|
|
|
|
|
|
|
getDomains() {
|
|
|
|
switch (config.http.displaySort) {
|
|
|
|
case 0:
|
2024-10-21 02:21:03 +02:00
|
|
|
return this.hideOther(config.email.domains) // No modification
|
2024-10-21 01:52:33 +02:00
|
|
|
case 1:
|
2024-10-21 02:21:03 +02:00
|
|
|
return this.hideOther(config.email.domains.sort()) // Sort alphabetically
|
2024-10-21 01:52:33 +02:00
|
|
|
case 2:
|
2024-10-21 02:21:03 +02:00
|
|
|
return this.hideOther(this.shuffleFirstItem(config.email.domains.sort())) // Sort alphabetically and shuffle first item
|
2024-10-21 01:52:33 +02:00
|
|
|
case 3:
|
2024-10-21 02:21:03 +02:00
|
|
|
return this.hideOther(this.shuffleArray(config.email.domains)) // Shuffle all
|
2024-10-21 01:52:33 +02:00
|
|
|
}
|
|
|
|
}
|
2024-10-03 07:25:20 +02:00
|
|
|
}
|
2024-10-03 01:22:10 +02:00
|
|
|
|
|
|
|
module.exports = Helper
|