Draft user workflow

This commit is contained in:
Henrik Hüttemann 2023-05-23 15:19:19 +02:00
parent 15697f3897
commit 59dbce5672
No known key found for this signature in database
GPG Key ID: 9F7BD10E0A8A111E
2 changed files with 42 additions and 12 deletions

View File

@ -12,7 +12,15 @@ const enum Entities {
Messages = 'rocketchat_message.json',
}
function loadRcExport(entity: Entities) {
type RcUser = {
username: string
name: string
roles: string[]
_id: string
__rooms: string[]
}
function loadRcExport(entity: Entities): Promise<void> {
const rl = readline.createInterface({
input: fs.createReadStream(`./inputs/${entity}`, {
encoding: 'utf-8',
@ -24,26 +32,43 @@ function loadRcExport(entity: Entities) {
const item = JSON.parse(line)
switch (entity) {
case Entities.Users:
log.info(`User: ${item.name}: ${item._id}`)
const rcUser: RcUser = item
log.info(`User: ${rcUser.name}: ${rcUser._id}`)
// Check for exclusion
if (storage.exclusionsLists.users.includes(item._id)) {
if (storage.exclusionsLists.users.includes(rcUser._id)) {
log.debug('User excluded. Skipping.')
break
}
// Lookup
let userMapping = storage.users.find((e) => e.rcId === item._id)
let userMapping = storage.users.find((e) => e.rcId === rcUser._id) // Lookup mapping
if (userMapping) {
log.debug('Mapping exists:', userMapping)
} else {
userMapping = {
rcId: item._id,
matrixId: `@${item.username}:localhost`,
rcRooms: item.__rooms,
rcId: rcUser._id,
matrixId: `@${rcUser.username}:localhost`,
}
storage.users.push(userMapping)
storage.users.push(userMapping) // Save new mapping
log.debug('Mapping added:', userMapping)
// Add user to room mapping
rcUser.__rooms.forEach((rcRoomId: string) => {
const roomIndex = storage.rooms.findIndex(
(e) => e.rcId === rcRoomId
)
if (roomIndex >= 0) {
storage.rooms[roomIndex].members.push(rcUser._id)
log.debug(`Membership of ${rcUser.username} in ${rcRoomId} saved`)
} else {
storage.rooms.push({
rcId: rcRoomId,
matrixId: '',
members: [],
})
log.debug(`${rcUser.username} membership for ${rcRoomId} created`)
}
})
}
break
@ -60,12 +85,18 @@ function loadRcExport(entity: Entities) {
throw new Error(`Unhandled Entity: ${entity}`)
}
})
return new Promise((resolve, reject) => {
rl.on('close', () => {
resolve()
})
})
}
async function main() {
try {
await whoami()
await loadRcExport(Entities.Users)
log.info('Done.')
} catch (error) {
log.error(`Encountered an error booting up`)
}

View File

@ -1,12 +1,12 @@
export interface storage {
export type storage = {
users: {
rcId: string
matrixId: string
rcRooms: string[]
}[]
rooms: {
rcId: string
matrixId: string
members: string[]
}[]
messages: {
rcId: string
@ -24,7 +24,6 @@ export const storage: storage = {
{
rcId: '2ziHK8P748TeESitX',
matrixId: '@herhde:locahlost',
rcRooms: [],
},
],
rooms: [],