base: Rename the method to remove room state.

master
Damir Jelić 2020-05-22 09:54:42 +02:00
parent 5c7ea17eef
commit 6e83f7ffa1
3 changed files with 16 additions and 13 deletions

View File

@ -318,13 +318,13 @@ impl BaseClient {
// hashmaps. // hashmaps.
if self.invited_rooms.write().await.remove(room_id).is_some() { if self.invited_rooms.write().await.remove(room_id).is_some() {
if let Some(store) = self.state_store.read().await.as_ref() { if let Some(store) = self.state_store.read().await.as_ref() {
store.room_state_change(RoomState::Invited(room_id)).await?; store.delete_room_state(RoomState::Invited(room_id)).await?;
} }
} }
if self.left_rooms.write().await.remove(room_id).is_some() { if self.left_rooms.write().await.remove(room_id).is_some() {
if let Some(store) = self.state_store.read().await.as_ref() { if let Some(store) = self.state_store.read().await.as_ref() {
store.room_state_change(RoomState::Left(room_id)).await?; store.delete_room_state(RoomState::Left(room_id)).await?;
} }
} }
@ -369,7 +369,7 @@ impl BaseClient {
// spec can't happen. // spec can't happen.
if self.left_rooms.write().await.remove(room_id).is_some() { if self.left_rooms.write().await.remove(room_id).is_some() {
if let Some(store) = self.state_store.read().await.as_ref() { if let Some(store) = self.state_store.read().await.as_ref() {
store.room_state_change(RoomState::Left(room_id)).await?; store.delete_room_state(RoomState::Left(room_id)).await?;
} }
} }
@ -414,13 +414,13 @@ impl BaseClient {
// hashmaps. // hashmaps.
if self.invited_rooms.write().await.remove(room_id).is_some() { if self.invited_rooms.write().await.remove(room_id).is_some() {
if let Some(store) = self.state_store.read().await.as_ref() { if let Some(store) = self.state_store.read().await.as_ref() {
store.room_state_change(RoomState::Invited(room_id)).await?; store.delete_room_state(RoomState::Invited(room_id)).await?;
} }
} }
if self.joined_rooms.write().await.remove(room_id).is_some() { if self.joined_rooms.write().await.remove(room_id).is_some() {
if let Some(store) = self.state_store.read().await.as_ref() { if let Some(store) = self.state_store.read().await.as_ref() {
store.room_state_change(RoomState::Joined(room_id)).await?; store.delete_room_state(RoomState::Joined(room_id)).await?;
} }
} }

View File

@ -168,8 +168,8 @@ impl StateStore for JsonStore {
file.write_all(json.as_bytes()).await.map_err(Error::from) file.write_all(json.as_bytes()).await.map_err(Error::from)
} }
async fn room_state_change(&self, previous_room: RoomState<&RoomId>) -> Result<()> { async fn delete_room_state(&self, room: RoomState<&RoomId>) -> Result<()> {
let (room_id, room_state) = match &previous_room { let (room_id, room_state) = match &room {
RoomState::Joined(id) => (id, "joined"), RoomState::Joined(id) => (id, "joined"),
RoomState::Invited(id) => (id, "invited"), RoomState::Invited(id) => (id, "invited"),
RoomState::Left(id) => (id, "left"), RoomState::Left(id) => (id, "left"),
@ -315,7 +315,7 @@ mod test {
.await .await
.unwrap(); .unwrap();
assert!(store assert!(store
.room_state_change(RoomState::Joined(&id)) .delete_room_state(RoomState::Joined(&id))
.await .await
.is_ok()); .is_ok());
let AllRooms { joined, .. } = store.load_all_rooms().await.unwrap(); let AllRooms { joined, .. } = store.load_all_rooms().await.unwrap();
@ -339,7 +339,7 @@ mod test {
.await .await
.unwrap(); .unwrap();
assert!(store assert!(store
.room_state_change(RoomState::Invited(&id)) .delete_room_state(RoomState::Invited(&id))
.await .await
.is_ok()); .is_ok());
let AllRooms { invited, .. } = store.load_all_rooms().await.unwrap(); let AllRooms { invited, .. } = store.load_all_rooms().await.unwrap();

View File

@ -91,19 +91,22 @@ pub trait StateStore: Send + Sync {
/// An `Option::None` should be returned only if the `StateStore` tries to /// An `Option::None` should be returned only if the `StateStore` tries to
/// load but no state has been stored. /// load but no state has been stored.
async fn load_client_state(&self, _: &Session) -> Result<Option<ClientState>>; async fn load_client_state(&self, _: &Session) -> Result<Option<ClientState>>;
/// Load the state of all `Room`s. /// Load the state of all `Room`s.
/// ///
/// This will be mapped over in the client in order to store `Room`s in an async safe way. /// This will be mapped over in the client in order to store `Room`s in an async safe way.
async fn load_all_rooms(&self) -> Result<AllRooms>; async fn load_all_rooms(&self) -> Result<AllRooms>;
/// Save the current state of the `BaseClient` using the `StateStore::Store` type. /// Save the current state of the `BaseClient` using the `StateStore::Store` type.
async fn store_client_state(&self, _: ClientState) -> Result<()>; async fn store_client_state(&self, _: ClientState) -> Result<()>;
/// Save the state a single `Room`. /// Save the state a single `Room`.
async fn store_room_state(&self, _: RoomState<&Room>) -> Result<()>; async fn store_room_state(&self, _: RoomState<&Room>) -> Result<()>;
/// Signals to the `StateStore` a room has changed state.
/// Remove state for a room.
/// ///
/// This enables implementing types to update the database when `RoomState` changes. /// This is used when a user leaves a room or rejects an invitation.
/// A `RoomState` change is when a user joins, is invited, or leaves a room. async fn delete_room_state(&self, _room: RoomState<&RoomId>) -> Result<()>;
async fn room_state_change(&self, _previous: RoomState<&RoomId>) -> Result<()>;
} }
#[cfg(test)] #[cfg(test)]