From e2c49346c7fe90deb0800e8eb2f0e5ada17bac6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Soko=C5=82owski?= Date: Mon, 10 Feb 2020 21:44:04 +0100 Subject: [PATCH] use js-status-chat-key lib to display chat name from chat key MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jakub SokoĊ‚owski --- Dockerfile | 4 ++-- package.json | 1 + routes/index.js | 41 ++++++++++++++++++++++------------------- utils/index.js | 29 +++++++++++++++++++++++++++++ views/index.ejs | 6 +++--- 5 files changed, 57 insertions(+), 24 deletions(-) create mode 100644 utils/index.js diff --git a/Dockerfile b/Dockerfile index b4b200d..ce9c34c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,8 +7,8 @@ ADD package-lock.json /srv WORKDIR /srv -RUN npm install +RUN yarn install ADD . /srv -CMD npm start +CMD yarn start diff --git a/package.json b/package.json index 4b29fff..0327f08 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "ejs": "~2.5.7", "express": "~4.16.0", "http-errors": "~1.6.2", + "js-status-chat-name": "https://github.com/status-im/js-status-chat-name#v0.1.2", "qrcode": "^1.3.0" }, "devDependencies": { diff --git a/routes/index.js b/routes/index.js index 67dc1a4..0c4fdff 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,32 +1,24 @@ var express = require('express'); +var StatusIm = require('js-status-chat-name') var assetLinks = require('../resources/assetlinks.json'); var appleSiteAssociation = require('../resources/apple-app-site-association.json'); -var { makeQrCodeDataUri } = require('../utils/qrcodeUtils'); +var utils = require('../utils'); var router = express.Router(); router.get('/', function (req, res, next) { - function isAndroid(userAgent) { - return userAgent.toLowerCase().indexOf("android") > -1; - } - - function isIOS(userAgent) { - return userAgent.toLowerCase().indexOf("iphone") > -1; - } - if (req.query.redirect) { return next(); } - var userAgent = req.headers['user-agent']; - res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate'); res.header('Expires', '-1'); res.header('Pragma', 'no-cache'); - if (isAndroid(userAgent)) { + var userAgent = req.headers['user-agent']; + if (utils.isAndroid(userAgent)) { return res.redirect("https://play.google.com/store/apps/details?id=im.status.ethereum"); - } else if (isIOS(userAgent)) { + } else if (utils.isIOS(userAgent)) { return res.redirect("https://testflight.apple.com/join/J8EuJmey"); } @@ -49,19 +41,28 @@ router.get('/chat/:chatType/:chatId', function(req, res, next) { const { chatId, chatType } = req.params; res.render('index', { title: `Join the ${chatType} chat: #${chatId} in Status`, + info: `Chat in a public channel #${chatId} in Status.`, path: req.originalUrl, - id: `${chatId}` + chatId: chatId, + chatName: chatId, }); }); router.get('/user/:userId', function(req, res, next) { const { userId } = req.params; + chatName = userId + /* chat keys can be resolved to chat names */ + if (utils.isChatKey(userId)) { + chatName = StatusIm.chatKeyToChatName(userId) + } const options = { - title: `View user ${userId} profile in Status`, + title: `Join ${chatName} in Status`, + info: `Chat and transact with ${userId} in Status.`, path: req.originalUrl, - id: `${userId}` + chatId: userId, + chatName: chatName, }; - makeQrCodeDataUri(userId).then( + utils.makeQrCodeDataUri(userId).then( qrCodeDataUri => res.render('index', { ...options, qrCodeDataUri }), error => res.render('index', options) ); @@ -71,10 +72,12 @@ router.get('/extension/:extensionEndpoint', function(req, res, next) { const { extensionEndpoint } = req.params; const options = { title: `Open extension ${extensionEndpoint} in Status`, + info: `Open the ${extensionEndpoint} extension in Status.`, path: req.originalUrl, - id: `${extensionEndpoint}` + chatId: extensionEndpoint, + chatName: extensionEndpoint, }; - makeQrCodeDataUri('https://get.status.im/extension/' + extensionEndpoint).then( + utils.makeQrCodeDataUri('https://join.status.im/extension/' + extensionEndpoint).then( qrCodeDataUri => res.render('index', { ...options, qrCodeDataUri }), error => res.render('index', options) ); diff --git a/utils/index.js b/utils/index.js new file mode 100644 index 0000000..c3f61cd --- /dev/null +++ b/utils/index.js @@ -0,0 +1,29 @@ +var QRCode = require('qrcode') + +function isChatKey(str) { + if (typeof str != "string") { + throw TypeError("Expected chat key to be a string.") + } + /* Chat keys are prefixed with 0x04 and follwed by + * two 32 byte hexadecimal digits. */ + return /^0[xX]04[0-9a-fA-F]{128}$/.test(str.trim()) +} + +function makeQrCodeDataUri(x) { + return QRCode.toDataURL(x, {width: 300}) +} + +function isAndroid(userAgent) { + return userAgent.toLowerCase().indexOf("android") > -1; +} + +function isIOS(userAgent) { + return userAgent.toLowerCase().indexOf("iphone") > -1; +} + +module.exports = { + isChatKey, + isAndroid, + isIOS, + makeQrCodeDataUri, +} diff --git a/views/index.ejs b/views/index.ejs index 94301c2..2a74db0 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -163,19 +163,19 @@
-

<%= id %>

+

<%= chatName %>

<%if (locals.qrCodeDataUri) { %> <% } %>
-
<%= id %>
+
<%= chatId %>
Copy
Download Status
- Chat and transact with <%= id %> in Status. + <%- info %>