From 3e3463e3d3fcc83749d9f0ed570dab2be66608a5 Mon Sep 17 00:00:00 2001 From: NotAFile Date: Wed, 5 Sep 2018 00:50:40 +0200 Subject: [PATCH] use serde_derive for Member deserialization --- fractal-matrix-api/src/backend/room.rs | 13 +++++-------- fractal-matrix-api/src/backend/user.rs | 18 +++++------------- fractal-matrix-api/src/model/member.rs | 7 ++++++- 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/fractal-matrix-api/src/backend/room.rs b/fractal-matrix-api/src/backend/room.rs index 873c25cc..56372d20 100644 --- a/fractal-matrix-api/src/backend/room.rs +++ b/fractal-matrix-api/src/backend/room.rs @@ -1,5 +1,6 @@ extern crate url; extern crate urlencoding; +extern crate serde_json; use std::fs::File; use std::io::prelude::*; @@ -107,14 +108,10 @@ pub fn get_room_members(bk: &Backend, roomid: String) -> Result<(), Error> { |r: JsonValue| { let joined = r["joined"].as_object().unwrap(); let ms: Vec = joined.iter().map(|(mxid, member_data)| { - let alias = &member_data["display_name"]; - let avatar = &member_data["avatar_url"]; - - Member { - alias: alias.as_str().map(String::from), - avatar: avatar.as_str().map(String::from), - uid: mxid.to_string(), - } + let mut member: Member = serde_json::from_value( + member_data.clone()).unwrap(); + member.uid = mxid.to_string(); + member } ).collect(); tx.send(BKResponse::RoomMembers(roomid, ms)).unwrap(); diff --git a/fractal-matrix-api/src/backend/user.rs b/fractal-matrix-api/src/backend/user.rs index ffac5d28..482e45f5 100644 --- a/fractal-matrix-api/src/backend/user.rs +++ b/fractal-matrix-api/src/backend/user.rs @@ -436,20 +436,12 @@ pub fn search(bk: &Backend, term: String) -> Result<(), Error> { let mut users: Vec = vec![]; if let Some(arr) = js["results"].as_array() { for member in arr.iter() { - let alias = match member["display_name"].as_str() { - None => None, - Some(a) => Some(a.to_string()), - }; - let avatar = match member["avatar_url"].as_str() { - None => None, - Some(a) => Some(a.to_string()), - }; + let mut member_s: Member = serde_json::from_value( + member.clone()).unwrap(); + member_s.uid = member["user_id"] + .as_str().unwrap_or_default().to_string(); - users.push(Member{ - alias: alias, - uid: member["user_id"].as_str().unwrap_or_default().to_string(), - avatar: avatar, - }); + users.push(member_s); } } tx.send(BKResponse::UserSearch(users)).unwrap(); diff --git a/fractal-matrix-api/src/model/member.rs b/fractal-matrix-api/src/model/member.rs index fe2f18e3..c9172d50 100644 --- a/fractal-matrix-api/src/model/member.rs +++ b/fractal-matrix-api/src/model/member.rs @@ -2,8 +2,13 @@ use std::collections::HashMap; #[derive(Debug, Serialize, Deserialize)] pub struct Member { - pub alias: Option, + // The mxid is either inside the json object, or outside of it. + // Since we don't know, we always have to populate it manually + #[serde(default)] pub uid: String, + #[serde(rename = "display_name")] + pub alias: Option, + #[serde(rename = "avatar_url")] pub avatar: Option, }