From 5e66b2c72287e8cfbc04cb8388f56fd822abfdd2 Mon Sep 17 00:00:00 2001 From: Pierre Ozoux Date: Tue, 9 Apr 2024 17:11:16 +0200 Subject: [PATCH] feat: ease usage of a another matrix username --- src/entity/IdMapping.ts | 3 +++ src/handlers/users.test.ts | 3 ++- src/handlers/users.ts | 11 ++++++----- src/helpers/storage.ts | 4 ++-- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/entity/IdMapping.ts b/src/entity/IdMapping.ts index 7a3bdc4..362273c 100644 --- a/src/entity/IdMapping.ts +++ b/src/entity/IdMapping.ts @@ -8,6 +8,9 @@ export class IdMapping { @Column() matrixId?: string // Matrix ID of the entity + @Column({ nullable: true }) + rcUsername?: string // rcUsername of the entity + @Column('integer') type!: number // Type of the entity; 0 = user, 1 = room, 2 = message diff --git a/src/handlers/users.test.ts b/src/handlers/users.test.ts index 9e8d0af..05368b2 100644 --- a/src/handlers/users.test.ts +++ b/src/handlers/users.test.ts @@ -102,9 +102,10 @@ test('users are excluded', () => { }) test('creating mapping', async () => { - await expect(createMapping(rcUser._id, matrixUser)).resolves.toBe(undefined) + await expect(createMapping(rcUser, matrixUser)).resolves.toBe(undefined) expect(mockedStorage.save).toHaveBeenCalledWith({ rcId: rcUser._id, + rcUsername: "testuser", matrixId: matrixUser.user_id, type: entities[Entity.Users].mappingType, accessToken: matrixUser.access_token, diff --git a/src/handlers/users.ts b/src/handlers/users.ts index 0849169..ab85d1b 100644 --- a/src/handlers/users.ts +++ b/src/handlers/users.ts @@ -130,12 +130,13 @@ export function userIsExcluded(rcUser: RcUser): boolean { * @param matrixUser The Matrix user object, including ID and access token */ export async function createMapping( - rcId: string, + rcUser: RcUser, matrixUser: MatrixUser ): Promise { const mapping = new IdMapping() - mapping.rcId = rcId + mapping.rcId = rcUser._id mapping.matrixId = matrixUser.user_id + mapping.rcUsername = rcUser.username mapping.type = entities[Entity.Users].mappingType mapping.accessToken = matrixUser.access_token @@ -167,7 +168,7 @@ export async function createUser(rcUser: RcUser): Promise { * @param rcUser The RC user to handle */ export async function handle(rcUser: RcUser): Promise { - log.info(`Parsing user: ${rcUser.name}: ${rcUser._id}`) + log.info(`Parsing user: ${rcUser.name}: ${rcUser._id} ${rcUser.username}`) const matrixId = await getUserId(rcUser._id) if (matrixId) { @@ -177,10 +178,10 @@ export async function handle(rcUser: RcUser): Promise { log.info( `User ${rcUser.username} is defined as admin in ENV, mapping as such` ) - await createMapping(rcUser._id, adminAccessToken as unknown as MatrixUser) + await createMapping(rcUser, adminAccessToken as unknown as MatrixUser) } else if (!userIsExcluded(rcUser)) { const matrixUser = await createUser(rcUser) - await createMapping(rcUser._id, matrixUser) + await createMapping(rcUser, matrixUser) } } } diff --git a/src/helpers/storage.ts b/src/helpers/storage.ts index 5fea94d..51a0ef0 100644 --- a/src/helpers/storage.ts +++ b/src/helpers/storage.ts @@ -58,14 +58,14 @@ export function getMappingByMatrixId( /** * Search for a user IdMapping by its name - * @param username The Matrix or Rocket.Chat username to look up + * @param username The Rocket.Chat username to look up * @returns One found IdMapping or null */ export function getUserMappingByName( username: string ): Promise { return AppDataSource.manager.findOneBy(IdMapping, { - matrixId: ILike(`@${username.toLowerCase()}:%`), + rcUsername: ILike(`${username.toLowerCase()}`), type: entities[Entity.Users].mappingType, }) }