diff --git a/app.json b/app.json index a121acf..660134a 100644 --- a/app.json +++ b/app.json @@ -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" } } } diff --git a/application/config.sample.js b/application/config.sample.js index c525ed0..1813e0e 100644 --- a/application/config.sample.js +++ b/application/config.sample.js @@ -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) }, } diff --git a/application/helper.js b/application/helper.js index 2b5557f..dea2761 100644 --- a/application/helper.js +++ b/application/helper.js @@ -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 diff --git a/application/imap-service.js b/application/imap-service.js index 52819e9..e6f5324 100644 --- a/application/imap-service.js +++ b/application/imap-service.js @@ -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) } }) diff --git a/domain/mail-repository.js b/domain/mail-repository.js index 9beed61..d414ba5 100644 --- a/domain/mail-repository.js +++ b/domain/mail-repository.js @@ -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) => { diff --git a/infrastructure/web/routes/login.js b/infrastructure/web/routes/login.js index f80527d..604bf96 100644 --- a/infrastructure/web/routes/login.js +++ b/infrastructure/web/routes/login.js @@ -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, }) } diff --git a/package.json b/package.json index 2b4f9e0..ebb48f5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "48hr.email", - "version": "1.5.4", + "version": "1.6.0", "private": false, "scripts": { "start": "node --trace-warnings ./app.js",