Add to fav backend feature

See #34
This commit is contained in:
Daniel García Moreno 2018-01-11 14:18:58 +01:00
parent 01106ac77d
commit 2001bdc66a
5 changed files with 64 additions and 27 deletions

View file

@ -190,6 +190,10 @@ impl Backend {
let r = room::new_room(self, name, privacy); let r = room::new_room(self, name, privacy);
bkerror!(r, tx, BKResponse::NewRoomError); 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)) => { Ok(BKCommand::Search(roomid, term)) => {
let r = room::search(self, roomid, term); let r = room::search(self, roomid, term);
bkerror!(r, tx, BKResponse::SearchError); bkerror!(r, tx, BKResponse::SearchError);

View file

@ -481,3 +481,21 @@ pub fn make_search(bk: &Backend, roomid: String, term: String) -> Result<(), Err
Ok(()) 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(())
}

View file

@ -46,6 +46,7 @@ pub enum BKCommand {
AttachImage(String, Vec<u8>), AttachImage(String, Vec<u8>),
Search(String, Option<String>), Search(String, Option<String>),
NewRoom(String, RoomType), NewRoom(String, RoomType),
AddToFav(String, bool),
} }
#[derive(Debug)] #[derive(Debug)]
@ -79,6 +80,7 @@ pub enum BKResponse {
AttachedFile(Message), AttachedFile(Message),
SearchEnd, SearchEnd,
NewRoom(Room), NewRoom(Room),
AddedToFav(String, bool),
//errors //errors
UserNameError(Error), UserNameError(Error),
@ -106,6 +108,7 @@ pub enum BKResponse {
AttachFileError(Error), AttachFileError(Error),
SearchError(Error), SearchError(Error),
NewRoomError(Error), NewRoomError(Error),
AddToFavError(Error),
} }
#[derive(Debug)] #[derive(Debug)]

View file

@ -592,6 +592,10 @@ impl AppOp {
self.roomlist.connect(move |room| { self.roomlist.connect(move |room| {
bk.send(InternalCommand::SelectRoom(room)).unwrap(); 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; let mut godef = def;
if let Some(aroom) = self.active_room.clone() { if let Some(aroom) = self.active_room.clone() {
@ -1336,6 +1340,12 @@ impl AppOp {
self.set_active_room_by_id(r.id.clone()); 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) { pub fn change_room_config(&mut self) {
let name = self.gtk_builder let name = self.gtk_builder
.get_object::<gtk::Entry>("room_name_entry") .get_object::<gtk::Entry>("room_name_entry")
@ -2291,6 +2301,9 @@ fn backend_loop(rx: Receiver<BKResponse>) {
Ok(BKResponse::NewRoom(r)) => { Ok(BKResponse::NewRoom(r)) => {
APPOP!(new_room, (r)); APPOP!(new_room, (r));
} }
Ok(BKResponse::AddedToFav(r, tofav)) => {
APPOP!(added_to_fav, (r, tofav));
}
// errors // errors
Ok(BKResponse::NewRoomError(err)) => { Ok(BKResponse::NewRoomError(err)) => {

View file

@ -388,8 +388,6 @@ impl RoomList {
rooms, rooms,
}; };
rl.connect_dnd();
rl rl
} }
@ -430,6 +428,32 @@ impl RoomList {
self.rooms.get().connect(move |room| cb(room)); self.rooms.get().connect(move |room| cb(room));
} }
pub fn connect_fav<F: Fn(Room, bool) + 'static>(&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<String>) { pub fn set_room_avatar(&mut self, room: String, av: Option<String>) {
run_in_group!(self, &room, set_room_avatar, room, av); 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 // 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<F: Fn(String) + 'static>(&self, widget: gtk::EventBox, cb: F) { pub fn connect_drop<F: Fn(String) + 'static>(&self, widget: gtk::EventBox, cb: F) {
let flags = gtk::DestDefaults::empty(); let flags = gtk::DestDefaults::empty();
let action = gdk::DragAction::all(); let action = gdk::DragAction::all();