[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:
parent
4cef709f52
commit
feac079f59
|
@ -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
|
// Determines if we need to respond with a thumbnail or a web page
|
||||||
let is_thumb =
|
let route_type = match segments.last().map(Deref::deref) {
|
||||||
!segments.is_empty() &&
|
Some("thumb.png") => RouteType::Thumbnail,
|
||||||
segments.last().map(Deref::deref) == Some("thumb.png");
|
Some("pns.json") => RouteType::Json,
|
||||||
if is_thumb {
|
_ => RouteType::Html,
|
||||||
|
};
|
||||||
|
if route_type != RouteType::Html {
|
||||||
// Now that we've determined that, we don't need this segment anymore
|
// Now that we've determined that, we don't need this segment anymore
|
||||||
segments.pop();
|
segments.pop();
|
||||||
}
|
}
|
||||||
|
@ -245,12 +254,14 @@ fn route_request(req: &ScgiRequest) -> Route {
|
||||||
|
|
||||||
let name = name.map(|n| (*n).to_owned());
|
let name = name.map(|n| (*n).to_owned());
|
||||||
|
|
||||||
if is_thumb {
|
match route_type {
|
||||||
Route::SendThumbnail(name, prefs)
|
RouteType::Thumbnail => Route::SendThumbnail(name, prefs),
|
||||||
} else {
|
RouteType::Html => {
|
||||||
let uri = request_uri.trim_end_matches('/').to_owned();
|
let uri = request_uri.trim_end_matches('/').to_owned();
|
||||||
Route::SendPronounPage(name, prefs, uri)
|
Route::SendPronounPage(name, prefs, uri)
|
||||||
}
|
}
|
||||||
|
RouteType::Json => Route::SendJsonData(name, prefs),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Determines the type of route that the request should be met with, and necessary data
|
/// Determines the type of route that the request should be met with, and necessary data
|
||||||
|
@ -288,6 +299,11 @@ enum Route {
|
||||||
/// Takes the user's name (optional), and the preferences of that user
|
/// Takes the user's name (optional), and the preferences of that user
|
||||||
SendThumbnail(Option<String>, Option<UserPreferences>),
|
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.
|
/// Generate a link to the appropriate pronoun page based on the user's inputs.
|
||||||
GenerateLink(Vec<u8>),
|
GenerateLink(Vec<u8>),
|
||||||
|
|
||||||
|
@ -325,6 +341,8 @@ impl Route {
|
||||||
Route::SendStatic(data) => Ok(data.generate_response()),
|
Route::SendStatic(data) => Ok(data.generate_response()),
|
||||||
Route::SendPronounPage(name, prefs, url) =>
|
Route::SendPronounPage(name, prefs, url) =>
|
||||||
Route::send_pronoun_page(name, prefs, url, conf),
|
Route::send_pronoun_page(name, prefs, url, conf),
|
||||||
|
Route::SendJsonData(name, prefs) =>
|
||||||
|
Route::send_pronoun_json(name, prefs, conf),
|
||||||
Route::SendThumbnail(name, prefs) =>
|
Route::SendThumbnail(name, prefs) =>
|
||||||
Route::send_thumbnail(name, prefs, settings),
|
Route::send_thumbnail(name, prefs, settings),
|
||||||
Route::GenerateLink(body) =>
|
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")]
|
#[cfg(feature = "ogp_images")]
|
||||||
/// The method for the [`Route::SendThumbnail`] route
|
/// The method for the [`Route::SendThumbnail`] route
|
||||||
fn send_thumbnail(
|
fn send_thumbnail(
|
||||||
|
|
Loading…
Reference in a new issue