From 31ee1c2d3cc13e560d94df52b73d43f19310ab0f Mon Sep 17 00:00:00 2001 From: Nareshkumar Rao <_accounts@nareshkumarrao.com> Date: Thu, 29 Jul 2021 21:52:18 +0200 Subject: [PATCH 1/5] fixed bug re-sending contact verification upon login --- src/routes/LoginRoute.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/routes/LoginRoute.js b/src/routes/LoginRoute.js index 7163367..b1b7a9d 100644 --- a/src/routes/LoginRoute.js +++ b/src/routes/LoginRoute.js @@ -4,8 +4,16 @@ const { addContact, createUser } = require("../db/utils"); function LoginRoute(req, res) { const telegramResponse = req.body.telegramResponse; + authUser(telegramResponse, (success, msg) => { if (success) { + + // User is already logged in + if(req.session.user == telegramResponse.id){ + res.send({authorized: success}); + return; + } + const verified = req.session.verified; const verifiedBy = req.session.verifiedBy; req.session.regenerate(() => { @@ -26,6 +34,7 @@ function LoginRoute(req, res) { res.status(401).send({ authorized: success, message: msg }); } }); + } function authUser(telegramResponse, done) { From a4888597ba5b7f05275e0c13fd5423bc0f9edaad Mon Sep 17 00:00:00 2001 From: Nareshkumar Rao Date: Thu, 29 Jul 2021 21:53:41 +0200 Subject: [PATCH 2/5] ran linter --- src/routes/LoginRoute.js | 6 +- src/routes/TelegramWebhookRoute.js | 141 ++++++++++++++++------------- 2 files changed, 79 insertions(+), 68 deletions(-) diff --git a/src/routes/LoginRoute.js b/src/routes/LoginRoute.js index b1b7a9d..89d0292 100644 --- a/src/routes/LoginRoute.js +++ b/src/routes/LoginRoute.js @@ -7,10 +7,9 @@ function LoginRoute(req, res) { authUser(telegramResponse, (success, msg) => { if (success) { - // User is already logged in - if(req.session.user == telegramResponse.id){ - res.send({authorized: success}); + if (req.session.user == telegramResponse.id) { + res.send({ authorized: success }); return; } @@ -34,7 +33,6 @@ function LoginRoute(req, res) { res.status(401).send({ authorized: success, message: msg }); } }); - } function authUser(telegramResponse, done) { diff --git a/src/routes/TelegramWebhookRoute.js b/src/routes/TelegramWebhookRoute.js index 9c1e795..556d4e5 100644 --- a/src/routes/TelegramWebhookRoute.js +++ b/src/routes/TelegramWebhookRoute.js @@ -3,80 +3,93 @@ const { User, Contact } = require("../db/db"); const { sendTelegramMessage } = require("../telegram"); function TelegramWebhookRoute(req, res) { - - try{ - const messageText = req.body.message.text; - const telegramID = req.body.message.from.id; - if (messageText.toLowerCase() == "/covidpositive") { - userInfected(telegramID, (result) => { - if(result.saved){ - sendTelegramMessage(telegramID, "Thanks for informing us. We will notify the people you were in contact with!", ()=>{}); - informContacts(telegramID, ()=>{}); - }else{ - sendTelegramMessage(telegramID, "Sorry, something went wrong.", ()=>{}); - } - }); + try { + const messageText = req.body.message.text; + const telegramID = req.body.message.from.id; + if (messageText.toLowerCase() == "/covidpositive") { + userInfected(telegramID, (result) => { + if (result.saved) { + sendTelegramMessage( + telegramID, + "Thanks for informing us. We will notify the people you were in contact with!", + () => {} + ); + informContacts(telegramID, () => {}); + } else { + sendTelegramMessage( + telegramID, + "Sorry, something went wrong.", + () => {} + ); } + }); } - catch(e){ - console.log("Could not get Telegram Message"); - } - + } catch (e) { + console.log("Could not get Telegram Message"); + } - res.send(); + res.send(); } -function informContacts(telegramID, doneCallback){ - User.findOne({ +function informContacts(telegramID, doneCallback) { + User.findOne({ + where: { + telegram: telegramID, + }, + }).then((user) => { + if (user) { + const userRowID = user.id; + Contact.findAll({ where: { - telegram: telegramID, - } - }).then(user => { - if(user){ - const userRowID = user.id; - Contact.findAll({ - where: { - [Op.or]: [{user: userRowID}, {with: userRowID}], - } - }) - .then(result => { - result.forEach(contact => { - const otherPersonID = contact.user == userRowID ? contact.with : contact.user; - User.findOne({ - where: { - id: otherPersonID, - } - }).then(otherPerson => { - sendTelegramMessage(otherPerson.telegram, "You're infected.", ()=>{}); - }); - }); - }); - } - }); - + [Op.or]: [{ user: userRowID }, { with: userRowID }], + }, + }).then((result) => { + result.forEach((contact) => { + const otherPersonID = + contact.user == userRowID ? contact.with : contact.user; + User.findOne({ + where: { + id: otherPersonID, + }, + }).then((otherPerson) => { + sendTelegramMessage( + otherPerson.telegram, + "You're infected.", + () => {} + ); + }); + }); + }); + } + }); } function userInfected(telegramID, doneCallback) { - User.findOne({ - where: { - telegram: telegramID, - }, - }).then((user) => { - if (!user) { - done({saved: false}); - } else { - user.isInfected = true; - user.save().then(result => { - if(result){ - - doneCallback({saved: true}); - } - }).catch(err=>{doneCallback({saved: false})}); - } + User.findOne({ + where: { + telegram: telegramID, + }, + }) + .then((user) => { + if (!user) { + done({ saved: false }); + } else { + user.isInfected = true; + user + .save() + .then((result) => { + if (result) { + doneCallback({ saved: true }); + } + }) + .catch((err) => { + doneCallback({ saved: false }); + }); + } }) - .catch(err=>{ - doneCallback({saved: false}); + .catch((err) => { + doneCallback({ saved: false }); }); } -exports.TelegramWebhookRoute = TelegramWebhookRoute; \ No newline at end of file +exports.TelegramWebhookRoute = TelegramWebhookRoute; From 625ca5ecf0e651a528c1b247a0e303aebe3c74ac Mon Sep 17 00:00:00 2001 From: Nareshkumar Rao <_accounts@nareshkumarrao.com> Date: Thu, 29 Jul 2021 22:31:46 +0200 Subject: [PATCH 3/5] - moved strings to strings.js - added default parameter from sendTelegramMessage,... --- src/db/utils.js | 3 ++- src/routes/TelegramWebhookRoute.js | 33 ++++++++++++++++++------------ src/strings.js | 16 +++++++++++++++ src/telegram.js | 2 +- 4 files changed, 39 insertions(+), 15 deletions(-) create mode 100644 src/strings.js diff --git a/src/db/utils.js b/src/db/utils.js index 591db8c..b396bdc 100644 --- a/src/db/utils.js +++ b/src/db/utils.js @@ -1,3 +1,4 @@ +const { strings_en } = require("../strings"); const { sendTelegramMessage } = require("../telegram"); const { User, Contact } = require("./db"); @@ -11,7 +12,7 @@ function addContact(telegram, withUserID, done) { ); sendTelegramMessage( withUser.telegram, - "Someone scanned your QR code. You will be notified if they are tested positive with Covid. If you are tested positive, please tell this bot /COVIDPOSITIVE", + strings_en.telegram_qr_scanned, () => {} ); done(true, "Successfully added contact"); diff --git a/src/routes/TelegramWebhookRoute.js b/src/routes/TelegramWebhookRoute.js index 9c1e795..474c6f9 100644 --- a/src/routes/TelegramWebhookRoute.js +++ b/src/routes/TelegramWebhookRoute.js @@ -1,32 +1,39 @@ const { Op } = require("sequelize"); const { User, Contact } = require("../db/db"); +const { strings_en } = require("../strings"); const { sendTelegramMessage } = require("../telegram"); function TelegramWebhookRoute(req, res) { try{ - const messageText = req.body.message.text; - const telegramID = req.body.message.from.id; - if (messageText.toLowerCase() == "/covidpositive") { - userInfected(telegramID, (result) => { - if(result.saved){ - sendTelegramMessage(telegramID, "Thanks for informing us. We will notify the people you were in contact with!", ()=>{}); - informContacts(telegramID, ()=>{}); - }else{ - sendTelegramMessage(telegramID, "Sorry, something went wrong.", ()=>{}); - } - }); + if(req.body.message.connected_website){ + sendTelegramMessage(req.body.message.from.id, "Thanks for using OurSejahtera! Let's stay safer together <3"); + }else{ + const messageText = req.body.message.text; + const telegramID = req.body.message.from.id; + if (messageText.toLowerCase() == "/covidpositive") { + userInfected(telegramID, (result) => { + if(result.saved){ + sendTelegramMessage(telegramID, strings_en.telegram_inform_positive); + informContacts(telegramID); + }else{ + sendTelegramMessage(telegramID, "Sorry, something went wrong."); + } + }); + } } } catch(e){ console.log("Could not get Telegram Message"); } + + res.send(); } -function informContacts(telegramID, doneCallback){ +function informContacts(telegramID, doneCallback=()=>{}){ User.findOne({ where: { telegram: telegramID, @@ -47,7 +54,7 @@ function informContacts(telegramID, doneCallback){ id: otherPersonID, } }).then(otherPerson => { - sendTelegramMessage(otherPerson.telegram, "You're infected.", ()=>{}); + sendTelegramMessage(otherPerson.telegram, strings_en.telegram_inform_infect); }); }); }); diff --git a/src/strings.js b/src/strings.js new file mode 100644 index 0000000..22c95de --- /dev/null +++ b/src/strings.js @@ -0,0 +1,16 @@ +const strings_en = { + telegram_inform_infect: "ATTENTION! Someone you have been \ +in contact with has reported being tested POSITIVE with \ +COVID19. Please maintain a self-quarantine until getting \ +tested, and follow all local guidelines.", + + telegram_inform_positive: "Thanks for informing us. We will \ +notify the people you were in contact with! Please follow all \ +local COVID19 guidelines.", + + telegram_qr_scanned: "Someone scanned your QR code. You will \ +be notified if they report being tested positive with COVID19. If \ +you are tested positive, please tell me /COVIDPOSITIVE", +}; + +exports.strings_en = strings_en; \ No newline at end of file diff --git a/src/telegram.js b/src/telegram.js index 73c601c..537ada8 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -16,7 +16,7 @@ function setTelegramWebHook(done) { }); } -function sendTelegramMessage(telegramID, message, done) { +function sendTelegramMessage(telegramID, message, done=()=>{}) { const url = `https://api.telegram.org/bot${process.env.TELEGRAM_TOKEN}/sendMessage`; axios .post(url, { From 9a9cfbdb03a11876919ea69d676ef4e7abde10fd Mon Sep 17 00:00:00 2001 From: Nareshkumar Rao <_accounts@nareshkumarrao.com> Date: Thu, 29 Jul 2021 22:36:35 +0200 Subject: [PATCH 4/5] fix --- src/routes/TelegramWebhookRoute.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/routes/TelegramWebhookRoute.js b/src/routes/TelegramWebhookRoute.js index 542ccbe..f1c6a76 100644 --- a/src/routes/TelegramWebhookRoute.js +++ b/src/routes/TelegramWebhookRoute.js @@ -22,15 +22,10 @@ function TelegramWebhookRoute(req, res) { }); } } - }); - } - catch(e){ + }catch(e){ console.log("Could not get Telegram Message"); } - - - res.send(); } From 01d1090975bbc8cc8ef421f05c2e4c0cc393eb64 Mon Sep 17 00:00:00 2001 From: Nareshkumar Rao Date: Thu, 29 Jul 2021 22:40:41 +0200 Subject: [PATCH 5/5] ran linter --- src/routes/TelegramWebhookRoute.js | 101 +++++++++++++++-------------- src/strings.js | 11 ++-- src/telegram.js | 2 +- 3 files changed, 62 insertions(+), 52 deletions(-) diff --git a/src/routes/TelegramWebhookRoute.js b/src/routes/TelegramWebhookRoute.js index f1c6a76..e0c78d6 100644 --- a/src/routes/TelegramWebhookRoute.js +++ b/src/routes/TelegramWebhookRoute.js @@ -4,59 +4,66 @@ const { strings_en } = require("../strings"); const { sendTelegramMessage } = require("../telegram"); function TelegramWebhookRoute(req, res) { - - try{ - if(req.body.message.connected_website){ - sendTelegramMessage(req.body.message.from.id, "Thanks for using OurSejahtera! Let's stay safer together <3"); - }else{ - const messageText = req.body.message.text; - const telegramID = req.body.message.from.id; - if (messageText.toLowerCase() == "/covidpositive") { - userInfected(telegramID, (result) => { - if(result.saved){ - sendTelegramMessage(telegramID, strings_en.telegram_inform_positive); - informContacts(telegramID); - }else{ - sendTelegramMessage(telegramID, "Sorry, something went wrong."); - } - }); - } - } - }catch(e){ - console.log("Could not get Telegram Message"); + try { + if (req.body.message.connected_website) { + sendTelegramMessage( + req.body.message.from.id, + "Thanks for using OurSejahtera! Let's stay safer together <3" + ); + } else { + const messageText = req.body.message.text; + const telegramID = req.body.message.from.id; + if (messageText.toLowerCase() == "/covidpositive") { + userInfected(telegramID, (result) => { + if (result.saved) { + sendTelegramMessage( + telegramID, + strings_en.telegram_inform_positive + ); + informContacts(telegramID); + } else { + sendTelegramMessage(telegramID, "Sorry, something went wrong."); + } + }); + } } + } catch (e) { + console.log("Could not get Telegram Message"); + } res.send(); } -function informContacts(telegramID, doneCallback=()=>{}){ - User.findOne({ +function informContacts(telegramID, doneCallback = () => {}) { + User.findOne({ + where: { + telegram: telegramID, + }, + }).then((user) => { + if (user) { + const userRowID = user.id; + Contact.findAll({ where: { - telegram: telegramID, - } - }).then(user => { - if(user){ - const userRowID = user.id; - Contact.findAll({ - where: { - [Op.or]: [{user: userRowID}, {with: userRowID}], - } - }) - .then(result => { - result.forEach(contact => { - const otherPersonID = contact.user == userRowID ? contact.with : contact.user; - User.findOne({ - where: { - id: otherPersonID, - } - }).then(otherPerson => { - sendTelegramMessage(otherPerson.telegram, strings_en.telegram_inform_infect); - }); - }); - }); - } - }); - + [Op.or]: [{ user: userRowID }, { with: userRowID }], + }, + }).then((result) => { + result.forEach((contact) => { + const otherPersonID = + contact.user == userRowID ? contact.with : contact.user; + User.findOne({ + where: { + id: otherPersonID, + }, + }).then((otherPerson) => { + sendTelegramMessage( + otherPerson.telegram, + strings_en.telegram_inform_infect + ); + }); + }); + }); + } + }); } function userInfected(telegramID, doneCallback) { diff --git a/src/strings.js b/src/strings.js index 22c95de..210f529 100644 --- a/src/strings.js +++ b/src/strings.js @@ -1,16 +1,19 @@ const strings_en = { - telegram_inform_infect: "ATTENTION! Someone you have been \ + telegram_inform_infect: + "ATTENTION! Someone you have been \ in contact with has reported being tested POSITIVE with \ COVID19. Please maintain a self-quarantine until getting \ tested, and follow all local guidelines.", - telegram_inform_positive: "Thanks for informing us. We will \ + telegram_inform_positive: + "Thanks for informing us. We will \ notify the people you were in contact with! Please follow all \ local COVID19 guidelines.", - telegram_qr_scanned: "Someone scanned your QR code. You will \ + telegram_qr_scanned: + "Someone scanned your QR code. You will \ be notified if they report being tested positive with COVID19. If \ you are tested positive, please tell me /COVIDPOSITIVE", }; -exports.strings_en = strings_en; \ No newline at end of file +exports.strings_en = strings_en; diff --git a/src/telegram.js b/src/telegram.js index 537ada8..4fb241f 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -16,7 +16,7 @@ function setTelegramWebHook(done) { }); } -function sendTelegramMessage(telegramID, message, done=()=>{}) { +function sendTelegramMessage(telegramID, message, done = () => {}) { const url = `https://api.telegram.org/bot${process.env.TELEGRAM_TOKEN}/sendMessage`; axios .post(url, {