jitsi-roomsv2/backend/src/State/RoomsState.hs

74 lines
1.8 KiB
Haskell
Raw Normal View History

2023-08-13 12:06:23 +02:00
module State.RoomsState
2023-01-27 19:53:34 +01:00
( RoomsState,
initRoomsState,
HasRoomsState (..),
roomStateDiffers,
2025-01-19 13:30:56 +01:00
roomStateDiffInOpenRooms,
2023-01-27 19:53:34 +01:00
updateRoomState,
2023-02-19 11:41:32 +01:00
getRoomState,
2023-01-27 19:53:34 +01:00
)
where
import ClassyPrelude
2023-08-13 12:06:23 +02:00
import State.GenericTVarState
2023-02-19 11:41:32 +01:00
import State.RoomDataState (MonadRoomDataStateRead (getRoomDataState))
2025-01-19 13:30:56 +01:00
import Types.RoomData (RoomsData, roomNotEmpty, sameName)
2023-01-27 19:53:34 +01:00
2023-08-13 12:06:23 +02:00
type RoomsState = GenericTVarState RoomsData
2023-01-27 19:53:34 +01:00
initRoomsState :: IO RoomsState
2023-06-19 17:53:40 +02:00
initRoomsState = newTVarIO []
2023-01-27 19:53:34 +01:00
class HasRoomsState a where
getRoomsState :: a -> RoomsState
updateRoomState ::
( HasRoomsState env,
MonadIO m,
MonadReader env m
) =>
RoomsData ->
m ()
updateRoomState newData = do
state <- getRoomsState <$> ask
2023-06-19 19:09:20 +02:00
liftIO $ putStrLn "Upating room state"
2023-08-13 12:06:23 +02:00
updateGenericTVarState state newData
2023-06-19 19:09:20 +02:00
liftIO $ putStrLn "Done Upating room state"
2023-01-27 19:53:34 +01:00
2023-02-19 11:41:32 +01:00
getRoomState ::
2023-01-27 19:53:34 +01:00
( HasRoomsState env,
MonadIO m,
MonadReader env m
) =>
2023-02-19 11:41:32 +01:00
m RoomsData
getRoomState = do
state <- getRoomsState <$> ask
2023-08-13 12:06:23 +02:00
getGenericTVarState state
2023-02-19 11:41:32 +01:00
roomStateDiffers ::
( MonadRoomDataStateRead m
) =>
2023-01-27 19:53:34 +01:00
RoomsData ->
m Bool
roomStateDiffers newData = do
2023-02-19 11:41:32 +01:00
not . eqIgnoreOrdering newData <$> getRoomDataState
2023-01-27 19:53:34 +01:00
2025-01-19 13:30:56 +01:00
roomStateDiffInOpenRooms ::
2025-01-21 21:31:39 +01:00
( MonadRoomDataStateRead m,
MonadIO m
2025-01-19 13:30:56 +01:00
) =>
RoomsData ->
m (RoomsData, RoomsData)
roomStateDiffInOpenRooms newData = do
current <- getRoomDataState
2025-01-21 21:31:39 +01:00
liftIO $ putStrLn $ pack $ "Current rooms: " ++ show current
liftIO $ putStrLn $ pack $ "New rooms: " ++ show newData
2025-01-21 21:36:39 +01:00
let newRooms = filter roomNotEmpty $ filter (\newRoom -> isNothing $ find (sameName newRoom) (filter roomNotEmpty current)) newData
2025-01-21 21:21:21 +01:00
let oldRooms = filter roomNotEmpty $ filter (\oldRoom -> isNothing $ find (sameName oldRoom) newData) current
2025-01-19 13:30:56 +01:00
return (newRooms, oldRooms)
2023-01-27 19:53:34 +01:00
eqIgnoreOrdering :: (Eq a) => [a] -> [a] -> Bool
eqIgnoreOrdering a b = length a == length b && all (`elem` b) a