Skip to content

Commit 95c7f91

Browse files
authored
Merge pull request #98 from serokell/diogo/#93-message-edited
[#93] Handle edited events when old message is not in cache
2 parents a37dd4c + c78bb5b commit 95c7f91

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

docs/implementation_details.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
The bot's main function is to capture time references in a message and convert them
1010
to the receiver's time zone. This can be triggered by some events:
1111
* A new message was posted in a channel where the bot is present;
12-
* A message that has been previously converted was recently edited and it now contains some new time references;
12+
* A message has been edited and it now contains some new time references;
1313
* The user triggered an entrypoint from the message's context menu ``;
1414
* The user DMed the bot.
1515

src/TzBot/ProcessEvents/Message.hs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ import TzBot.Slack.Fixtures qualified as Fixtures
2828
import TzBot.TimeReference (TimeReference)
2929
import 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

Comments
 (0)