Add helper functions to get ID mappings

This commit is contained in:
Henrik Hüttemann 2023-06-22 15:43:40 +02:00
parent 6225cdc8bb
commit 3e1169235c
No known key found for this signature in database
GPG Key ID: 9F7BD10E0A8A111E
3 changed files with 52 additions and 11 deletions

View File

@ -8,6 +8,8 @@ import log from './helpers/logger'
import {
createMembership,
getMapping,
getRoomId,
getUserId,
initStorage,
save,
} from './helpers/storage'
@ -62,12 +64,12 @@ async function loadRcExport(entity: Entities) {
break
}
let mapping = await getMapping(rcUser._id, entities[entity].mappingType)
if (mapping && mapping.matrixId) {
log.debug('Mapping exists:', mapping)
const matrixUserId = await getUserId(rcUser._id)
if (matrixUserId) {
log.debug(`Mapping exists: ${rcUser._id} -> ${matrixUserId}`)
} else {
const matrixUser = await createUser(rcUser)
mapping = new IdMapping()
const mapping = new IdMapping()
mapping.rcId = rcUser._id
mapping.matrixId = matrixUser.user_id
mapping.type = entities[entity].mappingType
@ -91,15 +93,12 @@ async function loadRcExport(entity: Entities) {
const rcRoom: RcRoom = item
log.info(`Parsing room ${rcRoom.name || 'with ID: ' + rcRoom._id}`)
let roomMapping = await getMapping(
rcRoom._id,
entities[entity].mappingType
)
if (roomMapping && roomMapping.matrixId) {
log.debug('Mapping exists:', roomMapping)
const matrixRoomId = await getRoomId(rcRoom._id)
if (matrixRoomId) {
log.debug(`Mapping exists: ${rcRoom._id} -> ${matrixRoomId}`)
} else {
const matrixRoom = await createRoom(rcRoom)
roomMapping = new IdMapping()
const roomMapping = new IdMapping()
roomMapping.rcId = rcRoom._id
roomMapping.matrixId = matrixRoom.room_id
roomMapping.type = entities[entity].mappingType

View File

@ -5,6 +5,9 @@ import {
getAccessToken,
getMapping,
getMemberships,
getMessageId,
getRoomId,
getUserId,
initStorage,
save,
} from './storage'
@ -60,3 +63,30 @@ test('get membership', async () => {
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()
})

View File

@ -55,3 +55,15 @@ export async function getMemberships(rcRoomId: string): Promise<string[]> {
})
).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
}