Implementation of archive route. Simplified from v1. Not tested.

This commit is contained in:
Tomas Bures 2018-09-22 18:12:22 +02:00
parent a9e1700dbe
commit dda95ecdb3
9 changed files with 320 additions and 272 deletions

31
routes/archive.js Normal file
View file

@ -0,0 +1,31 @@
'use strict';
const router = require('../lib/router-async').create();
const CampaignSender = require('../lib/campaign-sender');
router.get('/:campaign/:list/:subscription', (req, res, next) => {
const cs = new CampaignSender();
cs.init({campaignCid: req.params.campaign})
.then(() => cs.getMessage(req.params.list, req.params.subscription))
.then(result => {
const {html} = result;
res.render('partials/tracking-scripts', {
layout: 'archive/layout-raw'
}, (err, scripts) => {
if (err) {
return next(err);
}
html = scripts ? html.replace(/<\/body\b/i, match => scripts + match) : html;
res.render('archive/view-raw', {
layout: 'archive/layout-raw',
message: html
});
});
})
.catch(err => next(err));
});
module.exports = router;

View file

@ -4,6 +4,7 @@ const log = require('npmlog');
const config = require('config');
const router = require('../lib/router-async').create();
const links = require('../models/links');
const interoperableErrors = require('../shared/interoperable-errors');
const trackImg = new Buffer('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7', 'base64');
@ -20,17 +21,6 @@ router.getAsync('/:campaign/:list/:subscription', async (req, res) => {
router.getAsync('/:campaign/:list/:subscription/:link', async (req, res) => {
const notFound = () => {
res.status(404);
return res.render('archive/view', {
layout: 'archive/layout',
message: _('Oops, we couldn\'t find a link for the URL you clicked'),
campaign: {
subject: 'Error 404'
}
});
};
const link = await links.resolve(req.params.link);
if (link) {
@ -40,7 +30,7 @@ router.getAsync('/:campaign/:list/:subscription/:link', async (req, res) => {
return res.redirect(url);
} else {
log.error('Redirect', 'Unresolved URL: <%s>', req.url);
return notFound();
throw new interoperableErrors.NotFoundError('Oops, we couldn\'t find a link for the URL you clicked');
}
});