Refactor get_object_visibility function
This commit is contained in:
parent
7c38c0a4d6
commit
278950252e
1 changed files with 37 additions and 53 deletions
|
@ -505,19 +505,40 @@ pub async fn get_object_tags(
|
||||||
Ok((mentions, hashtags, links, emojis))
|
Ok((mentions, hashtags, links, emojis))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_object_visibility(
|
fn get_audience(object: &Object) -> Result<Vec<String>, ValidationError> {
|
||||||
author: &DbActorProfile,
|
let primary_audience = match object.to {
|
||||||
primary_audience: Vec<String>,
|
Some(ref value) => {
|
||||||
secondary_audience: Vec<String>,
|
parse_array(value)
|
||||||
) -> Visibility {
|
.map_err(|_| ValidationError("invalid 'to' property value"))?
|
||||||
|
},
|
||||||
|
None => vec![],
|
||||||
|
};
|
||||||
|
let secondary_audience = match object.cc {
|
||||||
|
Some(ref value) => {
|
||||||
|
parse_array(value)
|
||||||
|
.map_err(|_| ValidationError("invalid 'cc' property value"))?
|
||||||
|
},
|
||||||
|
None => vec![],
|
||||||
|
};
|
||||||
let audience = [primary_audience, secondary_audience].concat();
|
let audience = [primary_audience, secondary_audience].concat();
|
||||||
|
Ok(audience)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_public_object(audience: &[String]) -> bool {
|
||||||
// Some servers (e.g. Takahe) use "as" namespace
|
// Some servers (e.g. Takahe) use "as" namespace
|
||||||
const PUBLIC_VARIANTS: [&str; 3] = [
|
const PUBLIC_VARIANTS: [&str; 3] = [
|
||||||
AP_PUBLIC,
|
AP_PUBLIC,
|
||||||
"as:Public",
|
"as:Public",
|
||||||
"Public",
|
"Public",
|
||||||
];
|
];
|
||||||
if audience.iter().any(|item| PUBLIC_VARIANTS.contains(&item.as_str())) {
|
audience.iter().any(|item| PUBLIC_VARIANTS.contains(&item.as_str()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_object_visibility(
|
||||||
|
author: &DbActorProfile,
|
||||||
|
audience: &[String],
|
||||||
|
) -> Visibility {
|
||||||
|
if is_public_object(audience) {
|
||||||
return Visibility::Public;
|
return Visibility::Public;
|
||||||
};
|
};
|
||||||
let actor = author.actor_json.as_ref()
|
let actor = author.actor_json.as_ref()
|
||||||
|
@ -607,25 +628,8 @@ pub async fn handle_note(
|
||||||
},
|
},
|
||||||
None => None,
|
None => None,
|
||||||
};
|
};
|
||||||
let primary_audience = match object.to {
|
let audience = get_audience(&object)?;
|
||||||
Some(value) => {
|
let visibility = get_object_visibility(&author, &audience);
|
||||||
parse_array(&value)
|
|
||||||
.map_err(|_| ValidationError("invalid 'to' property value"))?
|
|
||||||
},
|
|
||||||
None => vec![],
|
|
||||||
};
|
|
||||||
let secondary_audience = match object.cc {
|
|
||||||
Some(value) => {
|
|
||||||
parse_array(&value)
|
|
||||||
.map_err(|_| ValidationError("invalid 'cc' property value"))?
|
|
||||||
},
|
|
||||||
None => vec![],
|
|
||||||
};
|
|
||||||
let visibility = get_object_visibility(
|
|
||||||
&author,
|
|
||||||
primary_audience,
|
|
||||||
secondary_audience,
|
|
||||||
);
|
|
||||||
if visibility != Visibility::Public {
|
if visibility != Visibility::Public {
|
||||||
log::warn!(
|
log::warn!(
|
||||||
"processing note with visibility {:?} attributed to {}",
|
"processing note with visibility {:?} attributed to {}",
|
||||||
|
@ -742,13 +746,8 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_get_object_visibility_public() {
|
fn test_get_object_visibility_public() {
|
||||||
let author = DbActorProfile::default();
|
let author = DbActorProfile::default();
|
||||||
let primary_audience = vec![AP_PUBLIC.to_string()];
|
let audience = vec![AP_PUBLIC.to_string()];
|
||||||
let secondary_audience = vec![];
|
let visibility = get_object_visibility(&author, &audience);
|
||||||
let visibility = get_object_visibility(
|
|
||||||
&author,
|
|
||||||
primary_audience,
|
|
||||||
secondary_audience,
|
|
||||||
);
|
|
||||||
assert_eq!(visibility, Visibility::Public);
|
assert_eq!(visibility, Visibility::Public);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -762,13 +761,8 @@ mod tests {
|
||||||
}),
|
}),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
let primary_audience = vec![author_followers.to_string()];
|
let audience = vec![author_followers.to_string()];
|
||||||
let secondary_audience = vec![];
|
let visibility = get_object_visibility(&author, &audience);
|
||||||
let visibility = get_object_visibility(
|
|
||||||
&author,
|
|
||||||
primary_audience,
|
|
||||||
secondary_audience,
|
|
||||||
);
|
|
||||||
assert_eq!(visibility, Visibility::Followers);
|
assert_eq!(visibility, Visibility::Followers);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -784,13 +778,8 @@ mod tests {
|
||||||
}),
|
}),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
let primary_audience = vec![author_subscribers.to_string()];
|
let audience = vec![author_subscribers.to_string()];
|
||||||
let secondary_audience = vec![];
|
let visibility = get_object_visibility(&author, &audience);
|
||||||
let visibility = get_object_visibility(
|
|
||||||
&author,
|
|
||||||
primary_audience,
|
|
||||||
secondary_audience,
|
|
||||||
);
|
|
||||||
assert_eq!(visibility, Visibility::Subscribers);
|
assert_eq!(visibility, Visibility::Subscribers);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -800,13 +789,8 @@ mod tests {
|
||||||
actor_json: Some(DbActor::default()),
|
actor_json: Some(DbActor::default()),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
let primary_audience = vec!["https://example.com/users/1".to_string()];
|
let audience = vec!["https://example.com/users/1".to_string()];
|
||||||
let secondary_audience = vec![];
|
let visibility = get_object_visibility(&author, &audience);
|
||||||
let visibility = get_object_visibility(
|
|
||||||
&author,
|
|
||||||
primary_audience,
|
|
||||||
secondary_audience,
|
|
||||||
);
|
|
||||||
assert_eq!(visibility, Visibility::Direct);
|
assert_eq!(visibility, Visibility::Direct);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue