@@ -1,44 +1,59 @@
1−{-# LANGUAGE TemplateHaskell #-}
2−{-# LANGUAGE CPP #-}
3−{-# LANGUAGE RankNTypes #-}
1+{-# LANGUAGE CPP #-}
2+{-# LANGUAGE DeriveFoldable #-}
3+{-# LANGUAGE DeriveFunctor #-}
4+{-# LANGUAGE DeriveTraversable #-}
5+{-# LANGUAGE OverloadedStrings #-}
6+{-# LANGUAGE RankNTypes #-}
47{-# LANGUAGE ScopedTypeVariables #-}
5−{-# LANGUAGE OverloadedStrings #-}
8+{-# LANGUAGE TemplateHaskell #-}
69module Network.API.Mandrill.Types where
710
8−import Network.API.Mandrill.Utils
9−import Network.API.Mandrill.Orphans()
10−import Test.QuickCheck
11−import Text.Email.Validate
11+import Control.Applicative
12+import Control.Monad (mzero)
1213import Data.Char
1314import Data.Maybe
1415import Data.Time
15−import Control.Applicative
16+import Lens.Micro.TH (makeLenses)
17+import Network.API.Mandrill.Utils
18+import Test.QuickCheck
19+import Text.Email.Validate
1620#if MIN_VERSION_time(1,5,0)
17−import Data.Time.Format (defaultTimeLocale)
21+import Data.Time.Format (TimeLocale, defaultTimeLocale)
1822#else
19−import System.Locale (defaultTimeLocale)
23+import System.Locale (TimeLocale, defaultTimeLocale)
2024#endif
21−import qualified Data.ByteString as B
22−import qualified Data.ByteString.Base64 as Base64
23−import qualified Data.Text as T
24−import qualified Data.Text.Encoding as TL
25−import qualified Data.Text.Lazy as TL
26−import Control.Lens
27−import Data.Monoid
2825import Data.Aeson
29−import Data.Aeson.Types
3026import Data.Aeson.TH
31−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
3240import qualified Text.Blaze.Html.Renderer.Text as Blaze
41+import qualified Text.Email.Validate as TEV
3342
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
3449
3550--------------------------------------------------------------------------------
3651data MandrillError = MandrillError {
37− _merr_status :: !T.Text
38− , _merr_code :: !Int
39− , _merr_name :: !T.Text
52+ _merr_status :: !T.Text
53+ , _merr_code :: !Int
54+ , _merr_name :: !T.Text
4055 , _merr_message :: !T.Text
41− } deriving Show
56+ } deriving (Show, Eq)
4257
4358makeLenses ''MandrillError
4459deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillError
@@ -63,6 +78,7 @@ data MandrillRejectReason = RR_HardBounce
6378 | RR_InvalidSender
6479 | RR_Invalid
6580 | RR_TestModeLimit
81+ | RR_Unsigned
6682 | RR_Rule deriving Show
6783
6884deriveJSON defaultOptions {
@@ -75,7 +91,7 @@ deriveJSON defaultOptions {
7591-- which can be either a success or a failure.
7692data MandrillResponse k =
7793 MandrillSuccess k
78− | MandrillFailure MandrillError deriving Show
94+ | MandrillFailure MandrillError deriving (Show, Eq, Functor, Foldable, Traversable)
7995
8096instance FromJSON k => FromJSON (MandrillResponse k) where
8197 parseJSON v = case (parseMaybe parseJSON v) :: Maybe k of
@@ -93,14 +109,27 @@ data MandrillRecipientTag = To | Cc | Bcc deriving Show
93109deriveJSON defaultOptions { constructorTagModifier = map toLower } ''MandrillRecipientTag
94110
95111
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+
96125--------------------------------------------------------------------------------
97126-- | An array of recipient information.
98127data MandrillRecipient = MandrillRecipient {
99− _mrec_email :: EmailAddress
128+ _mrec_email :: MandrillEmail
100129 -- ^ The email address of the recipient
101− , _mrec_name :: Maybe T.Text
130+ , _mrec_name :: Maybe T.Text
102131 -- ^ The optional display name to use for the recipient
103− , _mrec_type :: Maybe MandrillRecipientTag
132+ , _mrec_type :: Maybe MandrillRecipientTag
104133 -- ^ The header type to use for the recipient.
105134 -- defaults to "to" if not provided
106135 } deriving Show
@@ -109,11 +138,11 @@ makeLenses ''MandrillRecipient
109138deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillRecipient
110139
111140newRecipient :: EmailAddress -> MandrillRecipient
112−newRecipient email = MandrillRecipient email Nothing Nothing
141+newRecipient email = MandrillRecipient (MandrillEmail email) Nothing Nothing
113142
114143instance Arbitrary MandrillRecipient where
115144 arbitrary = pure MandrillRecipient {
116− _mrec_email = fromJust (emailAddress "test@example.com")
145+ _mrec_email = MandrillEmail $ fromJust (emailAddress "test@example.com")
117146 , _mrec_name = Nothing
118147 , _mrec_type = Nothing
119148 }
@@ -129,6 +158,11 @@ unsafeMkMandrillHtml = MandrillHtml . Blaze.preEscapedToHtml
129158mkMandrillHtml :: Blaze.Html -> MandrillHtml
130159mkMandrillHtml = MandrillHtml
131160
161+#if MIN_VERSION_base(4,11,0)
162+instance Semigroup MandrillHtml where
163+ MandrillHtml m1 <> MandrillHtml m2 = MandrillHtml (m1 <> m2)
164+#endif
165+
132166instance Monoid MandrillHtml where
133167 mempty = MandrillHtml mempty
134168 mappend (MandrillHtml m1) (MandrillHtml m2) = MandrillHtml (m1 <> m2)
@@ -151,17 +185,23 @@ type MandrillTags = T.Text
151185
152186
153187--------------------------------------------------------------------------------
154−type MandrillHeaders = Value
188+type MandrillHeaders = Object
155189
156190
157191--------------------------------------------------------------------------------
158−type MandrillVars = Value
159192
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
160200
161201--------------------------------------------------------------------------------
162202data MandrillMergeVars = MandrillMergeVars {
163203 _mmvr_rcpt :: !T.Text
164− , _mmvr_vars :: [MandrillVars]
204+ , _mmvr_vars :: [MergeVar]
165205 } deriving Show
166206
167207makeLenses ''MandrillMergeVars
@@ -169,29 +209,33 @@ deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillMergeVars
169209
170210--------------------------------------------------------------------------------
171211data MandrillMetadata = MandrillMetadata {
172− _mmdt_rcpt :: !T.Text
173− , _mmdt_values :: MandrillVars
212+ _mmdt_rcpt :: !T.Text
213+ , _mmdt_values :: Object
174214 } deriving Show
175215
176216makeLenses ''MandrillMetadata
177217deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillMetadata
178218
179219
180−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
181226
182227instance ToJSON Base64ByteString where
183− 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
184230
185231instance FromJSON Base64ByteString where
186− parseJSON (String v) = case Base64.decode (TL.encodeUtf8 v) of
187− Left err -> fail err
188− Right rs -> return $ B64BS rs
232+ parseJSON (String v) = pure $ EncodedB64BS (TL.encodeUtf8 v)
189233 parseJSON rest = typeMismatch "Base64ByteString must be a String." rest
190234
191235--------------------------------------------------------------------------------
192236data MandrillWebContent = MandrillWebContent {
193− _mwct_type :: !T.Text
194− , _mwct_name :: !T.Text
237+ _mwct_type :: !T.Text
238+ , _mwct_name :: !T.Text
195239 -- ^ [for images] the Content ID of the image
196240 -- - use <img src="cid:THIS_VALUE"> to reference the image
197241 -- in your HTML content
@@ -204,67 +248,67 @@ deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillWebContent
204248--------------------------------------------------------------------------------
205249-- | The information on the message to send
206250data MandrillMessage = MandrillMessage {
207− _mmsg_html :: MandrillHtml
251+ _mmsg_html :: MandrillHtml
208252 -- ^ The full HTML content to be sent
209− , _mmsg_text :: Maybe T.Text
253+ , _mmsg_text :: Maybe T.Text
210254 -- ^ Optional full text content to be sent
211− , _mmsg_subject :: !T.Text
255+ , _mmsg_subject :: !T.Text
212256 -- ^ The message subject
213− , _mmsg_from_email :: EmailAddress
257+ , _mmsg_from_email :: MandrillEmail
214258 -- ^ The sender email address
215− , _mmsg_from_name :: Maybe T.Text
259+ , _mmsg_from_name :: Maybe T.Text
216260 -- ^ Optional from name to be used
217− , _mmsg_to :: [MandrillRecipient]
261+ , _mmsg_to :: [MandrillRecipient]
218262 -- ^ A list of recipient information
219− , _mmsg_headers :: MandrillHeaders
263+ , _mmsg_headers :: MandrillHeaders
220264 -- ^ optional extra headers to add to the message (most headers are allowed)
221− , _mmsg_important :: Maybe Bool
265+ , _mmsg_important :: Maybe Bool
222266 -- ^ whether or not this message is important, and should be delivered ahead
223267 -- of non-important messages
224− , _mmsg_track_opens :: Maybe Bool
268+ , _mmsg_track_opens :: Maybe Bool
225269 -- ^ whether or not to turn on open tracking for the message
226− , _mmsg_track_clicks :: Maybe Bool
270+ , _mmsg_track_clicks :: Maybe Bool
227271 -- ^ whether or not to turn on click tracking for the message
228− , _mmsg_auto_text :: Maybe Bool
272+ , _mmsg_auto_text :: Maybe Bool
229273 -- ^ whether or not to automatically generate a text part for messages that are not given text
230− , _mmsg_auto_html :: Maybe Bool
274+ , _mmsg_auto_html :: Maybe Bool
231275 -- ^ whether or not to automatically generate an HTML part for messages that are not given HTML
232− , _mmsg_inline_css :: Maybe Bool
276+ , _mmsg_inline_css :: Maybe Bool
233277 -- ^ whether or not to automatically inline all CSS styles provided in the message HTML
234278 -- - only for HTML documents less than 256KB in size
235− , _mmsg_url_strip_qs :: Maybe Bool
279+ , _mmsg_url_strip_qs :: Maybe Bool
236280 -- ^ whether or not to strip the query string from URLs when aggregating tracked URL data
237− , _mmsg_preserve_recipients :: Maybe Bool
281+ , _mmsg_preserve_recipients :: Maybe Bool
238282 -- ^ whether or not to expose all recipients in to "To" header for each email
239− , _mmsg_view_content_link :: Maybe Bool
283+ , _mmsg_view_content_link :: Maybe Bool
240284 -- ^ set to false to remove content logging for sensitive emails
241− , _mmsg_bcc_address :: Maybe T.Text
285+ , _mmsg_bcc_address :: Maybe T.Text
242286 -- ^ an optional address to receive an exact copy of each recipient's email
243− , _mmsg_tracking_domain :: Maybe T.Text
287+ , _mmsg_tracking_domain :: Maybe T.Text
244288 -- ^ a custom domain to use for tracking opens and clicks instead of mandrillapp.com
245− , _mmsg_signing_domain :: Maybe Bool
246− -- ^ 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
247291 -- (for "via" or "on behalf of" in email clients)
248− , _mmsg_return_path_domain :: Maybe Bool
292+ , _mmsg_return_path_domain :: Maybe Bool
249293 -- ^ a custom domain to use for the messages's return-path
250− , _mmsg_merge :: Maybe Bool
294+ , _mmsg_merge :: Maybe Bool
251295 -- ^ whether to evaluate merge tags in the message.
252296 -- Will automatically be set to true if either merge_vars
253297 -- or global_merge_vars are provided.
254− , _mmsg_global_merge_vars :: [MandrillVars]
298+ , _mmsg_global_merge_vars :: [MergeVar]
255299 -- ^ global merge variables to use for all recipients. You can override these per recipient.
256− , _mmsg_merge_vars :: [MandrillMergeVars]
300+ , _mmsg_merge_vars :: [MandrillMergeVars]
257301 -- ^ per-recipient merge variables, which override global merge variables with the same name.
258− , _mmsg_tags :: [MandrillTags]
302+ , _mmsg_tags :: [MandrillTags]
259303 -- ^ an array of string to tag the message with. Stats are accumulated using tags,
260304 -- though we only store the first 100 we see, so this should not be unique
261305 -- or change frequently. Tags should be 50 characters or less.
262306 -- Any tags starting with an underscore are reserved for internal use
263307 -- and will cause errors.
264− , _mmsg_subaccount :: Maybe T.Text
308+ , _mmsg_subaccount :: Maybe T.Text
265309 -- ^ the unique id of a subaccount for this message
266310 -- - must already exist or will fail with an error
267− , _mmsg_google_analytics_domains :: [T.Text]
311+ , _mmsg_google_analytics_domains :: [T.Text]
268312 -- ^ an array of strings indicating for which any matching URLs
269313 -- will automatically have Google Analytics parameters appended
270314 -- to their query string automatically.
@@ -272,17 +316,17 @@ data MandrillMessage = MandrillMessage {
272316 -- ^ optional string indicating the value to set for the utm_campaign
273317 -- tracking parameter. If this isn't provided the email's from address
274318 -- will be used instead.
275− , _mmsg_metadata :: MandrillVars
319+ , _mmsg_metadata :: Object
276320 -- ^ metadata an associative array of user metadata. Mandrill will store
277321 -- this metadata and make it available for retrieval.
278322 -- In addition, you can select up to 10 metadata fields to index
279323 -- and make searchable using the Mandrill search api.
280− , _mmsg_recipient_metadata :: [MandrillMetadata]
324+ , _mmsg_recipient_metadata :: [MandrillMetadata]
281325 -- ^ Per-recipient metadata that will override the global values
282326 -- specified in the metadata parameter.
283− , _mmsg_attachments :: [MandrillWebContent]
327+ , _mmsg_attachments :: [MandrillWebContent]
284328 -- ^ an array of supported attachments to add to the message
285− , _mmsg_images :: [MandrillWebContent]
329+ , _mmsg_images :: [MandrillWebContent]
286330 -- ^ an array of embedded images to add to the message
287331 } deriving Show
288332
@@ -293,10 +337,10 @@ instance Arbitrary MandrillMessage where
293337 arbitrary = MandrillMessage <$> arbitrary
294338 <*> pure Nothing
295339 <*> pure "Test Subject"
296− <*> pure (fromJust $ emailAddress "sender@example.com")
340+ <*> pure (MandrillEmail . fromJust $ emailAddress "sender@example.com")
297341 <*> pure Nothing
298342 <*> resize 2 arbitrary
299− <*> pure emptyObject
343+ <*> pure H.empty
300344 <*> pure Nothing
301345 <*> pure Nothing
302346 <*> pure Nothing
@@ -317,13 +361,24 @@ instance Arbitrary MandrillMessage where
317361 <*> pure Nothing
318362 <*> pure []
319363 <*> pure Nothing
320− <*> pure emptyObject
364+ <*> pure H.empty
321365 <*> pure []
322366 <*> pure []
323367 <*> pure []
324368
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+
325379--------------------------------------------------------------------------------
326380type MandrillKey = T.Text
381+type MandrillTemplate = T.Text
327382
328383newtype MandrillDate = MandrillDate {
329384 fromMandrillDate :: UTCTime
@@ -334,6 +389,6 @@ instance ToJSON MandrillDate where
334389
335390instance FromJSON MandrillDate where
336391 parseJSON = withText "MandrillDate" $ \t ->
337− case parseTimeM True 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
338393 Just d -> pure $ MandrillDate d
339394 _ -> fail "could not parse Mandrill date"