-- 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.ServantProto where

import Data.ByteString.Lazy qualified as LBS
import Data.List.NonEmpty (NonEmpty (..))
import Data.OpenApi
import Imports
import Network.HTTP.Media ((//))
import Servant

-- | Type to tell servant that it should unrender request body or render
-- response body with Protobuf
data Proto

-- | We do not use 'Data.ProtocolBuffers.Decode' so we get a little freedom in
-- defining separate data types which match one to one with the protobuf and the
-- data types which we actually use in business logic. Eventually we should
-- think of better ways of doing this, perhaps using mu-schema or proto-lens as
-- it is fairly difficult to keep our custom data type, e.g. in
-- Wire.API.Message.Proto in sync with the proto files.
class FromProto a where
  fromProto :: ByteString -> Either String a

class ToProto a where
  toProto :: a -> ByteString

instance Accept Proto where
  contentTypes :: Proxy Proto -> NonEmpty MediaType
contentTypes Proxy Proto
_ = (ByteString
"application" ByteString -> ByteString -> MediaType
// ByteString
"x-protobuf") MediaType -> [MediaType] -> NonEmpty MediaType
forall a. a -> [a] -> NonEmpty a
:| []

instance (FromProto a) => MimeUnrender Proto a where
  mimeUnrender :: Proxy Proto -> ByteString -> Either String a
mimeUnrender Proxy Proto
_ ByteString
bs = ByteString -> Either String a
forall a. FromProto a => ByteString -> Either String a
fromProto (ByteString -> ByteString
LBS.toStrict ByteString
bs)

-- | This wrapper can be used to get the raw protobuf representation of a type.
-- It is used when the protobuf is supposed to be forwarded somewhere like a
-- federated remote, this saves us from having to re-encode it.
data RawProto a = RawProto
  { forall a. RawProto a -> ByteString
rpRaw :: ByteString,
    forall a. RawProto a -> a
rpValue :: a
  }

instance (FromProto a) => FromProto (RawProto a) where
  fromProto :: ByteString -> Either String (RawProto a)
fromProto ByteString
x = (a -> RawProto a) -> Either String a -> Either String (RawProto a)
forall a b. (a -> b) -> Either String a -> Either String b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (ByteString -> a -> RawProto a
forall a. ByteString -> a -> RawProto a
RawProto ByteString
x) (ByteString -> Either String a
forall a. FromProto a => ByteString -> Either String a
fromProto ByteString
x)

instance (ToSchema a) => ToSchema (RawProto a) where
  declareNamedSchema :: Proxy (RawProto a) -> Declare (Definitions Schema) NamedSchema
declareNamedSchema Proxy (RawProto a)
_ = Proxy a -> Declare (Definitions Schema) NamedSchema
forall a.
ToSchema a =>
Proxy a -> Declare (Definitions Schema) NamedSchema
declareNamedSchema (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @a)