-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat : added /available endpoint and related tests
- Loading branch information
Showing
4 changed files
with
532 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,7 +38,7 @@ | |
"namespaces": { | ||
"users": [ | ||
{ | ||
"exclusive": false, | ||
"exclusive": true, | ||
"regex": "@_irc_bridge_.*" | ||
} | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { errMsg, send, type expressAppHandler } from '@twake/utils' | ||
import type MatrixClientServer from '..' | ||
|
||
interface Parameters { | ||
username: string | ||
} | ||
|
||
const matrixIdRegex = /^@[0-9a-zA-Z._=-]+:[0-9a-zA-Z.-]+$/ | ||
|
||
const available = (clientServer: MatrixClientServer): expressAppHandler => { | ||
return (req, res) => { | ||
// @ts-expect-error req has query | ||
const userId = (req.query as Parameters).username | ||
if (!matrixIdRegex.test(userId)) { | ||
clientServer.logger.error('Invalid user ID') | ||
send( | ||
res, | ||
400, | ||
errMsg('invalidParam', 'Invalid user ID'), | ||
clientServer.logger | ||
) | ||
return | ||
} | ||
for (const appService of clientServer.conf.application_services) { | ||
if ( | ||
// eslint-disable-next-line @typescript-eslint/prefer-optional-chain | ||
appService.namespaces.users !== undefined && | ||
appService.namespaces.users !== null && | ||
appService.namespaces.users.some( | ||
(namespace) => | ||
new RegExp(namespace.regex).test(userId) && namespace.exclusive | ||
) | ||
) { | ||
send( | ||
res, | ||
400, | ||
errMsg( | ||
'exclusive', | ||
'The desired username is in the exclusive namespace claimed by an application service.' | ||
), | ||
clientServer.logger | ||
) | ||
return | ||
} | ||
clientServer.matrixDb | ||
.get('users', ['name'], { name: userId }) | ||
.then((rows) => { | ||
if (rows.length > 0) { | ||
send(res, 400, errMsg('userInUse'), clientServer.logger) | ||
} else { | ||
send(res, 200, { available: true }, clientServer.logger) | ||
} | ||
}) | ||
.catch((e) => { | ||
// istanbul ignore next | ||
clientServer.logger.error('Error while checking user availability', e) | ||
// istanbul ignore next | ||
send(res, 500, e, clientServer.logger) | ||
}) | ||
} | ||
} | ||
} | ||
|
||
export default available |
Oops, something went wrong.