Add OGP tags to template

This commit is contained in:
Emi Simpson 2021-10-25 15:48:41 -04:00
parent b1b85e6ec7
commit eb695f27bd
Signed by: Emi
GPG key ID: A12F2C2FFDC3D847
2 changed files with 37 additions and 3 deletions

View file

@ -19,19 +19,24 @@ use image::{DynamicImage, ImageOutputFormat};
#[cfg(feature = "ogp_images")]
use ogp_images::render_today;
// TODO: Make this configurable
const HOSTNAME: &str = "pronouns.today";
#[derive(Template)]
#[template(path = "index.html")]
struct IndexTemplate<'a> {
name: Option<String>,
pronoun: &'a Pronoun,
pronouns: Vec<(usize, &'a Pronoun)>,
url: String,
}
fn render_page(pronoun: &Pronoun, settings: &InstanceSettings, name: Option<String>) -> String {
fn render_page(pronoun: &Pronoun, settings: &InstanceSettings, name: Option<String>, url: String) -> String {
IndexTemplate {
name,
pronoun,
pronouns: settings.pronoun_list.iter().enumerate().collect(),
url,
}
.render()
.unwrap()
@ -62,6 +67,14 @@ async fn create_link(
.finish())
}
fn form_full_url(host: &str, name: Option<&str>, prefstr: Option<&str>) -> String {
["https:/", host].into_iter()
.chain(name)
.chain(prefstr)
.collect::<Vec<&str>>()
.join("/")
}
/// Determine some basic information about a request
///
/// Determines the name and prefstring properties, if available, and also computes the pronoun that
@ -87,11 +100,16 @@ async fn handle_basic_request(
req: HttpRequest,
) -> Result<impl Responder> {
let (_, name, pronoun) = get_request_info(&settings, &req);
let (prefstr, name, pronoun) = get_request_info(&settings, &req);
Ok(HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(render_page(pronoun?, &settings, name.map(str::to_owned))))
.body(render_page(
pronoun?,
&settings,
name.map(str::to_owned),
form_full_url(HOSTNAME, name, prefstr)
)))
}
#[cfg(feature = "ogp_images")]

View file

@ -2,6 +2,22 @@
<head>
<title>{% if name.is_some() %} {{ name.as_ref().unwrap() }}'s {% else %}
My{% endif %} Pronouns Today</title>
<!-- OGP Tags -->
{% if name.is_some() %}
<meta property="og:title" content="{{ name.as_ref().unwrap() }}'s Pronouns Today" />
{% else %}
<meta property="og:title" content="Random Daily Pronouns" />
{% endif %}
<meta property="og:site_name" content="Pronouns.Today" />
<meta property="og:type" content="website" />
<meta property="og:url" content="{{ url }}" />
<meta property="og:image:url" content="{{ url }}/thumb.png" />
<meta property="og:image:type" content="image/png" />
<meta property="og:image:width" content="800" />
<meta property="og:image:height" content="400" />
<meta property="og:image:alt" content="The pronouns {{pronoun.subject_pronoun | capitalize}}/{{pronoun.object_pronoun | capitalize}}" />
<style>
#form {
display: none;