aboutsummaryrefslogtreecommitdiff
path: root/src/F2Md/Config.hs
diff options
context:
space:
mode:
authorYuchen Pei <hi@ypei.me>2022-05-27 18:57:24 +1000
committerYuchen Pei <hi@ypei.me>2022-05-27 18:57:24 +1000
commitcaf354c7e5bcb5142d8b4358824d22d3de122f34 (patch)
tree7e19673f611424f4b6c44ee5316aad94aab2371a /src/F2Md/Config.hs
parent44a5cd797d72003f580f18871d9e70c06afeb13b (diff)
break apart the code, timestamp read imp.
Diffstat (limited to 'src/F2Md/Config.hs')
-rw-r--r--src/F2Md/Config.hs41
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