basic, working rss feeds
This commit is contained in:
parent
8029981a05
commit
45df3425d1
3 changed files with 62 additions and 39 deletions
2
docker/dev/Dockerfile
vendored
2
docker/dev/Dockerfile
vendored
|
@ -21,7 +21,7 @@ COPY server/Cargo.toml server/Cargo.lock ./
|
||||||
RUN sudo chown -R rust:rust .
|
RUN sudo chown -R rust:rust .
|
||||||
RUN mkdir -p ./src/bin \
|
RUN mkdir -p ./src/bin \
|
||||||
&& echo 'fn main() { println!("Dummy") }' > ./src/bin/main.rs
|
&& echo 'fn main() { println!("Dummy") }' > ./src/bin/main.rs
|
||||||
RUN cargo build --releasecargo
|
RUN cargo build --release
|
||||||
RUN rm -f ./target/x86_64-unknown-linux-musl/release/deps/lemmy_server*
|
RUN rm -f ./target/x86_64-unknown-linux-musl/release/deps/lemmy_server*
|
||||||
COPY server/src ./src/
|
COPY server/src ./src/
|
||||||
COPY server/migrations ./migrations/
|
COPY server/migrations ./migrations/
|
||||||
|
|
|
@ -1,28 +1,46 @@
|
||||||
extern crate rss;
|
extern crate rss;
|
||||||
extern crate htmlescape;
|
extern crate htmlescape;
|
||||||
|
|
||||||
use rss::ChannelBuilder;
|
use super::*;
|
||||||
use rss::ItemBuilder;
|
|
||||||
use actix_web::HttpResponse;
|
|
||||||
use actix_web::body::Body;
|
|
||||||
use crate::Settings;
|
use crate::Settings;
|
||||||
use crate::db::{establish_connection, ListingType, SortType};
|
use crate::db::{establish_connection, ListingType, SortType, Crud};
|
||||||
use crate::db::community_view::SiteView;
|
use crate::db::community_view::SiteView;
|
||||||
use crate::db::post_view::PostView;
|
use crate::db::post_view::PostView;
|
||||||
use self::rss::Item;
|
use crate::db::user::User_;
|
||||||
|
use crate::db::community::Community;
|
||||||
|
use actix_web::{HttpResponse, web, Result};
|
||||||
|
use actix_web::body::Body;
|
||||||
|
use rss::{ChannelBuilder, Item, ItemBuilder};
|
||||||
|
use diesel::result::Error;
|
||||||
|
|
||||||
pub fn get_feed() -> HttpResponse<Body> {
|
pub fn get_feed(info: web::Path<(char, String)>) -> HttpResponse<Body> {
|
||||||
|
return match get_feed_internal(info) {
|
||||||
|
Ok(body) => HttpResponse::Ok()
|
||||||
|
.content_type("application/rss+xml")
|
||||||
|
.body(body),
|
||||||
|
// TODO: handle the specific type of error (403, 500, etc)
|
||||||
|
Err(e) => HttpResponse::InternalServerError().finish(),
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_feed_internal(info: web::Path<(char, String)>) -> Result<String, Error> {
|
||||||
let conn = establish_connection();
|
let conn = establish_connection();
|
||||||
let site_view = match SiteView::read(&conn) {
|
|
||||||
Ok(site_view) => site_view,
|
|
||||||
Err(_e) => return HttpResponse::InternalServerError().finish(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let post = match PostView::list(&conn,
|
let mut community_id: Option<i32> = None;
|
||||||
|
let mut creator_id: Option<i32> = None;
|
||||||
|
// TODO: add a feed for /type/all
|
||||||
|
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),
|
||||||
|
}
|
||||||
|
|
||||||
|
let post = PostView::list(&conn,
|
||||||
ListingType::All,
|
ListingType::All,
|
||||||
&SortType::New,
|
&SortType::New,
|
||||||
None,
|
community_id,
|
||||||
None,
|
creator_id,
|
||||||
None,
|
None,
|
||||||
None,
|
None,
|
||||||
None,
|
None,
|
||||||
|
@ -30,35 +48,40 @@ pub fn get_feed() -> HttpResponse<Body> {
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
None,
|
None,
|
||||||
None,) {
|
None,)?;
|
||||||
Ok(post) => post,
|
|
||||||
Err(_e) => return HttpResponse::InternalServerError().finish(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut items: Vec<Item> = Vec::new();
|
let mut items: Vec<Item> = Vec::new();
|
||||||
for p in post {
|
for p in post {
|
||||||
let i = ItemBuilder::default()
|
// TODO: this may cause a lot of db queries
|
||||||
.title(p.name)
|
let user = User_::read(&conn, p.creator_id)?;
|
||||||
.link(p.url)
|
let dt = DateTime::<Utc>::from_utc(p.published, Utc);
|
||||||
.content(p.body)
|
let mut i = ItemBuilder::default();
|
||||||
.author(p.creator_id)
|
i.title(htmlescape::encode_minimal(&p.name));
|
||||||
.pub_date(p.published)
|
i.author(htmlescape::encode_minimal(&user.name));
|
||||||
.build()
|
i.pub_date(htmlescape::encode_minimal(&dt.to_rfc2822()));
|
||||||
.unwrap();
|
if p.url.is_some() {
|
||||||
items.append(&i);
|
i.link(p.url.unwrap());
|
||||||
|
}
|
||||||
|
if p.body.is_some() {
|
||||||
|
i.content(p.body.unwrap());
|
||||||
|
}
|
||||||
|
// TODO: any other fields?
|
||||||
|
// https://rust-syndication.github.io/rss/rss/struct.ItemBuilder.html
|
||||||
|
items.push(i.build().unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
let channel = ChannelBuilder::default()
|
let site_view = SiteView::read(&conn)?;
|
||||||
.title(htmlescape::encode_minimal(&site_view.name))
|
let mut channel_builder = ChannelBuilder::default();
|
||||||
|
channel_builder.title(htmlescape::encode_minimal(&site_view.name))
|
||||||
.link(format!("https://{}", Settings::get().hostname))
|
.link(format!("https://{}", Settings::get().hostname))
|
||||||
.description(htmlescape::encode_minimal(&site_view.description.unwrap()))
|
.items(items);
|
||||||
.pub_date("asd")
|
if site_view.description.is_some() {
|
||||||
.items(items)
|
channel_builder.description(htmlescape::encode_minimal(&site_view.description.unwrap()));
|
||||||
.build()
|
}
|
||||||
.unwrap();
|
// TODO: any other fields?
|
||||||
|
// https://rust-syndication.github.io/rss/rss/struct.ChannelBuilder.html
|
||||||
|
let channel = channel_builder.build().unwrap();
|
||||||
channel.write_to(::std::io::sink()).unwrap();
|
channel.write_to(::std::io::sink()).unwrap();
|
||||||
|
|
||||||
return HttpResponse::Ok()
|
return Ok(channel.to_string());
|
||||||
.content_type("application/rss+xml")
|
|
||||||
.body(channel.to_string());
|
|
||||||
}
|
}
|
|
@ -206,7 +206,7 @@ fn main() {
|
||||||
"/.well-known/nodeinfo",
|
"/.well-known/nodeinfo",
|
||||||
web::get().to(nodeinfo::node_info_well_known),
|
web::get().to(nodeinfo::node_info_well_known),
|
||||||
)
|
)
|
||||||
.route("/feed.xml", web::get().to(feeds::get_feed))
|
.route("/feeds/{type}/{name}.xml", web::get().to(feeds::get_feed))
|
||||||
// static resources
|
// static resources
|
||||||
.service(actix_files::Files::new("/static", front_end_dir()))
|
.service(actix_files::Files::new("/static", front_end_dir()))
|
||||||
})
|
})
|
||||||
|
|
Reference in a new issue