Skip to content
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

[#112] Do not display "You are in this timezone" #118

Merged
merged 2 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/TzBot/ProcessEvents/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import TzBot.Feedback.Dialog (insertDialogEntry)
import TzBot.Feedback.Dialog.Types
import TzBot.Logger
import TzBot.Parser (parseTimeRefs)
import TzBot.Render (ConversionPairs, asForModalM, renderAllConversionPairs, renderTemplate)
import TzBot.Render
(ConversionPairs, ModalOrEphemeral(..), renderAllConversionPairs, renderTemplate)
import TzBot.Slack (BotM, getUserCached, startModal)
import TzBot.Slack.API
import TzBot.Slack.API.MessageBlock
Expand Down Expand Up @@ -51,7 +52,7 @@ openModalCommon message channelId whoTriggeredId triggerId mkModalFunc = do
whoTriggered <- getUserCached whoTriggeredId
pure $
renderAllConversionPairs whoTriggered $
renderTemplate asForModalM msgTimestamp sender neTimeRefs
renderTemplate IsModal msgTimestamp sender neTimeRefs

guid <- ReportDialogId <$> liftIO genText
let metadata = ReportDialogEntry
Expand Down
4 changes: 2 additions & 2 deletions src/TzBot/ProcessEvents/Message.hs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ processMessageEvent' evt mEventType sender timeRefs =

handleChannelMessageCommon :: Maybe Text -> NonEmpty TimeReference -> BotM ()
handleChannelMessageCommon mbPermalink neTimeRefs = do
let ephemeralTemplate = renderTemplate asForMessageM now sender neTimeRefs
let ephemeralTemplate = renderTemplate IsEphemeral now sender neTimeRefs

let sendActionLocal userInChannelId = do
userInChannel <- getUserCached userInChannelId
Expand Down Expand Up @@ -210,7 +210,7 @@ processMessageEvent' evt mEventType sender timeRefs =
-- only to them, showing it in the way how other users would see
-- it if it were sent to the common channel.
let mbEphemeralMessage = renderAllConversionPairs sender $
renderTemplate asForModalM now sender neTimeRefs
renderTemplate IsModal now sender neTimeRefs
whenJust mbEphemeralMessage $ \eph -> do
logInfo [int||
Received message from the DM, sending conversion to the author
Expand Down
40 changes: 20 additions & 20 deletions src/TzBot/Render.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ module TzBot.Render
, asForSenderS
, asForOthersS

, ModalFlag
, asForModalM
, asForMessageM
, ModalOrEphemeral(..)
) where

import TzPrelude
Expand Down Expand Up @@ -133,27 +131,29 @@ renderSlackBlocks forSender =
withMaybe mbNote [conversionBlock] $ \note -> [conversionBlock, mkNoteBlock note]

-- | Render a template that can be later specialized to different users.
renderTemplate :: ModalFlag -> UTCTime -> User -> NE.NonEmpty TimeReference -> Template
renderTemplate modalFlag now sender timeRefs =
Template $ NE.map (renderEphemeralMessageConversionPair modalFlag sender)
renderTemplate :: ModalOrEphemeral -> UTCTime -> User -> NE.NonEmpty TimeReference -> Template
renderTemplate modalOrEphemeral now sender timeRefs =
Template $ NE.map (renderEphemeralMessageConversionPair modalOrEphemeral sender)
$ attach (timeReferenceToUTC (uTz sender) now) timeRefs

-- | This flag defines whether time references are rendered to be shown
-- in a chat or in a modal view.
newtype ModalFlag = ModalFlag Bool

asForModalM, asForMessageM :: ModalFlag
asForModalM = ModalFlag True
asForMessageM = ModalFlag False
-- | This flag defines whether a time reference conversion will be rendered
-- in an ephemeral message or in a modal view.
data ModalOrEphemeral = IsModal | IsEphemeral
deriving stock (Eq, Show)

-- | Renders a time reference and the result of a successful conversion.
--
-- Returns `Nothing` when we're attempting to render an ephemeral message
-- for the sender of the original message,
-- and the time reference is relative to the sender's timezone.
renderOnSuccess
:: ModalFlag
:: ModalOrEphemeral
-> User
-> TimeReference
-> TimeRefSuccess
-> User
-> Maybe ConversionPair
renderOnSuccess (ModalFlag forModal) sender timeRef timeRefSucess user = do
renderOnSuccess modalOrEphemeral sender timeRef timeRefSucess user = do
let userTzLabel = uTz user
renderedUserTime = do
let q = renderUserTime userTzLabel timeRefSucess.trsUtcResult
Expand All @@ -166,9 +166,9 @@ renderOnSuccess (ModalFlag forModal) sender timeRef timeRefSucess user = do
then Just renderedUserTime
else do
let isNotSender = ((/=) `on` uId) sender user
shouldShowThisConversion = isNotSender || forModal
shouldShowThisConversion = isNotSender || (modalOrEphemeral == IsModal)
guard shouldShowThisConversion
Just "You are in this timezone"
Just renderedUserTime
totalClockChanges = checkForClockChanges timeRef timeRefSucess userTzLabel
mbClockChangeWarning = do
guard $ not $ null totalClockChanges
Expand Down Expand Up @@ -345,13 +345,13 @@ shownTimezoneOnErrors implicitSenderTimezone tzLabel forSender
-- and under `Right` we collect valid time references that should be rendered differently
-- for each user.
renderEphemeralMessageConversionPair
:: ModalFlag
:: ModalOrEphemeral
-> User
-> (TimeReference, TimeReferenceToUTCResult)
-> EitherTemplateUnit
renderEphemeralMessageConversionPair modalFlag sender (timeRef, result) = case result of
renderEphemeralMessageConversionPair modalOrEphemeral sender (timeRef, result) = case result of
TRTUSuccess timeRefSuc ->
Right $ renderOnSuccess modalFlag sender timeRef timeRefSuc
Right $ renderOnSuccess modalOrEphemeral sender timeRef timeRefSuc
TRTUAmbiguous overlapInfo ->
Left $ renderOnOverlap sender timeRef overlapInfo
TRTUInvalid gapInfo ->
Expand Down
14 changes: 7 additions & 7 deletions test/Test/TzBot/RenderSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ test_renderSpec = TestGroup "Render"
mkChatCase arbitraryTime1 "10am" userMoscow userMoscow2
[ convertWithoutNotes
"\"10am\", 30 January 2023 in Europe/Moscow"
"You are in this timezone"
"10:00, Monday, 30 January 2023 in your timezone (Europe/Moscow)"
]
, testCase "Back to author, same timezone" $
mkChatCase arbitraryTime1 "10am" userMoscow userMoscow
Expand Down Expand Up @@ -161,7 +161,7 @@ test_renderSpec = TestGroup "Render"
mkModalCase arbitraryTime1 "10am" userMoscow userMoscow
[ convertWithoutNotes
"\"10am\", 30 January 2023 in Europe/Moscow"
"You are in this timezone"
"10:00, Monday, 30 January 2023 in your timezone (Europe/Moscow)"
]
]

Expand Down Expand Up @@ -254,19 +254,19 @@ convertWithoutNotes :: Text -> Text -> ConversionPair
convertWithoutNotes q w = ConversionPair q w Nothing Nothing

mkModalCase :: HasCallStack => UTCTime -> Text -> User -> User -> [ConversionPair] -> Assertion
mkModalCase = mkTestCase asForModalM
mkModalCase = mkTestCase IsModal

mkChatCase :: HasCallStack => UTCTime -> Text -> User -> User -> [ConversionPair] -> Assertion
mkChatCase = mkTestCase asForMessageM
mkChatCase = mkTestCase IsEphemeral

convertWithCommonNote :: Text -> Text -> Text -> ConversionPair
convertWithCommonNote q w e = ConversionPair q w (Just e) (Just e)

mkTestCase :: HasCallStack => ModalFlag -> UTCTime -> Text -> User -> User -> [ConversionPair] -> Assertion
mkTestCase modalFlag eventTimestamp refText sender otherUser expectedOtherUserConversions = do
mkTestCase :: HasCallStack => ModalOrEphemeral -> UTCTime -> Text -> User -> User -> [ConversionPair] -> Assertion
mkTestCase modalOrEphemeral eventTimestamp refText sender otherUser expectedOtherUserConversions = do
let [timeRef] = parseTimeRefs refText
ephemeralTemplate =
renderTemplate modalFlag eventTimestamp sender $
renderTemplate modalOrEphemeral eventTimestamp sender $
NE.singleton timeRef

otherUserConversions = renderAllConversionPairs otherUser ephemeralTemplate
Expand Down