From 17cd3cdca452da51fe8622ff65a8dcf88f7905ea Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Thu, 15 Dec 2022 22:11:08 +0400 Subject: [PATCH] core: make ttl optional in TimedMessagesPreference (#1583) * core: make ttl Maybe in TimedMessagesPreference * omitNothingFields --- src/Simplex/Chat/Messages.hs | 5 +++-- src/Simplex/Chat/Types.hs | 8 +++++--- tests/MobileTests.hs | 4 ++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Simplex/Chat/Messages.hs b/src/Simplex/Chat/Messages.hs index 3125d0e8e..bd54ab39a 100644 --- a/src/Simplex/Chat/Messages.hs +++ b/src/Simplex/Chat/Messages.hs @@ -305,8 +305,9 @@ ciTimedToTTL timed_ = timed_ >>= \CITimed {ttl} -> Just ttl contactCITimedTTL :: Contact -> Maybe Int contactCITimedTTL Contact {mergedPreferences = ContactUserPreferences {timedMessages = ContactUserPreference {enabled, userPreference}}} | forUser enabled && forContact enabled = case userPreference of - CUPContact TimedMessagesPreference {ttl = t} -> Just t - CUPUser TimedMessagesPreference {ttl = t} -> Just t + CUPContact TimedMessagesPreference {ttl = Just t} -> Just t + CUPUser TimedMessagesPreference {ttl = Just t} -> Just t + _ -> Nothing | otherwise = Nothing groupCITimedTTL :: GroupInfo -> Maybe Int diff --git a/src/Simplex/Chat/Types.hs b/src/Simplex/Chat/Types.hs index 1be5103a0..bb9e1aa14 100644 --- a/src/Simplex/Chat/Types.hs +++ b/src/Simplex/Chat/Types.hs @@ -548,7 +548,7 @@ toChatPrefs FullPreferences {fullDelete, voice, timedMessages} = defaultChatPrefs :: FullPreferences defaultChatPrefs = FullPreferences - { timedMessages = TimedMessagesPreference {allow = FANo, ttl = 86400}, + { timedMessages = TimedMessagesPreference {allow = FANo, ttl = Nothing}, fullDelete = FullDeletePreference {allow = FANo}, -- receipts = SimplePreference {allow = FANo}, voice = VoicePreference {allow = FAYes} @@ -572,11 +572,13 @@ emptyGroupPrefs = GroupPreferences Nothing Nothing Nothing Nothing data TimedMessagesPreference = TimedMessagesPreference { allow :: FeatureAllowed, - ttl :: Int + ttl :: Maybe Int } deriving (Eq, Show, Generic, FromJSON) -instance ToJSON TimedMessagesPreference where toEncoding = J.genericToEncoding J.defaultOptions +instance ToJSON TimedMessagesPreference where + toJSON = J.genericToJSON J.defaultOptions {J.omitNothingFields = True} + toEncoding = J.genericToEncoding J.defaultOptions {J.omitNothingFields = True} data FullDeletePreference = FullDeletePreference {allow :: FeatureAllowed} deriving (Eq, Show, Generic, FromJSON) diff --git a/tests/MobileTests.hs b/tests/MobileTests.hs index ad6938002..29c3f2d98 100644 --- a/tests/MobileTests.hs +++ b/tests/MobileTests.hs @@ -32,9 +32,9 @@ activeUserExists = "{\"resp\":{\"type\":\"chatCmdError\",\"chatError\":{\"type\" activeUser :: String #if defined(darwin_HOST_OS) && defined(swiftJSON) -activeUser = "{\"resp\":{\"activeUser\":{\"user\":{\"userId\":1,\"userContactId\":1,\"localDisplayName\":\"alice\",\"profile\":{\"profileId\":1,\"displayName\":\"alice\",\"fullName\":\"Alice\",\"localAlias\":\"\"},\"fullPreferences\":{\"timedMessages\":{\"allow\":\"no\",\"ttl\":86400},\"fullDelete\":{\"allow\":\"no\"},\"voice\":{\"allow\":\"yes\"}},\"activeUser\":true}}}" +activeUser = "{\"resp\":{\"activeUser\":{\"user\":{\"userId\":1,\"userContactId\":1,\"localDisplayName\":\"alice\",\"profile\":{\"profileId\":1,\"displayName\":\"alice\",\"fullName\":\"Alice\",\"localAlias\":\"\"},\"fullPreferences\":{\"timedMessages\":{\"allow\":\"no\"},\"fullDelete\":{\"allow\":\"no\"},\"voice\":{\"allow\":\"yes\"}},\"activeUser\":true}}}" #else -activeUser = "{\"resp\":{\"type\":\"activeUser\",\"user\":{\"userId\":1,\"userContactId\":1,\"localDisplayName\":\"alice\",\"profile\":{\"profileId\":1,\"displayName\":\"alice\",\"fullName\":\"Alice\",\"localAlias\":\"\"},\"fullPreferences\":{\"timedMessages\":{\"allow\":\"no\",\"ttl\":86400},\"fullDelete\":{\"allow\":\"no\"},\"voice\":{\"allow\":\"yes\"}},\"activeUser\":true}}}" +activeUser = "{\"resp\":{\"type\":\"activeUser\",\"user\":{\"userId\":1,\"userContactId\":1,\"localDisplayName\":\"alice\",\"profile\":{\"profileId\":1,\"displayName\":\"alice\",\"fullName\":\"Alice\",\"localAlias\":\"\"},\"fullPreferences\":{\"timedMessages\":{\"allow\":\"no\"},\"fullDelete\":{\"allow\":\"no\"},\"voice\":{\"allow\":\"yes\"}},\"activeUser\":true}}}" #endif chatStarted :: String