Skip to content

Commit 09440b1

Browse files
committed
Limit long titles/descriptions
1 parent 5bbcb17 commit 09440b1

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/Share/BackgroundJobs/Webhooks/Worker.hs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ instance ToJSON (MessageContent 'Slack) where
236236
[ "text" .= preText,
237237
"attachments"
238238
.= [ Aeson.object
239-
[ "title" .= title,
239+
[ "title" .= cutOffText 250 title,
240240
"title_link" .= uriToText mainLink,
241241
"text" .= content,
242242
"author_name" .= authorName,
@@ -254,13 +254,13 @@ instance ToJSON (MessageContent 'Discord) where
254254
Aeson.object
255255
[ "username" .= ("Share Notifications" :: Text),
256256
"avatar_url" .= Links.unisonLogoImage,
257-
"content" .= preText,
257+
"content" .= cutOffText 1950 preText,
258258
"embeds"
259259
.= [ Aeson.object
260-
[ "title" .= title,
260+
[ "title" .= cutOffText 250 title,
261261
"url" .= uriToText mainLink,
262-
"description" .= content,
263-
"author" .= Aeson.object ["name" .= authorName, "url" .= uriToText authorLink, "icon_url" .= fmap uriToText authorAvatarUrl],
262+
"description" .= cutOffText 4000 content,
263+
"author" .= Aeson.object ["name" .= cutOffText 250 authorName, "url" .= uriToText authorLink, "icon_url" .= fmap uriToText authorAvatarUrl],
264264
"timestamp" .= (Just $ Text.pack $ Time.formatTime Time.defaultTimeLocale "%FT%T%QZ" timestamp),
265265
"thumbnail" .= fmap (\url -> Aeson.object ["url" .= uriToText url]) thumbnailUrl
266266
]
@@ -270,9 +270,9 @@ instance ToJSON (MessageContent 'Discord) where
270270
buildWebhookRequest :: NotificationWebhookId -> URI -> NotificationEvent NotificationEventId UnifiedDisplayInfo UTCTime HydratedEventPayload -> WebhookEventPayload JWTParam -> Background (Either WebhookSendFailure HTTPClient.Request)
271271
buildWebhookRequest webhookId uri event defaultPayload = do
272272
if
273-
| isSlackWebhook uri -> buildChatAppPayload (Proxy @Slack) uri
274-
| isDiscordWebhook uri -> buildChatAppPayload (Proxy @Discord) uri
275-
| otherwise -> pure $ buildDefaultPayload
273+
| isSlackWebhook uri -> buildChatAppPayload (Proxy @Slack) uri
274+
| isDiscordWebhook uri -> buildChatAppPayload (Proxy @Discord) uri
275+
| otherwise -> pure $ buildDefaultPayload
276276
where
277277
isSlackWebhook :: URI -> Bool
278278
isSlackWebhook uri =
@@ -355,6 +355,13 @@ buildWebhookRequest webhookId uri event defaultPayload = do
355355
}
356356
)
357357

358+
-- | Nicely cut off text so that it doesn't exceed the max length
359+
cutOffText :: Int -> Text -> Text
360+
cutOffText maxLength text =
361+
if Text.length text > maxLength
362+
then Text.take (maxLength - 3) text <> "..."
363+
else text
364+
358365
attemptWebhookSend ::
359366
AuthZ.AuthZReceipt ->
360367
(NotificationEvent NotificationEventId UnifiedDisplayInfo UTCTime HydratedEventPayload -> NotificationWebhookId -> IO (Maybe WebhookSendFailure)) ->

0 commit comments

Comments
 (0)