@@ -28,7 +28,12 @@ import TzBot.Slack.Fixtures qualified as Fixtures
2828import TzBot.TimeReference (TimeReference )
2929import TzBot.Util (whenT , withMaybe )
3030
31- data MessageEventType = METMessage | METMessageEdited
31+ data MessageEventType
32+ = METMessage
33+ | METMessageEdited
34+ -- ^ A message has been edited.
35+ Message
36+ -- ^ The state of the message _before_ it was edited.
3237 deriving stock (Eq )
3338
3439-- We don't need to handle MDMessageBroadcast anyhow,
@@ -41,9 +46,9 @@ filterMessageTypeWithLog evt = case meMessageDetails evt of
4146 MDMessage -> do
4247 logInfo [int ||Handling new message|]
4348 pure $ Just METMessage
44- MDMessageEdited {} -> do
49+ MDMessageEdited previousMsg -> do
4550 logInfo [int ||Message was edited|]
46- pure $ Just METMessageEdited
51+ pure $ Just $ METMessageEdited previousMsg
4752 MDMessageBroadcast -> do
4853 logInfo [int ||Incoming message is thread broadcast, ignoring|]
4954 pure Nothing
@@ -96,7 +101,7 @@ processMessageEvent' evt mEventType sender timeRefs =
96101 case meChannelType evt of
97102 Just CTDirectChannel -> handleDirectMessage
98103 _ -> case mEventType of
99- METMessageEdited -> handleMessageChanged
104+ METMessageEdited previousMsg -> handleMessageChanged previousMsg
100105 METMessage -> handleNewMessage
101106
102107 where
@@ -155,19 +160,21 @@ processMessageEvent' evt mEventType sender timeRefs =
155160 }
156161 sendEphemeralMessage req
157162
158- handleMessageChanged :: BotM ()
159- handleMessageChanged = katipAddNamespaceText " edit" do
163+ handleMessageChanged :: Message -> BotM ()
164+ handleMessageChanged previousMsg = katipAddNamespaceText " edit" do
160165 messageRefsCache <- asks bsMessageCache
161- mbMessageRefs <- Cache. lookup msgId messageRefsCache
162- -- if not found or expired, just ignore this message
163- -- it's too old or just didn't contain any time refs
164- whenJust mbMessageRefs $ \ oldRefs -> do
165- let newRefsFound = not $ all (`elem` oldRefs) timeRefs
166- -- no new references found, ignoring
167- when newRefsFound $ withNonEmptyTimeRefs timeRefs \ neTimeRefs -> do
168- Cache. insert msgId timeRefs messageRefsCache
169- permalink <- getMessagePermalinkCached channelId msgId
170- handleChannelMessageCommon (Just permalink) neTimeRefs
166+ -- Fetch the time references from the old message (before it was edited).
167+ -- If they're not found in the cache, parse the message.
168+ oldRefs <- do
169+ Cache. lookup msgId messageRefsCache >>= \ case
170+ Just cachedTimeRefs -> pure cachedTimeRefs
171+ Nothing -> getTimeReferencesFromMessage previousMsg
172+ let newRefsFound = not $ all (`elem` oldRefs) timeRefs
173+ -- If no new references are found, we ignore the edited message.
174+ when newRefsFound $ withNonEmptyTimeRefs timeRefs \ neTimeRefs -> do
175+ Cache. insert msgId timeRefs messageRefsCache
176+ permalink <- getMessagePermalinkCached channelId msgId
177+ handleChannelMessageCommon (Just permalink) neTimeRefs
171178
172179 handleNewMessage :: BotM ()
173180 handleNewMessage = do
@@ -196,7 +203,6 @@ processMessageEvent' evt mEventType sender timeRefs =
196203
197204 handleDirectMessage :: BotM ()
198205 handleDirectMessage =
199- when (mEventType /= METMessageEdited ) $
200206 withNonEmptyTimeRefs timeRefs $ \ neTimeRefs -> do
201207 -- According to
202208 -- https://forums.slackcommunity.com/s/question/0D53a00008vsItQCAU
0 commit comments