Gitter migration: Setup redirects (rollout pt. 3)
[gitter.git] / server / web / context-generator-request.js
blob9c3551190f7e1e83df79ac5e0f33c00fb439d8cc
1 'use strict';
3 var Promise = require('bluebird');
4 var presenceService = require('gitter-web-presence');
5 var isNative = require('./is-native');
7 /**
8 * Figures out whether to use desktop notifications for this user
9 */
10 var determineDesktopNotifications = Promise.method(function(user, req) {
11 if (!user) return true;
13 var agent = req.getParsedUserAgent();
14 var os = agent.os.family;
15 var clientType;
17 if (os === 'Mac OS X') {
18 clientType = 'osx';
19 } else if (os.indexOf('Windows') === 0) {
20 clientType = 'win';
21 } else if (os.indexOf('Linux') === 0) {
22 clientType = 'linux';
25 if (clientType) {
26 return presenceService
27 .isUserConnectedWithClientType(user.id, clientType)
28 .then(function(result) {
29 return !result;
30 });
33 return true;
34 });
36 function contextFromRequest(req) {
37 var user = req.user;
38 var events = req.session && req.session.events;
40 // Pass the feature toggles through to the client
41 var features;
42 if (req.fflip && req.fflip.features) {
43 features = Object.keys(req.fflip.features).filter(function(featureKey) {
44 return req.fflip.features[featureKey];
45 });
48 if (events) {
49 req.session.events = [];
52 var contextHash = {
53 events: events,
54 accessToken: req.accessToken,
55 isNativeDesktopApp: isNative(req),
56 locale: req.i18n.locales[req.i18n.locale],
57 features: features
60 if (!user) {
61 return contextHash;
64 return determineDesktopNotifications(user, req).then(function(desktopNotifications) {
65 contextHash.desktopNotifications = desktopNotifications;
66 return contextHash;
67 });
70 module.exports = Promise.method(contextFromRequest);