Move helper modules

This commit is contained in:
Henrik Hüttemann 2023-06-07 18:08:30 +02:00
parent ce5a668f1c
commit 98fe90264f
No known key found for this signature in database
GPG Key ID: 9F7BD10E0A8A111E
6 changed files with 21 additions and 23 deletions

View File

@ -19,7 +19,7 @@ Export them to `inputs/`
```shell
docker-compose run --rm -e SYNAPSE_SERVER_NAME=my.matrix.host -e SYNAPSE_REPORT_STATS=no synapse generate
docker-compose up -d
# Register an admin user
# Wait for the Server to boot, then register an admin user
docker-compose exec -it synapse register_new_matrix_user http://localhost:8008 -c /data/homeserver.yaml --admin --user verdiadmin --password verdiadmin
```

View File

@ -21,7 +21,7 @@
"lint-fix": "eslint src/ --fix --ext .ts",
"prefix": "npm run format-fix",
"fix": "npm run lint-fix",
"test": "jest",
"test": "rm -rf dist/ && jest",
"compile": "rm -rf dist/ && tsc",
"start": "npm run compile && node dist/app.js",
"prepare": "husky install"

View File

@ -2,23 +2,15 @@ import dotenv from 'dotenv'
dotenv.config()
import lineByLine from 'n-readlines'
import 'reflect-metadata'
import { DataSource } from 'typeorm'
import { IdMapping } from './entity/IdMapping'
import { Membership } from './entity/Membership'
import log from './logger'
import { whoami } from './synapse'
import log from './helpers/logger'
import { whoami } from './helpers/synapse'
import { RcUser, createUser } from './users'
import { getMapping, save, setMapping } from './helpers/storage'
log.info('rocketchat2matrix starts.')
const AppDataSource = new DataSource({
type: 'sqlite',
database: 'db.sqlite',
entities: [IdMapping, Membership],
synchronize: true,
logging: false,
})
const enum Entities {
Users = 'users',
Rooms = 'rooms',
@ -65,10 +57,7 @@ async function loadRcExport(entity: Entities) {
break
}
let mapping = await AppDataSource.manager.findOneBy(IdMapping, {
rcId: rcUser._id,
type: 0,
})
let mapping = await getMapping(rcUser._id, entities[entity].mappingType)
if (mapping && mapping.matrixId) {
log.debug('Mapping exists:', mapping)
} else {
@ -79,7 +68,7 @@ async function loadRcExport(entity: Entities) {
mapping.type = 0
mapping.accessToken = matrixUser.access_token
AppDataSource.manager.save(mapping)
await save(mapping)
log.debug('Mapping added:', mapping)
// Add user to room mapping (specific to users)
@ -88,7 +77,7 @@ async function loadRcExport(entity: Entities) {
membership.rcRoomId = rcRoomId
membership.rcUserId = rcUser._id
await AppDataSource.manager.save(membership)
await save(membership)
log.debug(`${rcUser.username} membership for ${rcRoomId} created`)
})
}

View File

@ -1,9 +1,10 @@
import { access_token } from './config/synapse_access_token.json'
import { access_token } from '../config/synapse_access_token.json'
import axios from 'axios'
import log from './logger'
import { getAccessToken } from './storage'
axios.defaults.baseURL = 'http://localhost:8008'
axios.defaults.headers.common['Authorization'] = ` Bearer ${access_token}`
axios.defaults.headers.common['Authorization'] = `Bearer ${access_token}`
axios.defaults.headers.post['Content-Type'] = 'application/json'
export { default as axios } from 'axios'
@ -20,3 +21,11 @@ export const whoami = () =>
reject()
})
})
export async function getUserSessionOptions(id: string) {
const accessToken = await getAccessToken(id)
if (!accessToken) {
throw new Error(`Could not retrieve access token for ID ${id}`)
}
return { headers: { Authorization: `Bearer ${accessToken}` } }
}

View File

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