Add post-slurping (get markdown source for post)

This commit is contained in:
Leonora Tindall 2022-11-01 14:50:14 -05:00
parent f1a0944688
commit 51a8e1ff88
Signed by: nora
GPG Key ID: 7A8B52EC67E09AAF
2 changed files with 46 additions and 2 deletions

View File

@ -12,7 +12,7 @@ mod cohost_posts;
mod syndication;
mod webfinger;
use cohost_account::{CohostAccount, COHOST_ACCOUNT_API_URL};
use cohost_posts::{cohost_posts_api_url, CohostPostsPage};
use cohost_posts::{cohost_posts_api_url, CohostPost, CohostPostsPage};
use webfinger::CohostWebfingerResource;
#[derive(Parser, Debug)]
@ -46,6 +46,12 @@ fn index() -> RawHtml<&'static str> {
RawHtml(include_str!("../static/index.html"))
}
#[derive(Responder)]
#[response(content_type = "text/markdown")]
struct MdResponse {
inner: String,
}
#[derive(Responder)]
#[response(content_type = "application/rss+xml")]
struct RssResponse {
@ -61,6 +67,28 @@ enum ErrorResponse {
InternalError(String),
}
async fn get_post_from_page(
client: &mut Client,
project_id: &str,
post_id: u64,
) -> Result<CohostPost, ErrorResponse> {
let mut page = 0;
loop {
let new_page = get_page_data(client, project_id, page).await?;
if new_page.items.is_empty() {
// Once there are no posts, we're done.
return Err(ErrorResponse::NotFound(
"End of posts reached, ID not found.".into(),
));
} else {
page += 1;
if let Some(post) = new_page.items.into_iter().find(|post| post.id == post_id) {
return Ok(post);
}
}
}
}
async fn get_full_post_data(
client: &mut Client,
project_id: &str,
@ -130,6 +158,17 @@ async fn syndication_rss_route(project: &str) -> Result<RssResponse, ErrorRespon
})
}
#[get("/<project>/<id>")]
async fn post_md_route(project: &str, id: u64) -> Result<MdResponse, ErrorResponse> {
let mut client = get_client()?;
let _project_data = get_project_data(&mut client, project).await?;
let post_data = get_post_from_page(&mut client, project, id).await?;
Ok(MdResponse {
inner: post_data.plain_body,
})
}
async fn get_project_data(
client: &mut Client,
project_id: &str,
@ -213,7 +252,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
let _rocket = rocket::build()
.mount(
&ARGS.base_url,
routes![index, webfinger_route, syndication_rss_route],
routes![index, webfinger_route, syndication_rss_route, post_md_route],
)
.ignite()
.await?

View File

@ -30,6 +30,11 @@
Go to <code>/project_name/feed.rss</code> to get a feed for a project.
For example, <a href="/noracodes/feed.rss"><code>/noracodes/feed.rss</code></a> will give you the feed for my page.
</p>
<p>
You can also get a particular post's original plain-text body at <code>/project_name/post_id/</code>, such as
<a href="/noracodes/169186/"><code>/noracodes/169186/</code></a>. (In a Cohost post URL, the ID is the numerical part after <code>/post/</code>.
For instance, in <code>https://cohost.org/noracodes/post/169186-october-update</code>, the ID is "169186".)
</p>
<p>
Webfinger resources for accounts are provided at the Webfinger well-known URL <code>/.well-known/webfinger?project_name</code>.
</p>