3 const Promise = require('bluebird');
4 const env = require('gitter-web-env');
5 const nconf = env.config;
6 const stats = env.stats;
7 const express = require('express');
8 const StatusError = require('statuserror');
9 const identifyRoute = env.middlewares.identifyRoute;
10 const featureToggles = require('../web/middlewares/feature-toggles');
11 const ensureLoggedIn = require('../web/middlewares/ensure-logged-in');
12 const preventClickjackingMiddleware = require('../web/middlewares/prevent-clickjacking');
13 const langs = require('langs');
14 const loginUtils = require('../web/login-utils');
15 const socialMetadataGenerator = require('./social-metadata-generator');
16 const fonts = require('../web/fonts');
17 const contextGenerator = require('../web/context-generator');
18 const generateAdminChatMessageReportSnapshot = require('./snapshots/admin-chat-message-report-snapshot');
20 const survivalMode = !!process.env.SURVIVAL_MODE || false;
23 * When Gitter hits a big news site, this setting disables
24 * the embedded chats on the home page, which helps with
27 var slashdotEffectSurvivalMode = survivalMode || !!process.env.SLASHDOT_EFFECT_SURVIVAL_MODE;
29 var router = express.Router({ caseSensitive: true, mergeParams: true });
32 nconf.get('web:homeurl'),
33 identifyRoute('homepage'),
34 preventClickjackingMiddleware,
36 require('../web/middlewares/unawesome-browser'),
37 function(req, res, next) {
38 if (req.user && req.query.redirect !== 'no') {
39 loginUtils.redirectUserToDefaultTroupe(req, res, next);
43 // This is code of the translation we are able to provide (e.g. en, zh-TW)
44 var locale = req.i18n.getLocale();
45 var requested = req.headers['accept-language'] || '';
46 requested = requested.split(';')[0] || '';
47 requested = requested.split(/,\s*/)[0];
48 requested = requested.toLowerCase();
50 var requestLangCode, requestLangLocalName;
51 // We compare the translation we provide with what user wants
52 // if we can't provide translation, we get localized name of the language so we can show "Want this in 中文 ?" message
53 // "en" for "en-gb" is fine, so is "zh-tw" for "zh-tw"
54 const translationRequired = locale !== requested && locale !== requested.split('-')[0];
55 if (translationRequired) {
56 var requestLang = langs.where('1', requested);
58 requestLangCode = requestLang['1'];
59 requestLangLocalName = requestLang.local;
63 var translatedBy = req.i18n.__('Translated By');
64 /* i18n doesn't like empty strings. Use a dash as a proxy */
65 if (translatedBy === '-') translatedBy = '';
67 // when the viewer is not logged in:
68 res.render('homepage', {
69 slashdotEffectSurvivalMode: slashdotEffectSurvivalMode,
70 bootScriptName: 'homepage',
71 cssFileName: 'styles/homepage.css',
72 wordy: locale === 'ru',
73 translationRequired: translationRequired,
74 requestLangCode: requestLangCode,
75 requestLangLocalName: requestLangLocalName,
76 translated: translatedBy,
77 fonts: fonts.getFonts(),
78 hasCachedFonts: fonts.hasCachedFonts(req.cookies),
79 socialMetadata: socialMetadataGenerator.getMetadata()
84 if (nconf.get('web:homeurl') !== '/') {
85 router.get('/', identifyRoute('homepage-landing'), preventClickjackingMiddleware, function(
90 if (req.query.redirect === 'no') {
91 res.relativeRedirect(nconf.get('web:homeurl') + '?redirect=no');
93 res.relativeRedirect(nconf.get('web:homeurl'));
98 res.render('landing');
102 router.get('/apps', identifyRoute('homepage-apps'), preventClickjackingMiddleware, function(
107 homeUrl: nconf.get('web:homeurl')
112 '/manifest.webmanifest',
113 identifyRoute('web-app-manifest'),
114 preventClickjackingMiddleware,
116 res.set('Content-Type', 'application/manifest+json');
117 res.render('manifest-webmanifest');
121 if (nconf.get('matrix:wellKnownServer')) {
123 '/.well-known/matrix/server',
124 identifyRoute('well-known-matrix'),
125 preventClickjackingMiddleware,
127 res.set('Content-Type', 'application/manifest+json');
128 res.render('wellknown-matrix', {
129 matrixWellKnownServer: nconf.get('matrix:wellKnownServer')
135 router.get('/robots.txt', identifyRoute('homepage-robots'), preventClickjackingMiddleware, function(
139 res.set('Content-Type', 'text/text');
140 res.render('robotstxt', {
141 allowCrawling: nconf.get('sitemap:allowCrawling'),
142 sitemap: nconf.get('sitemap:location')
146 router.get('/humans.txt', identifyRoute('homepage-humans'), preventClickjackingMiddleware, function(
150 res.set('Content-Type', 'text/text');
151 res.render('humanstxt');
155 '/-/unawesome-browser',
156 identifyRoute('homepage-unawesome-browser'),
157 preventClickjackingMiddleware,
159 res.status(406 /* Not Acceptable */).render('unawesome-browser', {});
163 router.get('/about/early-bird', identifyRoute('earlybird'), preventClickjackingMiddleware, function(
167 res.relativeRedirect('/');
171 '/about/gitlab/mailing-list',
173 identifyRoute('gitlab-mailing-list'),
174 preventClickjackingMiddleware,
178 stats.event('gitlab_ml_opt_in', {
180 username: user.username
183 res.render('gitlab-mailing-list');
187 // old campaign that still gets some hits
188 router.get('/about/*', identifyRoute('homepage-about'), preventClickjackingMiddleware, function(
192 res.redirect(nconf.get('web:homeurl'));
195 // This really doesn't seem like the right place for this?
196 // Does anyone know what this is for?
197 router.get('/_s/cdn/*', identifyRoute('homepage-cdn'), preventClickjackingMiddleware, function(
201 res.redirect(req.path.replace('/_s/cdn', ''));
205 '/-/admin/chat-message-reports',
206 identifyRoute('admin-chat-message-reports'),
207 preventClickjackingMiddleware,
209 if (!req.user || !req.user.staff) {
210 throw new StatusError(403, 'Only staff can view this area');
214 troupeContext: contextGenerator.generateBasicContext(req),
215 snapshots: generateAdminChatMessageReportSnapshot(req)
216 }).then(function({ troupeContext, snapshots }) {
217 troupeContext.snapshots = snapshots;
219 res.render('admin/chat-message-reports', {
220 bootScriptName: 'chat-message-reports',
221 cssFileName: 'styles/router-admin-dashboard.css',
222 troupeContext: troupeContext
228 module.exports = router;