diff --git a/src/handlers/users.ts b/src/handlers/users.ts index 90a7dbf..33a0da5 100644 --- a/src/handlers/users.ts +++ b/src/handlers/users.ts @@ -32,6 +32,12 @@ export type AccessToken = { user_id: string } +export type UserInfo = { + admin: boolean + displayname: string + name: string +} + export function mapUser(rcUser: RcUser): MatrixUser { return { user_id: '', @@ -75,10 +81,8 @@ async function registerUser(user: MatrixUser): Promise { return (await axios.post('/_synapse/admin/v1/register', user)).data } -async function userAlreadyExists(user: MatrixUser): Promise { - const user_data = (await axios.get('/_synapse/admin/v2/users/@' + user.username + ":" + getUserDomain())).data - log.debug(`Mapping exists:`, user_data) - return true +async function getUserData(user: MatrixUser): Promise { + return (await axios.get('/_synapse/admin/v2/users/@' + user.username + ":" + getUserDomain())).data } async function parseUserMemberships(rcUser: RcUser): Promise { @@ -123,9 +127,11 @@ export async function createMapping( export async function createUser(rcUser: RcUser): Promise { const user = mapUser(rcUser) - if (await userAlreadyExists(user)) { - log.info(`User ${rcUser.username} exists:`, user) - } else { + try { + const user_data = await getUserData(user) + + log.info(`User ${rcUser.username} exists:`, user_data) + } catch (error) { const nonce = await getUserRegistrationNonce() const mac = generateHmac({ ...user, nonce }) const accessToken = await registerUser({ ...user, nonce, mac })