Allow the user to update their profile metadata fields

Closes #54
master
Paul Woolcock 6 years ago
parent 375e4af286
commit 0ad1e3741c
  1. 40
      src/entities/account.rs
  2. 36
      src/requests/update_credentials.rs

@ -45,6 +45,26 @@ pub struct Account {
/// If the owner decided to switch accounts, new account is in /// If the owner decided to switch accounts, new account is in
/// this attribute /// this attribute
pub moved: Option<Box<Account>>, pub moved: Option<Box<Account>>,
/// List of profile metadata fields
pub fields: Option<Vec<MetadataField>>,
/// Boolean indicating whether this account is a bot or not
pub bot: Option<bool>,
}
/// A single name: value pair from a user's profile
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub struct MetadataField {
name: String,
value: String,
}
impl MetadataField {
pub(crate) fn new(name: &str, value: &str) -> MetadataField {
MetadataField {
name: name.into(),
value: value.into(),
}
}
} }
/// An extra object given from `verify_credentials` giving defaults about a user /// An extra object given from `verify_credentials` giving defaults about a user
@ -54,6 +74,7 @@ pub struct Source {
#[serde(deserialize_with = "string_or_bool")] #[serde(deserialize_with = "string_or_bool")]
sensitive: bool, sensitive: bool,
note: String, note: String,
fields: Vec<MetadataField>,
} }
fn string_or_bool<'de, D: Deserializer<'de>>(val: D) -> ::std::result::Result<bool, D::Error> { fn string_or_bool<'de, D: Deserializer<'de>>(val: D) -> ::std::result::Result<bool, D::Error> {
@ -81,7 +102,7 @@ fn string_or_bool<'de, D: Deserializer<'de>>(val: D) -> ::std::result::Result<bo
}) })
} }
#[derive(Debug, Default, Clone, Copy, Serialize, PartialEq)] #[derive(Debug, Default, Clone, Serialize, PartialEq)]
pub(crate) struct UpdateSource { pub(crate) struct UpdateSource {
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub(crate) privacy: Option<status_builder::Visibility>, pub(crate) privacy: Option<status_builder::Visibility>,
@ -101,4 +122,21 @@ pub(crate) struct Credentials {
pub(crate) header: Option<PathBuf>, pub(crate) header: Option<PathBuf>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub(crate) source: Option<UpdateSource>, pub(crate) source: Option<UpdateSource>,
#[serde(serialize_with = "fields_attributes_ser::ser")]
pub(crate) fields_attributes: Vec<MetadataField>,
}
mod fields_attributes_ser {
use super::*;
use serde::ser::{SerializeMap, Serializer};
pub(crate) fn ser<S>(attrs: &Vec<MetadataField>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut map = serializer.serialize_map(Some(attrs.len()))?;
for (i, field) in attrs.iter().enumerate() {
map.serialize_entry(&i, &field)?;
}
map.end()
}
} }

@ -3,7 +3,7 @@ use std::{
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use entities::account::{Credentials, UpdateSource}; use entities::account::{Credentials, MetadataField, UpdateSource};
use errors::Result; use errors::Result;
use status_builder; use status_builder;
@ -39,6 +39,7 @@ pub struct UpdateCredsRequest {
note: Option<String>, note: Option<String>,
avatar: Option<PathBuf>, avatar: Option<PathBuf>,
header: Option<PathBuf>, header: Option<PathBuf>,
field_attributes: Vec<MetadataField>,
// UpdateSource fields // UpdateSource fields
privacy: Option<status_builder::Visibility>, privacy: Option<status_builder::Visibility>,
@ -166,6 +167,23 @@ impl UpdateCredsRequest {
self self
} }
/// Add a metadata field
///
/// # Example
///
/// ```
/// # extern crate elefren;
/// use elefren::UpdateCredsRequest;
///
/// let mut builder = UpdateCredsRequest::new();
///
/// builder.field_attribute("some key", "some value");
/// ```
pub fn field_attribute(&mut self, name: &str, value: &str) -> &mut Self {
self.field_attributes.push(MetadataField::new(name, value));
self
}
pub(crate) fn build(&mut self) -> Result<Credentials> { pub(crate) fn build(&mut self) -> Result<Credentials> {
Ok(Credentials { Ok(Credentials {
display_name: self.display_name.clone(), display_name: self.display_name.clone(),
@ -176,6 +194,7 @@ impl UpdateCredsRequest {
privacy: self.privacy.clone(), privacy: self.privacy.clone(),
sensitive: self.sensitive.clone(), sensitive: self.sensitive.clone(),
}), }),
fields_attributes: self.field_attributes.clone(),
}) })
} }
} }
@ -183,7 +202,7 @@ impl UpdateCredsRequest {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use entities::account::{Credentials, UpdateSource}; use entities::account::{Credentials, MetadataField, UpdateSource};
use status_builder::Visibility; use status_builder::Visibility;
#[test] #[test]
@ -275,6 +294,19 @@ mod tests {
); );
} }
#[test]
fn test_update_creds_request_field_attribute() {
let mut builder = UpdateCredsRequest::new();
builder.field_attribute("foo", "bar");
assert_eq!(
builder,
UpdateCredsRequest {
field_attributes: vec![MetadataField::new("foo", "bar")],
..Default::default()
}
);
}
#[test] #[test]
fn test_update_creds_request_build() { fn test_update_creds_request_build() {
let mut builder = UpdateCredsRequest::new(); let mut builder = UpdateCredsRequest::new();

Loading…
Cancel
Save