summaryrefslogtreecommitdiff
path: root/fig-web/src/Fig/Web/Types.hs
blob: 176f2acfa7c6be47576c0cfc21f8c409ce8a0b45 (plain)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
module Fig.Web.Types
  ( LiveEvent(..), ShindigsSort(..)
  , Commands(..)
  , Channels(..)
  , newChannels
  , Globals(..)
  , newGlobals
  , ModuleArgs(..)
  , PublicOptions(..), SecureOptions(..)
  , PublicModuleArgs, SecureModuleArgs
  , PublicModule, SecureModule
  , PublicWebsockets, SecureWebsockets
  , PublicBusEvents, SecureBusEvents
  ) where

import Fig.Prelude

import qualified Control.Concurrent.Chan as Chan
import qualified Control.Concurrent.MVar as MVar

import Data.Word (Word32)
import qualified Data.Set as Set

import qualified Network.WebSockets as WS

import qualified Web.Scotty as Sc

import qualified Data.Aeson as Aeson

import Fig.Utils.DB
import Fig.Bus.Binary.Client
import Fig.Web.Utils

data LiveEvent
  = LiveEventOnline !(Set.Set Text)
  | LiveEventOffline !(Set.Set Text)
  deriving (Show, Eq, Ord)

data ShindigsSort = ShindigsSort
  { name :: Text
  , numbers :: [Word32]
  } deriving (Show, Eq, Ord, Generic)
instance Aeson.FromJSON ShindigsSort where
instance Aeson.ToJSON ShindigsSort where

data Channels = Channels
  { live :: !(Chan.Chan LiveEvent)
  , gizmo :: !(Chan.Chan Text)
  , model :: !(Chan.Chan WS.DataMessage)
  , shindigssort :: !(Chan.Chan ShindigsSort)
  }

newChannels :: IO Channels
newChannels = do
  live <- Chan.newChan
  gizmo <- Chan.newChan
  model <- Chan.newChan
  shindigssort <- Chan.newChan
  pure Channels {..}

newtype Globals = Globals
  { currentlyLive :: MVar.MVar (Set.Set Text)
  }

newGlobals :: IO Globals
newGlobals = do
  currentlyLive <- MVar.newMVar Set.empty
  pure Globals {..}

data ModuleArgs o = ModuleArgs
  { cfg :: Config
  , cmds :: Commands IO
  , db :: DB
  , globals :: Globals
  , channels :: Channels
  , options :: o
  }

data PublicOptions = PublicOptions
  {
  }

newtype SecureOptions = SecureOptions
  { simAuth :: Bool
  }

type PublicModuleArgs = ModuleArgs PublicOptions
type SecureModuleArgs = ModuleArgs SecureOptions 

type PublicModule = PublicModuleArgs -> Sc.ScottyM ()
type SecureModule = SecureModuleArgs -> Sc.ScottyM ()

type PublicWebsockets = PublicModuleArgs -> [WebsocketHandler]
type SecureWebsockets = SecureModuleArgs -> [WebsocketHandler]

type PublicBusEvents = PublicModuleArgs -> [BusEventHandler]
type SecureBusEvents = SecureModuleArgs -> [BusEventHandler]