Skip to content

Commit

Permalink
chore(Mattermost): Catch notification errors (#10729)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dschoordsch authored Jan 23, 2025
1 parent cd3506d commit 8e0502e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ REDIS_URL='redis://localhost:6379'
# Disable the built in Mattermost webhook integration
# MATTERMOST_DISABLED='false'
# For private instances with SSO and the Mattermost plugin, set the secret and URL
# MATTERMOST_SECRET=''
# MATTERMOST_URL=''
# MATTERMOST_SECRET='key_MATTERMOST_SECRET'
# MATTERMOST_URL='https://mattermost.example.com'
# MSTEAMS_DISABLED='false'

# MAIL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import {
makeHackedFieldButtonValue
} from './makeMattermostAttachments'

const MATTERMOST_URL = process.env.MATTERMOST_URL && new URL(process.env.MATTERMOST_URL)
const MATTERMOST_SECRET = process.env.MATTERMOST_SECRET

const notifyMattermost = async (
event: SlackNotification['event'],
channel: {webhookUrl: string | null; serverBaseUrl: string | null; sharedSecret: string | null},
Expand Down Expand Up @@ -358,13 +361,13 @@ async function getMattermost(
userId: string,
event: NotificationSettings['event']
) {
if (process.env.MATTERMOST_SECRET && process.env.MATTERMOST_URL) {
if (MATTERMOST_SECRET && MATTERMOST_URL) {
return [
MattermostNotificationHelper({
userId,
teamId,
serverBaseUrl: process.env.MATTERMOST_URL,
sharedSecret: process.env.MATTERMOST_SECRET,
serverBaseUrl: MATTERMOST_URL.toString(),
sharedSecret: MATTERMOST_SECRET,
webhookUrl: null
})
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {getTeamPromptResponsesByMeetingId} from '../../../../postgres/queries/getTeamPromptResponsesByMeetingIds'
import {SlackNotification} from '../../../../postgres/types'
import sendToSentry from '../../../../utils/sendToSentry'
import {DataLoaderWorker} from '../../../graphql'
import {NotificationIntegration, NotifyResponse} from './NotificationIntegrationHelper'

Expand Down Expand Up @@ -57,19 +58,30 @@ async function loadMeetingTeam(dataLoader: DataLoaderWorker, meetingId: string,
}
}

const fireAndForget = (
notifiers: NotificationIntegration[],
call: (notifier: NotificationIntegration) => Promise<NotifyResponse> | undefined
) => {
notifiers.forEach((notifier) => {
call(notifier)?.catch((e) => {
sendToSentry(e)
})
})
}

export const createNotifier = (loader: NotificationIntegrationLoader): Notifier => ({
async startMeeting(dataLoader: DataLoaderWorker, meetingId: string, teamId: string) {
const {meeting, team, user} = await loadMeetingTeam(dataLoader, meetingId, teamId)
if (!meeting || !team || !user) return
const notifiers = await loader(dataLoader, team.id, meeting.facilitatorUserId!, 'meetingStart')
notifiers.forEach((notifier) => notifier.startMeeting(meeting, team, user))
fireAndForget(notifiers, (notifier) => notifier.startMeeting(meeting, team, user))
},

async updateMeeting(dataLoader: DataLoaderWorker, meetingId: string, teamId: string) {
const {meeting, team, user} = await loadMeetingTeam(dataLoader, meetingId, teamId)
if (!meeting || !team || !user) return
const notifiers = await loader(dataLoader, team.id, meeting.facilitatorUserId!, 'meetingStart')
notifiers.forEach((notifier) => notifier.updateMeeting?.(meeting, team, user))
fireAndForget(notifiers, (notifier) => notifier.updateMeeting?.(meeting, team, user))
},

async endMeeting(dataLoader: DataLoaderWorker, meetingId: string, teamId: string) {
Expand All @@ -86,7 +98,9 @@ export const createNotifier = (loader: NotificationIntegrationLoader): Notifier
})
)
const notifiers = await loader(dataLoader, team.id, meeting.facilitatorUserId!, 'meetingEnd')
notifiers.forEach((notifier) => notifier.endMeeting(meeting, team, user, standupResponses))
fireAndForget(notifiers, (notifier) =>
notifier.endMeeting(meeting, team, user, standupResponses)
)
},

async startTimeLimit(
Expand All @@ -103,7 +117,9 @@ export const createNotifier = (loader: NotificationIntegrationLoader): Notifier
meeting.facilitatorUserId!,
'MEETING_STAGE_TIME_LIMIT_START'
)
notifiers.forEach((notifier) => notifier.startTimeLimit(scheduledEndTime, meeting, team, user))
fireAndForget(notifiers, (notifier) =>
notifier.startTimeLimit(scheduledEndTime, meeting, team, user)
)
},

async endTimeLimit(dataLoader: DataLoaderWorker, meetingId: string, teamId: string) {
Expand All @@ -115,14 +131,14 @@ export const createNotifier = (loader: NotificationIntegrationLoader): Notifier
meeting.facilitatorUserId!,
'MEETING_STAGE_TIME_LIMIT_END'
)
notifiers.forEach((notifier) => notifier.endTimeLimit(meeting, team, user))
fireAndForget(notifiers, (notifier) => notifier.endTimeLimit(meeting, team, user))
},

async integrationUpdated(dataLoader: DataLoaderWorker, teamId: string, userId: string) {
const notifiers = await loader(dataLoader, teamId, userId, 'meetingEnd')
const user = await dataLoader.get('users').load(userId)
if (!user) return
notifiers.forEach((notifier) => notifier.integrationUpdated(user))
fireAndForget(notifiers, (notifier) => notifier.integrationUpdated(user))
},

async standupResponseSubmitted(
Expand All @@ -144,7 +160,7 @@ export const createNotifier = (loader: NotificationIntegrationLoader): Notifier
meeting.facilitatorUserId!,
'STANDUP_RESPONSE_SUBMITTED'
)
notifiers.forEach((notifier) =>
fireAndForget(notifiers, (notifier) =>
notifier.standupResponseSubmitted(meeting, team, user, response)
)
}
Expand Down

0 comments on commit 8e0502e

Please sign in to comment.