[web] Add a JSON pronouns endpoint

Signed-off-by: Ben Aaron Goldberg <ben@benaaron.dev>
Signed-off-by: Sashanoraa <ben@benaaron.dev>
This commit is contained in:
Ben Aaron Goldberg 2021-11-13 18:04:40 -05:00 committed by Sashanoraa
parent 4cef709f52
commit feac079f59
1 changed files with 62 additions and 9 deletions

View File

@ -213,11 +213,20 @@ fn route_request(req: &ScgiRequest) -> Route {
}
}
#[derive(PartialEq, Eq)]
enum RouteType {
Html,
Thumbnail,
Json,
}
// Determines if we need to respond with a thumbnail or a web page
let is_thumb =
!segments.is_empty() &&
segments.last().map(Deref::deref) == Some("thumb.png");
if is_thumb {
let route_type = match segments.last().map(Deref::deref) {
Some("thumb.png") => RouteType::Thumbnail,
Some("pns.json") => RouteType::Json,
_ => RouteType::Html,
};
if route_type != RouteType::Html {
// Now that we've determined that, we don't need this segment anymore
segments.pop();
}
@ -245,11 +254,13 @@ fn route_request(req: &ScgiRequest) -> Route {
let name = name.map(|n| (*n).to_owned());
if is_thumb {
Route::SendThumbnail(name, prefs)
} else {
let uri = request_uri.trim_end_matches('/').to_owned();
Route::SendPronounPage(name, prefs, uri)
match route_type {
RouteType::Thumbnail => Route::SendThumbnail(name, prefs),
RouteType::Html => {
let uri = request_uri.trim_end_matches('/').to_owned();
Route::SendPronounPage(name, prefs, uri)
}
RouteType::Json => Route::SendJsonData(name, prefs),
}
}
@ -288,6 +299,11 @@ enum Route {
/// Takes the user's name (optional), and the preferences of that user
SendThumbnail(Option<String>, Option<UserPreferences>),
/// Respond with a json object containing the users pronouns
///
/// Takes the user's name (optional), and the preferences of that user
SendJsonData(Option<String>, Option<UserPreferences>),
/// Generate a link to the appropriate pronoun page based on the user's inputs.
GenerateLink(Vec<u8>),
@ -325,6 +341,8 @@ impl Route {
Route::SendStatic(data) => Ok(data.generate_response()),
Route::SendPronounPage(name, prefs, url) =>
Route::send_pronoun_page(name, prefs, url, conf),
Route::SendJsonData(name, prefs) =>
Route::send_pronoun_json(name, prefs, conf),
Route::SendThumbnail(name, prefs) =>
Route::send_thumbnail(name, prefs, settings),
Route::GenerateLink(body) =>
@ -370,6 +388,41 @@ impl Route {
})
}
/// The method for the [`Route::SendPronounPage`] route
fn send_pronoun_json(
name: Option<String>,
prefs: Option<UserPreferences>,
conf: &Conf,
) -> Result<Response, BadRequest> {
let settings = &conf.instance_settings;
let pronoun = Route::get_pronoun(name.as_ref(), prefs, settings)?;
let body = format!(r#"
{{
"subject_pronoun": "{}",
"object_pronoun": "{}",
"possesive_determiner": "{}",
"possesive_pronoun": "{}",
"reflexive_pronoun": "{}"
}}
"#,
pronoun.subject_pronoun,
pronoun.object_pronoun,
pronoun.possesive_determiner,
pronoun.possesive_pronoun,
pronoun.reflexive_pronoun);
Ok(Response {
status: b"200",
headers: Cow::Borrowed(&[
(b"Trans-Women", Cow::Borrowed(b"don't owe you femininity")),
(b"Trans-Men", Cow::Borrowed(b"don't owe you masculinity")),
(b"And-Stop-Projecting-Your-Dated-Gender-Norms", Cow::Borrowed(b"onto nonbinary people's life experiences")),
]),
body: body.into_bytes().into(),
})
}
#[cfg(feature = "ogp_images")]
/// The method for the [`Route::SendThumbnail`] route
fn send_thumbnail(