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

Fix to continue with iteration when no access token is present #17

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
103 changes: 53 additions & 50 deletions src/handlers/directChats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,62 +107,65 @@ export async function setDirectChats(

// Iterate over all users
for (const [user, chats] of Object.entries(userDirectChatMappings)) {
const userSessionOptions = formatUserSessionOptions(
(await getMappingByMatrixId(user))?.accessToken || ''
)

// Check if direct chats are already set
let settingExists = false
try {
const currentDirectChats = (
await axios.get(
`/_matrix/client/v3/user/${user}/account_data/m.direct`,
userSessionOptions
)
).data

settingExists =
currentDirectChats && Object.keys(currentDirectChats).length > 0

if (settingExists) {
if (JSON.stringify(currentDirectChats) !== JSON.stringify(chats)) {
// If chats are already set, but different, log the difference
log.debug(`User ${user} already has a different direct chat setting.`)
log.debug('Expected:', chats)
log.debug('Actual:', currentDirectChats)
} else {
// If chats are already set, but equal, log it
log.debug(
`User ${user} already has the expected direct chats configured, skipping.`
const accessToken = (await getMappingByMatrixId(user))?.accessToken || ''
if (accessToken.length > 0) {
const userSessionOptions = formatUserSessionOptions(accessToken)

// Check if direct chats are already set
let settingExists = false
try {
const currentDirectChats = (
await axios.get(
`/_matrix/client/v3/user/${user}/account_data/m.direct`,
userSessionOptions
)
).data

settingExists =
currentDirectChats && Object.keys(currentDirectChats).length > 0

if (settingExists) {
if (JSON.stringify(currentDirectChats) !== JSON.stringify(chats)) {
// If chats are already set, but different, log the difference
log.debug(
`User ${user} already has a different direct chat setting.`
)
log.debug('Expected:', chats)
log.debug('Actual:', currentDirectChats)
} else {
// If chats are already set, but equal, log it
log.debug(
`User ${user} already has the expected direct chats configured, skipping.`
)
}
}
} catch (error) {
// Catch errors if setting does not exist, yet
if (
!(
error instanceof AxiosError &&
error.response &&
error.response.data.errcode === 'M_NOT_FOUND'
)
) {
throw error
}
}
} catch (error) {
// Catch errors if setting does not exist, yet
if (
!(
error instanceof AxiosError &&
error.response &&
error.response.data.errcode === 'M_NOT_FOUND'

if (!settingExists) {
// Set direct chats if there are non set, yet
await axios.put(
`/_matrix/client/v3/user/${user}/account_data/m.direct`,
chats,
userSessionOptions
)
log.debug(
`Set ${
Object.keys(chats).length
} chats as direct chats for user ${user}`
)
) {
throw error
}
}

if (!settingExists) {
// Set direct chats if there are non set, yet
await axios.put(
`/_matrix/client/v3/user/${user}/account_data/m.direct`,
chats,
userSessionOptions
)
log.debug(
`Set ${
Object.keys(chats).length
} chats as direct chats for user ${user}`
)
}
}
}

Expand Down