From f008ab0a3211c0d5912fe0b99b936d8770fada17 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Mon, 30 May 2022 17:20:01 +1000 Subject: fixed a few things - redundant imports and deps - check file existence --- src/F2Md/Config.hs | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'src/F2Md/Config.hs') diff --git a/src/F2Md/Config.hs b/src/F2Md/Config.hs index c952d09..c17a55f 100644 --- a/src/F2Md/Config.hs +++ b/src/F2Md/Config.hs @@ -24,7 +24,6 @@ module F2Md.Config ( getUserdata , FeedUserdata(..) , updateLastUpdated - , updateLastUpdated' ) where import Control.Monad @@ -37,6 +36,7 @@ import Data.Time import GHC.Generics import qualified Data.HashMap.Strict as HM import Data.Aeson +import System.Posix.Files data Config = Config { dbPath :: String @@ -52,7 +52,7 @@ data FeedUserdata = FeedUserdata getUserdata :: FilePath -> IO ([FeedUserdata], Maybe FilePath, Maybe FilePath) getUserdata file = do - config <- decodeFileStrict file + config <- decodeFileStrict =<< expandPath file case config of Nothing -> return ([], Nothing, Nothing) Just config' -> do @@ -67,11 +67,14 @@ getUserdata' (Config dbPath _ feeds) = do return $ map (\url -> FeedUserdata url $ HM.lookup url decoded) feeds getLastUpdatedMap :: FilePath -> IO (HM.HashMap Text ZonedTime) -getLastUpdatedMap dbPath = - fromMaybe (HM.fromList []) <$> (decodeFileStrict =<< expandPath dbPath) - -updateLastUpdated' :: FilePath -> FeedUserdata -> [Message] -> IO () -updateLastUpdated' path feed msgs = +getLastUpdatedMap dbPath = do + exists <- fileExist =<< expandPath dbPath + if exists then + fromMaybe (HM.fromList []) <$> (decodeFileStrict =<< expandPath dbPath) + else return $ HM.fromList [] + +updateLastUpdated :: FilePath -> FeedUserdata -> [Message] -> IO () +updateLastUpdated path feed msgs = unless (null msgs) $ do decoded <- getLastUpdatedMap path encodeFile path $ @@ -80,8 +83,3 @@ updateLastUpdated' path feed msgs = (utcToZonedTime (zonedTimeZone $ mDate $ head msgs) . maximum $ zonedTimeToUTC . mDate <$> msgs) decoded - -updateLastUpdated :: FilePath -> [FeedUserdata] -> [[Message]] -> IO () -updateLastUpdated path feeds feedMsgs = do - mapM_ (uncurry (updateLastUpdated' path)) - (zip feeds feedMsgs) -- cgit v1.2.3