From 4de52f287fd9f0cdae9b6cb55678e85458cbbc04 Mon Sep 17 00:00:00 2001 From: Joris Guyonvarch Date: Wed, 15 Apr 2015 00:10:22 +0200 Subject: Adding a title to the mail --- src/AdListener.hs | 52 +++++++++++++++++----------------------------------- 1 file changed, 17 insertions(+), 35 deletions(-) (limited to 'src/AdListener.hs') diff --git a/src/AdListener.hs b/src/AdListener.hs index 04e070b..1de56dc 100644 --- a/src/AdListener.hs +++ b/src/AdListener.hs @@ -17,7 +17,7 @@ import Model.Ad import Model.URL import Model.Resume -import View.Ad (renderAds) +import View.Ad import Page import Parser.Detail @@ -63,50 +63,32 @@ listenToNewAdsWithResumes config viewedURLs resumes = time <- getCurrentFormattedTime if not (null newAds) then - let message = newAdsMessage time newAds + let message = renderConsoleAds time newAds in do T.putStrLn message - trySendMail config message + trySendMail config newAds else return () waitOneMinute listenToNewAdsWithViewedURLs config (viewedURLs ++ newURLs) -trySendMail :: Config -> Text -> IO () -trySendMail config message = +trySendMail :: Config -> [Ad] -> IO () +trySendMail config ads = case C.mailTo config of - Just mailTo -> - do - eitherMailSuccess <- sendMail mailTo message - case eitherMailSuccess of - Right () -> - putStrLn "Mail sent." - Left error -> - T.putStrLn . T.concat $ - [ "Error sending mail: " - , error - ] Nothing -> return () - -newAdsMessage :: Text -> [Ad] -> Text -newAdsMessage time newAds = - let newAdsMessage = - T.concat - [ "\nAt " - , time - , ", got " - , T.pack . show . length $ newAds - , " new ad" - , if length newAds > 1 then "s" else "" - ] - line = T.map (\_ -> '-') newAdsMessage - in T.intercalate - "\n" - [ newAdsMessage - , T.concat [line, "\n"] - , renderAds newAds - ] + Just mailTo -> + let (title, message) = renderAds ads + in do + eitherMailSuccess <- sendMail mailTo title message + case eitherMailSuccess of + Right () -> + putStrLn "\nMail sent." + Left error -> + T.putStrLn . T.concat $ + [ "\nError sending mail: " + , error + ] listenError :: Config -> [URL] -> Text -> IO () listenError config viewedURLs error = do -- cgit v1.2.3