Move handlers and clean up

This commit is contained in:
Henrik Hüttemann 2023-06-07 18:23:20 +02:00
parent ae3353c6cb
commit 4692c7edea
No known key found for this signature in database
GPG Key ID: 9F7BD10E0A8A111E
7 changed files with 30 additions and 15 deletions

View File

@ -4,10 +4,10 @@ import lineByLine from 'n-readlines'
import 'reflect-metadata'
import { IdMapping } from './entity/IdMapping'
import { Membership } from './entity/Membership'
import { RcUser, createUser } from './handlers/users'
import log from './helpers/logger'
import { getMapping, initStorage, save } from './helpers/storage'
import { whoami } from './helpers/synapse'
import { RcUser, createUser } from './users'
import { getMapping, save, setMapping } from './helpers/storage'
log.info('rocketchat2matrix starts.')
@ -101,7 +101,7 @@ async function loadRcExport(entity: Entities) {
async function main() {
try {
await whoami()
await AppDataSource.initialize()
await initStorage()
await loadRcExport(Entities.Users)
log.info('Done.')
} catch (error) {

View File

@ -1,6 +1,5 @@
import log from './helpers/logger'
import { getAccessToken } from './helpers/storage'
import { axios, getUserSessionOptions } from './helpers/synapse'
import log from '../helpers/logger'
import { axios, getUserSessionOptions } from '../helpers/synapse'
import { RcUser } from './users'
export const enum RcRoomTypes {
@ -36,7 +35,7 @@ export type MatrixRoom = {
topic?: string
is_direct?: boolean
preset?: MatrixRoomPresets
_creatorId?: string
_creatorId: string
}
export function mapRoom(rcRoom: RcRoom): MatrixRoom {
@ -44,6 +43,7 @@ export function mapRoom(rcRoom: RcRoom): MatrixRoom {
creation_content: {
'm.federate': false,
},
_creatorId: '',
}
rcRoom.name && (room.name = rcRoom.name)
rcRoom.name && (room.room_alias_name = rcRoom.name)
@ -53,17 +53,17 @@ export function mapRoom(rcRoom: RcRoom): MatrixRoom {
case 'd':
room.is_direct = true
room.preset = MatrixRoomPresets.trusted
room._creatorId = rcRoom.uids?.[0]
room._creatorId = rcRoom.uids?.[0] || ''
break
case 'c':
room.preset = MatrixRoomPresets.public
room._creatorId = rcRoom.u?._id
room._creatorId = rcRoom.u?._id || ''
break
case 'p':
room.preset = MatrixRoomPresets.private
room._creatorId = rcRoom.u?._id
room._creatorId = rcRoom.u?._id || ''
break
default:
@ -71,6 +71,11 @@ export function mapRoom(rcRoom: RcRoom): MatrixRoom {
log.error(message)
throw new Error(message)
}
if (!room._creatorId) {
const message = `Creator ID could not be determined for room of type ${rcRoom.t}`
log.error(message)
throw new Error(message)
}
return room
}
@ -80,7 +85,7 @@ export async function createRoom(rcRoom: RcRoom): Promise<MatrixRoom> {
await axios.post(
'/_matrix/client/v3/createRoom',
room,
await getUserSessionOptions(room._creatorId!)
await getUserSessionOptions(room._creatorId)
)
).data.room_id

View File

@ -1,6 +1,12 @@
process.env.REGISTRATION_SHARED_SECRET = 'ThisIsSoSecretWow'
import axios from 'axios'
import { MatrixUser, RcUser, createUser, generateHmac, mapUser } from './users'
import {
MatrixUser,
RcUser,
createUser,
generateHmac,
mapUser,
} from '../handlers/users'
jest.mock('axios')
const mockedAxios = axios as jest.Mocked<typeof axios>

View File

@ -1,6 +1,6 @@
import log from './helpers/logger'
import { axios } from './helpers/synapse'
import { createHmac } from 'node:crypto'
import log from '../helpers/logger'
import { axios } from '../helpers/synapse'
export type RcUser = {
_id: string

View File

@ -10,6 +10,10 @@ const AppDataSource = new DataSource({
logging: false,
})
export async function initStorage() {
await AppDataSource.initialize()
}
export function getMapping(id: string, type: number) {
return AppDataSource.manager.findOneBy(IdMapping, {
rcId: id,

View File

@ -1,5 +1,5 @@
import { access_token } from '../config/synapse_access_token.json'
import axios from 'axios'
import { access_token } from '../config/synapse_access_token.json'
import log from './logger'
import { getAccessToken } from './storage'