From efdd2c933f856b5039a79866862339488277f302 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20H=C3=BCttemann?= Date: Wed, 24 May 2023 18:21:33 +0200 Subject: [PATCH] Add user creation to workflow --- src/app.ts | 12 ++++++++---- src/users.ts | 11 ++++++++--- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/app.ts b/src/app.ts index 2d88190..4e15ac7 100644 --- a/src/app.ts +++ b/src/app.ts @@ -3,7 +3,7 @@ import readline from 'node:readline' import log from './logger' import { whoami } from './synapse' import { storage } from './storage' -import { RcUser } from './users' +import { RcUser, createUser } from './users' log.info('rocketchat2matrix starts.') @@ -21,7 +21,7 @@ function loadRcExport(entity: Entities): Promise { crlfDelay: Infinity, }) - rl.on('line', (line) => { + rl.on('line', async (line) => { const item = JSON.parse(line) switch (entity) { case Entities.Users: @@ -29,7 +29,10 @@ function loadRcExport(entity: Entities): Promise { log.info(`User: ${rcUser.name}: ${rcUser._id}`) // Check for exclusion - if (storage.exclusionsLists.users.includes(rcUser._id)) { + if ( + rcUser.roles.some((e) => ['app', 'bot'].includes(e)) || + storage.exclusionsLists.users.includes(rcUser._id) + ) { log.debug('User excluded. Skipping.') break } @@ -38,9 +41,10 @@ function loadRcExport(entity: Entities): Promise { if (userMapping && userMapping.matrixId) { log.debug('Mapping exists:', userMapping) } else { + const matrixUser = await createUser(rcUser) userMapping = { rcId: rcUser._id, - matrixId: `@${rcUser.username}:localhost`, // Create user on synapse + matrixId: matrixUser.user_id, } storage.users.push(userMapping) // Save new mapping log.debug('Mapping added:', userMapping) diff --git a/src/users.ts b/src/users.ts index e85a5ce..aac8145 100644 --- a/src/users.ts +++ b/src/users.ts @@ -1,3 +1,4 @@ +import log from './logger' import { axios } from './synapse' import { createHmac } from 'node:crypto' @@ -10,12 +11,12 @@ export type RcUser = { } export type MatrixUser = { - user_id?: string - nonce?: string + user_id: string username: string displayname: string - password?: string + password: string admin: boolean + nonce?: string mac?: string } @@ -56,6 +57,10 @@ export async function createUser(rcUser: RcUser): Promise { user.nonce = await getUserRegistrationNonce() user.mac = generateHmac(user) user.user_id = await registerUser(user) + log.info(`User ${rcUser.username} created:`, user) + + delete user.nonce + delete user.mac return user }