Skip to content

Commit

Permalink
[#107] Correctly parse channel_join events
Browse files Browse the repository at this point in the history
Problem: The parser fails to parse events of type `message` and subtype
`channel_join`, and the logs are flooded with errors.

This seems to happen because the parser expects every event of type
`message` to have a `blocks` field, and that's not always true.

Solution: Make the `blocks` field optional.
  • Loading branch information
dcastro committed Sep 6, 2023
1 parent b953048 commit 84c74b6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/TzBot/ProcessEvents/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ getTextPiecesFromMessage
-> m [Text]
getTextPiecesFromMessage message = do
let throwAwayTooShort = filter (\x -> T.compareLength x 2 == GT)
throwAwayTooShort <$> case unUnknown $ msgBlocks message of
Left unknownBlocksValue -> do
throwAwayTooShort <$> case unUnknown <$> msgBlocks message of
Nothing ->
pure $ ignoreCodeBlocksManually $ mText message
Just (Left unknownBlocksValue) -> do
logWarn [int||
Failed to parse message blocks, \
trying to ignore code blocks manually
Expand All @@ -96,7 +98,7 @@ getTextPiecesFromMessage message = do
#{encodePrettyToTextBuilder unknownBlocksValue}
|]
pure $ ignoreCodeBlocksManually $ mText message
Right blocks -> do
Just (Right blocks) -> do
let (pieces, exErrs) = extractPieces blocks
let (l1Errs, l2Errs) = splitExtractErrors exErrs
when (not $ null l1Errs) $
Expand Down
4 changes: 2 additions & 2 deletions src/TzBot/Slack/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ data Message = Message
, mThreadId :: Maybe ThreadId
, mEdited :: Bool
, mSubType :: Maybe Text
, msgBlocks :: WithUnknown [MessageBlock]
, msgBlocks :: Maybe (WithUnknown [MessageBlock])
} deriving stock (Eq, Show, Generic)

instance FromJSON Message where
Expand All @@ -241,7 +241,7 @@ parseMessage o = do
mSubType <- o .:? "subtype"
mEdited <- fmap (isJust @Text) . runMaybeT $
MaybeT (o .:? "edited") >>= MaybeT . (.:? "ts")
msgBlocks <- o .: "blocks"
msgBlocks <- o .:? "blocks"
pure Message {..}

-- | See: https://api.slack.com/types/user
Expand Down

0 comments on commit 84c74b6

Please sign in to comment.