From 245633e523fc732eb48cdfa8574918a57da8ca23 Mon Sep 17 00:00:00 2001 From: Mike Roberts Date: Wed, 11 Sep 2024 16:52:45 -0400 Subject: [PATCH] Use user settings when publishing web push events --- src/app/domain/user/userNotifyable.ts | 40 +++++++++++++++++++ .../webPush/cicadaEventWebPushPublisher.ts | 7 +++- src/cdk/stacks/main/userFacingWeb.ts | 7 +++- 3 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 src/app/domain/user/userNotifyable.ts diff --git a/src/app/domain/user/userNotifyable.ts b/src/app/domain/user/userNotifyable.ts new file mode 100644 index 0000000..735cdad --- /dev/null +++ b/src/app/domain/user/userNotifyable.ts @@ -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 { + 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 +} diff --git a/src/app/domain/webPush/cicadaEventWebPushPublisher.ts b/src/app/domain/webPush/cicadaEventWebPushPublisher.ts index 8891481..0d202ef 100644 --- a/src/app/domain/webPush/cicadaEventWebPushPublisher.ts +++ b/src/app/domain/webPush/cicadaEventWebPushPublisher.ts @@ -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. @@ -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) ) } @@ -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( diff --git a/src/cdk/stacks/main/userFacingWeb.ts b/src/cdk/stacks/main/userFacingWeb.ts index 3527d3a..32dd62b 100644 --- a/src/cdk/stacks/main/userFacingWeb.ts +++ b/src/cdk/stacks/main/userFacingWeb.ts @@ -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', {