Skip to content

Commit

Permalink
[#108] Skip over emoji blocks
Browse files Browse the repository at this point in the history
Problem: right now, when find an emoji block in a message, we split the
message in two. This means this message:

```
Hey, let's meet at 10:30 🕐 tomorrow?
```

Will be interpreted as two separate sentences, `["Hey, let's meet at
10:30", "tomorrow?"]`, and "tomorrow" won't be taken into account.

Solution: skip over emoji blocks, and collate adjacent text-like blocks.
This is not parsed as a single sentence `["Hey, let's meet at 10:30
tomorrow?"]`
  • Loading branch information
dcastro committed Sep 8, 2023
1 parent 1b2761e commit 30b919c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
21 changes: 15 additions & 6 deletions src/TzBot/Slack/API/MessageBlock.hs
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,24 @@ 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
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 Down
5 changes: 2 additions & 3 deletions test/Test/TzBot/MessageBlocksSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ test_messageBlocksSpec = TestGroup "Message blocks" $
, "3.1quote block "
, " 3.2quote block"
, "4.1plain "
, " 4.1strike "
, " 4.1bold "
, " 4.1strike 4.1bold "
, "4.2plain "
, " 4.2strike github 4.2bold "
, "between the lists\n"
Expand All @@ -69,7 +68,7 @@ test_messageBlocksSpec = TestGroup "Message blocks" $
, "end!"
]
getLevel2Errors (snd res) @?=
[ "emoji", "user", "broadcast"
[ "user", "broadcast"
]
]

Expand Down

0 comments on commit 30b919c

Please sign in to comment.