From d4925b7cddd4bf14e473470f8c45783be0edce7c Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Wed, 23 Mar 2022 20:52:00 +0000 Subject: [PATCH] core: api to update user profile in one request (#461) --- src/Simplex/Chat.hs | 6 +++++- src/Simplex/Chat/Controller.hs | 1 + tests/ChatTests.hs | 5 ++++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index 607c771f17..7c5314db9e 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -276,6 +276,7 @@ processChatCommand = \case `E.finally` deleteContactRequest st userId connReqId withAgent $ \a -> rejectContact a connId invId pure $ CRContactRequestRejected cReq + APIUpdateProfile profile -> withUser (`updateProfile` profile) GetUserSMPServers -> CRUserSMPServers <$> withUser (\user -> withStore (`getSMPServers` user)) SetUserSMPServers smpServers -> withUser $ \user -> withChatLock $ do withStore $ \st -> overwriteSMPServers st user smpServers @@ -1572,6 +1573,7 @@ chatCommandP = <|> "/_delete " *> (APIDeleteChat <$> chatTypeP <*> A.decimal) <|> "/_accept " *> (APIAcceptContact <$> A.decimal) <|> "/_reject " *> (APIRejectContact <$> A.decimal) + <|> "/_profile " *> (APIUpdateProfile <$> jsonP) <|> "/smp_servers default" $> SetUserSMPServers [] <|> "/smp_servers " *> (SetUserSMPServers <$> smpServersP) <|> "/smp_servers" $> GetUserSMPServers @@ -1628,7 +1630,7 @@ chatCommandP = <|> (CPBefore <$ "before=" <*> A.decimal <* A.space <* "count=" <*> A.decimal) msgContentP = "text " *> (MCText . safeDecodeUtf8 <$> A.takeByteString) - <|> "json " *> (J.eitherDecodeStrict' <$?> A.takeByteString) + <|> "json " *> jsonP msgDeleteMode = "broadcast" $> MDBroadcast <|> "internal" $> MDInternal displayName = safeDecodeUtf8 <$> (B.cons <$> A.satisfy refChar <*> A.takeTill (== ' ')) sendMsgQuote msgDir = SendMessageQuote <$> displayName <* A.space <*> pure msgDir <*> quotedMsg <*> A.takeByteString @@ -1642,6 +1644,8 @@ chatCommandP = userProfile = do (cName, fullName) <- userNames pure Profile {displayName = cName, fullName, image = Nothing} + jsonP :: J.FromJSON a => Parser a + jsonP = J.eitherDecodeStrict' <$?> A.takeByteString groupProfile = do gName <- displayName fullName <- fullNameP gName diff --git a/src/Simplex/Chat/Controller.hs b/src/Simplex/Chat/Controller.hs index 6ea60d7549..d2bb4bfeb4 100644 --- a/src/Simplex/Chat/Controller.hs +++ b/src/Simplex/Chat/Controller.hs @@ -103,6 +103,7 @@ data ChatCommand | APIDeleteChat ChatType Int64 | APIAcceptContact Int64 | APIRejectContact Int64 + | APIUpdateProfile Profile | GetUserSMPServers | SetUserSMPServers [SMPServer] | ChatHelp HelpSection diff --git a/tests/ChatTests.hs b/tests/ChatTests.hs index a3e633495a..19b28a7cbb 100644 --- a/tests/ChatTests.hs +++ b/tests/ChatTests.hs @@ -794,11 +794,14 @@ testUpdateProfileImage = testChat2 aliceProfile bobProfile $ \alice bob -> do connectUsers alice bob - -- Note we currently don't support removing profile image. alice ##> "/profile_image data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIAQMAAAD+wSzIAAAABlBMVEX///+/v7+jQ3Y5AAAADklEQVQI12P4AIX8EAgALgAD/aNpbtEAAAAASUVORK5CYII=" alice <## "profile image updated" alice ##> "/profile_image" alice <## "profile image removed" + alice ##> "/_profile {\"displayName\": \"alice2\", \"fullName\": \"\"}" + alice <## "user profile is changed to alice2 (your contacts are notified)" + bob <## "contact alice changed to alice2" + bob <## "use @alice2 to send messages" (bob