--wip-- [skip ci]
This commit is contained in:
parent
91fa37f82d
commit
ef86d6e369
28
src/app.ts
28
src/app.ts
@ -8,6 +8,7 @@ import { RcUser, createUser } from './handlers/users'
|
|||||||
import log from './helpers/logger'
|
import log from './helpers/logger'
|
||||||
import { getMapping, initStorage, save } from './helpers/storage'
|
import { getMapping, initStorage, save } from './helpers/storage'
|
||||||
import { whoami } from './helpers/synapse'
|
import { whoami } from './helpers/synapse'
|
||||||
|
import { RcRoom, createRoom } from './handlers/rooms'
|
||||||
|
|
||||||
log.info('rocketchat2matrix starts.')
|
log.info('rocketchat2matrix starts.')
|
||||||
|
|
||||||
@ -65,7 +66,7 @@ async function loadRcExport(entity: Entities) {
|
|||||||
mapping = new IdMapping()
|
mapping = new IdMapping()
|
||||||
mapping.rcId = rcUser._id
|
mapping.rcId = rcUser._id
|
||||||
mapping.matrixId = matrixUser.user_id
|
mapping.matrixId = matrixUser.user_id
|
||||||
mapping.type = 0
|
mapping.type = entities[entity].mappingType
|
||||||
mapping.accessToken = matrixUser.access_token
|
mapping.accessToken = matrixUser.access_token
|
||||||
|
|
||||||
await save(mapping)
|
await save(mapping)
|
||||||
@ -85,7 +86,25 @@ async function loadRcExport(entity: Entities) {
|
|||||||
break
|
break
|
||||||
|
|
||||||
case Entities.Rooms:
|
case Entities.Rooms:
|
||||||
log.debug(`Room: ${item.name}`)
|
const rcRoom: RcRoom = item
|
||||||
|
log.debug(`Room: ${rcRoom.name}`, rcRoom)
|
||||||
|
|
||||||
|
let roomMapping = await getMapping(
|
||||||
|
rcRoom._id,
|
||||||
|
entities[entity].mappingType
|
||||||
|
)
|
||||||
|
if (roomMapping && roomMapping.matrixId) {
|
||||||
|
log.debug('Mapping exists:', roomMapping)
|
||||||
|
} else {
|
||||||
|
const matrixRoom = await createRoom(rcRoom)
|
||||||
|
roomMapping = new IdMapping()
|
||||||
|
roomMapping.rcId = rcRoom._id
|
||||||
|
roomMapping.matrixId = matrixRoom.room_id
|
||||||
|
roomMapping.type = entities[entity].mappingType
|
||||||
|
|
||||||
|
await save(roomMapping)
|
||||||
|
log.debug('Mapping added:', roomMapping)
|
||||||
|
}
|
||||||
break
|
break
|
||||||
|
|
||||||
case Entities.Messages:
|
case Entities.Messages:
|
||||||
@ -102,10 +121,11 @@ async function main() {
|
|||||||
try {
|
try {
|
||||||
await whoami()
|
await whoami()
|
||||||
await initStorage()
|
await initStorage()
|
||||||
await loadRcExport(Entities.Users)
|
// await loadRcExport(Entities.Users)
|
||||||
|
await loadRcExport(Entities.Rooms)
|
||||||
log.info('Done.')
|
log.info('Done.')
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log.error(`Encountered an error while booting up`)
|
log.error(`Encountered an error while booting up: ${error}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -35,7 +35,7 @@ export type MatrixRoom = {
|
|||||||
topic?: string
|
topic?: string
|
||||||
is_direct?: boolean
|
is_direct?: boolean
|
||||||
preset?: MatrixRoomPresets
|
preset?: MatrixRoomPresets
|
||||||
_creatorId: string
|
_creatorId?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export function mapRoom(rcRoom: RcRoom): MatrixRoom {
|
export function mapRoom(rcRoom: RcRoom): MatrixRoom {
|
||||||
@ -72,22 +72,33 @@ export function mapRoom(rcRoom: RcRoom): MatrixRoom {
|
|||||||
throw new Error(message)
|
throw new Error(message)
|
||||||
}
|
}
|
||||||
if (!room._creatorId) {
|
if (!room._creatorId) {
|
||||||
const message = `Creator ID could not be determined for room of type ${rcRoom.t}`
|
log.warn(
|
||||||
log.error(message)
|
`Creator ID could not be determined for room ${rcRoom.name} of type ${rcRoom.t}.`
|
||||||
throw new Error(message)
|
)
|
||||||
}
|
}
|
||||||
return room
|
return room
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function createRoom(rcRoom: RcRoom): Promise<MatrixRoom> {
|
export async function createRoom(rcRoom: RcRoom): Promise<MatrixRoom> {
|
||||||
const room: MatrixRoom = mapRoom(rcRoom)
|
const room: MatrixRoom = mapRoom(rcRoom)
|
||||||
|
let sessionOptions = {}
|
||||||
|
if (room._creatorId) {
|
||||||
|
try {
|
||||||
|
sessionOptions = await getUserSessionOptions(room._creatorId)
|
||||||
|
log.debug('Room user session generated:', sessionOptions)
|
||||||
|
} catch (error) {
|
||||||
|
log.warn(error)
|
||||||
|
// TODO: Skip room, if it has 0-1 member or is a direct chat?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.debug('Creating room:', room)
|
||||||
|
delete room._creatorId
|
||||||
|
|
||||||
room.room_id = (
|
room.room_id = (
|
||||||
await axios.post(
|
await axios.post('/_matrix/client/v3/createRoom', room, sessionOptions)
|
||||||
'/_matrix/client/v3/createRoom',
|
|
||||||
room,
|
|
||||||
await getUserSessionOptions(room._creatorId)
|
|
||||||
)
|
|
||||||
).data.room_id
|
).data.room_id
|
||||||
|
|
||||||
|
// TODO: Add members
|
||||||
|
|
||||||
return room
|
return room
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user