-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remind contexts and apply to time refs #82
Open
YuriRomanowski
wants to merge
2
commits into
main
Choose a base branch
from
YuriRomanowski/remind-contexts-and-apply-to-time-refs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,5 @@ stack-hie.yaml* | |
.vscode/ | ||
result | ||
*.txt | ||
stack-hie.yaml | ||
stack-hie.yaml.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -110,6 +110,7 @@ tests: | |
- tasty-hspec | ||
- tasty-hunit | ||
- tasty-quickcheck | ||
- text | ||
- time | ||
- tztime | ||
- QuickCheck | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,13 +19,14 @@ import UnliftIO qualified | |
import TzBot.Cache qualified as Cache | ||
import TzBot.Config (Config(..)) | ||
import TzBot.Logger | ||
import TzBot.ProcessEvents.Common (getTimeReferencesFromMessage) | ||
import TzBot.ProcessEvents.Common | ||
import TzBot.Render | ||
import TzBot.Slack | ||
import TzBot.Slack.API | ||
import TzBot.Slack.Events | ||
import TzBot.Slack.Fixtures qualified as Fixtures | ||
import TzBot.TimeReference (TimeReference(..)) | ||
import TzBot.TimeContext (emptyTimeContext) | ||
import TzBot.TimeReference (TimeReference) | ||
import TzBot.Util (whenT, withMaybe) | ||
|
||
data MessageEventType = METMessage | METMessageEdited | ||
|
@@ -80,19 +81,16 @@ processMessageEvent evt = | |
katipAddContext (MessageContext msgId) $ | ||
whenJustM (filterMessageTypeWithLog evt) $ \mEventType -> | ||
whenJustM (withSenderNotBot evt) $ \sender -> do | ||
timeRefs <- getTimeReferencesFromMessage msg | ||
processMessageEvent' evt mEventType sender timeRefs | ||
processMessageEvent' evt mEventType sender | ||
where | ||
msg = meMessage evt | ||
msgId = mMessageId $ meMessage evt | ||
|
||
processMessageEvent' | ||
:: MessageEvent | ||
-> MessageEventType | ||
-> User | ||
-> [TimeReference] | ||
-> BotM () | ||
processMessageEvent' evt mEventType sender timeRefs = | ||
processMessageEvent' evt mEventType sender = | ||
case meChannelType evt of | ||
Just CTDirectChannel -> handleDirectMessage | ||
_ -> case mEventType of | ||
|
@@ -155,25 +153,52 @@ processMessageEvent' evt mEventType sender timeRefs = | |
} | ||
sendEphemeralMessage req | ||
|
||
-- threadId is the same as its parent's messageId, | ||
-- so use messageId if there's no thread yet | ||
getMessageThreadId :: ThreadId | ||
getMessageThreadId = fromMaybe (ThreadId $ unMessageId msgId) mbThreadId | ||
|
||
handleMessageChanged :: BotM () | ||
handleMessageChanged = katipAddNamespaceText "edit" do | ||
messageRefsCache <- asks bsMessageCache | ||
mbMessageRefs <- Cache.lookup msgId messageRefsCache | ||
convStateCache <- asks bsConversationStateCache | ||
mbMessageRefsAndState <- Cache.lookup msgId messageRefsCache | ||
-- if not found or expired, just ignore this message | ||
-- it's too old or just didn't contain any time refs | ||
whenJust mbMessageRefs $ \oldRefs -> do | ||
let newRefsFound = not $ all (`elem` oldRefs) timeRefs | ||
whenJust mbMessageRefsAndState $ \(oldRefs, stateBefore) -> do | ||
(newRefs, stateAfter) <- | ||
getTimeReferencesAndNewStateFromMessage stateBefore msg | ||
mbConversationState <- Cache.lookup getMessageThreadId convStateCache | ||
-- If the conversation state was defined after processing this | ||
-- message, we should update it. | ||
whenJust mbConversationState \(lastMsgId, _conversationState) -> | ||
when (lastMsgId == msgId) $ | ||
Cache.insert getMessageThreadId (msgId, stateAfter) convStateCache | ||
|
||
let newRefsFound = not $ all (`elem` oldRefs) newRefs | ||
-- no new references found, ignoring | ||
when newRefsFound $ withNonEmptyTimeRefs timeRefs \neTimeRefs -> do | ||
Cache.insert msgId timeRefs messageRefsCache | ||
when newRefsFound $ withNonEmptyTimeRefs newRefs \neTimeRefs -> do | ||
-- This cache always keeps only "before" state in order to correctly | ||
-- translate further edits. | ||
Cache.insert msgId (newRefs, stateBefore) messageRefsCache | ||
permalink <- getMessagePermalinkCached channelId msgId | ||
handleChannelMessageCommon (Just permalink) neTimeRefs | ||
|
||
handleNewMessage :: BotM () | ||
handleNewMessage = do | ||
withNonEmptyTimeRefs timeRefs $ \neTimeRefs -> do | ||
convStateCache <- asks bsConversationStateCache | ||
conversationState <- | ||
fmap (fromMaybe emptyTimeContext . fmap snd . join) $ | ||
traverse (\t -> Cache.lookup t convStateCache) mbThreadId | ||
(timeRefs, newState) <- | ||
getTimeReferencesAndNewStateFromMessage conversationState msg | ||
when (not $ null timeRefs) $ | ||
-- save message only if time references are present | ||
asks bsMessageCache >>= Cache.insert msgId timeRefs | ||
asks bsMessageCache >>= Cache.insert msgId (timeRefs, newState) | ||
Cache.insert getMessageThreadId (msgId, newState) convStateCache | ||
asks bsMessageCache >>= Cache.insert msgId (timeRefs, conversationState) | ||
|
||
withNonEmptyTimeRefs timeRefs $ \neTimeRefs -> do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems that we will eventually rewrite this logic using some abstracting technique and write some tests, it gets a bit tricky as more internal state is kept. |
||
handleChannelMessageCommon Nothing neTimeRefs | ||
|
||
handleChannelMessageCommon :: Maybe Text -> NonEmpty TimeReference -> BotM () | ||
|
@@ -195,8 +220,9 @@ processMessageEvent' evt mEventType sender timeRefs = | |
ephemeralsMailing channelId sendActionLocal | ||
|
||
handleDirectMessage :: BotM () | ||
handleDirectMessage = | ||
when (mEventType /= METMessageEdited) $ | ||
handleDirectMessage = when (mEventType /= METMessageEdited) $ do | ||
(timeRefs, _stateAfter) <- | ||
getTimeReferencesAndNewStateFromMessage emptyTimeContext msg | ||
withNonEmptyTimeRefs timeRefs $ \neTimeRefs -> do | ||
-- According to | ||
-- https://forums.slackcommunity.com/s/question/0D53a00008vsItQCAU | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
-- SPDX-FileCopyrightText: 2022 Serokell <https://serokell.io/> | ||
-- | ||
-- SPDX-License-Identifier: MPL-2.0 | ||
|
||
module TzBot.TimeContext where | ||
|
||
import Universum hiding (many, toList, try) | ||
|
||
import Control.Lens.TH (makeLensesWith) | ||
import TzBot.Instances () | ||
import TzBot.TimeReference | ||
import TzBot.Util | ||
|
||
data TimeContext = TimeContext | ||
{ tcCurrentDateRef :: Maybe (Matched DateReference) | ||
, tcCurrentLocRef :: Maybe (Matched LocationReference) | ||
} deriving stock (Show, Eq, Generic) | ||
|
||
emptyTimeContext :: TimeContext | ||
emptyTimeContext = TimeContext Nothing Nothing | ||
|
||
makeLensesWith postfixFields ''TimeContext |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This ideally should be in the config