Gitter migration: Setup redirects (rollout pt. 3)
[gitter.git] / server / web / user-agent-tagger.js
blob2fc44e7fce041f4a4879694d3239a7fff57cd804
1 'use strict';
3 var isPhone = require('./is-phone');
4 var isNative = require('./is-native');
5 var _ = require('lodash');
7 // prior to 1.2.1, the ios app would incorrectly send its build number instead of the version nubmer
8 var mobileBuildVersionMapping = {
9 '600': '1.2.0',
10 '598': '1.1.1',
11 '595': '1.1.0',
12 '587': '1.0.0'
15 function getType(req, userAgentString) {
16 return isPhone(req) || userAgentString.indexOf('Mobile') >= 0 ? 'mobile' : 'desktop';
19 function getGitterAppMetadata(req, userAgentString) {
20 // e.g GitterBeta/1.2.0
21 var extension = userAgentString.substring(userAgentString.indexOf('Gitter')).split(' ')[0];
23 var parts = extension.split('/');
25 var family = parts[0];
26 var version = parts[1];
28 if (getType(req, userAgentString) === 'mobile') {
29 version = mobileBuildVersionMapping[version] || version;
32 var versionParts = (version || '').split('.');
34 return {
35 family: family,
36 major: versionParts[0] || '',
37 minor: versionParts[1] || '',
38 patch: versionParts[2] || ''
42 function createVersionString(versionObject) {
43 const { major, minor, patch } = versionObject;
44 const versionNumbers = [major || '0', minor || '0', patch || '0'];
45 return versionNumbers.join('.');
48 function tagify(ua) {
49 return {
50 'agent:type': ua.type,
51 'agent:family': ua.family,
52 'agent:version': createVersionString(ua),
53 'agent:device:family': ua.device.family,
54 'agent:device:version': createVersionString(ua.device),
55 'agent:os:family': ua.os.family,
56 'agent:os:version': createVersionString(ua.os)
60 module.exports = function(req) {
61 var userAgentString = req.headers['user-agent'];
63 // no useragentstring? no tags for you.
64 if (!userAgentString) return {};
66 var userAgentObj = req.getParsedUserAgent().toJSON();
68 if (isNative(req)) {
69 var appMetadata = getGitterAppMetadata(req, userAgentString);
70 userAgentObj = _.extend({}, userAgentObj, appMetadata);
72 userAgentObj.type = getType(req, userAgentString);
74 return tagify(userAgentObj);