summaryrefslogtreecommitdiff
path: root/fig-web/src/Fig/Web/Module/Circle.hs
diff options
context:
space:
mode:
Diffstat (limited to 'fig-web/src/Fig/Web/Module/Circle.hs')
-rw-r--r--fig-web/src/Fig/Web/Module/Circle.hs61
1 files changed, 61 insertions, 0 deletions
diff --git a/fig-web/src/Fig/Web/Module/Circle.hs b/fig-web/src/Fig/Web/Module/Circle.hs
new file mode 100644
index 0000000..5c21477
--- /dev/null
+++ b/fig-web/src/Fig/Web/Module/Circle.hs
@@ -0,0 +1,61 @@
+module Fig.Web.Module.Circle
+ ( public
+ , publicWebsockets
+ , publicBusEvents
+ ) where
+
+import Fig.Prelude
+
+import qualified Control.Concurrent.MVar as MVar
+import qualified Control.Concurrent.Chan as Chan
+
+import qualified Data.Text as Text
+import qualified Data.Set as Set
+
+import qualified Network.WebSockets as WS
+
+import Fig.Utils.SExpr
+import Fig.Web.Utils
+import Fig.Web.Types
+
+public :: Module
+public a = do
+ onGet "/api/circle" do
+ live <- liftIO $ MVar.readMVar a.globals.currentlyLive
+ respondText $ pretty . SExprList @Void $ SExprString <$> Set.toList live
+
+publicWebsockets :: Websockets
+publicWebsockets a =
+ [ ( "/api/circle/events", \conn -> do
+ c <- Chan.dupChan a.channels.live
+ forever do
+ ev <- liftIO $ Chan.readChan c
+ WS.sendTextData conn $ case ev of
+ LiveEventOnline online ->
+ pretty $ SExprList @Void
+ [ SExprString "online"
+ , SExprList $ SExprString <$> Set.toList online
+ ]
+ LiveEventOffline offline ->
+ pretty $ SExprList @Void
+ [ SExprString "offline"
+ , SExprList $ SExprString <$> Set.toList offline
+ ]
+ )
+ ]
+
+publicBusEvents :: BusEvents
+publicBusEvents a =
+ [ ("monitor twitch stream online", \d -> do
+ let dstr = decodeUtf8 d
+ let live = Text.splitOn " " dstr
+ let new = Set.fromList live
+ old <- MVar.swapMVar a.globals.currentlyLive new
+ let online = Set.difference new old
+ let offline = Set.difference old new
+ unless (Set.null online && Set.null offline) do
+ log $ "Newly online: " <> Text.intercalate " " (Set.toList online) <> ", newly offline: " <> Text.intercalate " " (Set.toList offline)
+ unless (Set.null online) . Chan.writeChan a.channels.live $ LiveEventOnline online
+ unless (Set.null offline) . Chan.writeChan a.channels.live $ LiveEventOnline offline
+ )
+ ]