From: Felix Ableitner Date: Sun, 1 Dec 2019 18:09:10 +0000 (+0100) Subject: fix sort parameter handling, handle errors X-Git-Url: http://these/git/%22https:/image.com/readmes/%7B%60%24%7BarchiveUrl%7D/%7Bsrc%7D?a=commitdiff_plain;h=cfbe8ac926c4a196bf31692cf634adae1f45e079;p=lemmy.git fix sort parameter handling, handle errors --- diff --git a/server/src/feeds.rs b/server/src/feeds.rs index bf5b802f..f711aced 100644 --- a/server/src/feeds.rs +++ b/server/src/feeds.rs @@ -8,29 +8,38 @@ use crate::db::community_view::SiteView; use crate::db::post_view::PostView; use crate::db::user::User_; use crate::db::community::Community; -use actix_web::{HttpResponse, web, Result, HttpRequest}; +use actix_web::{HttpResponse, web, Result}; use actix_web::body::Body; use rss::{ChannelBuilder, Item, ItemBuilder}; use diesel::result::Error; use std::str::FromStr; use self::rss::Guid; +use serde::Deserialize; -pub fn get_feed(path: web::Path<(char, String)>, req: HttpRequest) -> HttpResponse { - let sort_query = match req.match_info().query("sort").parse() { - Ok(param) => param, - Err(_) => SortType::Hot.to_string(), - }; - let sort_type = match SortType::from_str(&sort_query) { +#[derive(Deserialize)] +pub struct Params { + sort: Option, +} + +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(), }; - return match get_feed_internal(path, &sort_type) { - Ok(body) => HttpResponse::Ok() + let result = get_feed_internal(path, &sort_type); + if result.is_ok() { + let rss = result.unwrap(); + return HttpResponse::Ok() .content_type("application/rss+xml") - .body(body), - // TODO: handle the specific type of error (403, 500, etc) - Err(_) => HttpResponse::InternalServerError().finish(), + .body(rss); + } else { + let error = result.err().unwrap(); + return match error { + Error::NotFound => HttpResponse::NotFound().finish(), + _ => HttpResponse::InternalServerError().finish(), + } } } @@ -66,7 +75,6 @@ fn get_feed_internal(info: web::Path<(char, String)>, sort_type: &SortType) -> R i.title(htmlescape::encode_minimal(&p.name)); i.pub_date(htmlescape::encode_minimal(&dt.to_rfc2822())); - // TODO: there is probably a better way to get the lemmy post url let post_url = format!("https://{}/post/{}", Settings::get().hostname, p.id); let mut guid = Guid::default(); guid.set_permalink(true); diff --git a/server/src/main.rs b/server/src/main.rs index 6d1bf3c9..e5536647 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -7,11 +7,8 @@ use actix_files::NamedFile; use actix_web::*; use actix_web_actors::ws; use lemmy_server::db::establish_connection; -<<<<<<< HEAD use lemmy_server::nodeinfo; -======= use lemmy_server::feeds; ->>>>>>> Implement RSS feeds (fixes #118) use lemmy_server::websocket::server::*; use std::env; use std::time::{Duration, Instant};