|
|
@ -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? |
|
|
|