Commit 2001bdc6 authored by Daniel García Moreno's avatar Daniel García Moreno

Add to fav backend feature

See #34
parent 01106ac7
Pipeline #1992 passed with stage
in 63 minutes
......@@ -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);
......
......@@ -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(())
}
......@@ -46,6 +46,7 @@ pub enum BKCommand {
AttachImage(String, Vec<u8>),
Search(String, Option<String>),
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)]
......
......@@ -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::<gtk::Entry>("room_name_entry")
......@@ -2291,6 +2301,9 @@ fn backend_loop(rx: Receiver<BKResponse>) {
Ok(BKResponse::NewRoom(r)) => {
APPOP!(new_room, (r));
}
Ok(BKResponse::AddedToFav(r, tofav)) => {
APPOP!(added_to_fav, (r, tofav));
}
// errors
Ok(BKResponse::NewRoomError(err)) => {
......
......@@ -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<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>) {
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<F: Fn(String) + 'static>(&self, widget: gtk::EventBox, cb: F) {
let flags = gtk::DestDefaults::empty();
let action = gdk::DragAction::all();
......
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