Skip to content

Commit

Permalink
feat : added loggers to send calls and renamed fillTables to fillTabl…
Browse files Browse the repository at this point in the history
…esAndSend
  • Loading branch information
BichraiX committed Jul 22, 2024
1 parent 88aa4ac commit 9709354
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import type MatrixClientServer from '../../../index'
import Mailer from '../../../utils/mailer'
import {
fillTable,
fillTableAndSend,
getSubmitUrl,
preConfigureTemplate
} from '../../../register/email/requestToken'
Expand Down Expand Up @@ -55,23 +55,38 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => {
const dst = (obj as RequestTokenArgs).email
const nextLink = (obj as RequestTokenArgs).next_link
if (!clientSecretRe.test(clientSecret)) {
send(res, 400, errMsg('invalidParam', 'invalid client_secret'))
send(
res,
400,
errMsg('invalidParam', 'invalid client_secret'),
clientServer.logger
)
} else if (!validEmailRe.test(dst)) {
send(res, 400, errMsg('invalidEmail'))
send(res, 400, errMsg('invalidEmail'), clientServer.logger)
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
} else if (nextLink && !isValidUrl(nextLink)) {
send(res, 400, errMsg('invalidParam', 'invalid next_link'))
send(
res,
400,
errMsg('invalidParam', 'invalid next_link'),
clientServer.logger
)
} else if (
typeof sendAttempt !== 'number' ||
sendAttempt > maxAttemps
) {
send(res, 400, errMsg('invalidParam', 'Invalid send attempt'))
send(
res,
400,
errMsg('invalidParam', 'Invalid send attempt'),
clientServer.logger
)
} else {
clientServer.matrixDb
.get('user_threepids', ['user_id'], { address: dst })
.then((rows) => {
if (rows.length === 0) {
send(res, 400, errMsg('threepidNotFound'))
send(res, 400, errMsg('threepidNotFound'), clientServer.logger)
} else {
clientServer.matrixDb
.get(
Expand All @@ -85,10 +100,15 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => {
.then((rows) => {
if (rows.length > 0) {
if (sendAttempt === rows[0].last_send_attempt) {
send(res, 200, {
sid: rows[0].session_id,
submit_url: getSubmitUrl(clientServer.conf)
})
send(
res,
200,
{
sid: rows[0].session_id,
submit_url: getSubmitUrl(clientServer.conf)
},
clientServer.logger
)
} else {
clientServer.matrixDb
.deleteWhere('threepid_validation_session', [
Expand All @@ -104,7 +124,7 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => {
}
])
.then(() => {
fillTable(
fillTableAndSend(
// The calls to send are made in this function
clientServer,
dst,
Expand All @@ -121,11 +141,16 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => {
// istanbul ignore next
clientServer.logger.error('Deletion error', err)
// istanbul ignore next
send(res, 500, errMsg('unknown', err))
send(
res,
500,
errMsg('unknown', err),
clientServer.logger
)
})
}
} else {
fillTable(
fillTableAndSend(
// The calls to send are made in this function
clientServer,
dst,
Expand All @@ -143,15 +168,15 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => {
/* istanbul ignore next */
clientServer.logger.error('Send_attempt error', err)
/* istanbul ignore next */
send(res, 500, errMsg('unknown', err))
send(res, 500, errMsg('unknown', err), clientServer.logger)
})
}
})
.catch((err) => {
/* istanbul ignore next */
clientServer.logger.error('Error getting userID :', err)
/* istanbul ignore next */
send(res, 500, errMsg('unknown', err))
send(res, 500, errMsg('unknown', err), clientServer.logger)
})
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type MatrixClientServer from '../../../index'
import SmsSender from '../../../utils/smsSender'
import { getSubmitUrl } from '../../../register/email/requestToken'
import {
fillTable,
fillTableAndSend,
formatPhoneNumber,
preConfigureTemplate
} from '../../../register/msisdn/requestToken'
Expand Down Expand Up @@ -61,25 +61,50 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => {
const dst = formatPhoneNumber(phoneNumber, country)
const nextLink = (obj as RequestTokenArgs).next_link
if (!clientSecretRegex.test(clientSecret)) {
send(res, 400, errMsg('invalidParam', 'Invalid client_secret'))
send(
res,
400,
errMsg('invalidParam', 'Invalid client_secret'),
clientServer.logger
)
} else if (!validCountryRegex.test(country)) {
send(res, 400, errMsg('invalidParam', 'Invalid country'))
send(
res,
400,
errMsg('invalidParam', 'Invalid country'),
clientServer.logger
)
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
} else if (nextLink && !isValidUrl(nextLink)) {
send(res, 400, errMsg('invalidParam', 'Invalid next_link'))
send(
res,
400,
errMsg('invalidParam', 'Invalid next_link'),
clientServer.logger
)
} else if (!validPhoneNumberRegex.test(dst)) {
send(res, 400, errMsg('invalidParam', 'Invalid phone number'))
send(
res,
400,
errMsg('invalidParam', 'Invalid phone number'),
clientServer.logger
)
} else if (
typeof sendAttempt !== 'number' ||
sendAttempt > maxAttemps
) {
send(res, 400, errMsg('invalidParam', 'Invalid send attempt'))
send(
res,
400,
errMsg('invalidParam', 'Invalid send attempt'),
clientServer.logger
)
} else {
clientServer.matrixDb
.get('user_threepids', ['user_id'], { address: dst })
.then((rows) => {
if (rows.length === 0) {
send(res, 400, errMsg('threepidNotFound'))
send(res, 400, errMsg('threepidNotFound'), clientServer.logger)
} else {
clientServer.matrixDb
.get(
Expand All @@ -93,10 +118,15 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => {
.then((rows) => {
if (rows.length > 0) {
if (sendAttempt === rows[0].last_send_attempt) {
send(res, 200, {
sid: rows[0].session_id,
submit_url: getSubmitUrl(clientServer.conf)
})
send(
res,
200,
{
sid: rows[0].session_id,
submit_url: getSubmitUrl(clientServer.conf)
},
clientServer.logger
)
} else {
clientServer.matrixDb
.deleteWhere('threepid_validation_session', [
Expand All @@ -112,7 +142,7 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => {
}
])
.then(() => {
fillTable(
fillTableAndSend(
// The calls to send are made in this function
clientServer,
dst,
Expand All @@ -129,11 +159,16 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => {
// istanbul ignore next
clientServer.logger.error('Deletion error:', err)
// istanbul ignore next
send(res, 500, errMsg('unknown', err))
send(
res,
500,
errMsg('unknown', err),
clientServer.logger
)
})
}
} else {
fillTable(
fillTableAndSend(
// The calls to send are made in this function
clientServer,
dst,
Expand All @@ -151,15 +186,15 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => {
/* istanbul ignore next */
clientServer.logger.error('Send_attempt error:', err)
/* istanbul ignore next */
send(res, 500, errMsg('unknown', err))
send(res, 500, errMsg('unknown', err), clientServer.logger)
})
}
})
.catch((err) => {
/* istanbul ignore next */
clientServer.logger.error('Error getting userID :', err)
/* istanbul ignore next */
send(res, 500, errMsg('unknown', err))
send(res, 500, errMsg('unknown', err), clientServer.logger)
})
}
})
Expand Down
66 changes: 48 additions & 18 deletions packages/matrix-client-server/src/register/email/requestToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const mailBody = (
)
}

export const fillTable = (
export const fillTableAndSend = (
clientServer: MatrixClientServer,
dst: string,
clientSecret: string,
Expand All @@ -118,20 +118,25 @@ export const fillTable = (
last_send_attempt: sendAttempt
})
.then(() => {
send(res, 200, { sid, submit_url: getSubmitUrl(clientServer.conf) })
send(
res,
200,
{ sid, submit_url: getSubmitUrl(clientServer.conf) },
clientServer.logger
)
})
.catch((err) => {
// istanbul ignore next
clientServer.logger.error('Insertion error', err)
// istanbul ignore next
send(res, 500, errMsg('unknown', err))
send(res, 500, errMsg('unknown', err), clientServer.logger)
})
})
.catch((err) => {
/* istanbul ignore next */
clientServer.logger.error('Token error', err)
/* istanbul ignore next */
send(res, 500, errMsg('unknown', err))
send(res, 500, errMsg('unknown', err), clientServer.logger)
})
}

Expand All @@ -152,23 +157,38 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => {
const dst = (obj as RequestTokenArgs).email
const nextLink = (obj as RequestTokenArgs).next_link
if (!clientSecretRe.test(clientSecret)) {
send(res, 400, errMsg('invalidParam', 'invalid client_secret'))
send(
res,
400,
errMsg('invalidParam', 'invalid client_secret'),
clientServer.logger
)
} else if (!validEmailRe.test(dst)) {
send(res, 400, errMsg('invalidEmail'))
send(res, 400, errMsg('invalidEmail'), clientServer.logger)
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
} else if (nextLink && !isValidUrl(nextLink)) {
send(res, 400, errMsg('invalidParam', 'invalid next_link'))
send(
res,
400,
errMsg('invalidParam', 'invalid next_link'),
clientServer.logger
)
} else if (
typeof sendAttempt !== 'number' ||
sendAttempt > maxAttemps
) {
send(res, 400, errMsg('invalidParam', 'Invalid send attempt'))
send(
res,
400,
errMsg('invalidParam', 'Invalid send attempt'),
clientServer.logger
)
} else {
clientServer.matrixDb
.get('user_threepids', ['user_id'], { address: dst })
.then((rows) => {
if (rows.length > 0) {
send(res, 400, errMsg('threepidInUse'))
send(res, 400, errMsg('threepidInUse'), clientServer.logger)
} else {
clientServer.matrixDb
.get(
Expand All @@ -182,10 +202,15 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => {
.then((rows) => {
if (rows.length > 0) {
if (sendAttempt === rows[0].last_send_attempt) {
send(res, 200, {
sid: rows[0].session_id,
submit_url: getSubmitUrl(clientServer.conf)
})
send(
res,
200,
{
sid: rows[0].session_id,
submit_url: getSubmitUrl(clientServer.conf)
},
clientServer.logger
)
} else {
clientServer.matrixDb
.deleteWhere('threepid_validation_session', [
Expand All @@ -201,7 +226,7 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => {
}
])
.then(() => {
fillTable(
fillTableAndSend(
// The calls to send are made in this function
clientServer,
dst,
Expand All @@ -218,11 +243,16 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => {
// istanbul ignore next
clientServer.logger.error('Deletion error', err)
// istanbul ignore next
send(res, 500, errMsg('unknown', err))
send(
res,
500,
errMsg('unknown', err),
clientServer.logger
)
})
}
} else {
fillTable(
fillTableAndSend(
// The calls to send are made in this function
clientServer,
dst,
Expand All @@ -240,15 +270,15 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => {
/* istanbul ignore next */
clientServer.logger.error('Send_attempt error', err)
/* istanbul ignore next */
send(res, 500, errMsg('unknown', err))
send(res, 500, errMsg('unknown', err), clientServer.logger)
})
}
})
.catch((err) => {
/* istanbul ignore next */
clientServer.logger.error('Error getting userID :', err)
/* istanbul ignore next */
send(res, 500, errMsg('unknown', err))
send(res, 500, errMsg('unknown', err), clientServer.logger)
})
}
})
Expand Down
Loading

0 comments on commit 9709354

Please sign in to comment.