{-# LANGUAGE UndecidableInstances #-}
module IHP.DataSync.Controller where

import IHP.ControllerPrelude hiding (OrderByClause)
import qualified Control.Exception as Exception
import qualified IHP.Log as Log
import qualified Data.Aeson as Aeson
import qualified Data.Aeson.Encoding.Internal as Aeson

import Data.Aeson.TH
import Data.Aeson
import qualified Database.PostgreSQL.Simple as PG
import qualified Database.PostgreSQL.Simple.ToField as PG
import qualified Database.PostgreSQL.Simple.Types as PG
import qualified Data.HashMap.Strict as HashMap
import qualified Data.UUID.V4 as UUID
import qualified Control.Concurrent.MVar as MVar
import IHP.DataSync.Types
import IHP.DataSync.RowLevelSecurity
import IHP.DataSync.DynamicQuery
import IHP.DataSync.DynamicQueryCompiler
import qualified IHP.DataSync.ChangeNotifications as ChangeNotifications
import IHP.DataSync.REST.Controller (aesonValueToPostgresValue)
import qualified Data.ByteString.Char8 as ByteString
import qualified Data.ByteString.Builder as ByteString
import qualified IHP.PGListener as PGListener
import IHP.ApplicationContext
import Data.Set (Set)
import qualified Data.Set as Set
import qualified Data.Pool as Pool

import qualified IHP.GraphQL.Types as GraphQL
import qualified IHP.GraphQL.Parser as GraphQL
import qualified IHP.GraphQL.Compiler as GraphQL
import IHP.GraphQL.JSON ()
import qualified Data.Attoparsec.Text as Attoparsec
import qualified Network.WebSockets as WS
import IHP.DataSync.ControllerImpl (runDataSyncController, cleanupAllSubscriptions)

instance (
    PG.ToField (PrimaryKey (GetTableName CurrentUserRecord))
    , Show (PrimaryKey (GetTableName CurrentUserRecord))
    , HasNewSessionUrl CurrentUserRecord
    , Typeable CurrentUserRecord
    , HasField "id" CurrentUserRecord (Id' (GetTableName CurrentUserRecord))
    ) => WSApp DataSyncController where
    initialState :: DataSyncController
initialState = DataSyncController
DataSyncController

    run :: IO ()
run = do
        Text -> IO TableWithRLS
ensureRLSEnabled <- IO (Text -> IO TableWithRLS)
(?modelContext::ModelContext) => IO (Text -> IO TableWithRLS)
makeCachedEnsureRLSEnabled
        TableWithRLS -> IO ()
installTableChangeTriggers <- IO (TableWithRLS -> IO ())
(?modelContext::ModelContext) => IO (TableWithRLS -> IO ())
ChangeNotifications.makeCachedInstallTableChangeTriggers
        (Text -> IO TableWithRLS)
-> (TableWithRLS -> IO ())
-> IO ByteString
-> (DataSyncResponse -> IO ())
-> ((DataSyncResponse -> IO ()) -> DataSyncMessage -> IO ())
-> IO ()
forall a.
(HasField
   "id" CurrentUserRecord (Id' (GetTableName CurrentUserRecord)),
 ?applicationContext::ApplicationContext, ?connection::Connection,
 ?context::ControllerContext, ?modelContext::ModelContext,
 ?state::IORef DataSyncController,
 ToField (PrimaryKey (GetTableName CurrentUserRecord)),
 Typeable CurrentUserRecord, HasNewSessionUrl CurrentUserRecord,
 Show (PrimaryKey (GetTableName CurrentUserRecord))) =>
(Text -> IO TableWithRLS)
-> (TableWithRLS -> IO a)
-> IO ByteString
-> (DataSyncResponse -> IO ())
-> ((DataSyncResponse -> IO ()) -> DataSyncMessage -> IO ())
-> IO ()
runDataSyncController Text -> IO TableWithRLS
ensureRLSEnabled TableWithRLS -> IO ()
installTableChangeTriggers ((?connection::Connection, WebSocketsData ByteString) =>
IO ByteString
forall a. (?connection::Connection, WebSocketsData a) => IO a
receiveData @ByteString) DataSyncResponse -> IO ()
forall value.
(?connection::Connection, ToJSON value) =>
value -> IO ()
sendJSON (\DataSyncResponse -> IO ()
_ DataSyncMessage
_ -> () -> IO ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ())
    onClose :: IO ()
onClose = IO ()
(?state::IORef DataSyncController,
 ?applicationContext::ApplicationContext) =>
IO ()
cleanupAllSubscriptions