Add sqlite database for ID mappings
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Henrik HerHde Huettemann 2023-05-26 14:47:46 +02:00
parent 3d0f505961
commit c9537ab909
Signed by: HueHe
GPG key ID: 9F7BD10E0A8A111E
6 changed files with 2560 additions and 97 deletions

2
.gitignore vendored
View file

@ -133,3 +133,5 @@ dist
files/
src/config/synapse_access_token.json
inputs/
db.sqlite
db.sqlite-journal

2601
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -46,6 +46,9 @@
"dependencies": {
"axios": "^1.4.0",
"dotenv": "^16.0.3",
"reflect-metadata": "^0.1.13",
"sqlite3": "^5.1.6",
"typeorm": "^0.3.16",
"winston": "^3.8.2"
}
}

View file

@ -6,9 +6,20 @@ import readline from 'node:readline'
import { RcUser, createUser } from './users'
import { storage } from './storage'
import { whoami } from './synapse'
import 'reflect-metadata'
import { DataSource } from 'typeorm'
import { IdMapping } from './entity/IdMapping'
log.info('rocketchat2matrix starts.')
const AppDataSource = new DataSource({
type: 'sqlite',
database: 'db.sqlite',
entities: [IdMapping],
synchronize: true,
logging: false,
})
const enum Entities {
Users = 'users.json',
Rooms = 'rocketchat_room.json',
@ -39,17 +50,21 @@ function loadRcExport(entity: Entities): Promise<void> {
break
}
let userMapping = storage.users.find((e) => e.rcId === rcUser._id) // Lookup mapping
if (userMapping && userMapping.matrixId) {
log.debug('Mapping exists:', userMapping)
let mapping = await AppDataSource.manager.findOneBy(IdMapping, {
rcId: rcUser._id,
type: 0,
})
if (mapping && mapping.matrixId) {
log.debug('Mapping exists:', mapping)
} else {
const matrixUser = await createUser(rcUser)
userMapping = {
rcId: rcUser._id,
matrixId: matrixUser.user_id,
}
storage.users.push(userMapping) // Save new mapping
log.debug('Mapping added:', userMapping)
mapping = new IdMapping()
mapping.rcId = rcUser._id
mapping.matrixId = matrixUser.user_id
mapping.type = 0
AppDataSource.manager.save(mapping) // Save new mapping
log.debug('Mapping added:', mapping)
// Add user to room mapping (specific to users)
rcUser.__rooms.forEach((rcRoomId: string) => {
@ -94,6 +109,7 @@ function loadRcExport(entity: Entities): Promise<void> {
async function main() {
try {
await whoami()
await AppDataSource.initialize()
await loadRcExport(Entities.Users)
log.info('Done.')
} catch (error) {

13
src/entity/IdMapping.ts Normal file
View file

@ -0,0 +1,13 @@
import { Entity, Column, PrimaryColumn } from 'typeorm'
@Entity()
export class IdMapping {
@PrimaryColumn()
rcId!: string
@Column()
matrixId?: string
@Column('integer')
type!: number
}

View file

@ -14,8 +14,8 @@
"target": "ES2022", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
"experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
"emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */