make new_post route for the android widget

Signed-off-by: Christine Dodrill <me@christine.website>
This commit is contained in:
Cadey Ratio 2021-07-05 20:12:42 -04:00
parent 3c786a1c1f
commit 8ce69f76c7
3 changed files with 57 additions and 2 deletions

View File

@ -1,5 +1,5 @@
use super::LAST_MODIFIED; use super::LAST_MODIFIED;
use crate::{app::State, templates}; use crate::{app::State, post::Post, templates};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use prometheus::{opts, register_int_counter_vec, IntCounterVec}; use prometheus::{opts, register_int_counter_vec, IntCounterVec};
use std::{io, sync::Arc}; use std::{io, sync::Arc};
@ -22,6 +22,14 @@ pub async fn jsonfeed(state: Arc<State>, since: Option<String>) -> Result<impl R
Ok(warp::reply::json(&state.jf)) Ok(warp::reply::json(&state.jf))
} }
#[instrument(skip(state))]
pub async fn new_post(state: Arc<State>) -> Result<impl Reply, Rejection> {
let state = state.clone();
let everything = state.everything.clone();
let p: &Post = everything.iter().next().unwrap();
Ok(warp::reply::json(&p.new_post))
}
#[derive(Debug)] #[derive(Debug)]
pub enum RenderError { pub enum RenderError {
Build(warp::http::Error), Build(warp::http::Error),

View File

@ -44,6 +44,9 @@ async fn main() -> Result<()> {
); );
let healthcheck = warp::get().and(warp::path(".within").and(warp::path("health")).map(|| "OK")); let healthcheck = warp::get().and(warp::path(".within").and(warp::path("health")).map(|| "OK"));
let new_post = warp::path!(".within" / "website.within.xesite" / "new_post")
.and(with_state(state.clone()))
.and_then(handlers::feeds::new_post);
let base = warp::path!("blog" / ..); let base = warp::path!("blog" / ..);
let blog_index = base let blog_index = base
@ -164,7 +167,7 @@ async fn main() -> Result<()> {
.or(patrons) .or(patrons)
.or(jsonfeed.or(atom.or(sitemap)).or(rss)) .or(jsonfeed.or(atom.or(sitemap)).or(rss))
.or(favicon.or(robots).or(sw)) .or(favicon.or(robots).or(sw))
.or(contact) .or(contact.or(new_post))
.map(|reply| { .map(|reply| {
warp::reply::with_header( warp::reply::with_header(
reply, reply,

View File

@ -1,6 +1,7 @@
use chrono::prelude::*; use chrono::prelude::*;
use color_eyre::eyre::{eyre, Result, WrapErr}; use color_eyre::eyre::{eyre, Result, WrapErr};
use glob::glob; use glob::glob;
use serde::Serialize;
use std::{cmp::Ordering, path::PathBuf}; use std::{cmp::Ordering, path::PathBuf};
use tokio::fs; use tokio::fs;
@ -13,6 +14,15 @@ pub struct Post {
pub body_html: String, pub body_html: String,
pub date: DateTime<FixedOffset>, pub date: DateTime<FixedOffset>,
pub mentions: Vec<mi::WebMention>, pub mentions: Vec<mi::WebMention>,
pub new_post: NewPost,
}
/// Used with the Android app to show information in a widget.
#[derive(Eq, PartialEq, Debug, Clone, Serialize)]
pub struct NewPost {
pub title: String,
pub summary: String,
pub link: String,
} }
impl Into<jsonfeed::Item> for Post { impl Into<jsonfeed::Item> for Post {
@ -70,6 +80,33 @@ impl Post {
} }
} }
fn trim(string: &str) -> String {
let mut buf = String::new();
let mut capturing = false;
for line in string.lines() {
if line.starts_with("#") {
continue;
}
if line == "" {
if capturing && buf.len() > 260 {
break;
} else {
capturing = true;
continue;
}
}
if capturing {
buf.push_str(" ");
buf.push_str(line);
}
}
buf
}
async fn read_post(dir: &str, fname: PathBuf) -> Result<Post> { async fn read_post(dir: &str, fname: PathBuf) -> Result<Post> {
let body = fs::read_to_string(fname.clone()) let body = fs::read_to_string(fname.clone())
.await .await
@ -96,12 +133,19 @@ async fn read_post(dir: &str, fname: PathBuf) -> Result<Post> {
Err(_) => vec![], Err(_) => vec![],
}; };
let new_post = NewPost {
title: front_matter.title.clone(),
summary: trim(body).to_string(),
link: format!("https://christine.website/{}", link),
};
Ok(Post { Ok(Post {
front_matter, front_matter,
link, link,
body_html, body_html,
date, date,
mentions, mentions,
new_post,
}) })
} }