Keep value source when editing extra fields

This commit is contained in:
silverpill 2021-09-17 14:43:02 +00:00
parent 7fad429a8c
commit d2adda2034
3 changed files with 29 additions and 6 deletions

View file

@ -92,6 +92,7 @@ impl Actor {
.map(|prop| ExtraField { .map(|prop| ExtraField {
name: prop.name.clone(), name: prop.name.clone(),
value: prop.value.clone(), value: prop.value.clone(),
value_source: None,
}) })
.collect() .collect()
}, },

View file

@ -11,11 +11,17 @@ use crate::models::profiles::types::{
}; };
use crate::utils::files::{FileError, save_validated_b64_file, get_file_url}; use crate::utils::files::{FileError, save_validated_b64_file, get_file_url};
#[derive(Serialize)]
pub struct AccountField {
pub name: String,
pub value: String,
}
/// https://docs.joinmastodon.org/entities/source/ /// https://docs.joinmastodon.org/entities/source/
#[derive(Serialize)] #[derive(Serialize)]
pub struct Source { pub struct Source {
pub note: Option<String>, pub note: Option<String>,
pub fields: Vec<ExtraField>, pub fields: Vec<AccountField>,
} }
/// https://docs.joinmastodon.org/entities/account/ /// https://docs.joinmastodon.org/entities/account/
@ -29,7 +35,7 @@ pub struct Account {
pub note: Option<String>, pub note: Option<String>,
pub avatar: Option<String>, pub avatar: Option<String>,
pub header: Option<String>, pub header: Option<String>,
pub fields: Vec<ExtraField>, pub fields: Vec<AccountField>,
pub followers_count: i32, pub followers_count: i32,
pub following_count: i32, pub following_count: i32,
pub statuses_count: i32, pub statuses_count: i32,
@ -45,12 +51,22 @@ impl Account {
// Remote actor // Remote actor
None None
} else { } else {
let fields_sources = profile.extra_fields.clone()
.unpack().into_iter()
.map(|field| AccountField {
name: field.name,
value: field.value_source.unwrap_or(field.value),
})
.collect();
let source = Source { let source = Source {
note: profile.bio_source, note: profile.bio_source,
fields: profile.extra_fields.clone().unpack(), fields: fields_sources,
}; };
Some(source) Some(source)
}; };
let fields = profile.extra_fields.unpack().into_iter()
.map(|field| AccountField { name: field.name, value: field.value })
.collect();
Self { Self {
id: profile.id, id: profile.id,
username: profile.username, username: profile.username,
@ -60,7 +76,7 @@ impl Account {
note: profile.bio, note: profile.bio,
avatar: avatar_url, avatar: avatar_url,
header: header_url, header: header_url,
fields: profile.extra_fields.unpack(), fields,
followers_count: profile.follower_count, followers_count: profile.follower_count,
following_count: profile.following_count, following_count: profile.following_count,
statuses_count: profile.post_count, statuses_count: profile.post_count,

View file

@ -15,6 +15,7 @@ use crate::utils::html::clean_html;
pub struct ExtraField { pub struct ExtraField {
pub name: String, pub name: String,
pub value: String, pub value: String,
pub value_source: Option<String>,
} }
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
@ -91,9 +92,14 @@ impl ProfileUpdateData {
pub fn clean(&mut self) -> Result<(), ValidationError> { pub fn clean(&mut self) -> Result<(), ValidationError> {
// Validate and clean bio // Validate and clean bio
self.bio = self.bio.as_ref().map(|val| clean_html(val)); self.bio = self.bio.as_ref().map(|val| clean_html(val));
// Remove fields with empty labels // Clean extra fields and remove fields with empty labels
self.extra_fields = self.extra_fields.iter().cloned() self.extra_fields = self.extra_fields.iter().cloned()
.filter(|field| field.name.trim().len() > 0) .map(|mut field| {
field.name = field.name.trim().to_string();
field.value = clean_html(&field.value);
field
})
.filter(|field| field.name.len() > 0)
.collect(); .collect();
// Validate extra fields // Validate extra fields
if self.extra_fields.len() >= 10 { if self.extra_fields.len() >= 10 {