Remove acct field from ProfileCreateData struct

This commit is contained in:
silverpill 2022-10-04 19:18:05 +00:00
parent 5c56f831db
commit fa49e2b4c2
6 changed files with 16 additions and 30 deletions

View file

@ -73,8 +73,8 @@ async fn create_remote_profile(
actor.parse_attachments();
let mut profile_data = ProfileCreateData {
username: actor.preferred_username.clone(),
hostname: Some(actor_address.instance),
display_name: actor.name.clone(),
acct: actor_address.acct(&instance.host()),
bio: actor.summary.clone(),
avatar,
banner,

View file

@ -131,7 +131,9 @@ pub async fn receive_activity(
};
let signer_id = signer.actor_id(&config.instance_url());
log::debug!("activity signed by {}", signer_id);
if config.blocked_instances.iter().any(|instance| signer.acct.contains(instance)) {
if config.blocked_instances.iter()
.any(|instance| signer.hostname.as_ref() == Some(instance))
{
log::warn!("ignoring activity from blocked instance: {}", activity_raw);
return Ok(());
};

View file

@ -1,7 +1,6 @@
use tokio_postgres::GenericClient;
use uuid::Uuid;
use crate::activitypub::actors::types::ActorAddress;
use crate::database::catch_unique_violation;
use crate::database::query_macro::query;
use crate::errors::DatabaseError;
@ -29,10 +28,7 @@ pub async fn create_profile(
profile_data: ProfileCreateData,
) -> Result<DbActorProfile, DatabaseError> {
let profile_id = new_uuid();
// TODO: replace ProfileCreateData.acct with hostname field
let hostname = if profile_data.actor_json.is_some() {
let actor_address = profile_data.acct.parse::<ActorAddress>().unwrap();
let hostname = actor_address.instance;
if let Some(ref hostname) = profile_data.hostname {
db_client.execute(
"
INSERT INTO instance VALUES ($1)
@ -40,9 +36,6 @@ pub async fn create_profile(
",
&[&hostname],
).await?;
Some(hostname)
} else {
None
};
let row = db_client.query_one(
"
@ -58,7 +51,7 @@ pub async fn create_profile(
&[
&profile_id,
&profile_data.username,
&hostname,
&profile_data.hostname,
&profile_data.display_name,
&profile_data.bio,
&profile_data.bio,
@ -597,7 +590,6 @@ mod tests {
async fn test_create_profile_local() {
let profile_data = ProfileCreateData {
username: "test".to_string(),
acct: "test".to_string(),
..Default::default()
};
let db_client = create_test_database().await;
@ -615,7 +607,7 @@ mod tests {
async fn test_create_profile_remote() {
let profile_data = ProfileCreateData {
username: "test".to_string(),
acct: "test@example.com".to_string(),
hostname: Some("example.com".to_string()),
actor_json: Some(create_test_actor("https://example.com/users/test")),
..Default::default()
};
@ -637,14 +629,14 @@ mod tests {
let actor_id = "https://example.com/users/test";
let profile_data_1 = ProfileCreateData {
username: "test-1".to_string(),
acct: "test-1@example.com".to_string(),
hostname: Some("example.com".to_string()),
actor_json: Some(create_test_actor(actor_id)),
..Default::default()
};
create_profile(&db_client, profile_data_1).await.unwrap();
let profile_data_2 = ProfileCreateData {
username: "test-2".to_string(),
acct: "test-2@example.com".to_string(),
hostname: Some("example.com".to_string()),
actor_json: Some(create_test_actor(actor_id)),
..Default::default()
};

View file

@ -323,8 +323,8 @@ impl Default for DbActorProfile {
#[cfg_attr(test, derive(Default))]
pub struct ProfileCreateData {
pub username: String,
pub hostname: Option<String>,
pub display_name: Option<String>,
pub acct: String,
pub bio: Option<String>,
pub avatar: Option<String>,
pub banner: Option<String>,
@ -337,20 +337,12 @@ pub struct ProfileCreateData {
impl ProfileCreateData {
pub fn clean(&mut self) -> Result<(), ValidationError> {
validate_username(&self.username)?;
if self.hostname.is_some() != self.actor_json.is_some() {
return Err(ValidationError("hostname and actor_json field mismatch"));
};
if let Some(display_name) = &self.display_name {
validate_display_name(display_name)?;
};
let acct_username = if self.actor_json.is_none() {
// Local profile
self.acct.clone()
} else {
// Remote profile
let ActorAddress { username, .. } = self.acct.parse::<ActorAddress>()?;
username
};
if self.username != acct_username {
return Err(ValidationError("username doesn't match acct"));
};
if let Some(bio) = &self.bio {
let cleaned_bio = clean_bio(bio, self.actor_json.is_some())?;
self.bio = Some(cleaned_bio);
@ -500,8 +492,8 @@ mod tests {
fn test_clean_profile_create_data() {
let mut profile_data = ProfileCreateData {
username: "test".to_string(),
hostname: Some("example.org".to_string()),
display_name: Some("Test Test".to_string()),
acct: "test@example.org".to_string(),
actor_json: Some(Actor {
id: "https://example.org/test".to_string(),
..Default::default()

View file

@ -522,7 +522,7 @@ mod tests {
let source = create_user(db_client, source_data).await.unwrap();
let target_data = ProfileCreateData {
username: "followed".to_string(),
acct: "followed@example.org".to_string(),
hostname: Some("example.org".to_string()),
actor_json: Some(Actor::default()),
..Default::default()
};

View file

@ -77,8 +77,8 @@ pub async fn create_user(
// Create profile
let profile_data = ProfileCreateData {
username: user_data.username.clone(),
hostname: None,
display_name: None,
acct: user_data.username.clone(),
bio: None,
avatar: None,
banner: None,