Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ease usage of a another matrix username #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/entity/IdMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion src/handlers/users.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 6 additions & 5 deletions src/handlers/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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

Expand Down Expand Up @@ -167,7 +168,7 @@ export async function createUser(rcUser: RcUser): Promise<MatrixUser> {
* @param rcUser The RC user to handle
*/
export async function handle(rcUser: RcUser): Promise<void> {
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) {
Expand All @@ -177,10 +178,10 @@ export async function handle(rcUser: RcUser): Promise<void> {
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)
}
}
}
4 changes: 2 additions & 2 deletions src/helpers/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IdMapping | null> {
return AppDataSource.manager.findOneBy(IdMapping, {
matrixId: ILike(`@${username.toLowerCase()}:%`),
rcUsername: ILike(`${username.toLowerCase()}`),
type: entities[Entity.Users].mappingType,
})
}
Expand Down