state_store: only remove room state file don't bother moving the file
parent
8489be590a
commit
5c7ea17eef
|
@ -318,17 +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
|
store.room_state_change(RoomState::Invited(room_id)).await?;
|
||||||
.room_state_change(RoomState::Joined(room_id), 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
|
store.room_state_change(RoomState::Left(room_id)).await?;
|
||||||
.room_state_change(RoomState::Invited(room_id), RoomState::Left(room_id))
|
|
||||||
.await?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -373,9 +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
|
store.room_state_change(RoomState::Left(room_id)).await?;
|
||||||
.room_state_change(RoomState::Invited(room_id), RoomState::Left(room_id))
|
|
||||||
.await?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -420,17 +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
|
store.room_state_change(RoomState::Invited(room_id)).await?;
|
||||||
.room_state_change(RoomState::Left(room_id), 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
|
store.room_state_change(RoomState::Joined(room_id)).await?;
|
||||||
.room_state_change(RoomState::Left(room_id), RoomState::Joined(room_id))
|
|
||||||
.await?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -168,11 +168,7 @@ 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(
|
async fn room_state_change(&self, previous_room: RoomState<&RoomId>) -> Result<()> {
|
||||||
&self,
|
|
||||||
current_room: RoomState<&RoomId>,
|
|
||||||
previous_room: RoomState<&RoomId>,
|
|
||||||
) -> Result<()> {
|
|
||||||
let (room_id, room_state) = match &previous_room {
|
let (room_id, room_state) = match &previous_room {
|
||||||
RoomState::Joined(id) => (id, "joined"),
|
RoomState::Joined(id) => (id, "joined"),
|
||||||
RoomState::Invited(id) => (id, "invited"),
|
RoomState::Invited(id) => (id, "invited"),
|
||||||
|
@ -183,65 +179,15 @@ impl StateStore for JsonStore {
|
||||||
return Err(Error::StateStore("path for JsonStore not set".into()));
|
return Err(Error::StateStore("path for JsonStore not set".into()));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut from = self.path.read().await.clone();
|
let mut to_del = self.path.read().await.clone();
|
||||||
from.push("rooms");
|
to_del.push("rooms");
|
||||||
from.push(&format!("{}/{}.json", room_state, room_id));
|
to_del.push(&format!("{}/{}.json", room_state, room_id));
|
||||||
|
|
||||||
if !from.exists() {
|
if !to_del.exists() {
|
||||||
return Err(Error::StateStore(format!(
|
return Err(Error::StateStore(format!("file {:?} not found", to_del)));
|
||||||
"file {:?} not found for JsonStore",
|
|
||||||
from
|
|
||||||
)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
match current_room {
|
tokio::fs::remove_file(to_del).await.map_err(Error::from)
|
||||||
RoomState::Joined(room_id) => {
|
|
||||||
let mut to = self.path.read().await.clone();
|
|
||||||
to.push("rooms");
|
|
||||||
to.push("joined");
|
|
||||||
if !to.exists() {
|
|
||||||
async_fs::create_dir_all(&to).await?;
|
|
||||||
}
|
|
||||||
|
|
||||||
to.push(&format!("{}.json", room_id));
|
|
||||||
if !to.exists() {
|
|
||||||
async_fs::File::create(&to).await?;
|
|
||||||
}
|
|
||||||
// now that we know the `to` file exists move `from` to new correct state folder
|
|
||||||
tokio::fs::rename(from, to).await?;
|
|
||||||
}
|
|
||||||
RoomState::Invited(room_id) => {
|
|
||||||
let mut to = self.path.read().await.clone();
|
|
||||||
to.push("rooms");
|
|
||||||
to.push("invited");
|
|
||||||
if !to.exists() {
|
|
||||||
async_fs::create_dir_all(&to).await?;
|
|
||||||
}
|
|
||||||
|
|
||||||
to.push(&format!("{}.json", room_id));
|
|
||||||
if !to.exists() {
|
|
||||||
async_fs::File::create(&to).await?;
|
|
||||||
}
|
|
||||||
// now that we know the `to` file exists move `from` to new correct state folder
|
|
||||||
tokio::fs::rename(from, to).await?;
|
|
||||||
}
|
|
||||||
RoomState::Left(room_id) => {
|
|
||||||
let mut to = self.path.read().await.clone();
|
|
||||||
to.push("rooms");
|
|
||||||
to.push("left");
|
|
||||||
if !to.exists() {
|
|
||||||
async_fs::create_dir_all(&to).await?;
|
|
||||||
}
|
|
||||||
|
|
||||||
to.push(&format!("{}.json", room_id));
|
|
||||||
if !to.exists() {
|
|
||||||
async_fs::File::create(&to).await?;
|
|
||||||
}
|
|
||||||
// now that we know the `to` file exists move `from` to new correct state folder
|
|
||||||
tokio::fs::rename(from, to).await?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -369,11 +315,12 @@ mod test {
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert!(store
|
assert!(store
|
||||||
.room_state_change(RoomState::Left(&id), RoomState::Joined(&id))
|
.room_state_change(RoomState::Joined(&id))
|
||||||
.await
|
.await
|
||||||
.is_ok());
|
.is_ok());
|
||||||
let AllRooms { joined, left, .. } = store.load_all_rooms().await.unwrap();
|
let AllRooms { joined, .. } = store.load_all_rooms().await.unwrap();
|
||||||
assert_eq!(left.get(&id), Some(&Room::new(&id, &user)));
|
|
||||||
|
// test that we have removed the correct room
|
||||||
assert!(joined.is_empty());
|
assert!(joined.is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -392,13 +339,11 @@ mod test {
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert!(store
|
assert!(store
|
||||||
.room_state_change(RoomState::Joined(&id), RoomState::Invited(&id))
|
.room_state_change(RoomState::Invited(&id))
|
||||||
.await
|
.await
|
||||||
.is_ok());
|
.is_ok());
|
||||||
let AllRooms {
|
let AllRooms { invited, .. } = store.load_all_rooms().await.unwrap();
|
||||||
invited, joined, ..
|
// test that we have removed the correct room
|
||||||
} = store.load_all_rooms().await.unwrap();
|
|
||||||
assert_eq!(joined.get(&id), Some(&Room::new(&id, &user)));
|
|
||||||
assert!(invited.is_empty());
|
assert!(invited.is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -103,11 +103,7 @@ pub trait StateStore: Send + Sync {
|
||||||
///
|
///
|
||||||
/// This enables implementing types to update the database when `RoomState` changes.
|
/// This enables implementing types to update the database when `RoomState` changes.
|
||||||
/// A `RoomState` change is when a user joins, is invited, or leaves a room.
|
/// A `RoomState` change is when a user joins, is invited, or leaves a room.
|
||||||
async fn room_state_change(
|
async fn room_state_change(&self, _previous: RoomState<&RoomId>) -> Result<()>;
|
||||||
&self,
|
|
||||||
_current: RoomState<&RoomId>,
|
|
||||||
_previous: RoomState<&RoomId>,
|
|
||||||
) -> Result<()>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
Loading…
Reference in New Issue