diff options
author | Yuchen Pei <hi@ypei.me> | 2022-05-28 00:48:52 +1000 |
---|---|---|
committer | Yuchen Pei <hi@ypei.me> | 2022-05-28 00:48:52 +1000 |
commit | 20231d809b57c3050fb1cd812ea4c6e66cdaf6fb (patch) | |
tree | f395be8b27b4ff067ad1394fd39d01452433aef1 /src/F2Md/Config.hs | |
parent | caf354c7e5bcb5142d8b4358824d22d3de122f34 (diff) |
time and map updates
Diffstat (limited to 'src/F2Md/Config.hs')
-rw-r--r-- | src/F2Md/Config.hs | 54 |
1 files changed, 42 insertions, 12 deletions
diff --git a/src/F2Md/Config.hs b/src/F2Md/Config.hs index ccb1871..ef209dc 100644 --- a/src/F2Md/Config.hs +++ b/src/F2Md/Config.hs @@ -1,15 +1,20 @@ {-# LANGUAGE DeriveGeneric #-} -module F2Md.Config (getUserdata, FeedUserdata(..)) where +module F2Md.Config + ( getUserdata + , FeedUserdata(..) + , updateLastUpdated + ) where +import Data.List import F2Md.Utils +import F2Md.Types 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 @@ -20,22 +25,47 @@ data Config = Config instance FromJSON Config data FeedUserdata = FeedUserdata - { url :: Text - , lastUpdated :: Maybe ZonedTime } deriving (Generic, Show) + { fuUrl :: Text + , fuLastUpdated :: Maybe ZonedTime } deriving (Show) -getUserdata :: FilePath -> IO ([FeedUserdata], Maybe FilePath) +getUserdata :: FilePath -> IO ([FeedUserdata], Maybe FilePath, Maybe FilePath) getUserdata file = do - config <- decode <$> BSL.readFile file + config <- decodeFileStrict file case config of - Nothing -> return ([], Nothing) + Nothing -> return ([], Nothing, Nothing) Just config' -> do feedData <- getUserdata' config' maildir' <- expandPath $ maildir config' - return (feedData, Just maildir') + dbPath' <- expandPath $ dbPath config' + return (feedData, Just maildir', Just dbPath') 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 + decoded <- getLastUpdatedMap dbPath + 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 feeds feedMsgs = do + decoded <- getLastUpdatedMap path + encodeFile path $ foldl' + (\hm (feed, msgs) -> + case msgs of + [] -> hm + h : _ -> HM.insert (fuUrl feed) + (utcToZonedTime (zonedTimeZone $ mDate h) . maximum $ + (zonedTimeToUTC . mDate) <$> msgs) + hm) + decoded (zip feeds feedMsgs) + -- HM.fromList $ + -- mapMaybe + -- (\(feed, msgs) -> + -- case msgs of + -- [] -> fuLastUpdated feed >>= \date -> return (fuUrl feed, date) + -- h : _ -> + -- Just (fuUrl feed, + -- utcToZonedTime (zonedTimeZone $ mDate h) . maximum $ + -- (zonedTimeToUTC . mDate) <$> msgs)) $ zip feeds feedMsgs |