-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move and create federation-related types (#997)
* federator: some churn * move Handle to common-types * introduce Qualified to types-common * move EmailDomain to Data.Domain * define opaque user and conversation IDs * clarify terminology inside/outside the codebase * Add Data.IdMapping
- Loading branch information
Showing
46 changed files
with
351 additions
and
173 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
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
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
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
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,54 @@ | ||
module Data.Domain where | ||
|
||
import Data.Aeson (FromJSON (parseJSON), ToJSON (toJSON)) | ||
import qualified Data.Aeson as Aeson | ||
import qualified Data.Attoparsec.ByteString as Atto | ||
import Data.Bifunctor (bimap, first) | ||
import Data.ByteString.Conversion | ||
import qualified Data.Text.Encoding as Text.E | ||
import Imports | ||
import Test.QuickCheck (Arbitrary (arbitrary), elements) | ||
import qualified Text.Email.Validate as Email | ||
|
||
-- | FUTUREWORK: move this type upstream into the email-validate package? | ||
-- or become independent of email validation. | ||
newtype Domain | ||
= Domain {_domainText :: Text} | ||
deriving (Eq, Generic, Show) | ||
|
||
domainText :: Domain -> Text | ||
domainText = _domainText | ||
|
||
mkDomain :: Text -> Either String Domain | ||
mkDomain = | ||
bimap show Domain . Text.E.decodeUtf8' | ||
<=< validateDomain . Text.E.encodeUtf8 | ||
where | ||
-- this is a slightly hacky way of validating a domain, | ||
-- but Text.Email.Validate doesn't expose the parser for the domain. | ||
validateDomain = fmap Email.domainPart . Email.validate . ("local-part@" <>) | ||
|
||
instance FromByteString Domain where | ||
parser = do | ||
bs <- Atto.takeByteString | ||
case mkDomain =<< first show (Text.E.decodeUtf8' bs) of | ||
Left err -> fail ("Failed parsing ByteString as Domain: " <> err) | ||
Right domain -> pure domain | ||
|
||
instance ToJSON Domain where | ||
toJSON = Aeson.String . domainText | ||
|
||
instance FromJSON Domain where | ||
parseJSON = Aeson.withText "Domain" $ either fail pure . mkDomain | ||
|
||
instance Arbitrary Domain where | ||
arbitrary = | ||
either (error "arbitrary @Domain") id . mkDomain | ||
<$> elements | ||
[ "example.com", | ||
"beispiel.com" | ||
-- unicode domains are not supported, sadly: | ||
-- "例.com", | ||
-- "مثال.com", | ||
-- "dæmi.com" | ||
] |
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,59 @@ | ||
{-# LANGUAGE DerivingStrategies #-} | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
|
||
module Data.Handle where | ||
|
||
import Control.Applicative (optional) | ||
import Data.Aeson hiding ((<?>)) | ||
import Data.Attoparsec.Text | ||
import Data.ByteString.Conversion | ||
import Data.Hashable (Hashable) | ||
import qualified Data.Text as Text | ||
import Imports | ||
import Test.QuickCheck (Arbitrary (arbitrary), choose, elements) | ||
|
||
-------------------------------------------------------------------------------- | ||
-- Handle | ||
|
||
-- | Also called username. | ||
newtype Handle | ||
= Handle | ||
{fromHandle :: Text} | ||
deriving stock (Eq, Show, Generic) | ||
deriving newtype (ToJSON, ToByteString, Hashable) | ||
|
||
instance FromByteString Handle where | ||
parser = parser >>= maybe (fail "Invalid handle") return . parseHandle | ||
|
||
instance FromJSON Handle where | ||
parseJSON = | ||
withText "Handle" $ | ||
maybe (fail "Invalid handle") pure . parseHandle | ||
|
||
parseHandle :: Text -> Maybe Handle | ||
parseHandle t | ||
| isValidHandle t = Just (Handle t) | ||
| otherwise = Nothing | ||
|
||
isValidHandle :: Text -> Bool | ||
isValidHandle t = | ||
either (const False) (const True) $ | ||
parseOnly handle t | ||
where | ||
handle = | ||
count 2 (satisfy chars) | ||
*> count 254 (optional (satisfy chars)) | ||
*> endOfInput | ||
-- NOTE: Ensure that characters such as `@` and `+` should _NOT_ | ||
-- be used so that "phone numbers", "emails", and "handles" remain | ||
-- disjoint sets. | ||
-- The rationale behind max size here relates to the max length of | ||
-- an email address as defined here: | ||
-- http://www.rfc-editor.org/errata_search.php?rfc=3696&eid=1690 | ||
-- with the intent that in the enterprise world handle =~ email address | ||
chars = inClass "a-z0-9_.-" | ||
|
||
instance Arbitrary Handle where | ||
arbitrary = Handle . Text.pack <$> do | ||
let many n = replicateM n (elements $ ['a' .. 'z'] <> ['0' .. '9'] <> ['_'] <> ['-'] <> ['.']) | ||
((<>) <$> many 2 <*> (many =<< choose (0, 254))) |
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,34 @@ | ||
{-# LANGUAGE StrictData #-} | ||
|
||
module Data.IdMapping where | ||
|
||
import Data.Id | ||
import Data.Qualified | ||
import Imports | ||
import Test.QuickCheck (Arbitrary (arbitrary), oneof) | ||
|
||
data MappedOrLocalId a | ||
= Mapped (IdMapping a) | ||
| Local (Id a) | ||
deriving (Show) | ||
|
||
opaqueIdFromMappedOrLocal :: MappedOrLocalId a -> Id (Opaque a) | ||
opaqueIdFromMappedOrLocal = \case | ||
Local localId -> makeIdOpaque localId | ||
Mapped IdMapping {idMappingLocal} -> makeMappedIdOpaque idMappingLocal | ||
|
||
data IdMapping a | ||
= IdMapping | ||
{ idMappingLocal :: Id (Mapped a), | ||
idMappingGlobal :: Qualified (Id a) | ||
} | ||
deriving (Show) | ||
|
||
---------------------------------------------------------------------- | ||
-- ARBITRARY | ||
|
||
instance Arbitrary a => Arbitrary (MappedOrLocalId a) where | ||
arbitrary = oneof [Mapped <$> arbitrary, Local <$> arbitrary] | ||
|
||
instance Arbitrary a => Arbitrary (IdMapping a) where | ||
arbitrary = IdMapping <$> arbitrary <*> arbitrary |
Oops, something went wrong.