Skip to content

Commit

Permalink
feat : added /available endpoint and related tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BichraiX committed Jul 22, 2024
1 parent bffa57f commit 4be8cbf
Show file tree
Hide file tree
Showing 4 changed files with 532 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"namespaces": {
"users": [
{
"exclusive": false,
"exclusive": true,
"regex": "@_irc_bridge_.*"
}
]
Expand Down
13 changes: 9 additions & 4 deletions packages/matrix-client-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import putStatus from './presence/putStatus'
import getLogin from './login/getLogin'
import add from './account/3pid/add'
import refresh from './refresh'
import available from './register/available'

const tables = {
ui_auth_sessions: 'session_id TEXT NOT NULL, stage_type TEXT NOT NULL'
Expand Down Expand Up @@ -158,7 +159,8 @@ export default class MatrixClientServer extends MatrixIdentityServer<clientDbCol
'/_matrix/client/v3/presence/:userId/status': getStatus(this),
'/_matrix/client/v3/login': getLogin(this),
'/_matrix/client/v3/account/3pid/add': badMethod,
'/_matrix/client/v3/refresh': badMethod
'/_matrix/client/v3/refresh': badMethod,
'/_matrix/client/v3/register/available': available(this)
}
this.api.post = {
'/_matrix/client/v3/account/whoami': badMethod,
Expand Down Expand Up @@ -197,7 +199,8 @@ export default class MatrixClientServer extends MatrixIdentityServer<clientDbCol
'/_matrix/client/v3/presence/:userId/status': badMethod,
'/_matrix/client/v3/login': badMethod,
'/_matrix/client/v3/account/3pid/add': add(this),
'/_matrix/client/v3/refresh': refresh(this)
'/_matrix/client/v3/refresh': refresh(this),
'/_matrix/client/v3/register/available': badMethod
}
this.api.put = {
'/_matrix/client/v3/account/whoami': badMethod,
Expand Down Expand Up @@ -236,7 +239,8 @@ export default class MatrixClientServer extends MatrixIdentityServer<clientDbCol
'/_matrix/client/v3/presence/:userId/status': putStatus(this),
'/_matrix/client/v3/login': badMethod,
'/_matrix/client/v3/account/3pid/add': badMethod,
'/_matrix/client/v3/refresh': badMethod
'/_matrix/client/v3/refresh': badMethod,
'/_matrix/client/v3/register/available': badMethod
}
this.api.delete = {
'/_matrix/client/v3/account/whoami': badMethod,
Expand Down Expand Up @@ -265,7 +269,8 @@ export default class MatrixClientServer extends MatrixIdentityServer<clientDbCol
'/_matrix/client/v3/presence/:userId/status': badMethod,
'/_matrix/client/v3/login': badMethod,
'/_matrix/client/v3/account/3pid/add': badMethod,
'/_matrix/client/v3/refresh': badMethod
'/_matrix/client/v3/refresh': badMethod,
'/_matrix/client/v3/register/available': badMethod
}
resolve(true)
})
Expand Down
64 changes: 64 additions & 0 deletions packages/matrix-client-server/src/register/available.ts
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
Loading

0 comments on commit 4be8cbf

Please sign in to comment.