This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
/
get-terms-privacy.js
91 lines (74 loc) · 2.86 KB
/
get-terms-privacy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';
/**
* <locale>/legal/terms and <locale>/legal/privacy
* Translation done by fetching appropriate template for language.
* If language is not found, fall back to en-US.
*
* Either full HTML or a partial can be requested. Partials are
* requested by the front end to request translated documents and
* insert them into the DOM. Full HTML is used whenever a user
* browses to one of the pages directly.
*
* Partials are requested by setting the `Accepts` header to `text/partial`
* HTML is returned if `Accepts` is `text/html`
*/
var fs = require('fs');
var path = require('path');
var logger = require('intel').getLogger('route.get-terms-privacy');
var Promise = require('bluebird');
var config = require('../configuration');
var PAGE_TEMPLATE_DIRECTORY = path.join(config.get('page_template_root'), 'dist');
var templates = require('../legal-templates');
module.exports = function verRoute (i18n) {
var DEFAULT_LANG = config.get('i18n.defaultLang');
var getTemplate = templates(i18n, PAGE_TEMPLATE_DIRECTORY);
var route = {};
route.method = 'get';
// Match (allow for optional trailing slash):
// * /legal/terms
// * /<locale>/legal/terms
// * /legal/privacy
// * /<locale>/legal/privacy
route.path = /^\/(?:([a-zA-Z-\_]*)\/)?legal\/(terms|privacy)(?:\/)?$/;
route.process = function (req, res) {
var lang = req.params[0];
var page = req.params[1];
if (! lang) {
// abide should put a lang on the request, if not, use the default.
return res.redirect(getRedirectURL(req.lang || DEFAULT_LANG, page));
}
getTemplate(page, lang, DEFAULT_LANG)
.then(function (template) {
if (! template) {
logger.warn('%s->`%s` does not exist, redirecting to `%s`',
page, lang, DEFAULT_LANG);
return res.redirect(getRedirectURL(DEFAULT_LANG, page));
}
res.format({
'text/partial': function () {
res.send(template);
},
'text/html': function () {
var context = {};
context[page] = template;
// the HTML page removes the header to allow embedding.
res.removeHeader('X-FRAME-OPTIONS');
res.render(page, context);
}
});
}, function(err) {
logger.error(err);
return res.send(500, 'uh oh: ' + String(err));
});
};
function getRedirectURL(lang, page) {
// lang at this point may use `_` as the separator. Abide matches
// URLs with `-`. Use i18n.languageFrom to do any conversions and
// ensure abide is able to match the language.
return i18n.languageFrom(lang) + '/legal/' + page;
}
return route;
}