From dbf6b1f673915f383d25a6c547baa935e28774c8 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Thu, 5 May 2022 11:52:32 +0100 Subject: [PATCH] core: use filename in quote when there is no message text (#602) --- src/Simplex/Chat.hs | 16 +++++++++------ src/Simplex/Chat/View.hs | 6 +++--- tests/ChatTests.hs | 42 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 53 insertions(+), 11 deletions(-) diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index 8874a4efca..c75713ed4d 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -225,11 +225,11 @@ processChatCommand = \case prepareMsg fileInvitation_ = case quotedItemId_ of Nothing -> pure (MCSimple (ExtMsgContent mc fileInvitation_), Nothing) Just quotedItemId -> do - CChatItem _ ChatItem {meta = CIMeta {itemTs, itemSharedMsgId}, content = ciContent, formattedText} <- + CChatItem _ ChatItem {meta = CIMeta {itemTs, itemSharedMsgId}, content = ciContent, formattedText, file} <- withStore $ \st -> getDirectChatItem st userId chatId quotedItemId (origQmc, qd, sent) <- quoteData ciContent let msgRef = MsgRef {msgId = itemSharedMsgId, sentAt = itemTs, sent, memberId = Nothing} - qmc = quoteContent origQmc mc + qmc = quoteContent origQmc file mc quotedItem = CIQuote {chatDir = qd, itemId = Just quotedItemId, sharedMsgId = itemSharedMsgId, sentAt = itemTs, content = qmc, formattedText} pure (MCQuote QuotedMsg {msgRef, content = qmc} (ExtMsgContent mc fileInvitation_), Just quotedItem) where @@ -261,11 +261,11 @@ processChatCommand = \case prepareMsg fileInvitation_ membership = case quotedItemId_ of Nothing -> pure (MCSimple (ExtMsgContent mc fileInvitation_), Nothing) Just quotedItemId -> do - CChatItem _ ChatItem {chatDir, meta = CIMeta {itemTs, itemSharedMsgId}, content = ciContent, formattedText} <- + CChatItem _ ChatItem {chatDir, meta = CIMeta {itemTs, itemSharedMsgId}, content = ciContent, formattedText, file} <- withStore $ \st -> getGroupChatItem st user chatId quotedItemId (origQmc, qd, sent, GroupMember {memberId}) <- quoteData ciContent chatDir membership let msgRef = MsgRef {msgId = itemSharedMsgId, sentAt = itemTs, sent, memberId = Just memberId} - qmc = quoteContent origQmc mc + qmc = quoteContent origQmc file mc quotedItem = CIQuote {chatDir = qd, itemId = Just quotedItemId, sharedMsgId = itemSharedMsgId, sentAt = itemTs, content = qmc, formattedText} pure (MCQuote QuotedMsg {msgRef, content = qmc} (ExtMsgContent mc fileInvitation_), Just quotedItem) where @@ -276,9 +276,13 @@ processChatCommand = \case CTContactRequest -> pure $ chatCmdError "not supported" CTContactConnection -> pure $ chatCmdError "not supported" where - quoteContent qmc = \case + quoteContent :: forall d. MsgContent -> Maybe (CIFile d) -> MsgContent -> MsgContent + quoteContent qmc ciFile_ = \case MCText _ -> qmc - _ -> MCText $ msgContentText qmc + _ -> + let t = msgContentText qmc + fileName' = T.pack . (fileName :: CIFile d -> String) + in MCText $ if T.null t then maybe t fileName' ciFile_ else t unzipMaybe :: Maybe (a, b) -> (Maybe a, Maybe b) unzipMaybe t = (fst <$> t, snd <$> t) -- TODO discontinue diff --git a/src/Simplex/Chat/View.hs b/src/Simplex/Chat/View.hs index c63dbd68b3..5b79a664a1 100644 --- a/src/Simplex/Chat/View.hs +++ b/src/Simplex/Chat/View.hs @@ -223,9 +223,9 @@ viewChatItem chat ChatItem {chatDir, meta, content, quotedItem, file} = case cha withFile view dir l = maybe l (\f -> l <> view dir f meta) file sndMsg = msg viewSentMessage rcvMsg = msg viewReceivedMessage - msg view dir quote mc = case (msgContentText mc, file) of - ("", Just _) -> [] - -- (_, Just _) -> prependFirst " " $ ttyMsgContent mc + msg view dir quote mc = case (msgContentText mc, file, quote) of + ("", Just _, []) -> [] + ("", Just CIFile {fileName}, _) -> view dir quote (MCText $ T.pack fileName) meta _ -> view dir quote mc meta viewItemUpdate :: MsgDirectionI d => ChatInfo c -> ChatItem c d -> [StyledString] diff --git a/tests/ChatTests.hs b/tests/ChatTests.hs index 068b8551ec..54509eef8e 100644 --- a/tests/ChatTests.hs +++ b/tests/ChatTests.hs @@ -1278,12 +1278,50 @@ testSendImageWithTextAndQuote = (bob <## "completed receiving file 1 (test.jpg) from alice") (alice <## "completed sending file 1 (test.jpg) to bob") src <- B.readFile "./tests/fixtures/test.jpg" - dest <- B.readFile "./tests/tmp/test.jpg" - dest `shouldBe` src + B.readFile "./tests/tmp/test.jpg" `shouldReturn` src alice #$> ("/_get chat @2 count=100", chat'', [((0, "hi alice"), Nothing, Nothing), ((1, "hey bob"), Just (0, "hi alice"), Just "./tests/fixtures/test.jpg")]) alice @@@ [("@bob", "hey bob")] bob #$> ("/_get chat @2 count=100", chat'', [((1, "hi alice"), Nothing, Nothing), ((0, "hey bob"), Just (1, "hi alice"), Just "./tests/tmp/test.jpg")]) bob @@@ [("@alice", "hey bob")] + -- quoting (file + text) with file uses quoted text + bob ##> "/_send @2 file ./tests/fixtures/test.txt quoted 2 json {\"text\":\"\",\"type\":\"file\"}" + bob <# "@alice > hey bob" + bob <## " test.txt" + bob <# "/f @alice ./tests/fixtures/test.txt" + bob <## "use /fc 2 to cancel sending" + alice <# "bob> > hey bob" + alice <## " test.txt" + alice <# "bob> sends file test.txt (11 bytes / 11 bytes)" + alice <## "use /fr 2 [/ | ] to receive it" + alice ##> "/fr 2 ./tests/tmp" + alice <## "saving file 2 from bob to ./tests/tmp/test.txt" + concurrently_ + (alice <## "started receiving file 2 (test.txt) from bob") + (bob <## "started sending file 2 (test.txt) to alice") + concurrently_ + (alice <## "completed receiving file 2 (test.txt) from bob") + (bob <## "completed sending file 2 (test.txt) to alice") + txtSrc <- B.readFile "./tests/fixtures/test.txt" + B.readFile "./tests/tmp/test.txt" `shouldReturn` txtSrc + -- quoting (file without text) with file uses file name + alice ##> "/_send @2 file ./tests/fixtures/test.jpg quoted 3 json {\"text\":\"\",\"type\":\"image\",\"image\":\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIAQMAAAD+wSzIAAAABlBMVEX///+/v7+jQ3Y5AAAADklEQVQI12P4AIX8EAgALgAD/aNpbtEAAAAASUVORK5CYII=\"}" + alice <# "@bob > test.txt" + alice <## " test.jpg" + alice <# "/f @bob ./tests/fixtures/test.jpg" + alice <## "use /fc 3 to cancel sending" + bob <# "alice> > test.txt" + bob <## " test.jpg" + bob <# "alice> sends file test.jpg (136.5 KiB / 139737 bytes)" + bob <## "use /fr 3 [/ | ] to receive it" + bob ##> "/fr 3 ./tests/tmp" + bob <## "saving file 3 from alice to ./tests/tmp/test_1.jpg" + concurrently_ + (bob <## "started receiving file 3 (test.jpg) from alice") + (alice <## "started sending file 3 (test.jpg) to bob") + concurrently_ + (bob <## "completed receiving file 3 (test.jpg) from alice") + (alice <## "completed sending file 3 (test.jpg) to bob") + B.readFile "./tests/tmp/test_1.jpg" `shouldReturn` src testGroupSendImage :: IO () testGroupSendImage =