aboutsummaryrefslogtreecommitdiff
path: root/src/F2Md/Config.hs
blob: ccb187196aded9992188a556be0093bedc3fe270 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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