From caf354c7e5bcb5142d8b4358824d22d3de122f34 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Fri, 27 May 2022 18:57:24 +1000 Subject: break apart the code, timestamp read imp. --- src/F2Md/Config.hs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/F2Md/Config.hs (limited to 'src/F2Md/Config.hs') diff --git a/src/F2Md/Config.hs b/src/F2Md/Config.hs new file mode 100644 index 0000000..ccb1871 --- /dev/null +++ b/src/F2Md/Config.hs @@ -0,0 +1,41 @@ +{-# LANGUAGE DeriveGeneric #-} + +module F2Md.Config (getUserdata, FeedUserdata(..)) where + +import F2Md.Utils +import Data.Maybe +import Data.Text (Text) +import Data.Time +import GHC.Generics +import qualified Data.HashMap.Strict as HM +import Data.Aeson +import qualified Data.ByteString.Lazy as BSL + +data Config = Config + { dbPath :: String + , maildir :: String + , feeds :: [Text] + } deriving (Generic, Show) + +instance FromJSON Config + +data FeedUserdata = FeedUserdata + { url :: Text + , lastUpdated :: Maybe ZonedTime } deriving (Generic, Show) + +getUserdata :: FilePath -> IO ([FeedUserdata], Maybe FilePath) +getUserdata file = do + config <- decode <$> BSL.readFile file + case config of + Nothing -> return ([], Nothing) + Just config' -> do + feedData <- getUserdata' config' + maildir' <- expandPath $ maildir config' + return (feedData, Just maildir') + +getUserdata' :: Config -> IO [FeedUserdata] +getUserdata' (Config dbPath _ feeds) = do + decoded <- decode <$> (BSL.readFile =<< expandPath dbPath) + let decoded' = fromMaybe (HM.fromList []) decoded + return $ + map (\url -> FeedUserdata url $ HM.lookup url decoded') feeds -- cgit v1.2.3