Skip to content

Commit 2fa2cf5

Browse files
committed
Update slack messages
1 parent 8b939c0 commit 2fa2cf5

File tree

3 files changed

+42
-18
lines changed

3 files changed

+42
-18
lines changed

src/Share/BackgroundJobs/Webhooks/Worker.hs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE DataKinds #-}
12
{-# LANGUAGE MultiWayIf #-}
23
{-# LANGUAGE StandaloneDeriving #-}
34

@@ -241,55 +242,59 @@ buildWebhookRequest webhookId uri event defaultPayload = do
241242
HTTPClient.requestBody = HTTPClient.RequestBodyLBS $ Aeson.encode defaultPayload
242243
}
243244

244-
mkSlackAttachment :: Maybe URI -> Text -> Text -> Text -> UTCTime -> Aeson.Value
245-
mkSlackAttachment mainURI author title msg timestamp =
245+
mkSlackAttachment :: Text -> URI -> Text -> URI -> Maybe URI -> Text -> Text -> UTCTime -> Aeson.Value
246+
mkSlackAttachment preText mainURI authorName authorLink authorAvatarUrl title msg timestamp =
246247
let epochSeconds :: Int64
247248
epochSeconds = round (POSIX.utcTimeToPOSIXSeconds timestamp)
248249
t :: Text -> Text
249250
t x = x
250251
in Aeson.object $
251252
[ "mrkdwn_in" Aeson..= [t "text"],
252253
"color" .= t "#36a64f",
253-
-- "pretext" .= "Optional pre-text that appears above the attachment block",
254-
"author_name" .= author,
255-
-- "author_link" .= "http://flickr.com/bobby/",
256-
-- "author_icon" .= "https://placeimg.com/16/16/people",
254+
"pretext" .= preText,
255+
"author_name" .= authorName,
256+
"author_link" .= uriToText authorLink,
257257
"title" .= title,
258+
"title_link" .= uriToText mainURI,
258259
"text" .= msg,
259-
-- "thumb_url" .= "http://placekitten.com/g/200/200",
260+
"thumb_url" .= uriToText Links.unisonLogoImage,
260261
-- "footer" .= "footer",
261262
-- "footer_icon" .= "https://platform.slack-edge.com/img/default_application_icon.png",
262263
"ts" .= epochSeconds
263264
]
264-
<> (mainURI & foldMap \uri -> ["title_link" .= uriToText uri])
265+
<> (authorAvatarUrl & foldMap \uri -> ["author_icon" .= uriToText uri])
265266
buildSlackWebhookRequest :: URI -> AppM reqCtx (Either WebhookSendFailure HTTPClient.Request)
266267
buildSlackWebhookRequest uri = do
267268
let actorName = event.eventActor ^. DisplayInfo.name_
268-
actorHandle = "(" <> IDs.toText (event.eventActor ^. DisplayInfo.handle_) <> ")"
269+
actorHandle = "(" <> IDs.toText (PrefixedID @"@" $ event.eventActor ^. DisplayInfo.handle_) <> ")"
269270
actorAuthor = maybe "" (<> " ") actorName <> actorHandle
271+
actorAvatarUrl = event.eventActor ^. DisplayInfo.avatarUrl_
272+
actorLink <- Links.userProfilePage (event.eventActor ^. DisplayInfo.handle_)
270273
slackPayload <- case event.eventData of
271274
HydratedProjectBranchUpdatedPayload payload -> do
272275
let pbShorthand = (projectBranchShortHandFromParts payload.projectInfo.projectShortHand payload.branchInfo.branchShortHand)
273276
title = "Branch " <> IDs.toText pbShorthand <> " was just updated."
274-
msg = "Branch " <> IDs.toText pbShorthand <> " was just updated."
277+
msg = ""
278+
preText = title
275279
link <- Links.notificationLink event.eventData
276280
pure $
277281
Aeson.object
278-
[ "text" Aeson..= msg,
282+
[ "text" Aeson..= title,
279283
"attachments"
280-
Aeson..= [ mkSlackAttachment (Just link) actorAuthor title msg event.eventOccurredAt
284+
Aeson..= [ mkSlackAttachment preText link actorAuthor actorLink actorAvatarUrl title msg event.eventOccurredAt
281285
]
282286
]
283287
HydratedProjectContributionCreatedPayload payload -> do
284288
let pbShorthand = (projectBranchShortHandFromParts payload.projectInfo.projectShortHand payload.contributionInfo.contributionSourceBranch.branchShortHand)
285-
title = "New Contribution in " <> IDs.toText pbShorthand
286-
msg = payload.contributionInfo.contributionTitle <> maybe "" (<> "") payload.contributionInfo.contributionDescription
289+
title = payload.contributionInfo.contributionTitle
290+
msg = fromMaybe "" payload.contributionInfo.contributionDescription
291+
preText = "New Contribution in " <> IDs.toText pbShorthand
287292
link <- Links.notificationLink event.eventData
288293
pure $
289294
Aeson.object
290-
[ "text" Aeson..= msg,
295+
[ "text" Aeson..= preText,
291296
"attachments"
292-
Aeson..= [ mkSlackAttachment (Just link) actorAuthor title msg event.eventOccurredAt
297+
Aeson..= [ mkSlackAttachment preText link actorAuthor actorLink actorAvatarUrl title msg event.eventOccurredAt
293298
]
294299
]
295300
pure $

src/Share/Web/Share/DisplayInfo/Types.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module Share.Web.Share.DisplayInfo.Types
1616
unifiedOrg_,
1717
handle_,
1818
name_,
19+
avatarUrl_,
1920
)
2021
where
2122

@@ -64,6 +65,13 @@ name_ f = \case
6465
UnifiedOrg orgDisplayInfo -> do
6566
UnifiedOrg <$> (orgDisplayInfo & field @"user" . field @"name" %%~ f)
6667

68+
avatarUrl_ :: Lens UnifiedDisplayInfo UnifiedDisplayInfo (Maybe URI) (Maybe URI)
69+
avatarUrl_ f = \case
70+
UnifiedUser userDisplayInfo -> do
71+
UnifiedUser <$> (userDisplayInfo & field @"avatarUrl" %%~ f)
72+
UnifiedOrg orgDisplayInfo -> do
73+
UnifiedOrg <$> (orgDisplayInfo & field @"user" . field @"avatarUrl" %%~ f)
74+
6775
type UnifiedDisplayInfo = UserLike UserDisplayInfo OrgDisplayInfo
6876

6977
type UserLikeIds = UserLike UserId OrgId

src/Share/Web/UI/Links.hs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{-# LANGUAGE DataKinds #-}
2+
13
module Share.Web.UI.Links
24
( oauthRedirect,
35
oauthRedirectWithCodeAndState,
@@ -12,6 +14,9 @@ module Share.Web.UI.Links
1214
notificationLink,
1315
projectBranchBrowseLink,
1416
contributionLink,
17+
unisonLogoImage,
18+
19+
-- * Utilities
1520
isTrustedURI,
1621
errorRedirectLink,
1722
errorRedirect,
@@ -24,6 +29,7 @@ where
2429
import Control.Monad.Reader
2530
import Data.Map qualified as Map
2631
import Network.URI
32+
import Network.URI qualified as URI
2733
import Servant (ToHttpApiData (..))
2834
import Share.App
2935
import Share.Env qualified as Env
@@ -92,13 +98,13 @@ projectBranchBrowseLink (ProjectBranchShortHand {userHandle, projectSlug, contri
9298
let branchPath = case contributorHandle of
9399
Just contributor -> [IDs.toText contributor, IDs.toText branchName]
94100
Nothing -> [IDs.toText branchName]
95-
path = [IDs.toText userHandle, IDs.toText projectSlug, "code"] <> branchPath <> ["latest"]
101+
path = [IDs.toText (PrefixedID @"@" userHandle), IDs.toText projectSlug, "code"] <> branchPath <> ["latest"]
96102
shareUIPath path
97103

98104
-- E.g. https://share.unison-lang.org/@unison/base/contributions/100
99105
contributionLink :: ProjectShortHand -> ContributionNumber -> AppM reqCtx URI
100106
contributionLink (ProjectShortHand {userHandle, projectSlug}) contributionNumber = do
101-
let path = [IDs.toText userHandle, IDs.toText projectSlug, "contributions", IDs.toText contributionNumber]
107+
let path = [IDs.toText (PrefixedID @"@" userHandle), IDs.toText projectSlug, "contributions", IDs.toText contributionNumber]
102108
shareUIPath path
103109

104110
-- | Where the user should go when clicking on a notification
@@ -109,6 +115,11 @@ notificationLink = \case
109115
HydratedProjectContributionCreatedPayload payload ->
110116
contributionLink payload.projectInfo.projectShortHand payload.contributionInfo.contributionNumber
111117

118+
unisonLogoImage :: URI
119+
unisonLogoImage =
120+
URI.parseURI "https://share.unison-lang.org/static/unison-logo-circle.png"
121+
& fromMaybe (error "unisonLogoImage: invalid URI")
122+
112123
----------- Utilities -----------
113124

114125
-- | Construct a full URI to a path within share, with provided query params.

0 commit comments

Comments
 (0)