Merge branch 'nota/serde-derive-member' into 'master'

use serde_derive for Member deserialization

See merge request World/fractal!216
This commit is contained in:
Daniel Garcia Moreno 2018-09-05 07:20:16 +00:00
commit 22cfba6ac4
3 changed files with 16 additions and 22 deletions

View file

@ -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<Member> = 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();

View file

@ -436,20 +436,12 @@ pub fn search(bk: &Backend, term: String) -> Result<(), Error> {
let mut users: Vec<Member> = 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();

View file

@ -2,8 +2,13 @@ use std::collections::HashMap;
#[derive(Debug, Serialize, Deserialize)]
pub struct Member {
pub alias: Option<String>,
// 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<String>,
#[serde(rename = "avatar_url")]
pub avatar: Option<String>,
}