@@ -8,13 +8,10 @@ module Share.BackgroundJobs.Webhooks.Worker (worker) where
88
99import Control.Lens hiding ((.=) )
1010import Control.Monad.Except (ExceptT (.. ), runExceptT )
11- import Crypto.JWT (JWTError )
12- import Data.Aeson (FromJSON (.. ), ToJSON (.. ))
11+ import Data.Aeson (ToJSON (.. ))
1312import Data.Aeson qualified as Aeson
14- import Data.ByteString.Lazy.Char8 qualified as BL
1513import Data.List.Extra qualified as List
1614import Data.Text qualified as Text
17- import Data.Text.Encoding qualified as Text
1815import Data.Time (UTCTime )
1916import Ki.Unlifted qualified as Ki
2017import Network.HTTP.Client qualified as HTTPClient
@@ -24,6 +21,7 @@ import Network.URI qualified as URI
2421import Share.BackgroundJobs.Errors (reportError )
2522import Share.BackgroundJobs.Monad (Background )
2623import Share.BackgroundJobs.Webhooks.Queries qualified as WQ
24+ import Share.BackgroundJobs.Webhooks.Types
2725import Share.BackgroundJobs.Workers (newWorker )
2826import Share.ChatApps (Author (.. ))
2927import Share.ChatApps qualified as ChatApps
@@ -37,7 +35,7 @@ import Share.Metrics qualified as Metrics
3735import Share.Notifications.Ops qualified as NotOps
3836import Share.Notifications.Queries qualified as NQ
3937import Share.Notifications.Types
40- import Share.Notifications.Webhooks.Secrets (WebhookConfig (.. ), WebhookSecretError )
38+ import Share.Notifications.Webhooks.Secrets (WebhookConfig (.. ))
4139import Share.Notifications.Webhooks.Secrets qualified as Webhooks
4240import Share.Postgres qualified as PG
4341import Share.Postgres.Notifications qualified as Notif
@@ -53,42 +51,6 @@ import Share.Web.Share.DisplayInfo.Types qualified as DisplayInfo
5351import Share.Web.UI.Links qualified as Links
5452import UnliftIO qualified
5553
56- data WebhookSendFailure
57- = ReceiverError NotificationEventId NotificationWebhookId HTTP. Status BL. ByteString
58- | InvalidRequest NotificationEventId NotificationWebhookId UnliftIO. SomeException
59- | WebhookSecretFetchError NotificationEventId NotificationWebhookId WebhookSecretError
60- | JWTError NotificationEventId NotificationWebhookId JWTError
61- deriving stock (Show )
62-
63- instance Logging. Loggable WebhookSendFailure where
64- toLog = \ case
65- ReceiverError eventId webhookId status body ->
66- Logging. textLog
67- ( " Webhook receiver error: "
68- <> Text. pack (show status)
69- <> " "
70- <> Text. decodeUtf8 (BL. toStrict body)
71- )
72- & Logging. withTag (" status" , tShow status)
73- & Logging. withTag (" event_id" , tShow eventId)
74- & Logging. withTag (" webhook_id" , tShow webhookId)
75- & Logging. withSeverity Logging. UserFault
76- InvalidRequest eventId webhookId err ->
77- Logging. textLog (" Invalid request: " <> Text. pack (show err))
78- & Logging. withTag (" event_id" , tShow eventId)
79- & Logging. withTag (" webhook_id" , tShow webhookId)
80- & Logging. withSeverity Logging. UserFault
81- WebhookSecretFetchError eventId webhookId err ->
82- Logging. textLog (" Failed to fetch webhook secret: " <> Text. pack (show err))
83- & Logging. withTag (" event_id" , tShow eventId)
84- & Logging. withTag (" webhook_id" , tShow webhookId)
85- & Logging. withSeverity Logging. Error
86- JWTError eventId webhookId err ->
87- Logging. textLog (" JWT error: " <> Text. pack (show err))
88- & Logging. withTag (" event_id" , tShow eventId)
89- & Logging. withTag (" webhook_id" , tShow webhookId)
90- & Logging. withSeverity Logging. Error
91-
9254-- | Check every 10 minutes if we haven't heard on the notifications channel.
9355-- Just in case we missed a notification.
9456maxPollingIntervalSeconds :: Int
@@ -140,49 +102,6 @@ processWebhook authZReceipt = withSpan "background:webhooks:process-webhook" mem
140102webhookTimeout :: HTTPClient. ResponseTimeout
141103webhookTimeout = HTTPClient. responseTimeoutMicro (20 * 1000000 {- 20 seconds -} )
142104
143- data WebhookEventPayload jwt = WebhookEventPayload
144- { -- | The event ID of the notification event.
145- eventId :: NotificationEventId ,
146- -- | The time at which the event occurred.
147- occurredAt :: UTCTime ,
148- -- | The topic of the notification event.
149- topic :: NotificationTopic ,
150- -- | The data associated with the notification event.
151- data_ :: HydratedEvent ,
152- -- | A signed token containing all of the same data.
153- jwt :: jwt
154- }
155- deriving stock (Show , Eq )
156-
157- deriving via JWT. JSONJWTClaims (WebhookEventPayload () ) instance JWT. AsJWTClaims (WebhookEventPayload () )
158-
159- instance ToJSON (WebhookEventPayload JWTParam ) where
160- toJSON WebhookEventPayload {eventId, occurredAt, topic, data_, jwt} =
161- Aeson. object
162- [ " eventId" Aeson. .= eventId,
163- " occurredAt" Aeson. .= occurredAt,
164- " topic" Aeson. .= topic,
165- " data" Aeson. .= data_,
166- " signed" Aeson. .= jwt
167- ]
168-
169- instance ToJSON (WebhookEventPayload () ) where
170- toJSON WebhookEventPayload {eventId, occurredAt, topic, data_} =
171- Aeson. object
172- [ " eventId" Aeson. .= eventId,
173- " occurredAt" Aeson. .= occurredAt,
174- " topic" Aeson. .= topic,
175- " data" Aeson. .= data_
176- ]
177-
178- instance FromJSON (WebhookEventPayload () ) where
179- parseJSON = Aeson. withObject " WebhookEventPayload" $ \ o -> do
180- eventId <- o Aeson. .: " eventId"
181- occurredAt <- o Aeson. .: " occurredAt"
182- topic <- o Aeson. .: " topic"
183- data_ <- o Aeson. .: " data"
184- pure WebhookEventPayload {eventId, occurredAt, topic, data_, jwt = () }
185-
186105tryWebhook ::
187106 NotificationEvent NotificationEventId UnifiedDisplayInfo UTCTime HydratedEvent ->
188107 NotificationWebhookId ->
0 commit comments