fix sort parameter handling, handle errors

This commit is contained in:
Felix Ableitner 2019-12-01 19:09:10 +01:00
parent 2f2c5436c5
commit 49fea8a883
2 changed files with 21 additions and 16 deletions

View file

@ -8,29 +8,38 @@ use crate::db::community_view::SiteView;
use crate::db::post_view::PostView; use crate::db::post_view::PostView;
use crate::db::user::User_; use crate::db::user::User_;
use crate::db::community::Community; use crate::db::community::Community;
use actix_web::{HttpResponse, web, Result, HttpRequest}; use actix_web::{HttpResponse, web, Result};
use actix_web::body::Body; use actix_web::body::Body;
use rss::{ChannelBuilder, Item, ItemBuilder}; use rss::{ChannelBuilder, Item, ItemBuilder};
use diesel::result::Error; use diesel::result::Error;
use std::str::FromStr; use std::str::FromStr;
use self::rss::Guid; use self::rss::Guid;
use serde::Deserialize;
pub fn get_feed(path: web::Path<(char, String)>, req: HttpRequest) -> HttpResponse<Body> { #[derive(Deserialize)]
let sort_query = match req.match_info().query("sort").parse() { pub struct Params {
Ok(param) => param, sort: Option<String>,
Err(_) => SortType::Hot.to_string(), }
};
let sort_type = match SortType::from_str(&sort_query) { pub fn get_feed(path: web::Path<(char, String)>, info: web::Query<Params>) -> HttpResponse<Body> {
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, Ok(sort) => sort,
Err(_) => return HttpResponse::BadRequest().finish(), Err(_) => return HttpResponse::BadRequest().finish(),
}; };
return match get_feed_internal(path, &sort_type) { let result = get_feed_internal(path, &sort_type);
Ok(body) => HttpResponse::Ok() if result.is_ok() {
let rss = result.unwrap();
return HttpResponse::Ok()
.content_type("application/rss+xml") .content_type("application/rss+xml")
.body(body), .body(rss);
// TODO: handle the specific type of error (403, 500, etc) } else {
Err(_) => HttpResponse::InternalServerError().finish(), 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.title(htmlescape::encode_minimal(&p.name));
i.pub_date(htmlescape::encode_minimal(&dt.to_rfc2822())); 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 post_url = format!("https://{}/post/{}", Settings::get().hostname, p.id);
let mut guid = Guid::default(); let mut guid = Guid::default();
guid.set_permalink(true); guid.set_permalink(true);

View file

@ -7,11 +7,8 @@ use actix_files::NamedFile;
use actix_web::*; use actix_web::*;
use actix_web_actors::ws; use actix_web_actors::ws;
use lemmy_server::db::establish_connection; use lemmy_server::db::establish_connection;
<<<<<<< HEAD
use lemmy_server::nodeinfo; use lemmy_server::nodeinfo;
=======
use lemmy_server::feeds; use lemmy_server::feeds;
>>>>>>> Implement RSS feeds (fixes #118)
use lemmy_server::websocket::server::*; use lemmy_server::websocket::server::*;
use std::env; use std::env;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};