Commit 5e4c9123 authored by Julian Sparber's avatar Julian Sparber

room: refactor set_active_room_by_id()

This removes clones of Rooms, and removes some old code.
parent 4641fa46
......@@ -14,7 +14,6 @@ use globals;
use widgets;
use types::Member;
use types::Room;
impl AppOp {
pub fn add_to_invite(&mut self, u: Member) {
......@@ -229,17 +228,19 @@ impl AppOp {
self.invitation_roomid = None;
}
pub fn show_inv_dialog(&mut self, r: &Room) {
/* FIXME: move to a widget */
pub fn show_inv_dialog(&self, sender: Option<&Member>, room_name: Option<&String>) {
let dialog = self
.ui
.builder
.get_object::<gtk::MessageDialog>("invite_dialog")
.expect("Can't find invite_dialog in ui file.");
let room_name = r.name.clone().unwrap_or_default();
let empty = String::from("");
let room_name = room_name.unwrap_or(&empty);
let title = i18n_k("Join {room_name}?", &[("room_name", &room_name)]);
let secondary;
if let Some(ref sender) = r.inv_sender {
if let Some(ref sender) = sender {
let sender_name = sender.get_alias();
secondary = i18n_k(
"You’ve been invited to join to <b>{room_name}</b> room by <b>{sender_name}</b>",
......@@ -256,7 +257,6 @@ impl AppOp {
dialog.set_property_secondary_use_markup(true);
dialog.set_property_secondary_text(Some(&secondary));
self.invitation_roomid = Some(r.id.clone());
dialog.present();
}
......
......@@ -37,29 +37,13 @@ impl AppOp {
/// This function is used to mark as read the last message of a room when the focus comes in,
/// so we need to force the mark_as_read because the window isn't active yet
pub fn mark_active_room_messages(&mut self) {
let mut msg: Option<Message> = None;
if let Some(ref active_room_id) = self.active_room {
if let Some(ref r) = self.rooms.get(active_room_id) {
if let Some(m) = r.messages.last() {
msg = Some(m.clone());
}
}
}
// this is done here because in the above we've a reference to self and mark as read needs
// a mutable reference to self so we can't do it inside
if let Some(m) = msg {
self.mark_as_read(&m, Force(true));
}
self.mark_last_message_as_read(Force(true));
}
pub fn add_room_message(&mut self, msg: &Message) -> Option<()> {
if &msg.room == self.active_room.as_ref()? && !msg.redacted {
if let Some(ui_msg) = self.create_new_room_message(msg) {
if let Some(ref mut history) = self.history {
history.add_new_message(ui_msg);
}
if let Some(ui_msg) = self.create_new_room_message(msg) {
if let Some(ref mut history) = self.history {
history.add_new_message(ui_msg);
}
}
None
......@@ -119,7 +103,7 @@ impl AppOp {
None
}
pub fn mark_as_read(&mut self, msg: &Message, Force(force): Force) -> Option<()> {
pub fn mark_last_message_as_read(&mut self, Force(force): Force) -> Option<()> {
let window: gtk::Window = self
.ui
.builder
......@@ -140,8 +124,8 @@ impl AppOp {
self.backend
.send(BKCommand::MarkAsRead(
msg.room.clone(),
msg.id.clone().unwrap_or_default(),
last_message.room.clone(),
last_message.id.clone()?,
))
.unwrap();
}
......@@ -397,6 +381,7 @@ impl AppOp {
}
}
let mut msg_in_active = false;
let uid = self.uid.clone()?;
for msg in msgs.iter() {
let should_notify = msg.sender != uid
......@@ -407,16 +392,16 @@ impl AppOp {
self.notify(msg);
}
self.add_room_message(&msg);
if &msg.room == self.active_room.as_ref()? && !msg.redacted {
self.add_room_message(&msg);
msg_in_active = true;
}
self.roomlist.moveup(msg.room.clone());
self.roomlist.set_bold(msg.room.clone(), true);
}
if !msgs.is_empty() {
let active_room = self.active_room.clone()?;
let fs = msgs.iter().filter(|x| x.room == active_room);
let msg = fs.last()?;
self.mark_as_read(msg, Force(false));
if msg_in_active {
self.mark_last_message_as_read(Force(false));
}
None
......
......@@ -133,23 +133,14 @@ impl AppOp {
self.set_state(AppState::Chat);
}
pub fn set_active_room_by_id(&mut self, roomid: String) {
let mut room = None;
if let Some(r) = self.rooms.get(&roomid) {
room = Some(r.clone());
}
if let Some(r) = room {
if r.inv {
self.show_inv_dialog(&r);
pub fn set_active_room_by_id(&mut self, id: String) {
if let Some(room) = self.rooms.get(&id) {
if room.inv {
self.show_inv_dialog(room.inv_sender.as_ref(), room.name.as_ref());
self.invitation_roomid = Some(room.id.clone());
return;
}
self.set_active_room(&r);
}
}
pub fn set_active_room(&mut self, room: &Room) {
self.room_panel(RoomPanel::Room);
let msg_entry = self.ui.sventry.view.clone();
......@@ -158,35 +149,55 @@ impl AppOp {
let end = buffer.get_end_iter();
if let Some(msg) = buffer.get_text(&start, &end, false) {
let active_room_id = self.active_room.clone().unwrap_or_default();
if msg.len() > 0 {
if let Some(mark) = buffer.get_insert() {
let iter = buffer.get_iter_at_mark(&mark);
let msg_position = iter.get_offset();
self.unsent_messages
.insert(active_room_id, (msg, msg_position));
if let Some(ref active_room) = self.active_room {
if msg.len() > 0 {
if let Some(mark) = buffer.get_insert() {
let iter = buffer.get_iter_at_mark(&mark);
let msg_position = iter.get_offset();
self.unsent_messages
.insert(active_room.clone(), (msg, msg_position));
}
} else {
self.unsent_messages.remove(active_room);
}
} else {
self.unsent_messages.remove(&active_room_id);
}
}
}
self.active_room = Some(room.id.clone());
self.clear_tmp_msgs();
/* Transform id into the active_room */
let active_room = id;
// getting room details
self.backend
.send(BKCommand::SetRoom(active_room.clone()))
.unwrap();
/* create the intitial list of messages to fill the new room history */
let active_room = self.active_room.clone().unwrap_or_default();
let mut messages = vec![];
for msg in room.messages.iter() {
/* Make sure the message is from this room and not redacted */
if msg.room == active_room && !msg.redacted {
let row = self.create_new_room_message(msg);
if let Some(row) = row {
messages.push(row);
if let Some(room) = self.rooms.get(&active_room) {
for msg in room.messages.iter() {
/* Make sure the message is from this room and not redacted */
if msg.room == active_room && !msg.redacted {
let row = self.create_new_room_message(msg);
if let Some(row) = row {
messages.push(row);
}
}
}
let l = room.messages.len();
if l > 0 && l < globals::INITIAL_MESSAGES {
self.internal.send(InternalCommand::LoadMore).unwrap();
}
self.internal
.send(InternalCommand::AppendTmpMessages)
.unwrap();
self.set_current_room_detail(String::from("m.room.name"), room.name.clone());
self.set_current_room_detail(String::from("m.room.topic"), room.topic.clone());
}
/* make sure we remove the old room history first, because the lazy loading could try to
......@@ -200,42 +211,9 @@ impl AppOp {
history.create(messages);
self.history = Some(history);
let l = room.messages.len();
if l > 0 && l < globals::INITIAL_MESSAGES {
self.internal.send(InternalCommand::LoadMore).unwrap();
}
self.internal
.send(InternalCommand::AppendTmpMessages)
.unwrap();
if let Some(msg) = room.messages.iter().last() {
self.mark_as_read(msg, Force(false));
}
// getting room details
self.backend.send(BKCommand::SetRoom(room.clone())).unwrap();
self.set_room_topic_label(room.topic.clone());
let name_label = self
.ui
.builder
.get_object::<gtk::Label>("room_name")
.expect("Can't find room_name in ui file.");
name_label.set_text(&room.name.clone().unwrap_or_default());
let mut size = 24;
if let Some(r) = room.topic.clone() {
if !r.is_empty() {
size = 16;
}
}
self.set_current_room_avatar(room.avatar.clone(), size);
self.set_current_room_detail(String::from("m.room.name"), room.name.clone());
self.set_current_room_detail(String::from("m.room.topic"), room.topic.clone());
self.active_room = Some(active_room);
/* Mark the new active room as read */
self.mark_last_message_as_read(Force(false));
}
pub fn really_leave_active_room(&mut self) {
......@@ -394,16 +372,6 @@ impl AppOp {
self.roomlist
.set_room_avatar(roomid.clone(), r.avatar.clone());
}
if roomid == self.active_room.clone().unwrap_or_default() {
let mut size = 24;
if let Some(r) = self.rooms.get_mut(&roomid) {
if !r.clone().topic.unwrap_or_default().is_empty() {
size = 16;
}
}
self.set_current_room_avatar(avatar, size);
}
}
pub fn set_current_room_detail(&self, key: String, value: Option<String>) {
......@@ -426,8 +394,6 @@ impl AppOp {
};
}
pub fn set_current_room_avatar(&self, _avatar: Option<String>, _size: i32) {}
pub fn filter_rooms(&self, term: Option<String>) {
self.roomlist.filter_rooms(term);
}
......
......@@ -209,8 +209,8 @@ impl Backend {
let r = room::redact_msg(self, msg);
bkerror!(r, tx, BKResponse::SendMsgRedactionError);
}
Ok(BKCommand::SetRoom(room)) => {
let r = room::set_room(self, room);
Ok(BKCommand::SetRoom(id)) => {
let r = room::set_room(self, id);
bkerror!(r, tx, BKResponse::SetRoomError);
}
Ok(BKCommand::GetRoomAvatar(room)) => {
......
......@@ -28,10 +28,11 @@ use types::Room;
use serde_json::Value as JsonValue;
pub fn set_room(bk: &Backend, room: Room) -> Result<(), Error> {
get_room_detail(bk, room.id.clone(), String::from("m.room.topic"))?;
get_room_avatar(bk, room.id.clone())?;
get_room_members(bk, room.id.clone())?;
pub fn set_room(bk: &Backend, id: String) -> Result<(), Error> {
/* FIXME: remove clone and pass id by reference */
get_room_detail(bk, id.clone(), String::from("m.room.topic"))?;
get_room_avatar(bk, id.clone())?;
get_room_members(bk, id)?;
Ok(())
}
......
......@@ -60,7 +60,7 @@ pub enum BKCommand {
GetUserNameAsync(String, Sender<String>),
SendMsg(Message),
SendMsgRedaction(Message),
SetRoom(Room),
SetRoom(String),
ShutDown,
DirectoryProtocols,
DirectorySearch(String, String, String, bool),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment