-- This file is part of the Wire Server implementation.
--
-- Copyright (C) 2022 Wire Swiss GmbH <opensource@wire.com>
--
-- This program is free software: you can redistribute it and/or modify it under
-- the terms of the GNU Affero General Public License as published by the Free
-- Software Foundation, either version 3 of the License, or (at your option) any
-- later version.
--
-- This program is distributed in the hope that it will be useful, but WITHOUT
-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-- FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
-- details.
--
-- You should have received a copy of the GNU Affero General Public License along
-- with this program. If not, see <https://www.gnu.org/licenses/>.

module Wire.API.MLS.Group where

import Data.Aeson qualified as A
import Data.Json.Util
import Data.OpenApi qualified as S
import Data.Schema
import Imports
import Servant
import Wire.API.MLS.Serialisation
import Wire.Arbitrary

newtype GroupId = GroupId {GroupId -> ByteString
unGroupId :: ByteString}
  deriving (GroupId -> GroupId -> Bool
(GroupId -> GroupId -> Bool)
-> (GroupId -> GroupId -> Bool) -> Eq GroupId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: GroupId -> GroupId -> Bool
== :: GroupId -> GroupId -> Bool
$c/= :: GroupId -> GroupId -> Bool
/= :: GroupId -> GroupId -> Bool
Eq, Int -> GroupId -> ShowS
[GroupId] -> ShowS
GroupId -> String
(Int -> GroupId -> ShowS)
-> (GroupId -> String) -> ([GroupId] -> ShowS) -> Show GroupId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> GroupId -> ShowS
showsPrec :: Int -> GroupId -> ShowS
$cshow :: GroupId -> String
show :: GroupId -> String
$cshowList :: [GroupId] -> ShowS
showList :: [GroupId] -> ShowS
Show, (forall x. GroupId -> Rep GroupId x)
-> (forall x. Rep GroupId x -> GroupId) -> Generic GroupId
forall x. Rep GroupId x -> GroupId
forall x. GroupId -> Rep GroupId x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. GroupId -> Rep GroupId x
from :: forall x. GroupId -> Rep GroupId x
$cto :: forall x. Rep GroupId x -> GroupId
to :: forall x. Rep GroupId x -> GroupId
Generic, Eq GroupId
Eq GroupId =>
(GroupId -> GroupId -> Ordering)
-> (GroupId -> GroupId -> Bool)
-> (GroupId -> GroupId -> Bool)
-> (GroupId -> GroupId -> Bool)
-> (GroupId -> GroupId -> Bool)
-> (GroupId -> GroupId -> GroupId)
-> (GroupId -> GroupId -> GroupId)
-> Ord GroupId
GroupId -> GroupId -> Bool
GroupId -> GroupId -> Ordering
GroupId -> GroupId -> GroupId
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: GroupId -> GroupId -> Ordering
compare :: GroupId -> GroupId -> Ordering
$c< :: GroupId -> GroupId -> Bool
< :: GroupId -> GroupId -> Bool
$c<= :: GroupId -> GroupId -> Bool
<= :: GroupId -> GroupId -> Bool
$c> :: GroupId -> GroupId -> Bool
> :: GroupId -> GroupId -> Bool
$c>= :: GroupId -> GroupId -> Bool
>= :: GroupId -> GroupId -> Bool
$cmax :: GroupId -> GroupId -> GroupId
max :: GroupId -> GroupId -> GroupId
$cmin :: GroupId -> GroupId -> GroupId
min :: GroupId -> GroupId -> GroupId
Ord)
  deriving (Gen GroupId
Gen GroupId -> (GroupId -> [GroupId]) -> Arbitrary GroupId
GroupId -> [GroupId]
forall a. Gen a -> (a -> [a]) -> Arbitrary a
$carbitrary :: Gen GroupId
arbitrary :: Gen GroupId
$cshrink :: GroupId -> [GroupId]
shrink :: GroupId -> [GroupId]
Arbitrary) via (GenericUniform GroupId)
  deriving (Text -> Either Text GroupId
ByteString -> Either Text GroupId
(Text -> Either Text GroupId)
-> (ByteString -> Either Text GroupId)
-> (Text -> Either Text GroupId)
-> FromHttpApiData GroupId
forall a.
(Text -> Either Text a)
-> (ByteString -> Either Text a)
-> (Text -> Either Text a)
-> FromHttpApiData a
$cparseUrlPiece :: Text -> Either Text GroupId
parseUrlPiece :: Text -> Either Text GroupId
$cparseHeader :: ByteString -> Either Text GroupId
parseHeader :: ByteString -> Either Text GroupId
$cparseQueryParam :: Text -> Either Text GroupId
parseQueryParam :: Text -> Either Text GroupId
FromHttpApiData, GroupId -> Text
GroupId -> ByteString
GroupId -> Builder
(GroupId -> Text)
-> (GroupId -> Builder)
-> (GroupId -> ByteString)
-> (GroupId -> Text)
-> (GroupId -> Builder)
-> ToHttpApiData GroupId
forall a.
(a -> Text)
-> (a -> Builder)
-> (a -> ByteString)
-> (a -> Text)
-> (a -> Builder)
-> ToHttpApiData a
$ctoUrlPiece :: GroupId -> Text
toUrlPiece :: GroupId -> Text
$ctoEncodedUrlPiece :: GroupId -> Builder
toEncodedUrlPiece :: GroupId -> Builder
$ctoHeader :: GroupId -> ByteString
toHeader :: GroupId -> ByteString
$ctoQueryParam :: GroupId -> Text
toQueryParam :: GroupId -> Text
$ctoEncodedQueryParam :: GroupId -> Builder
toEncodedQueryParam :: GroupId -> Builder
ToHttpApiData, Proxy GroupId -> Schema
(Proxy GroupId -> Schema) -> ToParamSchema GroupId
forall a. (Proxy a -> Schema) -> ToParamSchema a
$ctoParamSchema :: Proxy GroupId -> Schema
toParamSchema :: Proxy GroupId -> Schema
S.ToParamSchema) via Base64ByteString
  deriving ([GroupId] -> Value
[GroupId] -> Encoding
GroupId -> Value
GroupId -> Encoding
(GroupId -> Value)
-> (GroupId -> Encoding)
-> ([GroupId] -> Value)
-> ([GroupId] -> Encoding)
-> ToJSON GroupId
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
$ctoJSON :: GroupId -> Value
toJSON :: GroupId -> Value
$ctoEncoding :: GroupId -> Encoding
toEncoding :: GroupId -> Encoding
$ctoJSONList :: [GroupId] -> Value
toJSONList :: [GroupId] -> Value
$ctoEncodingList :: [GroupId] -> Encoding
toEncodingList :: [GroupId] -> Encoding
A.ToJSON, Value -> Parser [GroupId]
Value -> Parser GroupId
(Value -> Parser GroupId)
-> (Value -> Parser [GroupId]) -> FromJSON GroupId
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
$cparseJSON :: Value -> Parser GroupId
parseJSON :: Value -> Parser GroupId
$cparseJSONList :: Value -> Parser [GroupId]
parseJSONList :: Value -> Parser [GroupId]
A.FromJSON, Typeable GroupId
Typeable GroupId =>
(Proxy GroupId -> Declare (Definitions Schema) NamedSchema)
-> ToSchema GroupId
Proxy GroupId -> Declare (Definitions Schema) NamedSchema
forall a.
Typeable a =>
(Proxy a -> Declare (Definitions Schema) NamedSchema) -> ToSchema a
$cdeclareNamedSchema :: Proxy GroupId -> Declare (Definitions Schema) NamedSchema
declareNamedSchema :: Proxy GroupId -> Declare (Definitions Schema) NamedSchema
S.ToSchema) via (Schema GroupId)

instance IsString GroupId where
  fromString :: String -> GroupId
fromString = ByteString -> GroupId
GroupId (ByteString -> GroupId)
-> (String -> ByteString) -> String -> GroupId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
forall a. IsString a => String -> a
fromString

instance ParseMLS GroupId where
  parseMLS :: Get GroupId
parseMLS = ByteString -> GroupId
GroupId (ByteString -> GroupId) -> Get ByteString -> Get GroupId
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall w. (Binary w, Integral w) => Get ByteString
parseMLSBytes @VarInt

instance SerialiseMLS GroupId where
  serialiseMLS :: GroupId -> Put
serialiseMLS (GroupId ByteString
gid) = forall w. (Binary w, Integral w) => ByteString -> Put
serialiseMLSBytes @VarInt ByteString
gid

instance ToSchema GroupId where
  schema :: ValueSchema NamedSwaggerDoc GroupId
schema =
    ByteString -> GroupId
GroupId
      (ByteString -> GroupId)
-> SchemaP NamedSwaggerDoc Value Value GroupId ByteString
-> ValueSchema NamedSwaggerDoc GroupId
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GroupId -> ByteString
unGroupId
        (GroupId -> ByteString)
-> SchemaP NamedSwaggerDoc Value Value ByteString ByteString
-> SchemaP NamedSwaggerDoc Value Value GroupId ByteString
forall (p :: * -> * -> *) a a' b.
Profunctor p =>
(a -> a') -> p a' b -> p a b
.= Text
-> SchemaP SwaggerDoc Value Value ByteString ByteString
-> SchemaP NamedSwaggerDoc Value Value ByteString ByteString
forall doc doc' v m a b.
HasObject doc doc' =>
Text -> SchemaP doc v m a b -> SchemaP doc' v m a b
named Text
"GroupId" (ByteString -> Base64ByteString
Base64ByteString (ByteString -> Base64ByteString)
-> SchemaP SwaggerDoc Value Value Base64ByteString ByteString
-> SchemaP SwaggerDoc Value Value ByteString ByteString
forall (p :: * -> * -> *) a a' b.
Profunctor p =>
(a -> a') -> p a' b -> p a b
.= (Base64ByteString -> ByteString)
-> SchemaP SwaggerDoc Value Value Base64ByteString Base64ByteString
-> SchemaP SwaggerDoc Value Value Base64ByteString ByteString
forall a b.
(a -> b)
-> SchemaP SwaggerDoc Value Value Base64ByteString a
-> SchemaP SwaggerDoc Value Value Base64ByteString b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Base64ByteString -> ByteString
fromBase64ByteString (SchemaP
  NamedSwaggerDoc Value Value Base64ByteString Base64ByteString
-> SchemaP SwaggerDoc Value Value Base64ByteString Base64ByteString
forall doc doc' v m a b.
HasObject doc doc' =>
SchemaP doc' v m a b -> SchemaP doc v m a b
unnamed SchemaP
  NamedSwaggerDoc Value Value Base64ByteString Base64ByteString
forall a. ToSchema a => ValueSchema NamedSwaggerDoc a
schema))

newtype GroupIdGen = GroupIdGen {GroupIdGen -> Word32
unGroupIdGen :: Word32}
  deriving (GroupIdGen -> GroupIdGen -> Bool
(GroupIdGen -> GroupIdGen -> Bool)
-> (GroupIdGen -> GroupIdGen -> Bool) -> Eq GroupIdGen
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: GroupIdGen -> GroupIdGen -> Bool
== :: GroupIdGen -> GroupIdGen -> Bool
$c/= :: GroupIdGen -> GroupIdGen -> Bool
/= :: GroupIdGen -> GroupIdGen -> Bool
Eq, Int -> GroupIdGen -> ShowS
[GroupIdGen] -> ShowS
GroupIdGen -> String
(Int -> GroupIdGen -> ShowS)
-> (GroupIdGen -> String)
-> ([GroupIdGen] -> ShowS)
-> Show GroupIdGen
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> GroupIdGen -> ShowS
showsPrec :: Int -> GroupIdGen -> ShowS
$cshow :: GroupIdGen -> String
show :: GroupIdGen -> String
$cshowList :: [GroupIdGen] -> ShowS
showList :: [GroupIdGen] -> ShowS
Show, (forall x. GroupIdGen -> Rep GroupIdGen x)
-> (forall x. Rep GroupIdGen x -> GroupIdGen) -> Generic GroupIdGen
forall x. Rep GroupIdGen x -> GroupIdGen
forall x. GroupIdGen -> Rep GroupIdGen x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. GroupIdGen -> Rep GroupIdGen x
from :: forall x. GroupIdGen -> Rep GroupIdGen x
$cto :: forall x. Rep GroupIdGen x -> GroupIdGen
to :: forall x. Rep GroupIdGen x -> GroupIdGen
Generic, Eq GroupIdGen
Eq GroupIdGen =>
(GroupIdGen -> GroupIdGen -> Ordering)
-> (GroupIdGen -> GroupIdGen -> Bool)
-> (GroupIdGen -> GroupIdGen -> Bool)
-> (GroupIdGen -> GroupIdGen -> Bool)
-> (GroupIdGen -> GroupIdGen -> Bool)
-> (GroupIdGen -> GroupIdGen -> GroupIdGen)
-> (GroupIdGen -> GroupIdGen -> GroupIdGen)
-> Ord GroupIdGen
GroupIdGen -> GroupIdGen -> Bool
GroupIdGen -> GroupIdGen -> Ordering
GroupIdGen -> GroupIdGen -> GroupIdGen
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: GroupIdGen -> GroupIdGen -> Ordering
compare :: GroupIdGen -> GroupIdGen -> Ordering
$c< :: GroupIdGen -> GroupIdGen -> Bool
< :: GroupIdGen -> GroupIdGen -> Bool
$c<= :: GroupIdGen -> GroupIdGen -> Bool
<= :: GroupIdGen -> GroupIdGen -> Bool
$c> :: GroupIdGen -> GroupIdGen -> Bool
> :: GroupIdGen -> GroupIdGen -> Bool
$c>= :: GroupIdGen -> GroupIdGen -> Bool
>= :: GroupIdGen -> GroupIdGen -> Bool
$cmax :: GroupIdGen -> GroupIdGen -> GroupIdGen
max :: GroupIdGen -> GroupIdGen -> GroupIdGen
$cmin :: GroupIdGen -> GroupIdGen -> GroupIdGen
min :: GroupIdGen -> GroupIdGen -> GroupIdGen
Ord)
  deriving (Gen GroupIdGen
Gen GroupIdGen
-> (GroupIdGen -> [GroupIdGen]) -> Arbitrary GroupIdGen
GroupIdGen -> [GroupIdGen]
forall a. Gen a -> (a -> [a]) -> Arbitrary a
$carbitrary :: Gen GroupIdGen
arbitrary :: Gen GroupIdGen
$cshrink :: GroupIdGen -> [GroupIdGen]
shrink :: GroupIdGen -> [GroupIdGen]
Arbitrary) via (GenericUniform GroupIdGen)