{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE TemplateHaskell #-}

-- 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.Team.Conversation
  ( -- * TeamConversation
    TeamConversation,
    newTeamConversation,
    conversationId,

    -- * TeamConversationList
    TeamConversationList,
    newTeamConversationList,
    teamConversations,
  )
where

import Control.Lens (makeLenses, (?~))
import Data.Aeson qualified as A
import Data.Id (ConvId)
import Data.OpenApi qualified as S
import Data.Schema
import Imports
import Wire.Arbitrary (Arbitrary, GenericUniform (..))

--------------------------------------------------------------------------------
-- TeamConversation

newtype TeamConversation = TeamConversation
  { TeamConversation -> ConvId
_conversationId :: ConvId
  }
  deriving stock (TeamConversation -> TeamConversation -> Bool
(TeamConversation -> TeamConversation -> Bool)
-> (TeamConversation -> TeamConversation -> Bool)
-> Eq TeamConversation
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TeamConversation -> TeamConversation -> Bool
== :: TeamConversation -> TeamConversation -> Bool
$c/= :: TeamConversation -> TeamConversation -> Bool
/= :: TeamConversation -> TeamConversation -> Bool
Eq, Int -> TeamConversation -> ShowS
[TeamConversation] -> ShowS
TeamConversation -> String
(Int -> TeamConversation -> ShowS)
-> (TeamConversation -> String)
-> ([TeamConversation] -> ShowS)
-> Show TeamConversation
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TeamConversation -> ShowS
showsPrec :: Int -> TeamConversation -> ShowS
$cshow :: TeamConversation -> String
show :: TeamConversation -> String
$cshowList :: [TeamConversation] -> ShowS
showList :: [TeamConversation] -> ShowS
Show, (forall x. TeamConversation -> Rep TeamConversation x)
-> (forall x. Rep TeamConversation x -> TeamConversation)
-> Generic TeamConversation
forall x. Rep TeamConversation x -> TeamConversation
forall x. TeamConversation -> Rep TeamConversation x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. TeamConversation -> Rep TeamConversation x
from :: forall x. TeamConversation -> Rep TeamConversation x
$cto :: forall x. Rep TeamConversation x -> TeamConversation
to :: forall x. Rep TeamConversation x -> TeamConversation
Generic)
  deriving (Gen TeamConversation
Gen TeamConversation
-> (TeamConversation -> [TeamConversation])
-> Arbitrary TeamConversation
TeamConversation -> [TeamConversation]
forall a. Gen a -> (a -> [a]) -> Arbitrary a
$carbitrary :: Gen TeamConversation
arbitrary :: Gen TeamConversation
$cshrink :: TeamConversation -> [TeamConversation]
shrink :: TeamConversation -> [TeamConversation]
Arbitrary) via (GenericUniform TeamConversation)
  deriving ([TeamConversation] -> Value
[TeamConversation] -> Encoding
TeamConversation -> Value
TeamConversation -> Encoding
(TeamConversation -> Value)
-> (TeamConversation -> Encoding)
-> ([TeamConversation] -> Value)
-> ([TeamConversation] -> Encoding)
-> ToJSON TeamConversation
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
$ctoJSON :: TeamConversation -> Value
toJSON :: TeamConversation -> Value
$ctoEncoding :: TeamConversation -> Encoding
toEncoding :: TeamConversation -> Encoding
$ctoJSONList :: [TeamConversation] -> Value
toJSONList :: [TeamConversation] -> Value
$ctoEncodingList :: [TeamConversation] -> Encoding
toEncodingList :: [TeamConversation] -> Encoding
A.ToJSON, Value -> Parser [TeamConversation]
Value -> Parser TeamConversation
(Value -> Parser TeamConversation)
-> (Value -> Parser [TeamConversation])
-> FromJSON TeamConversation
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
$cparseJSON :: Value -> Parser TeamConversation
parseJSON :: Value -> Parser TeamConversation
$cparseJSONList :: Value -> Parser [TeamConversation]
parseJSONList :: Value -> Parser [TeamConversation]
A.FromJSON, Typeable TeamConversation
Typeable TeamConversation =>
(Proxy TeamConversation
 -> Declare (Definitions Schema) NamedSchema)
-> ToSchema TeamConversation
Proxy TeamConversation -> Declare (Definitions Schema) NamedSchema
forall a.
Typeable a =>
(Proxy a -> Declare (Definitions Schema) NamedSchema) -> ToSchema a
$cdeclareNamedSchema :: Proxy TeamConversation -> Declare (Definitions Schema) NamedSchema
declareNamedSchema :: Proxy TeamConversation -> Declare (Definitions Schema) NamedSchema
S.ToSchema) via (Schema TeamConversation)

managedDesc :: Text
managedDesc :: Text
managedDesc =
  Text
"This field MUST NOT be used by clients. "
    Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"It is here only for backwards compatibility of the interface."

instance ToSchema TeamConversation where
  schema :: ValueSchema NamedSwaggerDoc TeamConversation
schema =
    Text
-> (NamedSwaggerDoc -> NamedSwaggerDoc)
-> ObjectSchema SwaggerDoc TeamConversation
-> ValueSchema NamedSwaggerDoc TeamConversation
forall doc doc' a.
HasObject doc doc' =>
Text -> (doc' -> doc') -> ObjectSchema doc a -> ValueSchema doc' a
objectWithDocModifier
      Text
"TeamConversation"
      ((Maybe Text -> Identity (Maybe Text))
-> NamedSwaggerDoc -> Identity NamedSwaggerDoc
forall s a. HasDescription s a => Lens' s a
Lens' NamedSwaggerDoc (Maybe Text)
description ((Maybe Text -> Identity (Maybe Text))
 -> NamedSwaggerDoc -> Identity NamedSwaggerDoc)
-> Text -> NamedSwaggerDoc -> NamedSwaggerDoc
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Text
"Team conversation data")
      (ObjectSchema SwaggerDoc TeamConversation
 -> ValueSchema NamedSwaggerDoc TeamConversation)
-> ObjectSchema SwaggerDoc TeamConversation
-> ValueSchema NamedSwaggerDoc TeamConversation
forall a b. (a -> b) -> a -> b
$ ConvId -> TeamConversation
TeamConversation
        (ConvId -> TeamConversation)
-> SchemaP SwaggerDoc Object [Pair] TeamConversation ConvId
-> ObjectSchema SwaggerDoc TeamConversation
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TeamConversation -> ConvId
_conversationId (TeamConversation -> ConvId)
-> SchemaP SwaggerDoc Object [Pair] ConvId ConvId
-> SchemaP SwaggerDoc Object [Pair] TeamConversation ConvId
forall (p :: * -> * -> *) a a' b.
Profunctor p =>
(a -> a') -> p a' b -> p a b
.= Text
-> SchemaP NamedSwaggerDoc Value Value ConvId ConvId
-> SchemaP SwaggerDoc Object [Pair] ConvId ConvId
forall doc' doc a b.
HasField doc' doc =>
Text
-> SchemaP doc' Value Value a b -> SchemaP doc Object [Pair] a b
field Text
"conversation" SchemaP NamedSwaggerDoc Value Value ConvId ConvId
forall a. ToSchema a => ValueSchema NamedSwaggerDoc a
schema
        ObjectSchema SwaggerDoc TeamConversation
-> SchemaP SwaggerDoc Object [Pair] TeamConversation ()
-> ObjectSchema SwaggerDoc TeamConversation
forall a b.
SchemaP SwaggerDoc Object [Pair] TeamConversation a
-> SchemaP SwaggerDoc Object [Pair] TeamConversation b
-> SchemaP SwaggerDoc Object [Pair] TeamConversation a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* () -> TeamConversation -> ()
forall a b. a -> b -> a
const ()
          (TeamConversation -> ())
-> SchemaP SwaggerDoc Object [Pair] () ()
-> SchemaP SwaggerDoc Object [Pair] TeamConversation ()
forall (p :: * -> * -> *) a a' b.
Profunctor p =>
(a -> a') -> p a' b -> p a b
.= Text
-> (SwaggerDoc -> SwaggerDoc)
-> SchemaP SwaggerDoc Value Value () ()
-> SchemaP SwaggerDoc Object [Pair] () ()
forall doc' doc a b.
HasField doc' doc =>
Text
-> (doc' -> doc')
-> SchemaP doc' Value Value a b
-> SchemaP doc Object [Pair] a b
fieldWithDocModifier
            Text
"managed"
            ((Maybe Text -> Identity (Maybe Text))
-> SwaggerDoc -> Identity SwaggerDoc
forall s a. HasDescription s a => Lens' s a
Lens' SwaggerDoc (Maybe Text)
description ((Maybe Text -> Identity (Maybe Text))
 -> SwaggerDoc -> Identity SwaggerDoc)
-> Text -> SwaggerDoc -> SwaggerDoc
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Text
managedDesc)
            (Bool -> SchemaP SwaggerDoc Value Value () ()
forall a. ToJSON a => a -> SchemaP SwaggerDoc Value Value () ()
c (Bool
False :: Bool))
    where
      c :: (A.ToJSON a) => a -> ValueSchema SwaggerDoc ()
      c :: forall a. ToJSON a => a -> SchemaP SwaggerDoc Value Value () ()
c a
val = SwaggerDoc
-> (Value -> Parser ())
-> (() -> Maybe Value)
-> SchemaP SwaggerDoc Value Value () ()
forall doc v b a w.
doc -> (v -> Parser b) -> (a -> Maybe w) -> SchemaP doc v w a b
mkSchema SwaggerDoc
forall a. Monoid a => a
mempty (Parser () -> Value -> Parser ()
forall a b. a -> b -> a
const (() -> Parser ()
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ())) (Maybe Value -> () -> Maybe Value
forall a b. a -> b -> a
const (Value -> Maybe Value
forall a. a -> Maybe a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> Value
forall a. ToJSON a => a -> Value
A.toJSON a
val)))

newTeamConversation :: ConvId -> TeamConversation
newTeamConversation :: ConvId -> TeamConversation
newTeamConversation = ConvId -> TeamConversation
TeamConversation

--------------------------------------------------------------------------------
-- TeamConversationList

newtype TeamConversationList = TeamConversationList {TeamConversationList -> [TeamConversation]
teamConversations :: [TeamConversation]}
  deriving ((forall x. TeamConversationList -> Rep TeamConversationList x)
-> (forall x. Rep TeamConversationList x -> TeamConversationList)
-> Generic TeamConversationList
forall x. Rep TeamConversationList x -> TeamConversationList
forall x. TeamConversationList -> Rep TeamConversationList x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. TeamConversationList -> Rep TeamConversationList x
from :: forall x. TeamConversationList -> Rep TeamConversationList x
$cto :: forall x. Rep TeamConversationList x -> TeamConversationList
to :: forall x. Rep TeamConversationList x -> TeamConversationList
Generic)
  deriving stock (TeamConversationList -> TeamConversationList -> Bool
(TeamConversationList -> TeamConversationList -> Bool)
-> (TeamConversationList -> TeamConversationList -> Bool)
-> Eq TeamConversationList
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TeamConversationList -> TeamConversationList -> Bool
== :: TeamConversationList -> TeamConversationList -> Bool
$c/= :: TeamConversationList -> TeamConversationList -> Bool
/= :: TeamConversationList -> TeamConversationList -> Bool
Eq, Int -> TeamConversationList -> ShowS
[TeamConversationList] -> ShowS
TeamConversationList -> String
(Int -> TeamConversationList -> ShowS)
-> (TeamConversationList -> String)
-> ([TeamConversationList] -> ShowS)
-> Show TeamConversationList
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TeamConversationList -> ShowS
showsPrec :: Int -> TeamConversationList -> ShowS
$cshow :: TeamConversationList -> String
show :: TeamConversationList -> String
$cshowList :: [TeamConversationList] -> ShowS
showList :: [TeamConversationList] -> ShowS
Show)
  deriving newtype (Gen TeamConversationList
Gen TeamConversationList
-> (TeamConversationList -> [TeamConversationList])
-> Arbitrary TeamConversationList
TeamConversationList -> [TeamConversationList]
forall a. Gen a -> (a -> [a]) -> Arbitrary a
$carbitrary :: Gen TeamConversationList
arbitrary :: Gen TeamConversationList
$cshrink :: TeamConversationList -> [TeamConversationList]
shrink :: TeamConversationList -> [TeamConversationList]
Arbitrary)
  deriving ([TeamConversationList] -> Value
[TeamConversationList] -> Encoding
TeamConversationList -> Value
TeamConversationList -> Encoding
(TeamConversationList -> Value)
-> (TeamConversationList -> Encoding)
-> ([TeamConversationList] -> Value)
-> ([TeamConversationList] -> Encoding)
-> ToJSON TeamConversationList
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
$ctoJSON :: TeamConversationList -> Value
toJSON :: TeamConversationList -> Value
$ctoEncoding :: TeamConversationList -> Encoding
toEncoding :: TeamConversationList -> Encoding
$ctoJSONList :: [TeamConversationList] -> Value
toJSONList :: [TeamConversationList] -> Value
$ctoEncodingList :: [TeamConversationList] -> Encoding
toEncodingList :: [TeamConversationList] -> Encoding
A.ToJSON, Value -> Parser [TeamConversationList]
Value -> Parser TeamConversationList
(Value -> Parser TeamConversationList)
-> (Value -> Parser [TeamConversationList])
-> FromJSON TeamConversationList
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
$cparseJSON :: Value -> Parser TeamConversationList
parseJSON :: Value -> Parser TeamConversationList
$cparseJSONList :: Value -> Parser [TeamConversationList]
parseJSONList :: Value -> Parser [TeamConversationList]
A.FromJSON, Typeable TeamConversationList
Typeable TeamConversationList =>
(Proxy TeamConversationList
 -> Declare (Definitions Schema) NamedSchema)
-> ToSchema TeamConversationList
Proxy TeamConversationList
-> Declare (Definitions Schema) NamedSchema
forall a.
Typeable a =>
(Proxy a -> Declare (Definitions Schema) NamedSchema) -> ToSchema a
$cdeclareNamedSchema :: Proxy TeamConversationList
-> Declare (Definitions Schema) NamedSchema
declareNamedSchema :: Proxy TeamConversationList
-> Declare (Definitions Schema) NamedSchema
S.ToSchema) via (Schema TeamConversationList)

instance ToSchema TeamConversationList where
  schema :: ValueSchema NamedSwaggerDoc TeamConversationList
schema =
    Text
-> (NamedSwaggerDoc -> NamedSwaggerDoc)
-> ObjectSchema SwaggerDoc TeamConversationList
-> ValueSchema NamedSwaggerDoc TeamConversationList
forall doc doc' a.
HasObject doc doc' =>
Text -> (doc' -> doc') -> ObjectSchema doc a -> ValueSchema doc' a
objectWithDocModifier
      Text
"TeamConversationList"
      ((Maybe Text -> Identity (Maybe Text))
-> NamedSwaggerDoc -> Identity NamedSwaggerDoc
forall s a. HasDescription s a => Lens' s a
Lens' NamedSwaggerDoc (Maybe Text)
description ((Maybe Text -> Identity (Maybe Text))
 -> NamedSwaggerDoc -> Identity NamedSwaggerDoc)
-> Text -> NamedSwaggerDoc -> NamedSwaggerDoc
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Text
"Team conversation list")
      (ObjectSchema SwaggerDoc TeamConversationList
 -> ValueSchema NamedSwaggerDoc TeamConversationList)
-> ObjectSchema SwaggerDoc TeamConversationList
-> ValueSchema NamedSwaggerDoc TeamConversationList
forall a b. (a -> b) -> a -> b
$ [TeamConversation] -> TeamConversationList
TeamConversationList
        ([TeamConversation] -> TeamConversationList)
-> SchemaP
     SwaggerDoc Object [Pair] TeamConversationList [TeamConversation]
-> ObjectSchema SwaggerDoc TeamConversationList
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TeamConversationList -> [TeamConversation]
teamConversations (TeamConversationList -> [TeamConversation])
-> SchemaP
     SwaggerDoc Object [Pair] [TeamConversation] [TeamConversation]
-> SchemaP
     SwaggerDoc Object [Pair] TeamConversationList [TeamConversation]
forall (p :: * -> * -> *) a a' b.
Profunctor p =>
(a -> a') -> p a' b -> p a b
.= Text
-> SchemaP
     SwaggerDoc Value Value [TeamConversation] [TeamConversation]
-> SchemaP
     SwaggerDoc Object [Pair] [TeamConversation] [TeamConversation]
forall doc' doc a b.
HasField doc' doc =>
Text
-> SchemaP doc' Value Value a b -> SchemaP doc Object [Pair] a b
field Text
"conversations" (ValueSchema NamedSwaggerDoc TeamConversation
-> SchemaP
     SwaggerDoc Value Value [TeamConversation] [TeamConversation]
forall ndoc doc a.
(HasArray ndoc doc, HasName ndoc) =>
ValueSchema ndoc a -> ValueSchema doc [a]
array ValueSchema NamedSwaggerDoc TeamConversation
forall a. ToSchema a => ValueSchema NamedSwaggerDoc a
schema)

newTeamConversationList :: [TeamConversation] -> TeamConversationList
newTeamConversationList :: [TeamConversation] -> TeamConversationList
newTeamConversationList = [TeamConversation] -> TeamConversationList
TeamConversationList

makeLenses ''TeamConversation