From 331b4633fc951c90df9a03c34e1a4804ce675149 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Mon, 13 May 2024 20:39:13 +0100 Subject: [PATCH] core: platform independent encoding for delete command (#4176) --- src/Simplex/Chat.hs | 20 ++++++++++++++------ src/Simplex/Chat/Controller.hs | 8 +++----- tests/ChatTests/Direct.hs | 6 +++--- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index 27b9cfa0cd..2925ef000e 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -1023,12 +1023,12 @@ processChatCommand' vr = \case liftIO $ updateNoteFolderUnreadChat db user nf unreadChat ok user _ -> pure $ chatCmdError (Just user) "not supported" - APIDeleteChat cRef@(ChatRef cType chatId) chatDeleteMode -> withUser $ \user@User {userId} -> case cType of + APIDeleteChat cRef@(ChatRef cType chatId) cdm -> withUser $ \user@User {userId} -> case cType of CTDirect -> do ct <- withStore $ \db -> getContact db vr user chatId filesInfo <- withStore' $ \db -> getContactFileInfo db user ct withContactLock "deleteChat direct" chatId . procCmd $ - case chatDeleteMode of + case cdm of CDMFull notify -> do cancelFilesInProgress user filesInfo deleteFilesLocally filesInfo @@ -1574,7 +1574,7 @@ processChatCommand' vr = \case CPContactAddress (CAPContactViaAddress Contact {contactId}) -> processChatCommand $ APIConnectContactViaAddress userId incognito contactId _ -> processChatCommand $ APIConnect userId incognito (Just cReqUri) - DeleteContact cName -> withContactName cName $ \ctId -> APIDeleteChat (ChatRef CTDirect ctId) (CDMFull True) + DeleteContact cName cdm -> withContactName cName $ \ctId -> APIDeleteChat (ChatRef CTDirect ctId) cdm ClearContact cName -> withContactName cName $ APIClearChat . ChatRef CTDirect APIListContacts userId -> withUserId userId $ \user -> CRContactsList user <$> withStore' (\db -> getUserContacts db vr user) @@ -7128,8 +7128,7 @@ chatCommandP = "/read user" $> UserRead, "/_read chat " *> (APIChatRead <$> chatRefP <*> optional (A.space *> ((,) <$> ("from=" *> A.decimal) <* A.space <*> ("to=" *> A.decimal)))), "/_unread chat " *> (APIChatUnread <$> chatRefP <* A.space <*> onOffP), - "/_delete " *> (APIDeleteChat <$> chatRefP <* A.space <*> jsonP), - "/_delete " *> (APIDeleteChat <$> chatRefP <*> (CDMFull <$> (A.space *> "notify=" *> onOffP <|> pure True))), + "/_delete " *> (APIDeleteChat <$> chatRefP <*> chatDeleteMode), "/_clear chat " *> (APIClearChat <$> chatRefP), "/_accept" *> (APIAcceptContact <$> incognitoOnOffP <* A.space <*> A.decimal), "/_reject " *> (APIRejectContact <$> A.decimal), @@ -7235,7 +7234,7 @@ chatCommandP = ("/remove " <|> "/rm ") *> char_ '#' *> (RemoveMember <$> displayName <* A.space <* char_ '@' <*> displayName), ("/leave " <|> "/l ") *> char_ '#' *> (LeaveGroup <$> displayName), ("/delete #" <|> "/d #") *> (DeleteGroup <$> displayName), - ("/delete " <|> "/d ") *> char_ '@' *> (DeleteContact <$> displayName), + ("/delete " <|> "/d ") *> char_ '@' *> (DeleteContact <$> displayName <*> chatDeleteMode), "/clear *" $> ClearNoteFolder, "/clear #" *> (ClearGroup <$> displayName), "/clear " *> char_ '@' *> (ClearContact <$> displayName), @@ -7387,6 +7386,15 @@ chatCommandP = mcTextP = MCText . safeDecodeUtf8 <$> A.takeByteString msgContentP = "text " *> mcTextP <|> "json " *> jsonP ciDeleteMode = "broadcast" $> CIDMBroadcast <|> "internal" $> CIDMInternal + chatDeleteMode = + A.choice + [ " full" *> (CDMFull <$> notifyP), + " entity" *> (CDMEntity <$> notifyP), + " messages" $> CDMMessages, + CDMFull <$> notifyP -- backwards compatible + ] + where + notifyP = " notify=" *> onOffP <|> pure True displayName = safeDecodeUtf8 <$> (quoted "'" <|> takeNameTill isSpace) where takeNameTill p = diff --git a/src/Simplex/Chat/Controller.hs b/src/Simplex/Chat/Controller.hs index c21d1bf768..58e0e2e321 100644 --- a/src/Simplex/Chat/Controller.hs +++ b/src/Simplex/Chat/Controller.hs @@ -395,7 +395,7 @@ data ChatCommand | Connect IncognitoEnabled (Maybe AConnectionRequestUri) | APIConnectContactViaAddress UserId IncognitoEnabled ContactId | ConnectSimplex IncognitoEnabled -- UserId (not used in UI) - | DeleteContact ContactName + | DeleteContact ContactName ChatDeleteMode | ClearContact ContactName | APIListContacts UserId | ListContacts @@ -827,9 +827,9 @@ clqNoFilters :: ChatListQuery clqNoFilters = CLQFilters {favorite = False, unread = False} data ChatDeleteMode - = CDMFull {notify :: Bool} -- delete both contact and conversation + = CDMFull {notify :: Bool} -- delete both contact and conversation | CDMEntity {notify :: Bool} -- delete contact (connection), keep conversation - | CDMMessages -- delete conversation, keep contact - can be re-opened from Contacts view + | CDMMessages -- delete conversation, keep contact - can be re-opened from Contacts view deriving (Show) data ConnectionPlan @@ -1400,8 +1400,6 @@ $(JQ.deriveJSON (enumJSON $ dropPrefix "HS") ''HelpSection) $(JQ.deriveJSON (sumTypeJSON $ dropPrefix "CLQ") ''ChatListQuery) -$(JQ.deriveJSON (sumTypeJSON $ dropPrefix "CDM") ''ChatDeleteMode) - $(JQ.deriveJSON (sumTypeJSON $ dropPrefix "ILP") ''InvitationLinkPlan) $(JQ.deriveJSON (sumTypeJSON $ dropPrefix "CAP") ''ContactAddressPlan) diff --git a/tests/ChatTests/Direct.hs b/tests/ChatTests/Direct.hs index 16082cb30b..54f31ff50f 100644 --- a/tests/ChatTests/Direct.hs +++ b/tests/ChatTests/Direct.hs @@ -346,7 +346,7 @@ testDeleteContactDeletesProfile = connectUsers alice bob alice <##> bob -- alice deletes contact, profile is deleted - alice ##> "/_delete @2 {\"type\": \"full\", \"notify\": true}" + alice ##> "/_delete @2 full notify=on" alice <## "bob: contact is deleted" bob <## "alice (Alice) deleted contact with you" alice ##> "/_contacts 1" @@ -366,7 +366,7 @@ testDeleteContactKeepConversation = connectUsers alice bob alice <##> bob - alice ##> "/_delete @2 {\"type\": \"entity\", \"notify\": true}" + alice ##> "/_delete @2 entity notify=on" alice <## "bob: contact is deleted" bob <## "alice (Alice) deleted contact with you" @@ -386,7 +386,7 @@ testDeleteConversationKeepContact = alice @@@ [("@bob", "hey")] - alice ##> "/_delete @2 {\"type\": \"messages\", \"notify\": true}" + alice ##> "/_delete @2 messages" alice <## "bob: contact is deleted" alice @@@ [("@bob", "")] -- UI would filter