core: fix cancelling inline file transfer (#1867)

* core: fix cancelling inline file transfer

* fix test
This commit is contained in:
Evgeny Poberezkin
2023-02-01 00:01:22 +00:00
committed by GitHub
parent c2cd58f63d
commit ea64be55e1
2 changed files with 12 additions and 10 deletions

View File

@@ -3461,7 +3461,7 @@ cancelSndFile user FileTransferMeta {fileId} fts sendCancel = do
catMaybes <$> forM fts (\ft -> cancelSndFileTransfer user ft sendCancel)
cancelSndFileTransfer :: ChatMonad m => User -> SndFileTransfer -> Bool -> m (Maybe ConnId)
cancelSndFileTransfer user ft@SndFileTransfer {agentConnId = AgentConnId acId, fileStatus, fileInline} sendCancel =
cancelSndFileTransfer user@User {userId} ft@SndFileTransfer {fileId, connId, agentConnId = AgentConnId acId, fileStatus, fileInline} sendCancel =
if fileStatus == FSCancelled || fileStatus == FSComplete
then pure Nothing
else cancel' `catchError` (\e -> toView (CRChatError (Just user) e) >> pure fileConnId)
@@ -3470,10 +3470,13 @@ cancelSndFileTransfer user ft@SndFileTransfer {agentConnId = AgentConnId acId, f
withStore' $ \db -> do
updateSndFileStatus db ft FSCancelled
deleteSndFileChunks db ft
when sendCancel $
withAgent (\a -> void (sendMessage a acId SMP.noMsgFlags $ smpEncode FileChunkCancel))
when sendCancel $ case fileInline of
Just _ -> do
(sharedMsgId, conn) <- withStore $ \db -> (,) <$> getSharedMsgIdByFileId db userId fileId <*> getConnectionById db user connId
void . sendDirectMessage conn (BFileChunk sharedMsgId FileChunkCancel) $ ConnectionId connId
_ -> withAgent $ \a -> void . sendMessage a acId SMP.noMsgFlags $ smpEncode FileChunkCancel
pure fileConnId
fileConnId = if isNothing fileInline then Just acId else Nothing
fileConnId = if isJust fileInline then Nothing else Just acId
closeFileHandle :: ChatMonad m => Int64 -> (ChatController -> TVar (Map Int64 Handle)) -> m ()
closeFileHandle fileId files = do

View File

@@ -89,7 +89,7 @@ chatTests = do
describe "sending and receiving files" $ do
describe "send and receive file" $ fileTestMatrix2 runTestFileTransfer
it "send and receive file inline (without accepting)" testInlineFileTransfer
xit "accept inline file transfer, sender cancels during transfer" testAcceptInlineFileSndCancelDuringTransfer
it "accept inline file transfer, sender cancels during transfer" testAcceptInlineFileSndCancelDuringTransfer
it "send and receive small file inline (default config)" testSmallInlineFileTransfer
it "small file sent without acceptance is ignored in terminal by default" testSmallInlineFileIgnored
it "receive file inline with inline=on option" testReceiveInline
@@ -1941,14 +1941,13 @@ testAcceptInlineFileSndCancelDuringTransfer =
[ do
alice <##. "cancelled sending file 1 (test_1MB.pdf)"
alice <## "completed sending file 1 (test_1MB.pdf) to bob",
bob <## "completed receiving file 1 (test_1MB.pdf) from alice"
do
bob <## "completed receiving file 1 (test_1MB.pdf) from alice"
bob <## "alice cancelled sending file 1 (test_1MB.pdf)"
]
_ <- getTermLine alice
alice #> "@bob hi"
bob #> "@alice hey"
_ <- getTermLine bob
bob <## "alice cancelled sending file 1 (test_1MB.pdf)"
bob <# "alice> hi"
bob #> "@alice hey"
alice <# "bob> hey"
where
cfg = testCfg {inlineFiles = defaultInlineFilesConfig {offerChunks = 100, receiveChunks = 50}}