[fractal] Backend: use Deserialize in room_search
- From: Julian Sparber <jsparber src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [fractal] Backend: use Deserialize in room_search
- Date: Wed, 23 Jan 2019 15:14:58 +0000 (UTC)
commit 9f1af03b08d02b3a85128166bdb276673a1b8f43
Author: Alejandro DomÃnguez <adomu net-c com>
Date: Mon Jan 14 12:18:14 2019 +0100
Backend: use Deserialize in room_search
We replace our own deserializing logic in `room_search()` in `backend/directory.rs`
with the derived Deserialize trait on the room types.
fractal-matrix-api/src/backend/directory.rs | 49 +++++++++++------------------
fractal-matrix-api/src/types.rs | 1 +
2 files changed, 20 insertions(+), 30 deletions(-)
---
diff --git a/fractal-matrix-api/src/backend/directory.rs b/fractal-matrix-api/src/backend/directory.rs
index da04250e..4416c2ff 100644
--- a/fractal-matrix-api/src/backend/directory.rs
+++ b/fractal-matrix-api/src/backend/directory.rs
@@ -13,7 +13,8 @@ use crate::util::cache_path;
use crate::util::json_q;
use crate::util::media;
-use crate::types::{Room, RoomMembership};
+use crate::types::PublicRoomsResponse;
+use crate::types::Room;
use crate::types::SupportedProtocols;
pub fn protocols(bk: &Backend) {
@@ -89,35 +90,23 @@ pub fn room_search(
&url,
&attrs,
move |r: JsonValue| {
- let next_branch = r["next_batch"].as_str().unwrap_or_default();
- data.lock().unwrap().rooms_since = String::from(next_branch);
-
- let mut rooms: Vec<Room> = vec![];
- for room in r["chunk"].as_array().unwrap() {
- // Panic when we have rooms without an id
- let id = room["room_id"]
- .as_str()
- .expect("Couldn't handle room: no valid id");
- let alias = room["canonical_alias"].as_str();
- let name = room["name"].as_str();
- let avatar = room["avatar_url"].as_str();
- /* download the avatar for the room */
- if let Some(avatar) = avatar {
- if let Ok(dest) = cache_path(id) {
- let _ = media(&base.clone(), avatar, Some(&dest));
- }
- }
-
- let mut r = Room::new(id.to_string(), RoomMembership::None);
- r.name = name.map(String::from);
- r.alias = alias.map(String::from);
- r.avatar = avatar.map(String::from);
- r.topic = room["topic"].as_str().map(String::from);
- r.n_members = room["num_joined_members"].as_i64().unwrap_or(0) as i32;
- r.world_readable = room["world_readable"].as_bool().unwrap_or(false);
- r.guest_can_join = room["guest_can_join"].as_bool().unwrap_or(false);
- rooms.push(r);
- }
+ let rooms = serde_json::from_value(r)
+ .map(|pr: PublicRoomsResponse| {
+ data.lock().unwrap().rooms_since = pr.next_batch.unwrap_or_default();
+
+ pr.chunk
+ .into_iter()
+ .map(Into::into)
+ .inspect(|r: &Room| {
+ if let Some(avatar) = r.avatar.clone() {
+ if let Ok(dest) = cache_path(&r.id) {
+ media(&base.clone(), &avatar, Some(&dest)).unwrap_or_default();
+ }
+ }
+ })
+ .collect()
+ })
+ .unwrap_or_default();
tx.send(BKResponse::DirectorySearch(rooms)).unwrap();
},
diff --git a/fractal-matrix-api/src/types.rs b/fractal-matrix-api/src/types.rs
index ac7ddaf1..9bdc699b 100644
--- a/fractal-matrix-api/src/types.rs
+++ b/fractal-matrix-api/src/types.rs
@@ -4,6 +4,7 @@ pub use crate::model::member::MemberList;
pub use crate::model::message::Message;
pub use crate::model::protocol::ProtocolInstance;
pub use crate::model::protocol::SupportedProtocols;
+pub use crate::model::room::PublicRoomsResponse;
pub use crate::model::room::Room;
pub use crate::model::room::RoomList;
pub use crate::model::room::RoomMembership;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]