diff options
Diffstat (limited to 'src/F2Md/Config.hs')
-rw-r--r-- | src/F2Md/Config.hs | 41 |
1 files changed, 41 insertions, 0 deletions
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 |