From b2e46554fede1db2358bb5c02b41b70d9f37ad93 Mon Sep 17 00:00:00 2001 From: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com> Date: Wed, 20 Dec 2023 21:03:27 +0400 Subject: [PATCH] use withStoreBatch --- src/Simplex/Chat.hs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index b758b7901b..f7ecbe8aa3 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -5617,7 +5617,7 @@ sendBatchedDirectMessages :: forall e m. (MsgEncodingI e, ChatMonad m) => Connec sendBatchedDirectMessages conn@Connection {connId} events connOrGroupId = do when (connDisabled conn) $ throwChatError (CEConnectionDisabled conn) (errs, msgs) <- partitionEithers <$> createSndMessages - unless (null errs) $ toView $ CRChatErrors Nothing (map ChatErrorStore errs) + unless (null errs) $ toView $ CRChatErrors Nothing errs forM_ (L.nonEmpty msgs) $ \msgs' -> do let (largeMsgs, msgBatches) = partitionBatches $ batchChatMessages msgs' errs' = map (\SndMessage {msgId} -> ChatError $ CELargeChatMsg msgId) largeMsgs @@ -5626,15 +5626,16 @@ sendBatchedDirectMessages conn@Connection {connId} events connOrGroupId = do let batchBody = LB.toStrict $ Builder.toLazyByteString batchBuilder agentMsgId <- withAgent $ \a -> sendMessage a (aConnId conn) MsgFlags {notification = True} batchBody let sndMsgDelivery = SndMsgDelivery {connId, agentMsgId} - withStore' $ \db -> forM_ sndMsgs $ \SndMessage {msgId} -> - createSndMsgDelivery db sndMsgDelivery msgId + withStoreBatch' $ \db -> map (\SndMessage {msgId} -> createSndMsgDelivery db sndMsgDelivery msgId) sndMsgs where - createSndMessages :: m [Either StoreError SndMessage] + createSndMessages :: m [Either ChatError SndMessage] createSndMessages = do gVar <- asks idsDrg ChatConfig {chatVRange} <- asks config - withStore' $ \db -> forM (toList events) $ \event -> - runExceptT $ createNewSndMessage db gVar connOrGroupId (newMsg chatVRange event) + withStoreBatch $ \db -> map (createMsg db gVar chatVRange) (toList events) + createMsg db gVar chatVRange evnt = do + r <- runExceptT $ createNewSndMessage db gVar connOrGroupId (newMsg chatVRange evnt) + pure $ first ChatErrorStore r newMsg chatVRange chatMsgEvent sharedMsgId = do let r = encodeChatMessage ChatMessage {chatVRange, msgId = Just sharedMsgId, chatMsgEvent} fmap (NewMessage chatMsgEvent) r