From c4f958644facaecd1343187983429abb0c1a501e Mon Sep 17 00:00:00 2001 From: Diogo Castro Date: Thu, 7 Sep 2023 15:42:14 +0100 Subject: [PATCH] [Chore] Refactor `ModalFlag` --- src/TzBot/ProcessEvents/Common.hs | 5 ++-- src/TzBot/ProcessEvents/Message.hs | 4 ++-- src/TzBot/Render.hs | 38 +++++++++++++++--------------- test/Test/TzBot/RenderSpec.hs | 10 ++++---- 4 files changed, 29 insertions(+), 28 deletions(-) diff --git a/src/TzBot/ProcessEvents/Common.hs b/src/TzBot/ProcessEvents/Common.hs index 377a485..0879993 100644 --- a/src/TzBot/ProcessEvents/Common.hs +++ b/src/TzBot/ProcessEvents/Common.hs @@ -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 @@ -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 diff --git a/src/TzBot/ProcessEvents/Message.hs b/src/TzBot/ProcessEvents/Message.hs index 3782388..2362844 100644 --- a/src/TzBot/ProcessEvents/Message.hs +++ b/src/TzBot/ProcessEvents/Message.hs @@ -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 @@ -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 diff --git a/src/TzBot/Render.hs b/src/TzBot/Render.hs index ad2f351..0c9296d 100644 --- a/src/TzBot/Render.hs +++ b/src/TzBot/Render.hs @@ -26,9 +26,7 @@ module TzBot.Render , asForSenderS , asForOthersS - , ModalFlag - , asForModalM - , asForMessageM + , ModalOrEphemeral(..) ) where import TzPrelude @@ -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 @@ -166,7 +166,7 @@ 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 renderedUserTime totalClockChanges = checkForClockChanges timeRef timeRefSucess userTzLabel @@ -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 -> diff --git a/test/Test/TzBot/RenderSpec.hs b/test/Test/TzBot/RenderSpec.hs index f2dde83..d62ce0c 100644 --- a/test/Test/TzBot/RenderSpec.hs +++ b/test/Test/TzBot/RenderSpec.hs @@ -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