Skip to content

Commit

Permalink
Use user settings when publishing web push events
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebroberts committed Sep 11, 2024
1 parent 417fb11 commit 245633e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
40 changes: 40 additions & 0 deletions src/app/domain/user/userNotifyable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { AppState } from '../../environment/AppState'
import { GithubUserId, GithubWorkflowKey } from '../types/GithubKeys'
import { calculateUserSettings } from './calculatedUserSettings'
import { getUserSettings } from './persistedUserSettings'
import { getWorkflowsForUser } from './userVisible'
import { logger } from '../../util/logging'

export async function filterWorkflowNotifyEnabled(
appState: AppState,
userIds: GithubUserId[],
workflow: GithubWorkflowKey
): Promise<GithubUserId[]> {
const enabledUserIds = []
for (const userId of userIds) {
if (await getWorkflowNotifyEnabledForUser(appState, userId, workflow)) {
enabledUserIds.push(userId)
}
}
return enabledUserIds
}

export async function getWorkflowNotifyEnabledForUser(
appState: AppState,
userId: GithubUserId,
workflow: GithubWorkflowKey
) {
const userSettings = calculateUserSettings(
await getUserSettings(appState, userId),
await getWorkflowsForUser(appState, userId)
)
const yesNotify = userSettings.github.accounts
.get(workflow.ownerId)
?.repos.get(workflow.repoId)
?.workflows.get(workflow.workflowId)?.notify
if (yesNotify === undefined) {
logger.warn(`No calculated user notify setting for workflow`, { workflow, userSettings })
return false
}
return yesNotify
}
7 changes: 5 additions & 2 deletions src/app/domain/webPush/cicadaEventWebPushPublisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from '../github/githubWorkflowRunEvent'
import { isCicadaEventBridgeDetail } from '../../outboundInterfaces/eventBridgeBus'
import { CicadaWebNotification } from '../../outboundInterfaces/webPushWrapper'
import { filterWorkflowNotifyEnabled } from '../user/userNotifyable'

// TOEventually - these are going to create a lot of queries for subscription lookup for large organizations
// May be better to have one table / index for this.
Expand All @@ -24,10 +25,12 @@ export async function handleNewWorkflowRunEvent(appState: AppState, eventDetail:
}

const workflowRunEvent = eventDetail.data
const userIds = await getRelatedMemberIdsForRunEvent(appState, workflowRunEvent)
const notifyEnabledUserIds = await filterWorkflowNotifyEnabled(appState, userIds, workflowRunEvent)

await publishToSubscriptionsForUsers(
appState,
await getRelatedMemberIdsForRunEvent(appState, workflowRunEvent),
notifyEnabledUserIds,
generateRunEventNotification(workflowRunEvent)
)
}
Expand All @@ -45,7 +48,7 @@ export function generateRunEventNotification(
}
}

// TOEventually - add this back when notification preferences added
// TOEventually - add this back now that notification preferences added, maybe
// export async function handleNewPush(appState: AppState, eventDetail: unknown) {
// if (!isCicadaEventBridgeDetail(eventDetail) || !isGithubPush(eventDetail.data)) {
// logger.error(
Expand Down
7 changes: 6 additions & 1 deletion src/cdk/stacks/main/userFacingWeb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ function defineWebPushPublisher(scope: Construct, props: UserFacingWebEndpointsP
const lambdaFunction = new CicadaFunction(
scope,
cicadaFunctionProps(props, 'webPushPublisher', {
tablesReadAccess: ['web-push-subscriptions', 'github-account-memberships']
tablesReadAccess: [
'web-push-subscriptions',
'github-account-memberships',
'github-latest-workflow-runs',
'user-settings'
]
})
)
new Rule(scope, 'WebPushPublisherRule', {
Expand Down

0 comments on commit 245633e

Please sign in to comment.