Skip to content

Commit

Permalink
Merge pull request #122 from serokell/diogo/#108-handle-url-unfurl
Browse files Browse the repository at this point in the history
[#108] Handle messages with link previews
  • Loading branch information
dcastro authored Sep 8, 2023
2 parents 1b63767 + 983ca5f commit 4b243ec
Show file tree
Hide file tree
Showing 4 changed files with 617 additions and 14 deletions.
3 changes: 3 additions & 0 deletions src/TzBot/ProcessEvents/Message.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ filterMessageTypeWithLog evt = case meMessageDetails evt of
MDUserLeftChannel -> do
logInfo [int||Incoming message subtype=channel_leave, ignoring|]
pure Nothing
MDMessageUrlUnfurl -> do
logInfo [int||Incoming message with URL preview, ignoring|]
pure Nothing

withSenderNotBot :: MessageEvent -> BotM (Maybe User)
withSenderNotBot evt = do
Expand Down
10 changes: 9 additions & 1 deletion src/TzBot/Slack/API/MessageBlock.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
-}
module TzBot.Slack.API.MessageBlock
( -- * Block datatype
MessageBlock
MessageBlock(..)

-- * Extract errors (or, more precisely, warnings)
, ExtractError (..)
Expand All @@ -22,6 +22,14 @@ module TzBot.Slack.API.MessageBlock
-- * Functions
, extractPieces
, splitExtractErrors

-- * Internals
, BlockElementLevel1(..)
, BlockElementType(..)
, PlainBlockElementLevel1(..)
, BlockElementLevel2(..)
, ElementText(..)
, ElementLink(..)
) where

import TzPrelude
Expand Down
31 changes: 20 additions & 11 deletions src/TzBot/Slack/Events.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ data MessageDetails
= MDMessage
| MDMessageEdited Message
| MDMessageBroadcast -- message copied to channel from thread
| MDMessageUrlUnfurl
-- ^ This event is occurs when a user posts a message with a URL
-- and Slack displays a URL preview.
| MDUserJoinedChannel
| MDUserLeftChannel
deriving stock (Eq, Show, Generic)
Expand All @@ -52,18 +55,24 @@ instance FromJSON MessageEvent where
Just "channel_join" -> (,MDUserJoinedChannel) <$> parseMessage o
Just "channel_leave" -> (,MDUserLeftChannel) <$> parseMessage o
Just "message_changed" -> do
-- Explanation: when someone posts a message to a thread with channel broadcast,
-- two events come: message and then message_changed, the latter seemingly
-- corresponds to sending this message directly to the channel.
-- These messages lack "edited" field, and they are not really edited.
newMsg <- o .: "message" >>= parseMessage
case (mEdited newMsg, mSubType newMsg) of
(True, _) -> do
prevMsg <- o .: "previous_message" >>= parseMessage
pure (newMsg, MDMessageEdited prevMsg)
(False, Just "thread_broadcast") ->
pure (newMsg, MDMessageBroadcast)
_ -> fail "expected edited message"

messageDetails <-
if
| newMsg.mEdited -> do
prevMsg <- o .: "previous_message" >>= parseMessage
pure $ MDMessageEdited prevMsg
| not newMsg.mEdited && newMsg.mSubType == Just "thread_broadcast" ->
-- Explanation: when someone posts a message to a thread with channel broadcast,
-- two events come: message and then message_changed, the latter seemingly
-- corresponds to sending this message directly to the channel.
-- These messages lack "edited" field, and they are not really edited.
pure MDMessageBroadcast
| not newMsg.mEdited && subtype == Just "message_changed" ->
pure MDMessageUrlUnfurl
| otherwise -> fail "expected edited message"

pure (newMsg, messageDetails)
Just _unknownSubtype -> parseMessageFromTopObject
pure MessageEvent {..}

Expand Down
Loading

0 comments on commit 4b243ec

Please sign in to comment.