lemmy/server/src/feeds.rs

139 lines
3.9 KiB
Rust
Raw Normal View History

2019-11-16 02:17:42 +00:00
extern crate rss;
extern crate htmlescape;
2019-11-19 17:07:10 +00:00
use super::*;
2019-11-16 02:17:42 +00:00
use crate::Settings;
use crate::db::{establish_connection, ListingType, SortType};
2019-11-16 02:17:42 +00:00
use crate::db::community_view::SiteView;
use crate::db::post_view::PostView;
2019-11-19 17:07:10 +00:00
use crate::db::user::User_;
use crate::db::community::Community;
use actix_web::{HttpResponse, web, Result};
2019-11-19 17:07:10 +00:00
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;
2019-12-01 19:01:38 +00:00
use strum::ParseError;
2019-11-19 17:07:10 +00:00
#[derive(Deserialize)]
pub struct Params {
sort: Option<String>,
}
2019-12-01 19:01:38 +00:00
enum RequestType {
All,
Community,
User,
}
pub fn get_all_feed(info: web::Query<Params>) -> HttpResponse<Body> {
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<Params>) -> HttpResponse<Body> {
2019-12-01 19:01:38 +00:00
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(),
};
2019-12-01 19:01:38 +00:00
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()
2019-11-19 17:07:10 +00:00
.content_type("application/rss+xml")
.body(rss);
} else {
let error = result.err().unwrap();
return match error {
Error::NotFound => HttpResponse::NotFound().finish(),
_ => HttpResponse::InternalServerError().finish(),
}
2019-11-19 17:07:10 +00:00
}
}
2019-11-16 02:17:42 +00:00
2019-12-01 19:01:38 +00:00
fn get_sort_type(info: web::Query<Params>) -> Result<SortType, ParseError> {
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<String>)
-> Result<String, Error> {
2019-11-16 02:17:42 +00:00
let conn = establish_connection();
2019-11-19 17:07:10 +00:00
let mut community_id: Option<i32> = None;
let mut creator_id: Option<i32> = None;
2019-12-01 19:01:38 +00:00
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),
2019-11-19 17:07:10 +00:00
}
let post = PostView::list(&conn,
2019-11-16 02:17:42 +00:00
ListingType::All,
sort_type,
2019-11-19 17:07:10 +00:00
community_id,
creator_id,
2019-11-16 02:17:42 +00:00
None,
None,
None,
true,
false,
false,
None,
2019-11-19 17:07:10 +00:00
None,)?;
2019-11-16 02:17:42 +00:00
let mut items: Vec<Item> = Vec::new();
for p in post {
2019-11-19 17:07:10 +00:00
let dt = DateTime::<Utc>::from_utc(p.published, Utc);
let mut i = ItemBuilder::default();
i.title(htmlescape::encode_minimal(&p.name));
i.pub_date(htmlescape::encode_minimal(&dt.to_rfc2822()));
let post_url = format!("https://{}/post/{}", Settings::get().hostname, p.id);
let mut guid = Guid::default();
guid.set_permalink(true);
guid.set_value(&post_url);
i.guid(guid);
i.comments(post_url);
2019-11-19 17:07:10 +00:00
if p.url.is_some() {
i.link(p.url.unwrap());
}
if p.body.is_some() {
i.content(p.body.unwrap());
}
items.push(i.build().unwrap());
2019-11-16 02:17:42 +00:00
}
2019-11-19 17:07:10 +00:00
let site_view = SiteView::read(&conn)?;
let mut channel_builder = ChannelBuilder::default();
channel_builder.title(htmlescape::encode_minimal(&site_view.name))
2019-11-16 02:17:42 +00:00
.link(format!("https://{}", Settings::get().hostname))
2019-11-19 17:07:10 +00:00
.items(items);
if site_view.description.is_some() {
channel_builder.description(htmlescape::encode_minimal(&site_view.description.unwrap()));
}
let channel = channel_builder.build().unwrap();
2019-11-16 02:17:42 +00:00
channel.write_to(::std::io::sink()).unwrap();
2019-11-19 17:07:10 +00:00
return Ok(channel.to_string());
2019-11-16 02:17:42 +00:00
}