Add helper functions to get ID mappings
This commit is contained in:
parent
6225cdc8bb
commit
3e1169235c
21
src/app.ts
21
src/app.ts
@ -8,6 +8,8 @@ import log from './helpers/logger'
|
|||||||
import {
|
import {
|
||||||
createMembership,
|
createMembership,
|
||||||
getMapping,
|
getMapping,
|
||||||
|
getRoomId,
|
||||||
|
getUserId,
|
||||||
initStorage,
|
initStorage,
|
||||||
save,
|
save,
|
||||||
} from './helpers/storage'
|
} from './helpers/storage'
|
||||||
@ -62,12 +64,12 @@ async function loadRcExport(entity: Entities) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
let mapping = await getMapping(rcUser._id, entities[entity].mappingType)
|
const matrixUserId = await getUserId(rcUser._id)
|
||||||
if (mapping && mapping.matrixId) {
|
if (matrixUserId) {
|
||||||
log.debug('Mapping exists:', mapping)
|
log.debug(`Mapping exists: ${rcUser._id} -> ${matrixUserId}`)
|
||||||
} else {
|
} else {
|
||||||
const matrixUser = await createUser(rcUser)
|
const matrixUser = await createUser(rcUser)
|
||||||
mapping = new IdMapping()
|
const mapping = new IdMapping()
|
||||||
mapping.rcId = rcUser._id
|
mapping.rcId = rcUser._id
|
||||||
mapping.matrixId = matrixUser.user_id
|
mapping.matrixId = matrixUser.user_id
|
||||||
mapping.type = entities[entity].mappingType
|
mapping.type = entities[entity].mappingType
|
||||||
@ -91,15 +93,12 @@ async function loadRcExport(entity: Entities) {
|
|||||||
const rcRoom: RcRoom = item
|
const rcRoom: RcRoom = item
|
||||||
log.info(`Parsing room ${rcRoom.name || 'with ID: ' + rcRoom._id}`)
|
log.info(`Parsing room ${rcRoom.name || 'with ID: ' + rcRoom._id}`)
|
||||||
|
|
||||||
let roomMapping = await getMapping(
|
const matrixRoomId = await getRoomId(rcRoom._id)
|
||||||
rcRoom._id,
|
if (matrixRoomId) {
|
||||||
entities[entity].mappingType
|
log.debug(`Mapping exists: ${rcRoom._id} -> ${matrixRoomId}`)
|
||||||
)
|
|
||||||
if (roomMapping && roomMapping.matrixId) {
|
|
||||||
log.debug('Mapping exists:', roomMapping)
|
|
||||||
} else {
|
} else {
|
||||||
const matrixRoom = await createRoom(rcRoom)
|
const matrixRoom = await createRoom(rcRoom)
|
||||||
roomMapping = new IdMapping()
|
const roomMapping = new IdMapping()
|
||||||
roomMapping.rcId = rcRoom._id
|
roomMapping.rcId = rcRoom._id
|
||||||
roomMapping.matrixId = matrixRoom.room_id
|
roomMapping.matrixId = matrixRoom.room_id
|
||||||
roomMapping.type = entities[entity].mappingType
|
roomMapping.type = entities[entity].mappingType
|
||||||
|
|||||||
@ -5,6 +5,9 @@ import {
|
|||||||
getAccessToken,
|
getAccessToken,
|
||||||
getMapping,
|
getMapping,
|
||||||
getMemberships,
|
getMemberships,
|
||||||
|
getMessageId,
|
||||||
|
getRoomId,
|
||||||
|
getUserId,
|
||||||
initStorage,
|
initStorage,
|
||||||
save,
|
save,
|
||||||
} from './storage'
|
} from './storage'
|
||||||
@ -60,3 +63,30 @@ test('get membership', async () => {
|
|||||||
|
|
||||||
await expect(getMemberships('inexistent')).resolves.toStrictEqual([])
|
await expect(getMemberships('inexistent')).resolves.toStrictEqual([])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('get member by id', async () => {
|
||||||
|
await expect(getUserId(mapping.rcId)).resolves.toBe(mapping.matrixId)
|
||||||
|
await expect(getUserId('inexistent')).resolves.toBeFalsy()
|
||||||
|
})
|
||||||
|
|
||||||
|
test('get room by id', async () => {
|
||||||
|
const room = new IdMapping()
|
||||||
|
room.rcId = 'rcRoom'
|
||||||
|
room.matrixId = 'matrixRoom'
|
||||||
|
room.type = 1
|
||||||
|
await save(room)
|
||||||
|
|
||||||
|
await expect(getRoomId(room.rcId)).resolves.toBe(room.matrixId)
|
||||||
|
await expect(getRoomId('inexistent')).resolves.toBeFalsy()
|
||||||
|
})
|
||||||
|
|
||||||
|
test('get message by id', async () => {
|
||||||
|
const message = new IdMapping()
|
||||||
|
message.rcId = 'rcMessage'
|
||||||
|
message.matrixId = 'matrixMessage'
|
||||||
|
message.type = 2
|
||||||
|
await save(message)
|
||||||
|
|
||||||
|
await expect(getMessageId(message.rcId)).resolves.toBe(message.matrixId)
|
||||||
|
await expect(getMessageId('inexistent')).resolves.toBeFalsy()
|
||||||
|
})
|
||||||
|
|||||||
@ -55,3 +55,15 @@ export async function getMemberships(rcRoomId: string): Promise<string[]> {
|
|||||||
})
|
})
|
||||||
).map((entity) => entity.rcUserId)
|
).map((entity) => entity.rcUserId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getUserId(rcId: string): Promise<string | undefined> {
|
||||||
|
return (await getMapping(rcId, 0))?.matrixId
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getRoomId(rcId: string): Promise<string | undefined> {
|
||||||
|
return (await getMapping(rcId, 1))?.matrixId
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getMessageId(rcId: string): Promise<string | undefined> {
|
||||||
|
return (await getMapping(rcId, 2))?.matrixId
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user