More advanced domain sorting

main
ClaraCrazy 2024-10-21 01:52:33 +02:00
parent a11c41c954
commit 74c13678a8
No known key found for this signature in database
GPG Key ID: EBBC896ACB497011
7 changed files with 56 additions and 18 deletions

View File

@ -8,7 +8,7 @@
"disposable-mail"
],
"env": {
"DOMAINS": {
"EMAIL_DOMAINS": {
"description": "Email domains"
},
"EMAIL_PURGE_TIME": {
@ -19,6 +19,13 @@
"convert": true
}
},
"EMAIL_EXAMPLES": {
"description": "Examples of the domains",
"value": {
"account": "example@48hr.email",
"uids": [1, 2, 3]
}
},
"IMAP_USER": {
"description": "Username to login to the imap server"
},
@ -52,12 +59,8 @@
"description": "The branding of the site",
"value": ["48hr.email", "Crazyco", "https://crazyco.xyz"]
},
"HTTP_EXAMPLES": {
"description": "Examples of the domains",
"value": {
"email": "example@48hr.email",
"uids": [1, 2, 3]
}
"HTTP_DISPLAY_SORT": {
"description": "Sort the emails for use"
}
}
}

View File

@ -5,7 +5,11 @@ const config = {
time: 48, // Time value for when to purge
unit: 'hours', // minutes, hours, days
convert: true, // Convert to highest sensible unit (and round)
}
},
examples: process.env.EMAIL_EXAMPLES || { // Examples to use to demonstrate the service
account: "example@48hr.email", // example email to keep clean, besides the UIDs specified below
uids: [1, 2, 3] // example uids to keep
},
},
imap: { // IMAP configuration
user: process.env.IMAP_USER, // imap user
@ -19,10 +23,7 @@ const config = {
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 || { // Examples to use to demonstrate the service
email: "example@48hr.email", // example email to keep clean, besides the UIDs specified below
uids: [1, 2, 3] // example uids to keep
}
displaySort: process.env.HTTP_DISPLAY_SHUFFLE || 0, // Sorting logic used for display (0 does not modify, 1 sorts alphabetically, 2 sorts alphabetically and only shuffles the first item, 3 shuffles all)
},
}

View File

@ -98,6 +98,40 @@ class Helper {
}
return array
}
/**
* 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
}
/**
* Get a domain list from config for use
* @returns {Array}
*/
getDomains() {
switch (config.http.displaySort) {
case 0:
return config.email.domains // No modification
case 1:
return config.email.domains.sort() // Sort alphabetically
case 2:
return this.shuffleFirstItem(config.email.domains.sort()) // Sort alphabetically and shuffle first item
case 3:
return this.shuffleArray(config.email.domains) // Shuffle all
}
}
}
module.exports = Helper

View File

@ -222,7 +222,7 @@ class ImapService extends EventEmitter {
const uidsWithHeaders = await this._getMailHeaders(uids)
uidsWithHeaders.forEach(mail => {
if (mail['attributes'].date > DeleteOlderThan || this.config.http.examples.uids.includes(parseInt(mail['attributes'].uid))) {
if (mail['attributes'].date > DeleteOlderThan || this.config.email.examples.uids.includes(parseInt(mail['attributes'].uid))) {
uids = uids.filter(uid => uid !== mail['attributes'].uid)
}
})

View File

@ -13,7 +13,7 @@ class MailRepository {
getForRecipient(address) {
let mails = this.mailSummaries.get(address) || []
mails.forEach(mail => {
if (mail.to == this.config.http.examples.email && !this.config.http.examples.uids.includes(parseInt(mail.uid))) {
if (mail.to == this.config.email.examples.account && !this.config.email.examples.uids.includes(parseInt(mail.uid))) {
mails = mails.filter(m => m.uid != mail.uid)
debug('prevented non-example email from being shown in example inbox', mail.uid)
}
@ -31,7 +31,7 @@ class MailRepository {
}
removeUid(uid, address) {
if (!this.config.http.examples.uids.includes(parseInt(uid))) {
if (!this.config.email.examples.uids.includes(parseInt(uid))) {
var deleted = false
// TODO: make this more efficient, looping through each email is not cool.
this.mailSummaries.forEachAssociation((mails, to) => {

View File

@ -14,7 +14,7 @@ router.get('/', (req, res, _next) => {
title: `${config.http.branding[0]} | Your temporary Inbox`,
username: randomWord(),
purgeTime: purgeTime,
domains: helper.shuffleArray(config.email.domains),
domains: helper.getDomains(),
branding: config.http.branding,
})
})
@ -46,7 +46,7 @@ router.post(
title: `${config.http.branding[0]} | Your temporary Inbox`,
purgeTime: purgeTime,
username: randomWord(),
domains: helper.shuffleArray(config.email.domains),
domains: helper.getDomains(),
branding: config.http.branding,
})
}

View File

@ -1,6 +1,6 @@
{
"name": "48hr.email",
"version": "1.5.4",
"version": "1.6.0",
"private": false,
"scripts": {
"start": "node --trace-warnings ./app.js",