Add user creation to workflow

This commit is contained in:
Henrik Hüttemann 2023-05-24 18:21:33 +02:00
parent 62d6b5b30b
commit efdd2c933f
No known key found for this signature in database
GPG Key ID: 9F7BD10E0A8A111E
2 changed files with 16 additions and 7 deletions

View File

@ -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<void> {
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<void> {
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<void> {
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)

View File

@ -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<MatrixUser> {
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
}