Add post-slurping (get markdown source for post)
This commit is contained in:
		
							parent
							
								
									f1a0944688
								
							
						
					
					
						commit
						51a8e1ff88
					
				
							
								
								
									
										43
									
								
								src/main.rs
								
								
								
								
							
							
						
						
									
										43
									
								
								src/main.rs
								
								
								
								
							| 
						 | 
					@ -12,7 +12,7 @@ mod cohost_posts;
 | 
				
			||||||
mod syndication;
 | 
					mod syndication;
 | 
				
			||||||
mod webfinger;
 | 
					mod webfinger;
 | 
				
			||||||
use cohost_account::{CohostAccount, COHOST_ACCOUNT_API_URL};
 | 
					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;
 | 
					use webfinger::CohostWebfingerResource;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Parser, Debug)]
 | 
					#[derive(Parser, Debug)]
 | 
				
			||||||
| 
						 | 
					@ -46,6 +46,12 @@ fn index() -> RawHtml<&'static str> {
 | 
				
			||||||
    RawHtml(include_str!("../static/index.html"))
 | 
					    RawHtml(include_str!("../static/index.html"))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[derive(Responder)]
 | 
				
			||||||
 | 
					#[response(content_type = "text/markdown")]
 | 
				
			||||||
 | 
					struct MdResponse {
 | 
				
			||||||
 | 
					    inner: String,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Responder)]
 | 
					#[derive(Responder)]
 | 
				
			||||||
#[response(content_type = "application/rss+xml")]
 | 
					#[response(content_type = "application/rss+xml")]
 | 
				
			||||||
struct RssResponse {
 | 
					struct RssResponse {
 | 
				
			||||||
| 
						 | 
					@ -61,6 +67,28 @@ enum ErrorResponse {
 | 
				
			||||||
    InternalError(String),
 | 
					    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(
 | 
					async fn get_full_post_data(
 | 
				
			||||||
    client: &mut Client,
 | 
					    client: &mut Client,
 | 
				
			||||||
    project_id: &str,
 | 
					    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(
 | 
					async fn get_project_data(
 | 
				
			||||||
    client: &mut Client,
 | 
					    client: &mut Client,
 | 
				
			||||||
    project_id: &str,
 | 
					    project_id: &str,
 | 
				
			||||||
| 
						 | 
					@ -213,7 +252,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
 | 
				
			||||||
    let _rocket = rocket::build()
 | 
					    let _rocket = rocket::build()
 | 
				
			||||||
        .mount(
 | 
					        .mount(
 | 
				
			||||||
            &ARGS.base_url,
 | 
					            &ARGS.base_url,
 | 
				
			||||||
            routes![index, webfinger_route, syndication_rss_route],
 | 
					            routes![index, webfinger_route, syndication_rss_route, post_md_route],
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
        .ignite()
 | 
					        .ignite()
 | 
				
			||||||
        .await?
 | 
					        .await?
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -30,6 +30,11 @@
 | 
				
			||||||
        Go to <code>/project_name/feed.rss</code> to get a feed for a project.
 | 
					        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.
 | 
					        For example, <a href="/noracodes/feed.rss"><code>/noracodes/feed.rss</code></a> will give you the feed for my page.
 | 
				
			||||||
    </p>
 | 
					    </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>
 | 
					    <p>
 | 
				
			||||||
        Webfinger resources for accounts are provided at the Webfinger well-known URL <code>/.well-known/webfinger?project_name</code>.
 | 
					        Webfinger resources for accounts are provided at the Webfinger well-known URL <code>/.well-known/webfinger?project_name</code>.
 | 
				
			||||||
    </p>
 | 
					    </p>
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue