jitsi-roomsv2/backend/src/BroadcastUserData.hs

51 lines
1.5 KiB
Haskell
Raw Normal View History

module BroadcastUserData
( broadcastUserData,
2023-02-19 11:41:32 +01:00
broadCastToClientsGeneric,
MonadBroadcast (..),
)
where
2023-04-08 15:57:33 +02:00
import ClassyPrelude
import Data.Aeson (encode)
import Network.WebSockets qualified as WS
2023-08-13 12:06:23 +02:00
import State.ConnectedClientsState (ConnectedClients, MonadConnectedClientsRead (getConnctedClients))
2023-04-08 15:57:33 +02:00
import State.RoomDataState (MonadRoomDataStateRead (getRoomDataState))
2023-08-13 12:06:23 +02:00
import Types.ConnectionState (Client (..))
2023-04-08 15:57:33 +02:00
import Types.User (User, clientToUser)
import Types.UsersData (UsersData (..))
2023-02-19 11:41:32 +01:00
class (Monad m, MonadConnectedClientsRead m) => MonadBroadcast m where
broadCastToClients :: Text -> m ()
broadcastUserData ::
2023-02-19 11:41:32 +01:00
( MonadRoomDataStateRead m,
MonadBroadcast m
) =>
m ()
broadcastUserData = do
userWithOutRoom <- getUsersWithoutRoom
2023-02-19 11:41:32 +01:00
roomsData <- getRoomDataState
2023-02-07 20:30:25 +01:00
let usersData = UsersData {usersWithOutRoom = userWithOutRoom, roomsData = roomsData}
broadCastToClients $ (decodeUtf8 . toStrict . encode) usersData
getUsersWithoutRoom ::
2023-02-19 11:41:32 +01:00
( MonadConnectedClientsRead m
) =>
2023-04-08 15:57:33 +02:00
m [User]
getUsersWithoutRoom = map clientToUser . filter (not . joinedRoom) <$> getConnctedClients
2023-02-07 20:30:25 +01:00
2023-02-19 11:41:32 +01:00
broadCastToClientsGeneric ::
2023-02-07 20:30:25 +01:00
( MonadIO m,
2023-02-19 11:41:32 +01:00
MonadConnectedClientsRead m
2023-02-07 20:30:25 +01:00
) =>
Text ->
m ()
2023-02-19 11:41:32 +01:00
broadCastToClientsGeneric message = do
2023-06-19 17:53:40 +02:00
connectedClients <- getConnctedClients
liftIO (broadcast message connectedClients)
2023-02-19 11:41:32 +01:00
broadcast :: Text -> ConnectedClients -> IO ()
broadcast message clients = do
2025-01-21 21:31:39 +01:00
putStrLn $ "Broadcasting: " ++ message
2023-02-19 11:41:32 +01:00
forM_ clients $ \client -> WS.sendTextData (conn client) message