From c34eddb82a2961e0eba253cc296bab241c37de0d Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Fri, 4 Feb 2022 12:41:43 +0000 Subject: [PATCH] fix utf8 encoding for C API requests --- src/Simplex/Chat.hs | 5 ++--- src/Simplex/Chat/Mobile.hs | 2 +- src/Simplex/Chat/Terminal/Input.hs | 3 ++- tests/ChatTests.hs | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index 58b4e2807..3e4fde6f2 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -34,7 +34,6 @@ import qualified Data.Map.Strict as M import Data.Maybe (isJust, mapMaybe) import Data.Text (Text) import qualified Data.Text as T -import Data.Text.Encoding (encodeUtf8) import Data.Time.Clock (UTCTime, getCurrentTime) import Data.Time.LocalTime (getCurrentTimeZone) import Data.Word (Word32) @@ -109,8 +108,8 @@ withLock lock = (void . atomically $ takeTMVar lock) (atomically $ putTMVar lock ()) -execChatCommand :: (MonadUnliftIO m, MonadReader ChatController m) => String -> m ChatResponse -execChatCommand s = case parseAll chatCommandP . B.dropWhileEnd isSpace . encodeUtf8 $ T.pack s of +execChatCommand :: (MonadUnliftIO m, MonadReader ChatController m) => ByteString -> m ChatResponse +execChatCommand s = case parseAll chatCommandP $ B.dropWhileEnd isSpace s of Left e -> pure . CRChatError . ChatError $ CECommandError e Right cmd -> do ChatController {chatLock = l, smpAgent = a, currentUser} <- ask diff --git a/src/Simplex/Chat/Mobile.hs b/src/Simplex/Chat/Mobile.hs index e5bf539ae..280329363 100644 --- a/src/Simplex/Chat/Mobile.hs +++ b/src/Simplex/Chat/Mobile.hs @@ -117,7 +117,7 @@ chatStart ChatStore {dbFilePrefix, chatStore} = do pure cc chatSendCmd :: ChatController -> String -> IO JSONString -chatSendCmd cc s = LB.unpack . J.encode . APIResponse Nothing <$> runReaderT (execChatCommand s) cc +chatSendCmd cc s = LB.unpack . J.encode . APIResponse Nothing <$> runReaderT (execChatCommand $ B.pack s) cc chatRecvMsg :: ChatController -> IO JSONString chatRecvMsg ChatController {outputQ} = json <$> atomically (readTBQueue outputQ) diff --git a/src/Simplex/Chat/Terminal/Input.hs b/src/Simplex/Chat/Terminal/Input.hs index 3670acb43..11e43b325 100644 --- a/src/Simplex/Chat/Terminal/Input.hs +++ b/src/Simplex/Chat/Terminal/Input.hs @@ -9,6 +9,7 @@ import Control.Monad.IO.Unlift import Control.Monad.Reader import Data.List (dropWhileEnd) import qualified Data.Text as T +import Data.Text.Encoding (encodeUtf8) import Simplex.Chat import Simplex.Chat.Controller import Simplex.Chat.Terminal.Output @@ -29,7 +30,7 @@ runInputLoop ct = do q <- asks inputQ forever $ do s <- atomically $ readTBQueue q - r <- execChatCommand s + r <- execChatCommand . encodeUtf8 $ T.pack s liftIO . printToTerminal ct $ responseToView s r runTerminalInput :: (MonadUnliftIO m, MonadReader ChatController m) => ChatTerminal -> m () diff --git a/tests/ChatTests.hs b/tests/ChatTests.hs index 71e1be0b5..6ac2ba4f3 100644 --- a/tests/ChatTests.hs +++ b/tests/ChatTests.hs @@ -65,8 +65,8 @@ testAddContact = concurrently_ (bob <## "alice (Alice): contact is connected") (alice <## "bob (Bob): contact is connected") - alice #> "@bob hello" - bob <# "alice> hello" + alice #> "@bob hello 🙂" + bob <# "alice> hello 🙂" bob #> "@alice hi" alice <# "bob> hi" -- test adding the same contact one more time - local name will be different