Add sqlite database for ID mappings
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
3d0f505961
commit
c9537ab909
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -133,3 +133,5 @@ dist
|
|||
files/
|
||||
src/config/synapse_access_token.json
|
||||
inputs/
|
||||
db.sqlite
|
||||
db.sqlite-journal
|
||||
|
|
2601
package-lock.json
generated
2601
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
|
34
src/app.ts
34
src/app.ts
|
@ -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
13
src/entity/IdMapping.ts
Normal 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
|
||||
}
|
|
@ -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*'. */
|
||||
|
|
Loading…
Reference in a new issue