Skip to content

Commit 245633e

Browse files
committed
Use user settings when publishing web push events
1 parent 417fb11 commit 245633e

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

src/app/domain/user/userNotifyable.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { AppState } from '../../environment/AppState'
2+
import { GithubUserId, GithubWorkflowKey } from '../types/GithubKeys'
3+
import { calculateUserSettings } from './calculatedUserSettings'
4+
import { getUserSettings } from './persistedUserSettings'
5+
import { getWorkflowsForUser } from './userVisible'
6+
import { logger } from '../../util/logging'
7+
8+
export async function filterWorkflowNotifyEnabled(
9+
appState: AppState,
10+
userIds: GithubUserId[],
11+
workflow: GithubWorkflowKey
12+
): Promise<GithubUserId[]> {
13+
const enabledUserIds = []
14+
for (const userId of userIds) {
15+
if (await getWorkflowNotifyEnabledForUser(appState, userId, workflow)) {
16+
enabledUserIds.push(userId)
17+
}
18+
}
19+
return enabledUserIds
20+
}
21+
22+
export async function getWorkflowNotifyEnabledForUser(
23+
appState: AppState,
24+
userId: GithubUserId,
25+
workflow: GithubWorkflowKey
26+
) {
27+
const userSettings = calculateUserSettings(
28+
await getUserSettings(appState, userId),
29+
await getWorkflowsForUser(appState, userId)
30+
)
31+
const yesNotify = userSettings.github.accounts
32+
.get(workflow.ownerId)
33+
?.repos.get(workflow.repoId)
34+
?.workflows.get(workflow.workflowId)?.notify
35+
if (yesNotify === undefined) {
36+
logger.warn(`No calculated user notify setting for workflow`, { workflow, userSettings })
37+
return false
38+
}
39+
return yesNotify
40+
}

src/app/domain/webPush/cicadaEventWebPushPublisher.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from '../github/githubWorkflowRunEvent'
1111
import { isCicadaEventBridgeDetail } from '../../outboundInterfaces/eventBridgeBus'
1212
import { CicadaWebNotification } from '../../outboundInterfaces/webPushWrapper'
13+
import { filterWorkflowNotifyEnabled } from '../user/userNotifyable'
1314

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

2627
const workflowRunEvent = eventDetail.data
28+
const userIds = await getRelatedMemberIdsForRunEvent(appState, workflowRunEvent)
29+
const notifyEnabledUserIds = await filterWorkflowNotifyEnabled(appState, userIds, workflowRunEvent)
2730

2831
await publishToSubscriptionsForUsers(
2932
appState,
30-
await getRelatedMemberIdsForRunEvent(appState, workflowRunEvent),
33+
notifyEnabledUserIds,
3134
generateRunEventNotification(workflowRunEvent)
3235
)
3336
}
@@ -45,7 +48,7 @@ export function generateRunEventNotification(
4548
}
4649
}
4750

48-
// TOEventually - add this back when notification preferences added
51+
// TOEventually - add this back now that notification preferences added, maybe
4952
// export async function handleNewPush(appState: AppState, eventDetail: unknown) {
5053
// if (!isCicadaEventBridgeDetail(eventDetail) || !isGithubPush(eventDetail.data)) {
5154
// logger.error(

src/cdk/stacks/main/userFacingWeb.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,12 @@ function defineWebPushPublisher(scope: Construct, props: UserFacingWebEndpointsP
8080
const lambdaFunction = new CicadaFunction(
8181
scope,
8282
cicadaFunctionProps(props, 'webPushPublisher', {
83-
tablesReadAccess: ['web-push-subscriptions', 'github-account-memberships']
83+
tablesReadAccess: [
84+
'web-push-subscriptions',
85+
'github-account-memberships',
86+
'github-latest-workflow-runs',
87+
'user-settings'
88+
]
8489
})
8590
)
8691
new Rule(scope, 'WebPushPublisherRule', {

0 commit comments

Comments
 (0)