diff options
| author | LLLL Colonq <llll@colonq> | 2025-05-26 04:43:38 -0400 |
|---|---|---|
| committer | LLLL Colonq <llll@colonq> | 2025-05-26 04:45:07 -0400 |
| commit | 1f2e453d0c9f8412b9032cb4e655713ecdcf1fa3 (patch) | |
| tree | c2e19550aeec4c092dceefb37a85497a4b90b485 /fig-web/src/Fig/Web/Module/Circle.hs | |
| parent | b5003a97d3f02b7c8cb5e63468b781d8d849264d (diff) | |
web: Refactor major style
Diffstat (limited to 'fig-web/src/Fig/Web/Module/Circle.hs')
| -rw-r--r-- | fig-web/src/Fig/Web/Module/Circle.hs | 61 |
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 + ) + ] |
