Add members to channel if they posted a message
This commit is contained in:
parent
d2543dca2f
commit
a7afe7b570
@ -1,8 +1,17 @@
|
|||||||
|
import { AxiosError } from 'axios'
|
||||||
import { Entity, entities } from '../Entities'
|
import { Entity, entities } from '../Entities'
|
||||||
import { IdMapping } from '../entity/IdMapping'
|
import { IdMapping } from '../entity/IdMapping'
|
||||||
import log from '../helpers/logger'
|
import log from '../helpers/logger'
|
||||||
import { getMessageId, getRoomId, getUserId, save } from '../helpers/storage'
|
import {
|
||||||
|
getMapping,
|
||||||
|
getMappingByMatrixId,
|
||||||
|
getMessageId,
|
||||||
|
getRoomId,
|
||||||
|
getUserId,
|
||||||
|
save,
|
||||||
|
} from '../helpers/storage'
|
||||||
import { axios, formatUserSessionOptions } from '../helpers/synapse'
|
import { axios, formatUserSessionOptions } from '../helpers/synapse'
|
||||||
|
import { acceptInvitation, inviteMember } from './rooms'
|
||||||
|
|
||||||
const applicationServiceToken = process.env.AS_TOKEN || ''
|
const applicationServiceToken = process.env.AS_TOKEN || ''
|
||||||
if (!applicationServiceToken) {
|
if (!applicationServiceToken) {
|
||||||
@ -134,6 +143,7 @@ export async function handle(rcMessage: RcMessage): Promise<void> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
const event_id = await createMessage(
|
const event_id = await createMessage(
|
||||||
matrixMessage,
|
matrixMessage,
|
||||||
room_id,
|
room_id,
|
||||||
@ -141,6 +151,58 @@ export async function handle(rcMessage: RcMessage): Promise<void> {
|
|||||||
ts,
|
ts,
|
||||||
rcMessage._id
|
rcMessage._id
|
||||||
)
|
)
|
||||||
|
|
||||||
createMapping(rcMessage._id, event_id)
|
createMapping(rcMessage._id, event_id)
|
||||||
|
} catch (error) {
|
||||||
|
if (
|
||||||
|
error instanceof AxiosError &&
|
||||||
|
error.response &&
|
||||||
|
error.response.data.errcode === 'M_FORBIDDEN' &&
|
||||||
|
error.response.data.error === `User ${user_id} not in room ${room_id}`
|
||||||
|
) {
|
||||||
|
log.info(error.response.data.error + ', adding.')
|
||||||
|
|
||||||
|
const userMapping = await getMapping(
|
||||||
|
rcMessage.u._id,
|
||||||
|
entities[Entity.Users].mappingType
|
||||||
|
)
|
||||||
|
if (!userMapping || !userMapping.matrixId || !userMapping.accessToken) {
|
||||||
|
log.warn(`Could not determine joining user, skipping.`, rcMessage)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get room creator session or use empty axios options
|
||||||
|
let userSessionOptions = {}
|
||||||
|
const roomCreatorId = (
|
||||||
|
await axios.get(`/_synapse/admin/v1/rooms/${room_id}`)
|
||||||
|
).data.creator
|
||||||
|
if (!roomCreatorId) {
|
||||||
|
log.warn(
|
||||||
|
`Could not determine room creator for room ${room_id}, using admin credentials.`
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
const creatorMapping = await getMappingByMatrixId(roomCreatorId)
|
||||||
|
if (!creatorMapping?.accessToken) {
|
||||||
|
log.warn(`Could not access token for ${roomCreatorId}, skipping.`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
userSessionOptions = formatUserSessionOptions(
|
||||||
|
creatorMapping.accessToken
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
await inviteMember(userMapping.matrixId, room_id, userSessionOptions)
|
||||||
|
await acceptInvitation(userMapping, room_id)
|
||||||
|
|
||||||
|
const event_id = await createMessage(
|
||||||
|
matrixMessage,
|
||||||
|
room_id,
|
||||||
|
user_id,
|
||||||
|
ts,
|
||||||
|
rcMessage._id
|
||||||
|
)
|
||||||
|
createMapping(rcMessage._id, event_id)
|
||||||
|
} else {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user