Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#108] Improve block support #121

Merged
merged 4 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ tests:
- katip
- nyan-interpolation
- servant-client-core
- string-conversions
- tasty
- tasty-hspec
- tasty-hunit
Expand Down
30 changes: 24 additions & 6 deletions src/TzBot/Slack/API/MessageBlock.hs
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,26 @@ extractPieces mBlocks = runWriter $ concat <$> mapM goMessageBlock mBlocks
go :: Maybe Builder -> [Text] -> [WithUnknown BlockElementLevel2] -> Writer [ExtractError] [Text]
go mbCurPiece prevPieces (e:es) = case unUnknown e of
Left val -> do
let _type = fromMaybe "unknown" (val ^? key "type" . _String)
tell [EEUnknownBlockElementLevel2 $ UnknownBlockElementLevel2Error _type val]
go Nothing (prependMbCurrentToPrevious mbCurPiece prevPieces) es
let blockType = fromMaybe "unknown" (val ^? key "type" . _String)
case blockType of
"emoji" ->
-- skip over emoji blocks
go mbCurPiece prevPieces es
_ -> do
-- return an error if we find a block type we don't recognize.
when (blockType `notElem` knownBlockTypes) do
tell [EEUnknownBlockElementLevel2 $ UnknownBlockElementLevel2Error blockType val]
-- break the message in two separate `Text` pieces.
go Nothing (prependMbCurrentToPrevious mbCurPiece prevPieces) es
Right (BEL2ElementText elementText) -> do
let etTextB = fromText elementText.etText
if (elementText.etStyle >>= styCode) == Just True
-- ignore inline code block
then go Nothing (prependMbCurrentToPrevious mbCurPiece prevPieces) es
else go (Just $ maybe etTextB (<> etTextB) mbCurPiece) prevPieces es
then
-- ignore inline code block
go Nothing (prependMbCurrentToPrevious mbCurPiece prevPieces) es
else
-- collate this block's text with any adjacent text-like block
go (Just $ maybe etTextB (<> etTextB) mbCurPiece) prevPieces es
Right (BEL2ElementLink elementLink) -> do
let linkText = fromText elementLink.elText
go (Just $ maybe linkText (<> linkText) mbCurPiece) prevPieces es
Expand All @@ -220,3 +231,10 @@ extractPieces mBlocks = runWriter $ concat <$> mapM goMessageBlock mBlocks
prependMbCurrentToPrevious :: Maybe Builder -> [Text] -> [Text]
prependMbCurrentToPrevious mbCurPiece prevPieces =
maybe prevPieces ((: prevPieces) . cs . toLazyText) mbCurPiece

knownBlockTypes :: [Text]
knownBlockTypes =
[ "user" -- A user tag, e.g. `@gromak`
, "usergroup" -- A tag for a user group, e.g. `@ligo-lsp-project
, "broadcast" -- A tag like `@here` or `@channel`
]
Loading