From 2001bdc66a18d20b78995503d1f03a900bd89f66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Moreno?= Date: Thu, 11 Jan 2018 14:18:58 +0100 Subject: [PATCH] Add to fav backend feature See #34 --- fractal-api/src/backend/mod.rs | 4 +++ fractal-api/src/backend/room.rs | 18 ++++++++++ fractal-api/src/backend/types.rs | 3 ++ fractal-gtk/src/app.rs | 13 +++++++ fractal-gtk/src/widgets/roomlist.rs | 53 ++++++++++++++--------------- 5 files changed, 64 insertions(+), 27 deletions(-) diff --git a/fractal-api/src/backend/mod.rs b/fractal-api/src/backend/mod.rs index d67267f4..09eee954 100644 --- a/fractal-api/src/backend/mod.rs +++ b/fractal-api/src/backend/mod.rs @@ -190,6 +190,10 @@ impl Backend { let r = room::new_room(self, name, privacy); bkerror!(r, tx, BKResponse::NewRoomError); } + Ok(BKCommand::AddToFav(roomid, tofav)) => { + let r = room::add_to_fav(self, roomid, tofav); + bkerror!(r, tx, BKResponse::AddToFavError); + } Ok(BKCommand::Search(roomid, term)) => { let r = room::search(self, roomid, term); bkerror!(r, tx, BKResponse::SearchError); diff --git a/fractal-api/src/backend/room.rs b/fractal-api/src/backend/room.rs index 0c42b35d..11c7cc3b 100644 --- a/fractal-api/src/backend/room.rs +++ b/fractal-api/src/backend/room.rs @@ -481,3 +481,21 @@ pub fn make_search(bk: &Backend, roomid: String, term: String) -> Result<(), Err Ok(()) } + +pub fn add_to_fav(bk: &Backend, roomid: String, tofav: bool) -> Result<(), Error> { + let userid = bk.data.lock().unwrap().user_id.clone(); + let url = bk.url(&format!("user/{}/rooms/{}/tags/m.favourite", userid, roomid), vec![])?; + + let attrs = json!({ + "order": 0.5, + }); + + let tx = bk.tx.clone(); + let method = match tofav { true => "put", false => "delete" }; + query!(method, &url, &attrs, + |_| { tx.send(BKResponse::AddedToFav(roomid.clone(), tofav)).unwrap(); }, + |err| { tx.send(BKResponse::AddToFavError(err)).unwrap(); } + ); + + Ok(()) +} diff --git a/fractal-api/src/backend/types.rs b/fractal-api/src/backend/types.rs index 92c01680..0286c423 100644 --- a/fractal-api/src/backend/types.rs +++ b/fractal-api/src/backend/types.rs @@ -46,6 +46,7 @@ pub enum BKCommand { AttachImage(String, Vec), Search(String, Option), NewRoom(String, RoomType), + AddToFav(String, bool), } #[derive(Debug)] @@ -79,6 +80,7 @@ pub enum BKResponse { AttachedFile(Message), SearchEnd, NewRoom(Room), + AddedToFav(String, bool), //errors UserNameError(Error), @@ -106,6 +108,7 @@ pub enum BKResponse { AttachFileError(Error), SearchError(Error), NewRoomError(Error), + AddToFavError(Error), } #[derive(Debug)] diff --git a/fractal-gtk/src/app.rs b/fractal-gtk/src/app.rs index 3f7e3867..e76cb3b1 100644 --- a/fractal-gtk/src/app.rs +++ b/fractal-gtk/src/app.rs @@ -592,6 +592,10 @@ impl AppOp { self.roomlist.connect(move |room| { bk.send(InternalCommand::SelectRoom(room)).unwrap(); }); + let bk = self.backend.clone(); + self.roomlist.connect_fav(move |room, tofav| { + bk.send(BKCommand::AddToFav(room.id.clone(), tofav)).unwrap(); + }); let mut godef = def; if let Some(aroom) = self.active_room.clone() { @@ -1336,6 +1340,12 @@ impl AppOp { self.set_active_room_by_id(r.id.clone()); } + pub fn added_to_fav(&mut self, roomid: String, tofav: bool) { + if let Some(ref mut r) = self.rooms.get_mut(&roomid) { + r.fav = tofav; + } + } + pub fn change_room_config(&mut self) { let name = self.gtk_builder .get_object::("room_name_entry") @@ -2291,6 +2301,9 @@ fn backend_loop(rx: Receiver) { Ok(BKResponse::NewRoom(r)) => { APPOP!(new_room, (r)); } + Ok(BKResponse::AddedToFav(r, tofav)) => { + APPOP!(added_to_fav, (r, tofav)); + } // errors Ok(BKResponse::NewRoomError(err)) => { diff --git a/fractal-gtk/src/widgets/roomlist.rs b/fractal-gtk/src/widgets/roomlist.rs index df939800..ebbd96e0 100644 --- a/fractal-gtk/src/widgets/roomlist.rs +++ b/fractal-gtk/src/widgets/roomlist.rs @@ -388,8 +388,6 @@ impl RoomList { rooms, }; - rl.connect_dnd(); - rl } @@ -430,6 +428,32 @@ impl RoomList { self.rooms.get().connect(move |room| cb(room)); } + pub fn connect_fav(&self, cb: F) { + let acb = Arc::new(cb); + + let favw = self.fav.get().widget.clone(); + let r = self.rooms.clone(); + let f = self.fav.clone(); + let cb = acb.clone(); + self.connect_drop(favw, move |roomid| { + if let Some(room) = r.get().remove_room(roomid) { + cb(room.room.clone(), true); + f.get().add_room_up(room); + } + }); + + let rw = self.rooms.get().widget.clone(); + let r = self.rooms.clone(); + let f = self.fav.clone(); + let cb = acb.clone(); + self.connect_drop(rw, move |roomid| { + if let Some(room) = f.get().remove_room(roomid) { + cb(room.room.clone(), false); + r.get().add_room_up(room); + } + }); + } + pub fn set_room_avatar(&mut self, room: String, av: Option) { run_in_group!(self, &room, set_room_avatar, room, av); } @@ -507,31 +531,6 @@ impl RoomList { // TODO clicks on inv should show the invitation dialog } - pub fn connect_dnd(&self) { - let favw = self.fav.get().widget.clone(); - - let r = self.rooms.clone(); - let f = self.fav.clone(); - - self.connect_drop(favw, move |roomid| { - // TODO: Add to fav - if let Some(room) = r.get().remove_room(roomid) { - f.get().add_room_up(room); - } - }); - - let rw = self.rooms.get().widget.clone(); - let r = self.rooms.clone(); - let f = self.fav.clone(); - - self.connect_drop(rw, move |roomid| { - // TODO: remove from fav - if let Some(room) = f.get().remove_room(roomid) { - r.get().add_room_up(room); - } - }); - } - pub fn connect_drop(&self, widget: gtk::EventBox, cb: F) { let flags = gtk::DestDefaults::empty(); let action = gdk::DragAction::all();