diff --git a/keyserver/src/keyserver.js b/keyserver/src/keyserver.js --- a/keyserver/src/keyserver.js +++ b/keyserver/src/keyserver.js @@ -3,13 +3,10 @@ import olm from '@commapp/olm'; import cluster from 'cluster'; import cookieParser from 'cookie-parser'; -import { detect as detectBrowser } from 'detect-browser'; import express from 'express'; import expressWs from 'express-ws'; import os from 'os'; -import stores from 'lib/facts/stores.js'; - import './cron/cron.js'; import { migrate } from './database/migrations.js'; import { jsonEndpoints } from './endpoints.js'; @@ -27,7 +24,10 @@ createNewVersionResponder, markVersionDeployedResponder, } from './responders/version-responders.js'; -import { websiteResponder } from './responders/website-responders.js'; +import { + inviteResponder, + websiteResponder, +} from './responders/website-responders.js'; import { webWorkerResponder } from './responders/webworker-responders.js'; import { onConnection } from './socket/socket.js'; import { @@ -43,8 +43,6 @@ getCommAppURLFacts, } from './utils/urls.js'; -const inviteSecretRegex = /^[a-z0-9]+$/i; - (async () => { await Promise.all([olm.init(), prefetchAllURLFacts(), initENSCache()]); @@ -129,23 +127,7 @@ // receives this request, it means that the app is not installed and we // should redirect the user to a place from which the app can be // downloaded. - router.get('/invite/:secret', (req, res) => { - const { secret } = req.params; - const userAgent = req.get('User-Agent'); - const detectionResult = detectBrowser(userAgent); - let redirectUrl = stores.appStoreUrl; - if (detectionResult.os === 'Android OS') { - const isSecretValid = inviteSecretRegex.test(secret); - const referrer = isSecretValid - ? `&referrer=${encodeURIComponent(`utm_source=invite/${secret}`)}` - : ''; - redirectUrl = `${stores.googlePlayUrl}${referrer}`; - } - res.writeHead(301, { - Location: redirectUrl, - }); - res.end(); - }); + router.get('/invite/:secret', inviteResponder); // $FlowFixMe express-ws has side effects that can't be typed router.ws('/ws', onConnection); diff --git a/keyserver/src/responders/website-responders.js b/keyserver/src/responders/website-responders.js --- a/keyserver/src/responders/website-responders.js +++ b/keyserver/src/responders/website-responders.js @@ -1,6 +1,7 @@ // @flow import html from 'common-tags/lib/html/index.js'; +import { detect as detectBrowser } from 'detect-browser'; import type { $Response, $Request } from 'express'; import fs from 'fs'; import _keyBy from 'lodash/fp/keyBy.js'; @@ -10,6 +11,7 @@ import { promisify } from 'util'; import { baseLegalPolicies } from 'lib/facts/policies.js'; +import stores from 'lib/facts/stores.js'; import { daysToEntriesFromEntryInfos } from 'lib/reducers/entry-reducer.js'; import { freshMessageStore } from 'lib/reducers/message-reducer.js'; import { mostRecentlyReadThread } from 'lib/selectors/thread-selectors.js'; @@ -445,4 +447,24 @@ `); } -export { websiteResponder }; +const inviteSecretRegex = /^[a-z0-9]+$/i; + +function inviteResponder(req: $Request, res: $Response) { + const { secret } = req.params; + const userAgent = req.get('User-Agent'); + const detectionResult = detectBrowser(userAgent); + let redirectUrl = stores.appStoreUrl; + if (detectionResult.os === 'Android OS') { + const isSecretValid = inviteSecretRegex.test(secret); + const referrer = isSecretValid + ? `&referrer=${encodeURIComponent(`utm_source=invite/${secret}`)}` + : ''; + redirectUrl = `${stores.googlePlayUrl}${referrer}`; + } + res.writeHead(301, { + Location: redirectUrl, + }); + res.end(); +} + +export { websiteResponder, inviteResponder };