forked from haskell-servant/servant-swagger
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request haskell-servant#24 from wireapp/update-brig-types
[brig-types] import updates
- Loading branch information
Showing
6 changed files
with
266 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
module Brig.Types (module M) where | ||
|
||
import Brig.Types.Activation as M | ||
import Brig.Types.Client as M | ||
import Brig.Types.Connection as M | ||
import Brig.Types.Properties as M | ||
import Brig.Types.User as M | ||
import Brig.Types.Activation as M | ||
import Brig.Types.AddressBook as M | ||
import Brig.Types.Client as M | ||
import Brig.Types.Connection as M | ||
import Brig.Types.Properties as M | ||
import Brig.Types.Search as M | ||
import Brig.Types.User as M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Brig.Types.AddressBook | ||
( module Brig.Types.AddressBook | ||
) where | ||
|
||
import Data.Aeson | ||
import Data.ByteString (ByteString) | ||
import Data.Id | ||
import Data.Json.Util | ||
import Data.Text (Text) | ||
|
||
import qualified Data.ByteString.Base64 as B64 | ||
import qualified Data.Text.Encoding as T | ||
|
||
newtype CardId = CardId Text | ||
deriving (Eq, Show, Ord, FromJSON, ToJSON) | ||
|
||
-- The base64-encoded SHA-256 of an email address or a phone number | ||
newtype Entry = Entry { abEntrySha256 :: ByteString } | ||
deriving (Eq, Show, Ord) | ||
|
||
instance FromJSON Entry where | ||
parseJSON = withText "Entry" $ | ||
either (fail "Invalid Entry") (pure . Entry) . (B64.decode . T.encodeUtf8) | ||
|
||
-- Used only in tests but defined here to avoid orphan | ||
instance ToJSON Entry where | ||
toJSON = String . T.decodeUtf8 . B64.encode . abEntrySha256 | ||
|
||
data Card = Card | ||
{ cCardId :: !(Maybe CardId) -- Random card identifier, defined by clients | ||
, cEntries :: ![Entry] | ||
} deriving (Eq, Show) | ||
|
||
instance FromJSON Card where | ||
parseJSON = withObject "matching-card" $ \o -> | ||
Card <$> o .:? "card_id" | ||
<*> o .: "contact" | ||
|
||
instance ToJSON Card where | ||
toJSON c = object | ||
[ "card_id" .= cCardId c | ||
, "contact" .= cEntries c | ||
] | ||
|
||
newtype AddressBook = AddressBook | ||
{ abCards :: [Card] | ||
} deriving (Eq, Show) | ||
|
||
instance FromJSON AddressBook where | ||
parseJSON = withObject "address-book" $ \o -> | ||
AddressBook <$> o .: "cards" | ||
|
||
instance ToJSON AddressBook where | ||
toJSON ab = object | ||
[ "cards" .= abCards ab | ||
] | ||
|
||
-- V3 result | ||
|
||
data Match = Match | ||
{ mUser :: !UserId | ||
, mCardId :: !(Maybe CardId) -- Card id that was matched (Deprecated!) | ||
, mCards :: ![CardId] -- List of card ids matched | ||
} deriving (Eq, Ord, Show) | ||
|
||
instance FromJSON Match where | ||
parseJSON = withObject "match" $ \o -> | ||
Match <$> o .: "id" | ||
<*> o .:? "card_id" | ||
<*> o .:? "cards" .!= [] | ||
|
||
instance ToJSON Match where | ||
toJSON m = object | ||
$ "id" .= mUser m | ||
# "card_id" .= mCardId m | ||
# "cards" .= mCards m | ||
# [] | ||
|
||
data MatchingResult = MatchingResult | ||
{ mrMatches :: ![Match] | ||
, mrAuto :: ![UserId] | ||
} deriving (Eq, Ord, Show) | ||
|
||
instance FromJSON MatchingResult where | ||
parseJSON = withObject "matches" $ \o -> | ||
MatchingResult <$> o .: "results" | ||
<*> o .: "auto-connects" | ||
|
||
instance ToJSON MatchingResult where | ||
toJSON r = object | ||
[ "results" .= mrMatches r | ||
, "auto-connects" .= mrAuto r | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE StrictData #-} | ||
|
||
module Brig.Types.Search where | ||
|
||
import Data.Aeson | ||
import Data.Id (UserId) | ||
import Data.Json.Util | ||
import Data.Text (Text) | ||
|
||
|
||
data SearchResult a = SearchResult | ||
{ searchFound :: Int | ||
, searchReturned :: Int | ||
, searchTook :: Int | ||
, searchResults :: [a] | ||
} deriving Show | ||
|
||
data Contact = Contact | ||
{ contactUserId :: UserId | ||
, contactName :: Text | ||
, contactColorId :: Maybe Int | ||
, contactHandle :: Maybe Text | ||
} deriving Show | ||
|
||
-- | Encodes whether the (current) user has opted in/out of search | ||
newtype SearchableStatus = SearchableStatus { isSearchable :: Bool } | ||
deriving Show | ||
|
||
|
||
instance ToJSON a => ToJSON (SearchResult a) where | ||
toJSON r = object | ||
[ "found" .= searchFound r | ||
, "returned" .= searchReturned r | ||
, "took" .= searchTook r | ||
, "documents" .= searchResults r | ||
] | ||
|
||
instance FromJSON a => FromJSON (SearchResult a) where | ||
parseJSON = withObject "SearchResult" $ \o -> | ||
SearchResult <$> o .: "found" | ||
<*> o .: "returned" | ||
<*> o .: "took" | ||
<*> o .: "documents" | ||
|
||
instance ToJSON Contact where | ||
toJSON c = object | ||
$ "id" .= contactUserId c | ||
# "name" .= contactName c | ||
# "accent_id" .= contactColorId c | ||
# "handle" .= contactHandle c | ||
# [] | ||
|
||
instance FromJSON Contact where | ||
parseJSON = withObject "Contact" $ \o -> | ||
Contact <$> o .: "id" | ||
<*> o .: "name" | ||
<*> o .:? "accent_id" | ||
<*> o .:? "handle" | ||
|
||
instance ToJSON SearchableStatus where | ||
toJSON (SearchableStatus onoff) = object [ "searchable" .= onoff ] | ||
|
||
instance FromJSON SearchableStatus where | ||
parseJSON = withObject "SearchableStatus" $ | ||
fmap SearchableStatus . (.: "searchable") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters