2020-02-21 11:36:48 +00:00
|
|
|
var path = require('path');
|
2020-02-20 15:40:13 +00:00
|
|
|
var express = require('express')
|
2020-02-10 20:44:04 +00:00
|
|
|
var StatusIm = require('js-status-chat-name')
|
2020-02-21 11:36:48 +00:00
|
|
|
var links = require('../resources/links.json')
|
2020-02-20 15:40:13 +00:00
|
|
|
var assetLinks = require('../resources/assetlinks.json')
|
|
|
|
var appleSiteAssociation = require('../resources/apple-app-site-association.json')
|
|
|
|
var utils = require('../utils')
|
2018-10-05 05:31:10 +00:00
|
|
|
|
2020-02-20 15:40:13 +00:00
|
|
|
var router = express.Router()
|
2018-06-28 06:47:06 +00:00
|
|
|
|
2020-02-21 11:34:51 +00:00
|
|
|
/* Helper for generating pages */
|
2020-02-21 10:31:12 +00:00
|
|
|
const genPage = (res, options) => {
|
|
|
|
let opts = {
|
|
|
|
...options,
|
|
|
|
buttonTitle: 'Open in Status',
|
|
|
|
buttonUrl: options.path,
|
2020-02-20 15:40:13 +00:00
|
|
|
}
|
2020-02-21 10:31:12 +00:00
|
|
|
utils.makeQrCodeDataUri(options.path).then(
|
|
|
|
qrUri => res.render('index', { ...opts, qrUri }),
|
|
|
|
error => res.render('index', opts)
|
2020-02-20 15:40:13 +00:00
|
|
|
)
|
2020-02-21 10:31:12 +00:00
|
|
|
}
|
2018-06-28 06:47:06 +00:00
|
|
|
|
2020-02-21 10:31:12 +00:00
|
|
|
/* Helper for full URLs, can specify optional path */
|
|
|
|
const fullUrl = (req, path) => (
|
|
|
|
`${req.protocol}://${req.hostname}${path ? path : req.originalUrl}`
|
|
|
|
)
|
2018-06-28 07:27:15 +00:00
|
|
|
|
2020-02-21 11:11:54 +00:00
|
|
|
/* Open Website/Dapp in Status */
|
|
|
|
const handleSite = (req, res) => {
|
2020-02-20 15:40:13 +00:00
|
|
|
let { url } = req.params
|
|
|
|
url = url.replace(/https?:\/\//, '')
|
2020-02-21 10:31:12 +00:00
|
|
|
genPage(res, {
|
2020-02-20 15:40:13 +00:00
|
|
|
title: `Browse to ${url} in Status`,
|
|
|
|
info: `Browse to ${url} in Status`,
|
|
|
|
copyTarget: url,
|
|
|
|
headerName: `<a href="https://${url}">${url}</a>`,
|
2020-02-21 10:31:12 +00:00
|
|
|
path: fullUrl(req, `/b/${url}`),
|
|
|
|
})
|
2020-02-21 11:11:54 +00:00
|
|
|
}
|
2020-02-21 10:31:12 +00:00
|
|
|
|
2020-02-21 11:36:48 +00:00
|
|
|
/* Open User Profile from Chat Key in Status */
|
|
|
|
const handleChatKey = (req, res) => {
|
|
|
|
const chatKey = req.params[0]
|
|
|
|
chatName = StatusIm.chatKeyToChatName(chatKey)
|
|
|
|
genPage(res, {
|
|
|
|
title: `Join ${chatName} in Status`,
|
|
|
|
info: `Chat and transact with <span>${chatKey}</span> in Status.`,
|
|
|
|
copyTarget: chatKey,
|
|
|
|
headerName: chatName,
|
|
|
|
path: fullUrl(req),
|
|
|
|
})
|
|
|
|
}
|
2020-02-21 10:31:12 +00:00
|
|
|
|
2020-02-21 11:11:54 +00:00
|
|
|
/* Open User Profile from ENS Name in Status */
|
|
|
|
const handleEnsName = (req, res) => {
|
2020-02-21 11:34:51 +00:00
|
|
|
let username
|
|
|
|
try {
|
|
|
|
username = utils.normalizeEns(req.params[0])
|
|
|
|
} catch(error) { /* ENS names have the widest regex: .+ */
|
2020-02-21 11:36:48 +00:00
|
|
|
console.error(`Failed to parse: "${req.params[0]}", Error:`, error.message)
|
2020-02-21 11:34:51 +00:00
|
|
|
res.render('index', { title: 'Invalid Username Format!', error })
|
|
|
|
return
|
|
|
|
}
|
2020-02-21 10:31:12 +00:00
|
|
|
genPage(res, {
|
|
|
|
title: `Join @${username} in Status`,
|
|
|
|
info: `Chat and transact with <span>@${username}</span> in Status.`,
|
|
|
|
copyTarget: username,
|
2020-02-21 11:11:54 +00:00
|
|
|
headerName: `@${utils.showSpecialChars(username)}`,
|
2020-02-21 11:36:48 +00:00
|
|
|
path: fullUrl(req, `/@${username}`),
|
2020-02-21 10:31:12 +00:00
|
|
|
})
|
2020-02-21 11:11:54 +00:00
|
|
|
}
|
2020-02-21 10:31:12 +00:00
|
|
|
|
2020-02-21 11:11:54 +00:00
|
|
|
/* Open Public Channel in Status */
|
|
|
|
const handlePublicChannel = (req, res) => {
|
2020-02-21 10:31:12 +00:00
|
|
|
const chatName = req.params[0]
|
|
|
|
genPage(res, {
|
|
|
|
title: `Join #${chatName} in Status`,
|
|
|
|
info: `Join public channel <span>#${chatName}</span> on Status.`,
|
|
|
|
headerName: `#${chatName}`,
|
|
|
|
copyTarget: chatName,
|
|
|
|
path: fullUrl(req),
|
|
|
|
})
|
2020-02-21 11:11:54 +00:00
|
|
|
}
|
2020-02-21 10:31:12 +00:00
|
|
|
|
2020-02-21 11:36:48 +00:00
|
|
|
router.get('/.well-known/assetlinks.json', (req, res) => {
|
|
|
|
res.json(assetLinks)
|
|
|
|
})
|
|
|
|
|
|
|
|
router.get('/.well-known/apple-app-site-association', (req, res) => {
|
|
|
|
res.json(appleSiteAssociation)
|
|
|
|
})
|
|
|
|
|
|
|
|
router.get('/health', (req, res) => res.send('OK'))
|
|
|
|
|
|
|
|
router.get('/b/:url(*)', handleSite)
|
|
|
|
router.get('/browse/:url(*)', handleSite) /* Legacy */
|
|
|
|
|
|
|
|
router.get(/^\/(0[xX]04[0-9a-fA-F]{128})$/, handleChatKey)
|
|
|
|
router.get(/^\/user\/(0[xX]04[0-9a-fA-F]{128})$/, handleChatKey) /* Legacy */
|
|
|
|
|
|
|
|
router.get(/^\/@(.+)$/, handleEnsName)
|
|
|
|
router.get(/^\/user\/(.+)$/, handleEnsName) /* Legacy */
|
|
|
|
|
2020-02-21 11:11:54 +00:00
|
|
|
router.get(/^\/([a-z0-9-]+)$/, handlePublicChannel)
|
|
|
|
router.get(/^\/chat\/public\/([a-z0-9-]+)$/, handlePublicChannel)
|
2020-02-21 10:31:12 +00:00
|
|
|
|
2020-02-21 11:11:54 +00:00
|
|
|
/* Catchall for everything else */
|
|
|
|
router.get('*', (req, res, next) => {
|
2020-02-21 10:31:12 +00:00
|
|
|
if (req.query.redirect) {
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate')
|
|
|
|
res.header('Expires', '-1')
|
|
|
|
res.header('Pragma', 'no-cache')
|
|
|
|
|
2020-02-21 11:36:48 +00:00
|
|
|
let userAgent = req.headers['user-agent']
|
|
|
|
let redirect = links.getStatus
|
2020-02-21 10:31:12 +00:00
|
|
|
if (utils.isAndroid(userAgent)) {
|
2020-02-21 11:36:48 +00:00
|
|
|
redirect = links.playStore
|
2020-02-21 10:31:12 +00:00
|
|
|
} else if (utils.isIOS(userAgent)) {
|
2020-02-21 11:36:48 +00:00
|
|
|
redirect = links.appleStore
|
2020-02-21 10:31:12 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 11:36:48 +00:00
|
|
|
return res.redirect(redirect)
|
2020-02-20 15:40:13 +00:00
|
|
|
})
|
2018-06-28 06:47:06 +00:00
|
|
|
|
2020-02-20 15:40:13 +00:00
|
|
|
module.exports = router
|