aboutsummaryrefslogtreecommitdiff
path: root/vnext/server/middleware/android.js
blob: ae5f8fe8d47306a3bdfb5d71483617c34fc3ba10 (plain) (blame)
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
import debug from 'debug'
var log = debug('android')

import * as legacy_data from './legacy.json'

const releases_url = 'https://api.github.com/repos/Juick/Juick-Android/releases'

/**
 * Return android releases
 * @type {import('express').RequestParamHandler}
 */
const releases = async (req, res) => {
    let agent = req.headers['user-agent'] || 'unknown'
    let android_version = parse_agent_android_sdk_version(agent)
    log(`releases request from ${android_version || agent}`)
    if (android_version > 0) {
        if (is_legacy_android(android_version)) {
            log('responding with legacy stub')
            return res.json([legacy_data])
        } else {
            log('redirecting to Github')
            return res.redirect(releases_url)
        }
    }
    return res.json([])
}

const parse_agent_android_sdk_version = (agent = '') => {
    let version = agent.split(' ', 3)
    let is_android_app = version.length == 3 && version[2].startsWith('Android')
    if (is_android_app) {
        let android_version = version[2].split('/')
        let is_valid_version = android_version.length == 2
        return is_valid_version ? +(android_version[1]) : NaN
    }
    return NaN
}

const is_legacy_android = (version) => {
    return version < 24
}

export default releases