From eb6087f8406b04e19b738ea65a6cac621f8e08fd Mon Sep 17 00:00:00 2001 From: TurtIeSocks <58572875+TurtIeSocks@users.noreply.github.com> Date: Thu, 6 Oct 2022 13:07:44 -0400 Subject: [PATCH 1/3] Update user api - Add Discord and Telegram endpoints by ID - Add feedback if no user is found --- server/src/routes/api/v1/users.js | 39 ++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/server/src/routes/api/v1/users.js b/server/src/routes/api/v1/users.js index a97db2ea3..d657f1281 100644 --- a/server/src/routes/api/v1/users.js +++ b/server/src/routes/api/v1/users.js @@ -26,7 +26,8 @@ router.get('/:id', async (req, res) => { api.reactMapSecret && req.headers['react-map-secret'] === api.reactMapSecret ) { - res.status(200).json(await User.query().findById(req.params.id)) + const user = await User.query().findById(req.params.id) + res.status(200).json(user || { message: 'User not found' }) } else { throw new Error('Incorrect or missing API secret') } @@ -37,4 +38,40 @@ router.get('/:id', async (req, res) => { } }) +router.get('/discord/:id', async (req, res) => { + try { + if ( + api.reactMapSecret && + req.headers['react-map-secret'] === api.reactMapSecret + ) { + const user = await User.query().where('discordId', req.params.id).first() + res.status(200).json(user || { message: 'User not found' }) + } else { + throw new Error('Incorrect or missing API secret') + } + console.log(`[API] api/v1/users/discord/${req.params.id}`) + } catch (e) { + console.error(`[API Error] api/v1/users/discord/${req.params.id}`, e) + res.status(500).json({ status: 'ServerError', reason: e.message }) + } +}) + +router.get('/telegram/:id', async (req, res) => { + try { + if ( + api.reactMapSecret && + req.headers['react-map-secret'] === api.reactMapSecret + ) { + const user = await User.query().where('telegramId', req.params.id).first() + res.status(200).json(user || { message: 'User not found' }) + } else { + throw new Error('Incorrect or missing API secret') + } + console.log(`[API] api/v1/users/telegram/${req.params.id}`) + } catch (e) { + console.error(`[API Error] api/v1/users/telegram/${req.params.id}`, e) + res.status(500).json({ status: 'ServerError', reason: e.message }) + } +}) + module.exports = router From 47192a051cfdedff188ad17743f2258e23a9abdb Mon Sep 17 00:00:00 2001 From: TurtIeSocks <58572875+TurtIeSocks@users.noreply.github.com> Date: Thu, 6 Oct 2022 13:09:19 -0400 Subject: [PATCH 2/3] consistency --- server/src/routes/api/v1/users.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/server/src/routes/api/v1/users.js b/server/src/routes/api/v1/users.js index d657f1281..8a8818215 100644 --- a/server/src/routes/api/v1/users.js +++ b/server/src/routes/api/v1/users.js @@ -16,7 +16,7 @@ router.get('/', async (req, res) => { console.log('[API] api/v1/users') } catch (e) { console.error('[API Error] api/v1/sessions', e) - res.status(500).json({ status: 'ServerError', reason: e.message }) + res.status(500).json({ status: 'error', reason: e.message }) } }) @@ -27,14 +27,16 @@ router.get('/:id', async (req, res) => { req.headers['react-map-secret'] === api.reactMapSecret ) { const user = await User.query().findById(req.params.id) - res.status(200).json(user || { message: 'User not found' }) + res + .status(200) + .json(user || { status: 'error', reason: 'User Not Found' }) } else { throw new Error('Incorrect or missing API secret') } console.log(`[API] api/v1/users/${req.params.id}`) } catch (e) { console.error(`[API Error] api/v1/users/${req.params.id}`, e) - res.status(500).json({ status: 'ServerError', reason: e.message }) + res.status(500).json({ status: 'error', reason: e.message }) } }) @@ -45,14 +47,16 @@ router.get('/discord/:id', async (req, res) => { req.headers['react-map-secret'] === api.reactMapSecret ) { const user = await User.query().where('discordId', req.params.id).first() - res.status(200).json(user || { message: 'User not found' }) + res + .status(200) + .json(user || { status: 'error', reason: 'User Not Found' }) } else { throw new Error('Incorrect or missing API secret') } console.log(`[API] api/v1/users/discord/${req.params.id}`) } catch (e) { console.error(`[API Error] api/v1/users/discord/${req.params.id}`, e) - res.status(500).json({ status: 'ServerError', reason: e.message }) + res.status(500).json({ status: 'error', reason: e.message }) } }) @@ -63,14 +67,16 @@ router.get('/telegram/:id', async (req, res) => { req.headers['react-map-secret'] === api.reactMapSecret ) { const user = await User.query().where('telegramId', req.params.id).first() - res.status(200).json(user || { message: 'User not found' }) + res + .status(200) + .json(user || { status: 'error', reason: 'User Not Found' }) } else { throw new Error('Incorrect or missing API secret') } console.log(`[API] api/v1/users/telegram/${req.params.id}`) } catch (e) { console.error(`[API Error] api/v1/users/telegram/${req.params.id}`, e) - res.status(500).json({ status: 'ServerError', reason: e.message }) + res.status(500).json({ status: 'error', reason: e.message }) } }) From 5cc833774d5662cd43cbe32303d7592e70deac33 Mon Sep 17 00:00:00 2001 From: TurtIeSocks <58572875+TurtIeSocks@users.noreply.github.com> Date: Thu, 6 Oct 2022 13:31:35 -0400 Subject: [PATCH 3/3] support discord and telegram ids in sessions --- server/src/routes/api/v1/sessions.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/server/src/routes/api/v1/sessions.js b/server/src/routes/api/v1/sessions.js index d502f2f51..6bf079d35 100644 --- a/server/src/routes/api/v1/sessions.js +++ b/server/src/routes/api/v1/sessions.js @@ -27,10 +27,12 @@ router.get('/hasValid/:id', async (req, res) => { req.headers['react-map-secret'] === api.reactMapSecret ) { const results = await Session.query().whereRaw( - `json_extract(data, '$.passport.user.id') = ${req.params.id}`, + `json_extract(data, '$.passport.user.id') = ${req.params.id} + OR json_extract(data, '$.passport.user.discordId') = "${req.params.id}" + OR json_extract(data, '$.passport.user.telegramId') = "${req.params.id}"`, ) res.status(200).json({ - valid: Boolean(results.length), + valid: !!results.length, length: results.length, }) console.log(`[API] api/v1/sessions/hasValid/${req.params.id}`) @@ -50,7 +52,11 @@ router.get('/clearSessions/:id', async (req, res) => { req.headers['react-map-secret'] === api.reactMapSecret ) { const results = await Session.query() - .whereRaw(`json_extract(data, '$.passport.user.id') = ${req.params.id}`) + .whereRaw( + `json_extract(data, '$.passport.user.id') = ${req.params.id} + OR json_extract(data, '$.passport.user.discordId') = "${req.params.id}" + OR json_extract(data, '$.passport.user.telegramId') = "${req.params.id}"`, + ) .delete() res.status(200).json({ results }) console.log(`[API] api/v1/sessions/clearSessions/${req.params.id}`)