Skip to content

Commit

Permalink
[#22] Revise messages editing (#54)
Browse files Browse the repository at this point in the history
Problem: Currently message_changed event is handled by translating new
time references and sending ephemeral with them, which can be
inconvenient sometimes.

Solution: On message_changed event send an ephemeral containing message
permalink and all message time references translated.
  • Loading branch information
YuriRomanowski authored Feb 10, 2023
1 parent bd48796 commit 861fb8c
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 154 deletions.
1 change: 1 addition & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ library:
- utf8-string
- validation
- yaml
- utf8-string

executables:
tzbot-exe:
Expand Down
8 changes: 6 additions & 2 deletions src/TzBot/BotMain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import Universum

import Control.Monad.Managed (managed, runManaged)
import Data.ByteString qualified as BS
import Data.Map qualified as M
import Network.HTTP.Client (newManager)
import Network.HTTP.Client.TLS (tlsManagerSettings)
import Options.Applicative (execParser)
Expand All @@ -17,6 +16,7 @@ import Slacker
setGracefulShutdownHandler, setOnException)
import System.Directory (doesFileExist)
import Text.Interpolation.Nyan (int, rmode')
import Time (hour)

import TzBot.Cache
(TzCacheSettings(tcsExpiryRandomAmplitudeFraction), defaultTzCacheSettings, withTzCache,
Expand Down Expand Up @@ -78,13 +78,17 @@ run opts = do
& setGracefulShutdownHandler extractShutdownFunction

bsManager <- liftIO $ newManager tlsManagerSettings
bsMessagesReferences <- newIORef M.empty
bsFeedbackConfig <-
managed $ withFeedbackConfig bsConfig
bsUserInfoCache <-
managed $ withTzCache fifteenPercentAmplitudeSettings cCacheUsersInfo
bsConversationMembersCache <-
managed $ withTzCache fifteenPercentAmplitudeSettings cCacheConversationMembers
let defaultMessageInfoCachingTime = hour 1
bsMessageCache <-
managed $ withTzCacheDefault defaultMessageInfoCachingTime
bsMessageLinkCache <-
managed $ withTzCacheDefault defaultMessageInfoCachingTime
bsReportEntries <-
managed $ withTzCacheDefault cCacheReportDialog
-- auto-acknowledge received messages
Expand Down
14 changes: 7 additions & 7 deletions src/TzBot/Cache.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ module TzBot.Cache
, lookup

-- * Altering cache
, insertRandomized
, fetchWithCacheRandomized
, insert
, fetchWithCache
, update
) where

Expand Down Expand Up @@ -106,13 +106,13 @@ cleaningThread cleaningPeriod cache = forever $ do

-- | Generate a random expiry time and insert a key/value pair into
-- the cache with that expiry time.
insertRandomized
insert
:: (Eq k, Hashable k, MonadIO m)
=> k
-> v
-> TzCache k v
-> m ()
insertRandomized key val TzCache {..} = do
insert key val TzCache {..} = do
expiry <- case rcExpiryRandomAmplitude of
Nothing -> pure rcExpiry
Just randAmp -> do
Expand All @@ -124,13 +124,13 @@ insertRandomized key val TzCache {..} = do
-- If the value is either absent or expired, perform given fetch action
-- and insert the obtained value with configured expiration parameters
-- into the cache.
fetchWithCacheRandomized
fetchWithCache
:: (Eq k, Hashable k, MonadIO m, KatipContext m, Buildable k)
=> k
-> (k -> m v)
-> TzCache k v
-> m v
fetchWithCacheRandomized key fetchAction cache =
fetchWithCache key fetchAction cache =
katipAddNamespace "cache" $ do
logDebug [int||Fetching key=#{key}|]
mv <- liftIO $ Cache.lookup (rcCache cache) key
Expand All @@ -142,7 +142,7 @@ fetchWithCacheRandomized key fetchAction cache =
using provided fetching action
|]
v <- fetchAction key
insertRandomized key v cache
insert key v cache
pure v

lookup
Expand Down
2 changes: 1 addition & 1 deletion src/TzBot/Feedback/Dialog.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import TzBot.RunMonad (BotM, BotState(bsReportEntries))
insertDialogEntry :: ReportDialogId -> ReportDialogEntry -> BotM ()
insertDialogEntry id_ entry = do
dialogEntriesCache <- asks bsReportEntries
Cache.insertRandomized id_ entry dialogEntriesCache
Cache.insert id_ entry dialogEntriesCache

lookupDialogEntry :: ReportDialogId -> BotM (Maybe ReportDialogEntry)
lookupDialogEntry id_ = do
Expand Down
4 changes: 4 additions & 0 deletions src/TzBot/Logger.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Universum

import Data.Aeson (KeyValue((.=)), ToJSON(..), object)
import Katip
import Text.Interpolation.Nyan (int, rmode's)

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

Expand All @@ -26,6 +27,9 @@ logWarn t = withFrozenCallStack $ logSugar_ WarningS t
logDebug t = withFrozenCallStack $ logSugar_ DebugS t
logError t = withFrozenCallStack $ logSugar_ ErrorS t

logException :: (Exception e, KatipContext m) => e -> m ()
logException err = logError [int||Error occured: #s{err}|]

withLogger
:: Severity
-> ((Namespace, LogContexts, LogEnv) -> IO a)
Expand Down
2 changes: 0 additions & 2 deletions src/TzBot/ProcessEvents.hs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ handler shutdownRef bState _cfg e = run $ do
run action = void $ runBotM bState $ do
eithRes <- catchAllErrors action
whenLeft eithRes $ \eithErr -> do
let logException :: (Exception e) => e -> BotM ()
logException err = logError [int||Error occured: #s{err}|]
case eithErr of
Left someExc
| Just UserInterrupt <- fromException someExc ->
Expand Down
Loading

0 comments on commit 861fb8c

Please sign in to comment.