Skip to content

Commit

Permalink
Add SupportedClientFeatures list to UpdateClient.
Browse files Browse the repository at this point in the history
  • Loading branch information
fisx committed May 14, 2021
1 parent ffeaa51 commit 1fe3ea4
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 31 deletions.
1 change: 1 addition & 0 deletions docs/reference/cassandra-schema.cql
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,7 @@ CREATE TABLE brig_test.clients (
lat double,
lon double,
model text,
supported_features list<int>,
tstamp timestamp,
type int,
PRIMARY KEY (user, client)
Expand Down
60 changes: 59 additions & 1 deletion libs/wire-api/src/Wire/API/User/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ module Wire.API.User.Client
NewClient (..),
newClient,
UpdateClient (..),
SupportedClientFeature (..),
SupportedClientFeatureList (..),
RmClient (..),

-- * re-exports
Expand All @@ -51,13 +53,16 @@ module Wire.API.User.Client
modelUserClients,
modelNewClient,
modelUpdateClient,
modelClientSupportedFeatureList,
typeSupportedClientFeature,
modelDeleteClient,
modelClient,
modelSigkeys,
modelLocation, -- re-export from types-common
)
where

import qualified Cassandra as Cql
import Control.Lens ((?~), (^.))
import Data.Aeson
import Data.Domain (Domain)
Expand Down Expand Up @@ -517,7 +522,8 @@ instance FromJSON NewClient where
data UpdateClient = UpdateClient
{ updateClientPrekeys :: [Prekey],
updateClientLastKey :: Maybe LastPrekey,
updateClientLabel :: Maybe Text
updateClientLabel :: Maybe Text,
updateClientSupportedFeatures :: Maybe [SupportedClientFeature]
}
deriving stock (Eq, Show, Generic)
deriving (Arbitrary) via (GenericUniform UpdateClient)
Expand All @@ -540,13 +546,17 @@ modelUpdateClient = Doc.defineModel "UpdateClient" $ do
Doc.property "label" Doc.string' $ do
Doc.description "A new name for this client."
Doc.optional
Doc.property "supported-features" typeSupportedClientFeature $ do
Doc.description "Hints for the backend so it can behave in a backwards-compatible way."
Doc.optional

instance ToJSON UpdateClient where
toJSON c =
object $
"prekeys" .= updateClientPrekeys c
# "lastkey" .= updateClientLastKey c
# "label" .= updateClientLabel c
# "supported-features" .= updateClientSupportedFeatures c
# []

instance FromJSON UpdateClient where
Expand All @@ -555,6 +565,54 @@ instance FromJSON UpdateClient where
<$> o .:? "prekeys" .!= []
<*> o .:? "lastkey"
<*> o .:? "label"
<*> o .:? "supported-features"

-- FUTUREWORK: add golden tests for this?
data SupportedClientFeatureList = SupportedClientFeatureList {fromSupportedClientFeatureList :: [SupportedClientFeature]}
deriving stock (Eq, Ord, Show, Generic)
deriving (Arbitrary) via (GenericUniform SupportedClientFeatureList)
deriving (ToSchema) via (CustomSwagger '[FieldLabelModifier (StripPrefix "fromSupportedClient", CamelToSnake)] SupportedClientFeatureList)

modelClientSupportedFeatureList :: Doc.Model
modelClientSupportedFeatureList = Doc.defineModel "SupportedClientFeatureList" $ do
Doc.description "Hints provided by the client for the backend so it can behavior in a backwards-compatible way."
Doc.property "feature_list" (Doc.array typeSupportedClientFeature) $ do
Doc.description "Array containing all supported features."

instance ToJSON SupportedClientFeatureList where
toJSON (SupportedClientFeatureList l) = object ["feature_list" .= l]

instance FromJSON SupportedClientFeatureList where
parseJSON = withObject "SupportedClientFeatureList" $ \obj -> SupportedClientFeatureList <$> obj .: "feature_list"

data SupportedClientFeature = ClientSupportsLegalHold
deriving stock (Eq, Ord, Bounded, Enum, Show, Generic)
deriving (Arbitrary) via (GenericUniform SupportedClientFeature)
deriving (ToSchema) via (CustomSwagger '[ConstructorTagModifier (StripPrefix "ClientSupports", LowerCase)] SupportedClientFeature)

typeSupportedClientFeature :: Doc.DataType
typeSupportedClientFeature =
Doc.string $
Doc.enum
[ "legalhold"
]

instance ToJSON SupportedClientFeature where
toJSON ClientSupportsLegalHold = String "legalhold"

instance FromJSON SupportedClientFeature where
parseJSON (String "legalhold") = pure ClientSupportsLegalHold
parseJSON _ = fail "SupportedClientFeature"

instance Cql.Cql SupportedClientFeature where
ctype = Cql.Tagged Cql.IntColumn

toCql ClientSupportsLegalHold = Cql.CqlInt 1

fromCql (Cql.CqlInt i) = case i of
1 -> return ClientSupportsLegalHold
n -> Left $ "Unexpected SupportedClientFeature value: " ++ show n
fromCql _ = Left "SupportedClientFeature value: int expected"

--------------------------------------------------------------------------------
-- RmClient
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"lastkey":{"key":"\u0014 }Kg\u000be3","id":65535},"prekeys":[{"key":"","id":1},{"key":"","id":0},{"key":"","id":0},{"key":"","id":1},{"key":"","id":0},{"key":"","id":0},{"key":"","id":0}],"label":"\u001b\u0004\u0001ccn\u001f{Y5"}
{"supported-features":["legalhold"],"lastkey":{"key":"\u0014 }Kg\u000be3","id":65535},"prekeys":[{"key":"","id":1},{"key":"","id":0},{"key":"","id":0},{"key":"","id":1},{"key":"","id":0},{"key":"","id":0},{"key":"","id":0}],"label":"\u001b\u0004\u0001ccn\u001f{Y5"}
Loading

0 comments on commit 1fe3ea4

Please sign in to comment.