aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristine Dodrill <me@christine.website>2021-07-05 20:12:42 -0400
committerChristine Dodrill <me@christine.website>2021-07-05 20:12:51 -0400
commit8ce69f76c79f17c5a0fea1c5d96cd006da8171d0 (patch)
treea9dc888be43afc401e9288379ed9e65defea3cdf
parent3c786a1c1fba1155eae9a066887479f4245e2b92 (diff)
downloadxesite-8ce69f76c79f17c5a0fea1c5d96cd006da8171d0.tar.xz
xesite-8ce69f76c79f17c5a0fea1c5d96cd006da8171d0.zip
make new_post route for the android widget
Signed-off-by: Christine Dodrill <me@christine.website>
-rw-r--r--src/handlers/feeds.rs10
-rw-r--r--src/main.rs5
-rw-r--r--src/post/mod.rs44
3 files changed, 57 insertions, 2 deletions
diff --git a/src/handlers/feeds.rs b/src/handlers/feeds.rs
index d3f0289..3d540f6 100644
--- a/src/handlers/feeds.rs
+++ b/src/handlers/feeds.rs
@@ -1,5 +1,5 @@
use super::LAST_MODIFIED;
-use crate::{app::State, templates};
+use crate::{app::State, post::Post, templates};
use lazy_static::lazy_static;
use prometheus::{opts, register_int_counter_vec, IntCounterVec};
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))
}
+#[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)]
pub enum RenderError {
Build(warp::http::Error),
diff --git a/src/main.rs b/src/main.rs
index cac19cf..0576f97 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -44,6 +44,9 @@ async fn main() -> Result<()> {
);
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 blog_index = base
@@ -164,7 +167,7 @@ async fn main() -> Result<()> {
.or(patrons)
.or(jsonfeed.or(atom.or(sitemap)).or(rss))
.or(favicon.or(robots).or(sw))
- .or(contact)
+ .or(contact.or(new_post))
.map(|reply| {
warp::reply::with_header(
reply,
diff --git a/src/post/mod.rs b/src/post/mod.rs
index b5303a8..4c54404 100644
--- a/src/post/mod.rs
+++ b/src/post/mod.rs
@@ -1,6 +1,7 @@
use chrono::prelude::*;
use color_eyre::eyre::{eyre, Result, WrapErr};
use glob::glob;
+use serde::Serialize;
use std::{cmp::Ordering, path::PathBuf};
use tokio::fs;
@@ -13,6 +14,15 @@ pub struct Post {
pub body_html: String,
pub date: DateTime<FixedOffset>,
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 {
@@ -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> {
let body = fs::read_to_string(fname.clone())
.await
@@ -96,12 +133,19 @@ async fn read_post(dir: &str, fname: PathBuf) -> Result<Post> {
Err(_) => vec![],
};
+ let new_post = NewPost {
+ title: front_matter.title.clone(),
+ summary: trim(body).to_string(),
+ link: format!("https://christine.website/{}", link),
+ };
+
Ok(Post {
front_matter,
link,
body_html,
date,
mentions,
+ new_post,
})
}