Add function to find user by RC username
This commit is contained in:
parent
ed4c9689ec
commit
ca7787a8eb
@ -8,15 +8,12 @@ import {
|
|||||||
getMessageId,
|
getMessageId,
|
||||||
getRoomId,
|
getRoomId,
|
||||||
getUserId,
|
getUserId,
|
||||||
|
getUserMappingByName,
|
||||||
save,
|
save,
|
||||||
} from '../helpers/storage'
|
} from '../helpers/storage'
|
||||||
import {
|
import { axios, formatUserSessionOptions } from '../helpers/synapse'
|
||||||
axios,
|
|
||||||
formatUserSessionOptions,
|
|
||||||
getUserSessionOptions,
|
|
||||||
} from '../helpers/synapse'
|
|
||||||
import { acceptInvitation, inviteMember } from './rooms'
|
|
||||||
import reactionKeys from '../reactions.json'
|
import reactionKeys from '../reactions.json'
|
||||||
|
import { acceptInvitation, inviteMember } from './rooms'
|
||||||
|
|
||||||
const applicationServiceToken = process.env.AS_TOKEN || ''
|
const applicationServiceToken = process.env.AS_TOKEN || ''
|
||||||
if (!applicationServiceToken) {
|
if (!applicationServiceToken) {
|
||||||
@ -109,6 +106,7 @@ export async function handleReactions(
|
|||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
for (const [reaction, value] of Object.entries(reactions)) {
|
for (const [reaction, value] of Object.entries(reactions)) {
|
||||||
// Lookup key/emoji
|
// Lookup key/emoji
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
const reactionKey = (reactionKeys as any)[reaction]
|
const reactionKey = (reactionKeys as any)[reaction]
|
||||||
value.usernames.map(async (rcUsername: string) => {
|
value.usernames.map(async (rcUsername: string) => {
|
||||||
// generate transaction id
|
// generate transaction id
|
||||||
@ -116,7 +114,19 @@ export async function handleReactions(
|
|||||||
[matrixMessageId, reaction, rcUsername].join('\0')
|
[matrixMessageId, reaction, rcUsername].join('\0')
|
||||||
).toString('base64')
|
).toString('base64')
|
||||||
// lookup user access token
|
// lookup user access token
|
||||||
const userSessionOptions = await getUserSessionOptions(rcUsername)
|
const userMapping = await getUserMappingByName(rcUsername)
|
||||||
|
if (!userMapping) {
|
||||||
|
throw new Error(`Could not find user mapping for name: ${rcUsername}`)
|
||||||
|
}
|
||||||
|
if (!userMapping.accessToken) {
|
||||||
|
throw new Error(
|
||||||
|
`User mapping for name ${rcUsername} has no access token`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const userSessionOptions = formatUserSessionOptions(
|
||||||
|
userMapping.accessToken
|
||||||
|
)
|
||||||
log.http(
|
log.http(
|
||||||
`Adding reaction to message ${matrixMessageId} with symbol ${reactionKey} for user ${rcUsername}`
|
`Adding reaction to message ${matrixMessageId} with symbol ${reactionKey} for user ${rcUsername}`
|
||||||
)
|
)
|
||||||
@ -259,7 +269,7 @@ export async function handle(rcMessage: RcMessage): Promise<void> {
|
|||||||
`Parsing reactions for message ${rcMessage._id}`,
|
`Parsing reactions for message ${rcMessage._id}`,
|
||||||
rcMessage.reactions
|
rcMessage.reactions
|
||||||
)
|
)
|
||||||
await handleReactions(rcMessage, event_id, room_id)
|
await handleReactions(rcMessage.reactions, event_id, room_id)
|
||||||
}
|
}
|
||||||
await createMapping(rcMessage._id, event_id)
|
await createMapping(rcMessage._id, event_id)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -315,7 +325,7 @@ export async function handle(rcMessage: RcMessage): Promise<void> {
|
|||||||
`Parsing reactions for message ${rcMessage._id}`,
|
`Parsing reactions for message ${rcMessage._id}`,
|
||||||
rcMessage.reactions
|
rcMessage.reactions
|
||||||
)
|
)
|
||||||
await handleReactions(rcMessage, event_id, room_id)
|
await handleReactions(rcMessage.reactions, event_id, room_id)
|
||||||
}
|
}
|
||||||
await createMapping(rcMessage._id, event_id)
|
await createMapping(rcMessage._id, event_id)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { DataSource } from 'typeorm'
|
import { DataSource, ILike } from 'typeorm'
|
||||||
import { Entity, entities } from '../Entities'
|
import { Entity, entities } from '../Entities'
|
||||||
import { IdMapping } from '../entity/IdMapping'
|
import { IdMapping } from '../entity/IdMapping'
|
||||||
import { Membership } from '../entity/Membership'
|
import { Membership } from '../entity/Membership'
|
||||||
@ -35,6 +35,15 @@ export function getMappingByMatrixId(id: string): Promise<IdMapping | null> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getUserMappingByName(
|
||||||
|
username: string
|
||||||
|
): Promise<IdMapping | null> {
|
||||||
|
return AppDataSource.manager.findOneBy(IdMapping, {
|
||||||
|
matrixId: ILike(`@${username.toLowerCase()}:%`),
|
||||||
|
type: entities[Entity.Users].mappingType,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
export async function save(entity: IdMapping | Membership): Promise<void> {
|
export async function save(entity: IdMapping | Membership): Promise<void> {
|
||||||
await AppDataSource.manager.save(entity)
|
await AppDataSource.manager.save(entity)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user