Commit 738b82ce authored by Daniel García Moreno's avatar Daniel García Moreno

Managing room topic and room name change events

parent 4e0c51c6
......@@ -4,6 +4,7 @@ Fixs:
* Sort rooms by last message or fav?
Functionality:
* Show event messages in message list
* Register
* Room creation
* Change user display name
......@@ -13,8 +14,6 @@ Functionality:
* Store last read message to show differently
Events to manage:
* Room name changed
* Room topic changed
* Room avatar changed
* Join / leave from current user
* Invite event
......
......@@ -755,6 +755,49 @@ impl AppOp {
}
}
}
pub fn room_name_change(&mut self, roomid: String, name: String) {
let store: gtk::TreeStore = self.gtk_builder
.get_object("rooms_tree_store")
.expect("Couldn't find rooms_tree_store in ui file.");
let mut r = self.rooms.get_mut(&roomid).unwrap();
r.name = name.clone();
if roomid == self.active_room {
self.gtk_builder
.get_object::<gtk::Label>("room_name")
.expect("Can't find room_name in ui file.")
.set_text(&name);
}
if let Some(iter) = store.get_iter_first() {
loop {
let v1 = store.get_value(&iter, 1);
let id: &str = v1.get().unwrap();
if id == roomid {
store.set_value(&iter, 0, &gtk::Value::from(&name));
}
if !store.iter_next(&iter) {
break;
}
}
}
}
pub fn room_topic_change(&mut self, roomid: String, topic: String) {
let mut r = self.rooms.get_mut(&roomid).unwrap();
r.topic = topic.clone();
if roomid == self.active_room {
let t = self.gtk_builder
.get_object::<gtk::Label>("room_topic")
.expect("Can't find room_topic in ui file.");
t.set_tooltip_text(&topic[..]);
t.set_text(&topic);
}
}
}
/// State for the main thread.
......@@ -873,6 +916,14 @@ impl App {
Ok(BKResponse::MarkedAsRead(r, _)) => {
theop.lock().unwrap().update_room_notifications(&r, |_| 0);
}
Ok(BKResponse::RoomName(roomid, name)) => {
theop.lock().unwrap().room_name_change(roomid, name);
}
Ok(BKResponse::RoomTopic(roomid, topic)) => {
theop.lock().unwrap().room_topic_change(roomid, topic);
}
// errors
Ok(BKResponse::SyncError(_)) => {
theop.lock().unwrap().syncing = false;
......
......@@ -92,6 +92,8 @@ pub enum BKResponse {
SetRoomName,
SetRoomTopic,
SetRoomAvatar,
RoomName(String, String),
RoomTopic(String, String),
//errors
UserNameError(Error),
......@@ -433,9 +435,6 @@ impl Backend {
let data = self.data.clone();
get!(&url,
|r: JsonValue| {
// TODO: treat all events
//println!("sync: {:#?}", r);
let next_batch = String::from(r["next_batch"].as_str().unwrap_or(""));
if since.is_empty() {
let rooms = match get_rooms_from_json(r, &userid) {
......@@ -456,10 +455,32 @@ impl Backend {
tx.send(BKResponse::Rooms(rooms, def)).unwrap();
} else {
match get_rooms_timeline_from_json(&baseu, r) {
// Message events
match get_rooms_timeline_from_json(&baseu, &r) {
Ok(msgs) => tx.send(BKResponse::RoomMessages(msgs)).unwrap(),
Err(err) => tx.send(BKResponse::RoomMessagesError(err)).unwrap(),
}
};
// Other events
match parse_sync_events(&r) {
Err(err) => tx.send(BKResponse::SyncError(err)).unwrap(),
Ok(events) => {
for ev in events {
match ev.stype.as_ref() {
"m.room.name" => {
let name = strn!(ev.content["name"].as_str().unwrap_or(""));
tx.send(BKResponse::RoomName(ev.room.clone(), name)).unwrap();
}
"m.room.topic" => {
let t = strn!(ev.content["topic"].as_str().unwrap_or(""));
tx.send(BKResponse::RoomTopic(ev.room.clone(), t)).unwrap();
}
_ => {
println!("EVENT NOT MANAGED: {:?}", ev);
}
}
}
}
};
}
data.lock().unwrap().since = next_batch;
......@@ -842,7 +863,7 @@ impl Backend {
let uri = js["content_uri"].as_str().unwrap_or("");
let attrs = json!({ "url": uri });
match json_q("put", &roomurl, &attrs) {
Ok(js) => {
Ok(_) => {
tx.send(BKResponse::SetRoomAvatar).unwrap();
},
Err(err) => {
......
extern crate chrono;
extern crate serde_json;
use self::chrono::prelude::*;
use self::serde_json::Value as JsonValue;
#[derive(Debug)]
pub struct Message {
......@@ -95,3 +97,12 @@ impl Clone for Room {
}
}
}
#[derive(Debug)]
pub struct Event {
pub sender: String,
pub stype: String,
pub room: String,
pub id: String,
pub content: JsonValue,
}
......@@ -27,6 +27,7 @@ use self::time::Duration;
use error::Error;
use types::Message;
use types::Room;
use types::Event;
use self::reqwest::header::ContentType;
......@@ -195,7 +196,7 @@ pub fn get_rooms_from_json(r: JsonValue, userid: &str) -> Result<Vec<Room>, Erro
Ok(rooms)
}
pub fn get_rooms_timeline_from_json(baseu: &Url, r: JsonValue) -> Result<Vec<Message>, Error> {
pub fn get_rooms_timeline_from_json(baseu: &Url, r: &JsonValue) -> Result<Vec<Message>, Error> {
let rooms = &r["rooms"];
let join = rooms["join"].as_object().ok_or(Error::BackendError)?;
......@@ -220,6 +221,37 @@ pub fn get_rooms_timeline_from_json(baseu: &Url, r: JsonValue) -> Result<Vec<Mes
Ok(msgs)
}
pub fn parse_sync_events(r: &JsonValue) -> Result<Vec<Event>, Error> {
let rooms = &r["rooms"];
let join = rooms["join"].as_object().ok_or(Error::BackendError)?;
let mut evs: Vec<Event> = vec![];
for k in join.keys() {
let room = join.get(k).ok_or(Error::BackendError)?;
let timeline = room["timeline"]["events"].as_array();
if timeline.is_none() {
return Ok(evs);
}
let events = timeline.unwrap()
.iter()
.filter(|x| x["type"] != "m.room.message");
for ev in events {
println!("ev: {:#?}", ev);
evs.push(Event {
room: k.clone(),
sender: strn!(ev["sender"].as_str().unwrap_or("")),
content: ev["content"].clone(),
stype: strn!(ev["type"].as_str().unwrap_or("")),
id: strn!(ev["id"].as_str().unwrap_or("")),
});
}
}
Ok(evs)
}
pub fn get_media(url: &str) -> Result<Vec<u8>, Error> {
let client = reqwest::Client::new()?;
let mut conn = client.get(url)?;
......
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