jitsi-roomsv2/backend/src/RoomDataHandler.hs

131 lines
3.4 KiB
Haskell
Raw Normal View History

2023-01-29 12:08:50 +01:00
{-# LANGUAGE FlexibleContexts #-}
module RoomDataHandler
( roomDataHandler,
)
where
2023-02-19 11:41:32 +01:00
import BroadcastUserData (MonadBroadcast, broadcastUserData)
import ClassyPrelude
2023-01-29 12:08:50 +01:00
import Control.Monad.Except (MonadError, throwError)
import Data.Aeson (eitherDecodeStrict)
import Data.Aeson.Types (FromJSON)
2025-01-19 13:30:56 +01:00
import GHC.IO.Exception (ExitCode (ExitSuccess))
import Network.HTTP.Types (status200, status500)
import Network.Wai
( ResponseReceived,
consumeRequestBodyStrict,
responseLBS,
)
2023-02-19 11:41:32 +01:00
import State.RoomDataState
( MonadRoomDataStateModify (setRoomDataState),
MonadRoomDataStateRead,
)
2023-08-13 12:06:23 +02:00
import State.RoomsState
2025-01-19 13:30:56 +01:00
( roomStateDiffInOpenRooms,
roomStateDiffers,
)
2025-01-19 13:30:56 +01:00
import System.Process
import Text.Printf (printf)
import Types.RoomData (RoomData, prettyPrintOpenedRoom)
2023-02-19 11:41:32 +01:00
import Types.WebEnv
( HasWebEnv (getRequest),
getRespond,
)
roomDataHandler ::
( MonadIO m,
HasWebEnv env,
MonadReader env m,
2023-01-29 12:08:50 +01:00
MonadError ResponseReceived m,
2023-02-19 11:41:32 +01:00
MonadRoomDataStateRead m,
MonadRoomDataStateModify m,
MonadBroadcast m
) =>
m ResponseReceived
roomDataHandler = do
2023-01-29 12:08:50 +01:00
newRoomData <- parseBodyOrBadRequest
2023-06-19 19:04:53 +02:00
liftIO $ putStrLn "Got triggered from prosody"
2023-01-29 12:08:50 +01:00
whenM (roomStateDiffers newRoomData) $ do
2025-01-19 13:30:56 +01:00
(openedRooms, closedRooms) <- roomStateDiffInOpenRooms newRoomData
mapM_ notifyRoomOpend openedRooms
mapM_ notifyRoomClosed closedRooms
2023-02-19 11:41:32 +01:00
setRoomDataState newRoomData
2023-01-29 12:08:50 +01:00
broadcastUserData
success
parseBodyOrBadRequest ::
( MonadIO m,
HasWebEnv env,
MonadReader env m,
MonadError ResponseReceived m,
FromJSON a
) =>
m a
parseBodyOrBadRequest = do
2025-01-19 13:30:56 +01:00
liftIO $ putStrLn "Parsing body"
body <- getRequestBody
case eitherDecodeStrict body of
2023-01-29 12:08:50 +01:00
Left errorMessage -> do
response <- badRequest errorMessage
throwError response
Right newState -> return newState
getRequestBody ::
( MonadIO m,
HasWebEnv env,
MonadReader env m
) =>
m ByteString
getRequestBody = do
request <- getRequest <$> ask
liftIO $ toStrict <$> consumeRequestBodyStrict request
badRequest ::
( MonadIO m,
HasWebEnv env,
MonadReader env m
) =>
String ->
m ResponseReceived
badRequest errorMessage = do
respond' <- getRespond <$> ask
2023-02-07 20:30:25 +01:00
liftIO $ print errorMessage
liftIO $
respond' $
responseLBS
status500
[("Content-Type", "text/plain")]
2023-01-29 12:08:50 +01:00
(fromString ("Bad request. Reason: " <> errorMessage))
success ::
( MonadIO m,
HasWebEnv env,
MonadReader env m
) =>
m ResponseReceived
success = do
respond' <- getRespond <$> ask
liftIO $
respond' $
responseLBS
status200
[("Content-Type", "text/plain")]
""
2025-01-19 13:30:56 +01:00
notifyRoomOpend :: (MonadIO m) => RoomData -> m ()
notifyRoomOpend room = do
let (name, user) = prettyPrintOpenedRoom room
liftIO $ printf "Room %s opened by %s\n" name user
exitCode <- liftIO $ system $ printf "/nix/store/23nz5mjk3dj7027byc6g5avx3mfwwnqm-libnotify-0.8.3/bin/notify-send 'Room %s opened by %s'" name user
when (exitCode /= ExitSuccess) $ liftIO $ printf "Failed to notify room %s opened by %s\n" name user
notifyRoomClosed :: (MonadIO m) => RoomData -> m ()
notifyRoomClosed room = do
let (name, _) = prettyPrintOpenedRoom room
liftIO $ printf "Room %s closed\n" name
exitCode <- liftIO $ system $ printf "/nix/store/23nz5mjk3dj7027byc6g5avx3mfwwnqm-libnotify-0.8.3/bin/notify-send 'Room %s closed'" name
when (exitCode /= ExitSuccess) $ liftIO $ printf "Failed to notify room %s closed\n" name