@@ -18,16 +18,14 @@ import Network.HTTP.Types qualified as HTTP
1818import Share.BackgroundJobs.Errors (reportError )
1919import Share.BackgroundJobs.Monad (Background )
2020import Share.BackgroundJobs.Webhooks.Queries qualified as WQ
21- import Share.BackgroundJobs.Webhooks.Queries qualified as WebhookQ
22- import Share.BackgroundJobs.Webhooks.Types (WebhookPayloadData )
2321import Share.BackgroundJobs.Workers (newWorker )
2422import Share.Env qualified as Env
2523import Share.IDs (NotificationEventId , NotificationWebhookId )
2624import Share.JWT (JWTParam (.. ))
2725import Share.JWT qualified as JWT
2826import Share.Metrics qualified as Metrics
2927import Share.Notifications.Queries qualified as NQ
30- import Share.Notifications.Types (NotificationEvent (.. ), NotificationTopic , eventTopic )
28+ import Share.Notifications.Types (HydratedEventPayload , NotificationEvent (.. ), NotificationTopic , eventData_ , eventUserInfo_ , hydratedEventTopic )
3129import Share.Notifications.Webhooks.Secrets (WebhookConfig (.. ), WebhookSecretError )
3230import Share.Notifications.Webhooks.Secrets qualified as Webhooks
3331import Share.Postgres qualified as PG
@@ -36,6 +34,8 @@ import Share.Prelude
3634import Share.Utils.Logging qualified as Logging
3735import Share.Utils.URI (URIParam (.. ))
3836import Share.Web.Authorization qualified as AuthZ
37+ import Share.Web.Share.DisplayInfo.Queries qualified as DisplayInfoQ
38+ import Share.Web.Share.DisplayInfo.Types (UnifiedDisplayInfo )
3939import UnliftIO qualified
4040
4141data WebhookSendFailure
@@ -90,7 +90,7 @@ worker scope = do
9090 processWebhooks authZReceipt = do
9191 toIO <- UnliftIO. askRunInIO
9292 -- Need to unlift so we can use this in transactions
93- let tryWebhookIO eventId eventData webhookId = toIO $ tryWebhook eventId eventData webhookId
93+ let tryWebhookIO eventData webhookId = toIO $ tryWebhook eventData webhookId
9494 mayResult <- Metrics. recordWebhookSendingDuration $ PG. runTransaction $ runMaybeT $ do
9595 webhookInfo@ (eventId, webhookId) <- MaybeT WQ. getUnsentWebhook
9696 mayErr <- lift $ attemptWebhookSend authZReceipt tryWebhookIO eventId webhookId
@@ -127,7 +127,7 @@ data WebhookEventPayload jwt = WebhookEventPayload
127127 -- | The topic of the notification event.
128128 topic :: NotificationTopic ,
129129 -- | The data associated with the notification event.
130- data_ :: WebhookPayloadData ,
130+ data_ :: HydratedEventPayload ,
131131 -- | A signed token containing all of the same data.
132132 jwt :: jwt
133133 }
@@ -164,11 +164,10 @@ instance FromJSON (WebhookEventPayload ()) where
164164 <*> pure ()
165165
166166tryWebhook ::
167- NotificationEvent NotificationEventId UTCTime ->
168- WebhookPayloadData ->
167+ NotificationEvent NotificationEventId UnifiedDisplayInfo UTCTime HydratedEventPayload ->
169168 NotificationWebhookId ->
170169 Background (Maybe WebhookSendFailure )
171- tryWebhook event eventData webhookId = UnliftIO. handleAny (\ someException -> pure $ Just $ InvalidRequest event. eventId webhookId someException) do
170+ tryWebhook event webhookId = UnliftIO. handleAny (\ someException -> pure $ Just $ InvalidRequest event. eventId webhookId someException) do
172171 fmap (either Just (const Nothing )) $ runExceptT do
173172 proxiedHTTPManager <- asks Env. proxiedHttpClient
174173 WebhookConfig {uri = URIParam uri} <-
@@ -180,8 +179,8 @@ tryWebhook event eventData webhookId = UnliftIO.handleAny (\someException -> pur
180179 WebhookEventPayload
181180 { eventId = event. eventId,
182181 occurredAt = event. eventOccurredAt,
183- topic = eventTopic event. eventData,
184- data_ = eventData,
182+ topic = hydratedEventTopic event. eventData,
183+ data_ = event . eventData,
185184 jwt = ()
186185 }
187186 payloadJWT <-
@@ -208,14 +207,15 @@ tryWebhook event eventData webhookId = UnliftIO.handleAny (\someException -> pur
208207
209208attemptWebhookSend ::
210209 AuthZ. AuthZReceipt ->
211- (NotificationEvent NotificationEventId UTCTime -> WebhookPayloadData -> NotificationWebhookId -> IO (Maybe WebhookSendFailure )) ->
210+ (NotificationEvent NotificationEventId UnifiedDisplayInfo UTCTime HydratedEventPayload -> NotificationWebhookId -> IO (Maybe WebhookSendFailure )) ->
212211 NotificationEventId ->
213212 NotificationWebhookId ->
214213 PG. Transaction e (Maybe WebhookSendFailure )
215214attemptWebhookSend _authZReceipt tryWebhookIO eventId webhookId = do
216215 event <- NQ. expectEvent eventId
217- eventData <- WebhookQ. hydrateEventData event. eventData
218- PG. transactionUnsafeIO (tryWebhookIO event eventData webhookId) >>= \ case
216+ hydratedEvent <- forOf eventData_ event NQ. hydrateEventData
217+ populatedEvent <- hydratedEvent & DisplayInfoQ. unifiedDisplayInfoForUserOf eventUserInfo_
218+ PG. transactionUnsafeIO (tryWebhookIO populatedEvent webhookId) >>= \ case
219219 Just err -> do
220220 WQ. recordFailedDeliveryAttempt eventId webhookId
221221 pure $ Just err
0 commit comments