Browse Source

room: Fix updating category when room state changes

Now that the Room struct doesn't change, we need to match with the
category.
merge-requests/1461/head
Kévin Commaille 3 years ago
parent
commit
13300196c7
No known key found for this signature in database
GPG Key ID: 29A48C1F03620416
  1. 57
      src/session/model/room/mod.rs
  2. 15
      src/session/model/room/room_type.rs
  3. 6
      src/session/model/room_list.rs
  4. 30
      src/session/view/content/invite.rs

57
src/session/model/room/mod.rs

@ -309,37 +309,6 @@ impl Room {
fn set_matrix_room(&self, matrix_room: MatrixRoom) {
let imp = self.imp();
let new_state = matrix_room.state();
// Check if the previous type was different
if let Some(old_matrix_room) = imp.matrix_room.borrow().as_ref() {
let old_state = old_matrix_room.state();
if new_state == old_state {
return;
}
debug!("The matrix room struct for `Room` changed");
if old_state == RoomState::Invited && new_state == RoomState::Left {
// We rejected the invite or the invite was retracted, we should close the room
// if it is opened.
let session = self.session();
let selection = session.sidebar_list_model().selection_model();
if let Some(selected_room) = selection.selected_item().and_downcast::<Room>() {
if selected_room == *self {
selection.set_selected_item(None);
}
}
}
}
if new_state == RoomState::Joined {
// If we where invited or left before, the list was likely not completed or
// might have changed.
imp.members_loaded.set(false);
}
imp.matrix_room.replace(Some(matrix_room));
self.load_display_name();
@ -1357,9 +1326,29 @@ impl Room {
}
}
/// Reload the room from the SDK when it might have changed.
pub fn update_matrix_room(&self) {
self.set_matrix_room(self.session().client().get_room(self.room_id()).unwrap());
/// Reload the room from the SDK when its state might have changed.
pub fn update_room(&self) {
let state = self.matrix_room().state();
let category = self.category();
// Check if the previous state was different.
if category.is_state(state) {
// Nothing needs to be reloaded.
return;
}
debug!(room_id = %self.room_id(), ?state, "The state of `Room` changed");
if state == RoomState::Joined {
// If we where invited or left before, the list was likely not completed or
// might have changed.
self.imp().members_loaded.set(false);
}
self.load_category();
spawn!(clone!(@weak self as obj => async move {
obj.load_inviter().await;
}));
}
pub fn handle_left_response(&self, response_room: LeftRoom) {

15
src/session/model/room/room_type.rs

@ -1,6 +1,7 @@
use std::fmt;
use gtk::glib;
use matrix_sdk::RoomState;
use crate::session::model::CategoryType;
@ -65,6 +66,20 @@ impl RoomType {
}
}
}
/// Whether this `RoomType` corresponds to the given state.
pub fn is_state(&self, state: RoomState) -> bool {
match self {
RoomType::Invited => state == RoomState::Invited,
RoomType::Favorite
| RoomType::Normal
| RoomType::LowPriority
| RoomType::Outdated
| RoomType::Space
| RoomType::Direct => state == RoomState::Joined,
RoomType::Left => state == RoomState::Left,
}
}
}
impl fmt::Display for RoomType {

6
src/session/model/room_list.rs

@ -277,7 +277,7 @@ impl RoomList {
};
self.pending_rooms_remove((*room_id).into());
room.update_matrix_room();
room.update_room();
room.handle_left_response(left_room);
}
@ -291,7 +291,7 @@ impl RoomList {
};
self.pending_rooms_remove((*room_id).into());
room.update_matrix_room();
room.update_room();
room.handle_joined_response(joined_room);
}
@ -305,7 +305,7 @@ impl RoomList {
};
self.pending_rooms_remove((*room_id).into());
room.update_matrix_room();
room.update_room();
}
if !new_rooms.is_empty() {

30
src/session/view/content/invite.rs

@ -175,15 +175,29 @@ impl Invite {
let handler_id = room.connect_notify_local(
Some("category"),
clone!(@weak self as obj => move |room, _| {
if room.category() != RoomType::Invited {
let imp = obj.imp();
imp.reject_requests.borrow_mut().remove(room);
imp.accept_requests.borrow_mut().remove(room);
obj.reset();
if let Some(category_handler) = imp.category_handler.take() {
room.disconnect(category_handler);
}
let category = room.category();
if category == RoomType::Left {
// We rejected the invite or the invite was retracted, we should close the room
// if it is opened.
let session = room.session();
let selection = session.sidebar_list_model().selection_model();
if let Some(selected_room) = selection.selected_item().and_downcast::<Room>() {
if selected_room == *room {
selection.set_selected_item(None);
}
}
}
if category != RoomType::Invited {
let imp = obj.imp();
imp.reject_requests.borrow_mut().remove(room);
imp.accept_requests.borrow_mut().remove(room);
obj.reset();
if let Some(category_handler) = imp.category_handler.take() {
room.disconnect(category_handler);
}
}
}),
);
imp.category_handler.replace(Some(handler_id));

Loading…
Cancel
Save