Skip to content

Commit 861fb8c

Browse files
[#22] Revise messages editing (#54)
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.
1 parent bd48796 commit 861fb8c

File tree

12 files changed

+223
-154
lines changed

12 files changed

+223
-154
lines changed

package.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ library:
7272
- utf8-string
7373
- validation
7474
- yaml
75+
- utf8-string
7576

7677
executables:
7778
tzbot-exe:

src/TzBot/BotMain.hs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import Universum
88

99
import Control.Monad.Managed (managed, runManaged)
1010
import Data.ByteString qualified as BS
11-
import Data.Map qualified as M
1211
import Network.HTTP.Client (newManager)
1312
import Network.HTTP.Client.TLS (tlsManagerSettings)
1413
import Options.Applicative (execParser)
@@ -17,6 +16,7 @@ import Slacker
1716
setGracefulShutdownHandler, setOnException)
1817
import System.Directory (doesFileExist)
1918
import Text.Interpolation.Nyan (int, rmode')
19+
import Time (hour)
2020

2121
import TzBot.Cache
2222
(TzCacheSettings(tcsExpiryRandomAmplitudeFraction), defaultTzCacheSettings, withTzCache,
@@ -78,13 +78,17 @@ run opts = do
7878
& setGracefulShutdownHandler extractShutdownFunction
7979

8080
bsManager <- liftIO $ newManager tlsManagerSettings
81-
bsMessagesReferences <- newIORef M.empty
8281
bsFeedbackConfig <-
8382
managed $ withFeedbackConfig bsConfig
8483
bsUserInfoCache <-
8584
managed $ withTzCache fifteenPercentAmplitudeSettings cCacheUsersInfo
8685
bsConversationMembersCache <-
8786
managed $ withTzCache fifteenPercentAmplitudeSettings cCacheConversationMembers
87+
let defaultMessageInfoCachingTime = hour 1
88+
bsMessageCache <-
89+
managed $ withTzCacheDefault defaultMessageInfoCachingTime
90+
bsMessageLinkCache <-
91+
managed $ withTzCacheDefault defaultMessageInfoCachingTime
8892
bsReportEntries <-
8993
managed $ withTzCacheDefault cCacheReportDialog
9094
-- auto-acknowledge received messages

src/TzBot/Cache.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ module TzBot.Cache
1616
, lookup
1717

1818
-- * Altering cache
19-
, insertRandomized
20-
, fetchWithCacheRandomized
19+
, insert
20+
, fetchWithCache
2121
, update
2222
) where
2323

@@ -106,13 +106,13 @@ cleaningThread cleaningPeriod cache = forever $ do
106106

107107
-- | Generate a random expiry time and insert a key/value pair into
108108
-- the cache with that expiry time.
109-
insertRandomized
109+
insert
110110
:: (Eq k, Hashable k, MonadIO m)
111111
=> k
112112
-> v
113113
-> TzCache k v
114114
-> m ()
115-
insertRandomized key val TzCache {..} = do
115+
insert key val TzCache {..} = do
116116
expiry <- case rcExpiryRandomAmplitude of
117117
Nothing -> pure rcExpiry
118118
Just randAmp -> do
@@ -124,13 +124,13 @@ insertRandomized key val TzCache {..} = do
124124
-- If the value is either absent or expired, perform given fetch action
125125
-- and insert the obtained value with configured expiration parameters
126126
-- into the cache.
127-
fetchWithCacheRandomized
127+
fetchWithCache
128128
:: (Eq k, Hashable k, MonadIO m, KatipContext m, Buildable k)
129129
=> k
130130
-> (k -> m v)
131131
-> TzCache k v
132132
-> m v
133-
fetchWithCacheRandomized key fetchAction cache =
133+
fetchWithCache key fetchAction cache =
134134
katipAddNamespace "cache" $ do
135135
logDebug [int||Fetching key=#{key}|]
136136
mv <- liftIO $ Cache.lookup (rcCache cache) key
@@ -142,7 +142,7 @@ fetchWithCacheRandomized key fetchAction cache =
142142
using provided fetching action
143143
|]
144144
v <- fetchAction key
145-
insertRandomized key v cache
145+
insert key v cache
146146
pure v
147147

148148
lookup

src/TzBot/Feedback/Dialog.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import TzBot.RunMonad (BotM, BotState(bsReportEntries))
2020
insertDialogEntry :: ReportDialogId -> ReportDialogEntry -> BotM ()
2121
insertDialogEntry id_ entry = do
2222
dialogEntriesCache <- asks bsReportEntries
23-
Cache.insertRandomized id_ entry dialogEntriesCache
23+
Cache.insert id_ entry dialogEntriesCache
2424

2525
lookupDialogEntry :: ReportDialogId -> BotM (Maybe ReportDialogEntry)
2626
lookupDialogEntry id_ = do

src/TzBot/Logger.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Universum
1414

1515
import Data.Aeson (KeyValue((.=)), ToJSON(..), object)
1616
import Katip
17+
import Text.Interpolation.Nyan (int, rmode's)
1718

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

@@ -26,6 +27,9 @@ logWarn t = withFrozenCallStack $ logSugar_ WarningS t
2627
logDebug t = withFrozenCallStack $ logSugar_ DebugS t
2728
logError t = withFrozenCallStack $ logSugar_ ErrorS t
2829

30+
logException :: (Exception e, KatipContext m) => e -> m ()
31+
logException err = logError [int||Error occured: #s{err}|]
32+
2933
withLogger
3034
:: Severity
3135
-> ((Namespace, LogContexts, LogEnv) -> IO a)

src/TzBot/ProcessEvents.hs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ handler shutdownRef bState _cfg e = run $ do
8787
run action = void $ runBotM bState $ do
8888
eithRes <- catchAllErrors action
8989
whenLeft eithRes $ \eithErr -> do
90-
let logException :: (Exception e) => e -> BotM ()
91-
logException err = logError [int||Error occured: #s{err}|]
9290
case eithErr of
9391
Left someExc
9492
| Just UserInterrupt <- fromException someExc ->

0 commit comments

Comments
 (0)