From: Felix Ableitner Date: Sun, 1 Dec 2019 19:01:38 +0000 (+0100) Subject: Add feed for all X-Git-Url: http://these/git/%7B%60%24%7BwebArchiveUrl%7D/%22%7B%7D/readmes/%22https:/hacktivis.me/%7Bthis.props.icon%7D?a=commitdiff_plain;h=8e5c02f96745d7c30d9636124bbd72dae2f31d4b;p=lemmy.git Add feed for all --- diff --git a/server/src/feeds.rs b/server/src/feeds.rs index f711aced..4cd3d9cb 100644 --- a/server/src/feeds.rs +++ b/server/src/feeds.rs @@ -15,20 +15,47 @@ use diesel::result::Error; use std::str::FromStr; use self::rss::Guid; use serde::Deserialize; +use strum::ParseError; #[derive(Deserialize)] pub struct Params { sort: Option, } +enum RequestType { + All, + Community, + User, +} + +pub fn get_all_feed(info: web::Query) -> HttpResponse { + let sort_type = get_sort_type(info); + if sort_type.is_err() { + return HttpResponse::BadRequest().finish(); + } + + let result = get_feed_internal(&sort_type.unwrap(), RequestType::All, None); + return match result { + Ok(rss) => HttpResponse::Ok() + .content_type("application/rss+xml") + .body(rss), + Err(_) => HttpResponse::InternalServerError().finish(), + }; +} + pub fn get_feed(path: web::Path<(char, String)>, info: web::Query) -> HttpResponse { - let sort_query = info.sort.clone().unwrap_or(SortType::Hot.to_string()); - let sort_type: SortType = match SortType::from_str(&sort_query) { - Ok(sort) => sort, - Err(_) => return HttpResponse::BadRequest().finish(), + let sort_type = get_sort_type(info); + if sort_type.is_err() { + return HttpResponse::BadRequest().finish(); + } + + let request_type = match path.0 { + 'u' => RequestType::User, + 'c' => RequestType::Community, + _ => return HttpResponse::NotFound().finish(), }; - let result = get_feed_internal(path, &sort_type); + let result = get_feed_internal(&sort_type.unwrap(), request_type, Some(path.1.clone())); if result.is_ok() { let rss = result.unwrap(); return HttpResponse::Ok() @@ -43,15 +70,21 @@ pub fn get_feed(path: web::Path<(char, String)>, info: web::Query) -> Ht } } -fn get_feed_internal(info: web::Path<(char, String)>, sort_type: &SortType) -> Result { +fn get_sort_type(info: web::Query) -> Result { + let sort_query = info.sort.clone().unwrap_or(SortType::Hot.to_string()); + return SortType::from_str(&sort_query); +} + +fn get_feed_internal(sort_type: &SortType, request_type: RequestType, name: Option) + -> Result { let conn = establish_connection(); let mut community_id: Option = None; let mut creator_id: Option = None; - match info.0 { - 'c' => community_id = Some(Community::read_from_name(&conn,info.1.clone())?.id), - 'u' => creator_id = Some(User_::find_by_email_or_username(&conn,&info.1)?.id), - _ => return Err(Error::NotFound), + match request_type { + RequestType::All =>(), + RequestType::Community => community_id = Some(Community::read_from_name(&conn,name.unwrap())?.id), + RequestType::User => creator_id = Some(User_::find_by_email_or_username(&conn,&name.unwrap())?.id), } let post = PostView::list(&conn, diff --git a/server/src/main.rs b/server/src/main.rs index e5536647..976f5f5d 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -206,7 +206,7 @@ fn main() { .route("/feeds/{type}/{name}.xml", web::get().to(feeds::get_feed)) // TODO: probably need a different function for this (or just handle all of /feeds? // TODO: would be nice to use ListingType, but that doesnt include user - .route("/feeds/all.xml", web::get().to(feeds::get_feed)) + .route("/feeds/all.xml", web::get().to(feeds::get_all_feed)) // static resources .service(actix_files::Files::new("/static", front_end_dir())) })