{-# 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