Skip to content

Commit

Permalink
[Chore] Refactor ModalFlag
Browse files Browse the repository at this point in the history
  • Loading branch information
dcastro committed Sep 7, 2023
1 parent 281e137 commit c4f9586
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 28 deletions.
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
38 changes: 19 additions & 19 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,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
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
10 changes: 5 additions & 5 deletions test/Test/TzBot/RenderSpec.hs
Original file line number Diff line number Diff line change
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

0 comments on commit c4f9586

Please sign in to comment.