@@ -1,38 +1,59 @@
1−{-# LANGUAGE TemplateHaskell #-}
2−{-# LANGUAGE RankNTypes #-}
1+{-# LANGUAGE CPP #-}
2+{-# LANGUAGE DeriveFoldable #-}
3+{-# LANGUAGE DeriveFunctor #-}
4+{-# LANGUAGE DeriveTraversable #-}
5+{-# LANGUAGE OverloadedStrings #-}
6+{-# LANGUAGE RankNTypes #-}
37{-# LANGUAGE ScopedTypeVariables #-}
4−{-# LANGUAGE OverloadedStrings #-}
8+{-# LANGUAGE TemplateHaskell #-}
59module Network.API.Mandrill.Types where
610
7−import Network.API.Mandrill.Utils
8−import Test.QuickCheck
11+import Control.Applicative
12+import Control.Monad (mzero)
913import Data.Char
14+import Data.Maybe
1015import Data.Time
11−import Control.Applicative
12−import System.Locale (defaultTimeLocale)
13−import Data.Map (Map)
14−import qualified Data.Map as Map
15−import qualified Data.ByteString as B
16−import qualified Data.ByteString.Base64 as Base64
17−import qualified Data.Text as T
18−import qualified Data.Text.Encoding as TL
19−import qualified Data.Text.Lazy as TL
20−import Control.Lens
21−import Data.Monoid
16+import Lens.Micro.TH (makeLenses)
17+import Network.API.Mandrill.Utils
18+import Test.QuickCheck
19+import Text.Email.Validate
20+#if MIN_VERSION_time(1,5,0)
21+import Data.Time.Format (TimeLocale, defaultTimeLocale)
22+#else
23+import System.Locale (TimeLocale, defaultTimeLocale)
24+#endif
2225import Data.Aeson
23−import Data.Aeson.Types
2426import Data.Aeson.TH
25−import qualified Text.Blaze.Html as Blaze
27+import Data.Aeson.Types
28+import qualified Data.ByteString as B
29+import qualified Data.ByteString.Base64 as Base64
30+#if !MIN_VERSION_base(4,8,0)
31+import Data.Foldable
32+import Data.Traversable
33+#endif
34+import qualified Data.HashMap.Strict as H
35+import Data.Monoid
36+import qualified Data.Text as T
37+import qualified Data.Text.Encoding as TL
38+import qualified Data.Text.Lazy as TL
39+import qualified Text.Blaze.Html as Blaze
2640import qualified Text.Blaze.Html.Renderer.Text as Blaze
41+import qualified Text.Email.Validate as TEV
2742
43+timeParse :: ParseTime t => TimeLocale -> String -> String -> Maybe t
44+#if MIN_VERSION_time(1,5,0)
45+timeParse = parseTimeM True
46+#else
47+timeParse = parseTime
48+#endif
2849
2950--------------------------------------------------------------------------------
3051data MandrillError = MandrillError {
31− _merr_status :: !T.Text
32− , _merr_code :: !Int
33− , _merr_name :: !T.Text
52+ _merr_status :: !T.Text
53+ , _merr_code :: !Int
54+ , _merr_name :: !T.Text
3455 , _merr_message :: !T.Text
35− } deriving Show
56+ } deriving (Show, Eq)
3657
3758makeLenses ''MandrillError
3859deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillError
@@ -57,6 +78,7 @@ data MandrillRejectReason = RR_HardBounce
5778 | RR_InvalidSender
5879 | RR_Invalid
5980 | RR_TestModeLimit
81+ | RR_Unsigned
6082 | RR_Rule deriving Show
6183
6284deriveJSON defaultOptions {
@@ -69,7 +91,7 @@ deriveJSON defaultOptions {
6991-- which can be either a success or a failure.
7092data MandrillResponse k =
7193 MandrillSuccess k
72− | MandrillFailure MandrillError deriving Show
94+ | MandrillFailure MandrillError deriving (Show, Eq, Functor, Foldable, Traversable)
7395
7496instance FromJSON k => FromJSON (MandrillResponse k) where
7597 parseJSON v = case (parseMaybe parseJSON v) :: Maybe k of
@@ -87,14 +109,27 @@ data MandrillRecipientTag = To | Cc | Bcc deriving Show
87109deriveJSON defaultOptions { constructorTagModifier = map toLower } ''MandrillRecipientTag
88110
89111
112+--------------------------------------------------------------------------------
113+newtype MandrillEmail = MandrillEmail EmailAddress deriving Show
114+
115+instance ToJSON MandrillEmail where
116+ toJSON (MandrillEmail e) = String . TL.decodeUtf8 . toByteString $ e
117+
118+instance FromJSON MandrillEmail where
119+ parseJSON (String s) = case validate (TL.encodeUtf8 s) of
120+ Left err -> fail err
121+ Right v -> return . MandrillEmail $ v
122+ parseJSON o = typeMismatch "Expecting a String for MandrillEmail." o
123+
124+
90125--------------------------------------------------------------------------------
91126-- | An array of recipient information.
92127data MandrillRecipient = MandrillRecipient {
93− _mrec_email :: !T.Text
128+ _mrec_email :: MandrillEmail
94129 -- ^ The email address of the recipient
95− , _mrec_name :: Maybe T.Text
130+ , _mrec_name :: Maybe T.Text
96131 -- ^ The optional display name to use for the recipient
97− , _mrec_type :: Maybe MandrillRecipientTag
132+ , _mrec_type :: Maybe MandrillRecipientTag
98133 -- ^ The header type to use for the recipient.
99134 -- defaults to "to" if not provided
100135 } deriving Show
@@ -102,17 +137,35 @@ data MandrillRecipient = MandrillRecipient {
102137makeLenses ''MandrillRecipient
103138deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillRecipient
104139
140+newRecipient :: EmailAddress -> MandrillRecipient
141+newRecipient email = MandrillRecipient (MandrillEmail email) Nothing Nothing
142+
105143instance Arbitrary MandrillRecipient where
106− arbitrary = pure $ MandrillRecipient "test@example.com" Nothing Nothing
144+ arbitrary = pure MandrillRecipient {
145+ _mrec_email = MandrillEmail $ fromJust (emailAddress "test@example.com")
146+ , _mrec_name = Nothing
147+ , _mrec_type = Nothing
148+ }
107149
108150--------------------------------------------------------------------------------
109151newtype MandrillHtml = MandrillHtml Blaze.Html
110152
111−mkMandrillHtml :: T.Text -> MandrillHtml
112−mkMandrillHtml = MandrillHtml . Blaze.toHtml
153+unsafeMkMandrillHtml :: T.Text -> MandrillHtml
154+unsafeMkMandrillHtml = MandrillHtml . Blaze.preEscapedToHtml
155+
156+-- This might be slightly hairy because it violates
157+-- the nice encapsulation that newtypes offer.
158+mkMandrillHtml :: Blaze.Html -> MandrillHtml
159+mkMandrillHtml = MandrillHtml
160+
161+#if MIN_VERSION_base(4,11,0)
162+instance Semigroup MandrillHtml where
163+ MandrillHtml m1 <> MandrillHtml m2 = MandrillHtml (m1 <> m2)
164+#endif
113165
114−mkMandrillHtml' :: Blaze.Html -> MandrillHtml
115−mkMandrillHtml' = MandrillHtml
166+instance Monoid MandrillHtml where
167+ mempty = MandrillHtml mempty
168+ mappend (MandrillHtml m1) (MandrillHtml m2) = MandrillHtml (m1 <> m2)
116169
117170instance Show MandrillHtml where
118171 show (MandrillHtml h) = show $ Blaze.renderHtml h
@@ -121,7 +174,7 @@ instance ToJSON MandrillHtml where
121174 toJSON (MandrillHtml h) = String . TL.toStrict . Blaze.renderHtml $ h
122175
123176instance FromJSON MandrillHtml where
124− parseJSON (String h) = return $ MandrillHtml (Blaze.toHtml h)
177+ parseJSON (String h) = return $ MandrillHtml (Blaze.preEscapedToHtml h)
125178 parseJSON v = typeMismatch "Expecting a String for MandrillHtml" v
126179
127180instance Arbitrary MandrillHtml where
@@ -132,17 +185,23 @@ type MandrillTags = T.Text
132185
133186
134187--------------------------------------------------------------------------------
135−type MandrillHeaders = Value
188+type MandrillHeaders = Object
136189
137190
138191--------------------------------------------------------------------------------
139−type MandrillVars = Value
140192
193+data MergeVar = MergeVar {
194+ _mv_name :: !T.Text
195+ , _mv_content :: Value
196+ } deriving Show
197+
198+makeLenses ''MergeVar
199+deriveJSON defaultOptions { fieldLabelModifier = drop 4 } ''MergeVar
141200
142201--------------------------------------------------------------------------------
143202data MandrillMergeVars = MandrillMergeVars {
144203 _mmvr_rcpt :: !T.Text
145− , _mmvr_vars :: [MandrillVars]
204+ , _mmvr_vars :: [MergeVar]
146205 } deriving Show
147206
148207makeLenses ''MandrillMergeVars
@@ -150,29 +209,33 @@ deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillMergeVars
150209
151210--------------------------------------------------------------------------------
152211data MandrillMetadata = MandrillMetadata {
153− _mmdt_rcpt :: !T.Text
154− , _mmdt_values :: MandrillVars
212+ _mmdt_rcpt :: !T.Text
213+ , _mmdt_values :: Object
155214 } deriving Show
156215
157216makeLenses ''MandrillMetadata
158217deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillMetadata
159218
160219
161−newtype Base64ByteString = B64BS B.ByteString deriving Show
220+data Base64ByteString =
221+ EncodedB64BS B.ByteString
222+ -- ^ An already-encoded Base64 ByteString.
223+ | PlainBS B.ByteString
224+ -- ^ A plain Base64 ByteString which requires encoding.
225+ deriving Show
162226
163227instance ToJSON Base64ByteString where
164− toJSON (B64BS bs) = String . TL.decodeUtf8 . Base64.encode $ bs
228+ toJSON (PlainBS bs) = String . TL.decodeUtf8 . Base64.encode $ bs
229+ toJSON (EncodedB64BS bs) = String . TL.decodeUtf8 $ bs
165230
166231instance FromJSON Base64ByteString where
167− parseJSON (String v) = case Base64.decode (TL.encodeUtf8 v) of
168− Left err -> fail err
169− Right rs -> return $ B64BS rs
232+ parseJSON (String v) = pure $ EncodedB64BS (TL.encodeUtf8 v)
170233 parseJSON rest = typeMismatch "Base64ByteString must be a String." rest
171234
172235--------------------------------------------------------------------------------
173236data MandrillWebContent = MandrillWebContent {
174− _mwct_type :: !T.Text
175− , _mwct_name :: !T.Text
237+ _mwct_type :: !T.Text
238+ , _mwct_name :: !T.Text
176239 -- ^ [for images] the Content ID of the image
177240 -- - use <img src="cid:THIS_VALUE"> to reference the image
178241 -- in your HTML content
@@ -185,67 +248,67 @@ deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillWebContent
185248--------------------------------------------------------------------------------
186249-- | The information on the message to send
187250data MandrillMessage = MandrillMessage {
188− _mmsg_html :: MandrillHtml
251+ _mmsg_html :: MandrillHtml
189252 -- ^ The full HTML content to be sent
190− , _mmsg_text :: Maybe T.Text
253+ , _mmsg_text :: Maybe T.Text
191254 -- ^ Optional full text content to be sent
192− , _mmsg_subject :: !T.Text
255+ , _mmsg_subject :: !T.Text
193256 -- ^ The message subject
194− , _mmsg_from_email :: !T.Text
257+ , _mmsg_from_email :: MandrillEmail
195258 -- ^ The sender email address
196− , _mmsg_from_name :: Maybe T.Text
259+ , _mmsg_from_name :: Maybe T.Text
197260 -- ^ Optional from name to be used
198− , _mmsg_to :: [MandrillRecipient]
261+ , _mmsg_to :: [MandrillRecipient]
199262 -- ^ A list of recipient information
200− , _mmsg_headers :: MandrillHeaders
263+ , _mmsg_headers :: MandrillHeaders
201264 -- ^ optional extra headers to add to the message (most headers are allowed)
202− , _mmsg_important :: Maybe Bool
265+ , _mmsg_important :: Maybe Bool
203266 -- ^ whether or not this message is important, and should be delivered ahead
204267 -- of non-important messages
205− , _mmsg_track_opens :: Maybe Bool
268+ , _mmsg_track_opens :: Maybe Bool
206269 -- ^ whether or not to turn on open tracking for the message
207− , _mmsg_track_clicks :: Maybe Bool
270+ , _mmsg_track_clicks :: Maybe Bool
208271 -- ^ whether or not to turn on click tracking for the message
209− , _mmsg_auto_text :: Maybe Bool
272+ , _mmsg_auto_text :: Maybe Bool
210273 -- ^ whether or not to automatically generate a text part for messages that are not given text
211− , _mmsg_auto_html :: Maybe Bool
274+ , _mmsg_auto_html :: Maybe Bool
212275 -- ^ whether or not to automatically generate an HTML part for messages that are not given HTML
213− , _mmsg_inline_css :: Maybe Bool
276+ , _mmsg_inline_css :: Maybe Bool
214277 -- ^ whether or not to automatically inline all CSS styles provided in the message HTML
215278 -- - only for HTML documents less than 256KB in size
216− , _mmsg_url_strip_qs :: Maybe Bool
279+ , _mmsg_url_strip_qs :: Maybe Bool
217280 -- ^ whether or not to strip the query string from URLs when aggregating tracked URL data
218− , _mmsg_preserve_recipients :: Maybe Bool
281+ , _mmsg_preserve_recipients :: Maybe Bool
219282 -- ^ whether or not to expose all recipients in to "To" header for each email
220− , _mmsg_view_content_link :: Maybe Bool
283+ , _mmsg_view_content_link :: Maybe Bool
221284 -- ^ set to false to remove content logging for sensitive emails
222− , _mmsg_bcc_address :: Maybe T.Text
285+ , _mmsg_bcc_address :: Maybe T.Text
223286 -- ^ an optional address to receive an exact copy of each recipient's email
224− , _mmsg_tracking_domain :: Maybe T.Text
287+ , _mmsg_tracking_domain :: Maybe T.Text
225288 -- ^ a custom domain to use for tracking opens and clicks instead of mandrillapp.com
226− , _mmsg_signing_domain :: Maybe Bool
227− -- ^ a custom domain to use for SPF/DKIM signing instead of mandrill
289+ , _mmsg_signing_domain :: Maybe Bool
290+ -- ^ a custom domain to use for SPF/DKIM signing instead of mandrill
228291 -- (for "via" or "on behalf of" in email clients)
229− , _mmsg_return_path_domain :: Maybe Bool
292+ , _mmsg_return_path_domain :: Maybe Bool
230293 -- ^ a custom domain to use for the messages's return-path
231− , _mmsg_merge :: Maybe Bool
294+ , _mmsg_merge :: Maybe Bool
232295 -- ^ whether to evaluate merge tags in the message.
233296 -- Will automatically be set to true if either merge_vars
234297 -- or global_merge_vars are provided.
235− , _mmsg_global_merge_vars :: [MandrillVars]
298+ , _mmsg_global_merge_vars :: [MergeVar]
236299 -- ^ global merge variables to use for all recipients. You can override these per recipient.
237− , _mmsg_merge_vars :: [MandrillMergeVars]
300+ , _mmsg_merge_vars :: [MandrillMergeVars]
238301 -- ^ per-recipient merge variables, which override global merge variables with the same name.
239− , _mmsg_tags :: [MandrillTags]
302+ , _mmsg_tags :: [MandrillTags]
240303 -- ^ an array of string to tag the message with. Stats are accumulated using tags,
241304 -- though we only store the first 100 we see, so this should not be unique
242305 -- or change frequently. Tags should be 50 characters or less.
243306 -- Any tags starting with an underscore are reserved for internal use
244307 -- and will cause errors.
245− , _mmsg_subaccount :: Maybe T.Text
308+ , _mmsg_subaccount :: Maybe T.Text
246309 -- ^ the unique id of a subaccount for this message
247310 -- - must already exist or will fail with an error
248− , _mmsg_google_analytics_domains :: [T.Text]
311+ , _mmsg_google_analytics_domains :: [T.Text]
249312 -- ^ an array of strings indicating for which any matching URLs
250313 -- will automatically have Google Analytics parameters appended
251314 -- to their query string automatically.
@@ -253,17 +316,17 @@ data MandrillMessage = MandrillMessage {
253316 -- ^ optional string indicating the value to set for the utm_campaign
254317 -- tracking parameter. If this isn't provided the email's from address
255318 -- will be used instead.
256− , _mmsg_metadata :: MandrillVars
319+ , _mmsg_metadata :: Object
257320 -- ^ metadata an associative array of user metadata. Mandrill will store
258321 -- this metadata and make it available for retrieval.
259322 -- In addition, you can select up to 10 metadata fields to index
260323 -- and make searchable using the Mandrill search api.
261− , _mmsg_recipient_metadata :: [MandrillMetadata]
324+ , _mmsg_recipient_metadata :: [MandrillMetadata]
262325 -- ^ Per-recipient metadata that will override the global values
263326 -- specified in the metadata parameter.
264− , _mmsg_attachments :: [MandrillWebContent]
327+ , _mmsg_attachments :: [MandrillWebContent]
265328 -- ^ an array of supported attachments to add to the message
266− , _mmsg_images :: [MandrillWebContent]
329+ , _mmsg_images :: [MandrillWebContent]
267330 -- ^ an array of embedded images to add to the message
268331 } deriving Show
269332
@@ -274,10 +337,10 @@ instance Arbitrary MandrillMessage where
274337 arbitrary = MandrillMessage <$> arbitrary
275338 <*> pure Nothing
276339 <*> pure "Test Subject"
277− <*> pure "sender@example.com"
340+ <*> pure (MandrillEmail . fromJust $ emailAddress "sender@example.com")
278341 <*> pure Nothing
279342 <*> resize 2 arbitrary
280− <*> pure emptyObject
343+ <*> pure H.empty
281344 <*> pure Nothing
282345 <*> pure Nothing
283346 <*> pure Nothing
@@ -298,13 +361,24 @@ instance Arbitrary MandrillMessage where
298361 <*> pure Nothing
299362 <*> pure []
300363 <*> pure Nothing
301− <*> pure emptyObject
364+ <*> pure H.empty
302365 <*> pure []
303366 <*> pure []
304367 <*> pure []
305368
369+--------------------------------------------------------------------------------
370+-- | Key value pair for replacing content in templates via 'Editable Regions'
371+data MandrillTemplateContent = MandrillTemplateContent {
372+ _mtc_name :: T.Text
373+ , _mtc_content :: T.Text
374+ } deriving Show
375+
376+makeLenses ''MandrillTemplateContent
377+deriveJSON defaultOptions { fieldLabelModifier = drop 5 } ''MandrillTemplateContent
378+
306379--------------------------------------------------------------------------------
307380type MandrillKey = T.Text
381+type MandrillTemplate = T.Text
308382
309383newtype MandrillDate = MandrillDate {
310384 fromMandrillDate :: UTCTime
@@ -315,6 +389,6 @@ instance ToJSON MandrillDate where
315389
316390instance FromJSON MandrillDate where
317391 parseJSON = withText "MandrillDate" $ \t ->
318− case parseTime defaultTimeLocale "%Y-%m-%d %I:%M:%S%Q" (T.unpack t) of
392+ case timeParse defaultTimeLocale "%Y-%m-%d %I:%M:%S%Q" (T.unpack t) of
319393 Just d -> pure $ MandrillDate d
320394 _ -> fail "could not parse Mandrill date"