48hr.email/infrastructure/web/routes/inbox.js

116 lines
3.1 KiB
JavaScript
Raw Normal View History

2023-11-01 11:48:19 +01:00
const express = require('express')
const router = new express.Router()
2024-09-09 07:10:30 +02:00
const {param} = require('express-validator')
2023-11-01 13:04:44 +01:00
const config = require('../../../application/config')
2024-09-09 07:10:30 +02:00
const sanitizeAddress = param('address').customSanitizer(
2023-11-01 11:48:19 +01:00
(value, {req}) => {
return req.params.address
.replace(/[^A-Za-z0-9_.+@-]/g, '') // Remove special characters
.toLowerCase()
}
)
router.get('^/:address([^@/]+@[^@/]+)', sanitizeAddress, (req, res, _next) => {
const mailProcessingService = req.app.get('mailProcessingService')
res.render('inbox', {
2024-09-17 20:44:42 +02:00
title: `${config.http.branding[0]} | ` + req.params.address,
2023-11-01 11:48:19 +01:00
address: req.params.address,
2023-11-01 13:21:01 +01:00
mailSummaries: mailProcessingService.getMailSummaries(req.params.address),
2024-09-17 20:44:42 +02:00
madeby: config.http.branding[1],
madebysite: config.http.branding[2]
2023-11-01 11:48:19 +01:00
})
})
router.get(
'^/:address/:uid([0-9]+$)',
sanitizeAddress,
async (req, res, next) => {
try {
const mailProcessingService = req.app.get('mailProcessingService')
const mail = await mailProcessingService.getOneFullMail(
req.params.address,
req.params.uid
)
if (mail && mail != "womp womp") {
2023-11-01 11:48:19 +01:00
// Emails are immutable, cache if found
res.set('Cache-Control', 'private, max-age=600')
res.render('mail', {
2024-09-28 01:49:06 +02:00
title: mail.subject + " | " + req.params.address,
2023-11-01 11:48:19 +01:00
address: req.params.address,
2023-11-01 13:21:37 +01:00
mail,
2023-11-02 06:25:22 +01:00
uid: req.params.uid,
2024-09-17 20:44:42 +02:00
madeby: config.http.branding[1],
madebysite: config.http.branding[2]
2023-11-01 11:48:19 +01:00
})
} else {
2024-01-26 02:41:50 +01:00
res.render(
'error',
{
address: req.params.address,
message: 'This mail could not be found. It either does not exist or has been deleted from our servers!',
2024-09-17 20:44:42 +02:00
madeby: config.http.branding[1],
madebysite: config.http.branding[2],
2024-01-26 02:41:50 +01:00
}
)
2023-11-01 11:48:19 +01:00
}
} catch (error) {
console.error('error while fetching one email', error)
next(error)
}
}
)
2023-11-02 06:25:22 +01:00
router.get(
2024-09-28 01:49:06 +02:00
'^/:address/:uid/delete',
2023-11-02 06:25:22 +01:00
sanitizeAddress,
async (req, res, next) => {
try {
const mailProcessingService = req.app.get('mailProcessingService')
2023-11-02 06:27:28 +01:00
await mailProcessingService.deleteSpecificEmail(req.params.address, req.params.uid)
2023-11-03 06:11:17 +01:00
res.redirect(`/inbox/${req.params.address}`)
2023-11-02 06:25:22 +01:00
} catch (error) {
console.error('error while deleting email', error)
next(error)
}
}
)
2024-09-28 01:49:06 +02:00
router.get(
'^/:address/:uid/raw',
sanitizeAddress,
async (req, res, next) => {
try {
const mailProcessingService = req.app.get('mailProcessingService')
const mail = await mailProcessingService.getOneFullMail(
req.params.address,
req.params.uid
)
if (mail && mail != "womp womp") {
// Emails are immutable, cache if found
res.set('Cache-Control', 'private, max-age=600')
res.render('raw', {
title: mail.subject + " | raw | " + req.params.address,
mail
})
} else {
res.render(
'error',
{
address: req.params.address,
message: 'This mail could not be found. It either does not exist or has been deleted from our servers!',
madeby: config.http.branding[1],
madebysite: config.http.branding[2],
}
)
}
} catch (error) {
console.error('error while fetching one email', error)
next(error)
}
}
)
2023-11-01 11:48:19 +01:00
module.exports = router