diff options
author | Vitaly Takmazov | 2024-08-07 18:51:24 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2024-08-07 20:02:02 +0300 |
commit | 1dd0547e3fd8d4cacc125def5800c9019dd72cef (patch) | |
tree | 553bf65cbb873b117dbedf7f00ff905192314959 /vnext/server/middleware/android.js | |
parent | 0ac9aa79aeb618a18d0e5032059c6d0dbc80ae17 (diff) |
vnext: `releases` middleware
* Help android app to find upgrade path
Diffstat (limited to 'vnext/server/middleware/android.js')
-rw-r--r-- | vnext/server/middleware/android.js | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/vnext/server/middleware/android.js b/vnext/server/middleware/android.js new file mode 100644 index 00000000..ae5f8fe8 --- /dev/null +++ b/vnext/server/middleware/android.js @@ -0,0 +1,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 |