saveNewThreads

This commit is contained in:
towards-a-new-leftypol 2025-02-10 06:30:27 -05:00
parent dc186081b8
commit 1f4b12c907
1 changed files with 35 additions and 9 deletions

View File

@ -1,45 +1,71 @@
module Lib2 module Lib2
( getCatalogJSON ( getCatalogJSON
, ProgramException (..) , ProgramException (..)
, saveNewThreads
) where ) where
import Control.Monad.Trans.Except (ExceptT (..)) import Control.Monad.Trans.Except (ExceptT (..))
import System.FilePath ((</>)) import System.FilePath ((</>))
import qualified Data.Set as Set
import qualified Network.DataClient as Client import qualified Network.DataClient as Client
import qualified SitesType as Sites import qualified SitesType as Sites
import qualified BoardsType as Boards import qualified BoardsType as Boards
import Common.Network.HttpClient (HttpError) import Common.Network.HttpClient (HttpError)
import JSONParsing (Catalog, Thread) import qualified JSONParsing as JSON
import qualified ThreadType as Threads import qualified ThreadType as Threads
import Common.Server.JSONSettings (JSONSettings) import Common.Server.JSONSettings (JSONSettings)
import qualified Lib import qualified Lib
data ProgramException = HttpException HttpError data ProgramException = HttpException HttpError
deriving Show deriving Show
liftHttpIO :: IO (Either HttpError a) -> ExceptT ProgramException IO a
liftHttpIO = ExceptT . fmap (either (Left . HttpException) Right)
-- over http -- over http
getCatalogJSON getCatalogJSON
:: Sites.Site :: Sites.Site
-> Boards.Board -> Boards.Board
-> ExceptT ProgramException IO [ Catalog ] -> ExceptT ProgramException IO [ JSON.Catalog ]
getCatalogJSON site board = getCatalogJSON site board = liftHttpIO req
ExceptT $ fmap (either (Left . HttpException) Right) req
where where
path = Boards.pathpart board </> "catalog.json" path = Boards.pathpart board </> "catalog.json"
req = Client.getJSON $ Sites.url site </> path req = Client.getJSON $ Sites.url site </> path
ensureThreads saveNewThreads
:: JSONSettings :: JSONSettings
-> Boards.Board -> Boards.Board
-> [ Thread ] -> [ JSON.Thread ]
-> ExceptT ProgramException IO [ Threads.Thread ] -> ExceptT ProgramException IO [ Threads.Thread ]
ensureThreads settings web_threads = do saveNewThreads settings board web_threads = do
db_threads <- Client.getThreads settings (Boards.board_id board) (map no web_threads) db_threads <- liftHttpIO $
Client.getThreads
settings
(Boards.board_id board)
(map JSON.no web_threads)
let
archived_board_thread_ids :: Set.Set Int
archived_board_thread_ids =
Set.fromList $ map Threads.board_thread_id db_threads
threads_to_create :: [ JSON.Thread ]
threads_to_create =
filter
((`Set.notMember` archived_board_thread_ids) . JSON.no)
web_threads
board_id :: Int = Boards.board_id board
-- save new threads -- save new threads
Client.postThreads new_threads <- liftHttpIO $ Client.postThreads
settings settings
(map (Lib.apiThreadToArchiveThread board_id) threads_to_create) (map (Lib.apiThreadToArchiveThread board_id) threads_to_create)
return $ db_threads ++ new_threads