Skip to content

Commit

Permalink
[#109] Enhance MessageContext
Browse files Browse the repository at this point in the history
Problem: The `MessageContext` data type adds a `message_id` label to log
messages, so we can identify which Slack message the server was
processing when the log event occurred.

However, a message ID (or message timestamp, really) is not enough to
identify a message. A message is uniquely identified by the `(timestamp,
channel_id)` pair.

Solution: Add a `channel_id` label to `MessageContext`.
  • Loading branch information
dcastro committed Sep 7, 2023
1 parent 227b5b5 commit 019ebaf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
18 changes: 13 additions & 5 deletions src/TzBot/Logger.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Data.Aeson (KeyValue((.=)), ToJSON(..), object)
import Katip
import Text.Interpolation.Nyan (int, rmode's)

import TzBot.Slack.API (MessageId(..))
import TzBot.Slack.API (ChannelId(..), MessageId(..))

logSugar_ :: (KatipContext m, HasCallStack) => Severity -> Text -> m ()
logSugar_ sev = logLocM sev . logStr
Expand Down Expand Up @@ -50,7 +50,10 @@ withLogger logLevel action = do
katipAddNamespaceText :: (KatipContext m) => Text -> m a -> m a
katipAddNamespaceText txt = katipAddNamespace $ Namespace [txt]

-- contexts
----------------------------------------------------------------------------
-- Contexts
----------------------------------------------------------------------------

type EventType = Text
type EventId = Text
data EventContext = EventContext EventType EventId
Expand All @@ -67,11 +70,16 @@ instance ToObject EventContext
instance LogItem EventContext where
payloadKeys _verb _a = AllKeys

--
newtype MessageContext = MessageContext MessageId
-- | A message is uniquely identified by the Channel ID
-- and the message timestamp (i.e. `MessageId`).
data MessageContext = MessageContext ChannelId MessageId

instance ToJSON MessageContext where
toJSON (MessageContext msgId) = object ["message_id" .= msgId]
toJSON (MessageContext channelId msgId) =
object
[ "channel_id" .= channelId
, "message_id" .= msgId
]

instance ToObject MessageContext

Expand Down
7 changes: 2 additions & 5 deletions src/TzBot/ProcessEvents/Message.hs
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,11 @@ withSenderNotBot evt = do
processMessageEvent :: MessageEvent -> BotM ()
processMessageEvent evt =
katipAddNamespaceText "message" $
katipAddContext (MessageContext msgId) $
katipAddContext (MessageContext evt.meChannel evt.meMessage.mMessageId) $
whenJustM (filterMessageTypeWithLog evt) $ \mEventType ->
whenJustM (withSenderNotBot evt) $ \sender -> do
timeRefs <- getTimeReferencesFromMessage msg
timeRefs <- getTimeReferencesFromMessage evt.meMessage
processMessageEvent' evt mEventType sender timeRefs
where
msg = meMessage evt
msgId = mMessageId $ meMessage evt

processMessageEvent'
:: MessageEvent
Expand Down

0 comments on commit 019ebaf

Please sign in to comment.