Draft user workflow
This commit is contained in:
parent
15697f3897
commit
59dbce5672
49
src/app.ts
49
src/app.ts
@ -12,7 +12,15 @@ const enum Entities {
|
|||||||
Messages = 'rocketchat_message.json',
|
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({
|
const rl = readline.createInterface({
|
||||||
input: fs.createReadStream(`./inputs/${entity}`, {
|
input: fs.createReadStream(`./inputs/${entity}`, {
|
||||||
encoding: 'utf-8',
|
encoding: 'utf-8',
|
||||||
@ -24,26 +32,43 @@ function loadRcExport(entity: Entities) {
|
|||||||
const item = JSON.parse(line)
|
const item = JSON.parse(line)
|
||||||
switch (entity) {
|
switch (entity) {
|
||||||
case Entities.Users:
|
case Entities.Users:
|
||||||
log.info(`User: ${item.name}: ${item._id}`)
|
const rcUser: RcUser = item
|
||||||
|
log.info(`User: ${rcUser.name}: ${rcUser._id}`)
|
||||||
|
|
||||||
// Check for exclusion
|
// Check for exclusion
|
||||||
if (storage.exclusionsLists.users.includes(item._id)) {
|
if (storage.exclusionsLists.users.includes(rcUser._id)) {
|
||||||
log.debug('User excluded. Skipping.')
|
log.debug('User excluded. Skipping.')
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lookup
|
let userMapping = storage.users.find((e) => e.rcId === rcUser._id) // Lookup mapping
|
||||||
let userMapping = storage.users.find((e) => e.rcId === item._id)
|
|
||||||
if (userMapping) {
|
if (userMapping) {
|
||||||
log.debug('Mapping exists:', userMapping)
|
log.debug('Mapping exists:', userMapping)
|
||||||
} else {
|
} else {
|
||||||
userMapping = {
|
userMapping = {
|
||||||
rcId: item._id,
|
rcId: rcUser._id,
|
||||||
matrixId: `@${item.username}:localhost`,
|
matrixId: `@${rcUser.username}:localhost`,
|
||||||
rcRooms: item.__rooms,
|
|
||||||
}
|
}
|
||||||
storage.users.push(userMapping)
|
storage.users.push(userMapping) // Save new mapping
|
||||||
log.debug('Mapping added:', userMapping)
|
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
|
break
|
||||||
@ -60,12 +85,18 @@ function loadRcExport(entity: Entities) {
|
|||||||
throw new Error(`Unhandled Entity: ${entity}`)
|
throw new Error(`Unhandled Entity: ${entity}`)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
rl.on('close', () => {
|
||||||
|
resolve()
|
||||||
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
try {
|
try {
|
||||||
await whoami()
|
await whoami()
|
||||||
await loadRcExport(Entities.Users)
|
await loadRcExport(Entities.Users)
|
||||||
|
log.info('Done.')
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log.error(`Encountered an error booting up`)
|
log.error(`Encountered an error booting up`)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,12 @@
|
|||||||
export interface storage {
|
export type storage = {
|
||||||
users: {
|
users: {
|
||||||
rcId: string
|
rcId: string
|
||||||
matrixId: string
|
matrixId: string
|
||||||
rcRooms: string[]
|
|
||||||
}[]
|
}[]
|
||||||
rooms: {
|
rooms: {
|
||||||
rcId: string
|
rcId: string
|
||||||
matrixId: string
|
matrixId: string
|
||||||
|
members: string[]
|
||||||
}[]
|
}[]
|
||||||
messages: {
|
messages: {
|
||||||
rcId: string
|
rcId: string
|
||||||
@ -24,7 +24,6 @@ export const storage: storage = {
|
|||||||
{
|
{
|
||||||
rcId: '2ziHK8P748TeESitX',
|
rcId: '2ziHK8P748TeESitX',
|
||||||
matrixId: '@herhde:locahlost',
|
matrixId: '@herhde:locahlost',
|
||||||
rcRooms: [],
|
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
rooms: [],
|
rooms: [],
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user