Add pagination to the blog page

No UI to control it yet
This commit is contained in:
Bat 2018-07-20 18:42:35 +02:00
parent 3764e295b8
commit 67eb41add1
5 changed files with 62 additions and 8 deletions

View file

@ -91,6 +91,15 @@ impl Post {
.expect("Error loading recent posts for blog")
}
pub fn blog_page(conn: &PgConnection, blog: &Blog, (min, max): (i32, i32)) -> Vec<Post> {
posts::table.filter(posts::blog_id.eq(blog.id))
.order(posts::creation_date.desc())
.offset(min.into())
.limit((max - min).into())
.load::<Post>(conn)
.expect("Error loading a page of posts for blog")
}
pub fn get_authors(&self, conn: &PgConnection) -> Vec<User> {
use schema::users;
use schema::post_authors;

View file

@ -36,6 +36,7 @@ fn main() {
let pool = setup::check();
rocket::ignite()
.mount("/", routes![
routes::blogs::paginated_details,
routes::blogs::details,
routes::blogs::activity_details,
routes::blogs::outbox,

View file

@ -18,24 +18,30 @@ use plume_models::{
posts::Post,
users::User
};
use routes::Page;
#[get("/~/<name>", rank = 2)]
fn details(name: String, conn: DbConn, user: Option<User>) -> Template {
#[get("/~/<name>?<page>", rank = 2)]
fn paginated_details(name: String, conn: DbConn, user: Option<User>, page: Page) -> Template {
may_fail!(user, Blog::find_by_fqn(&*conn, name), "Requested blog couldn't be found", |blog| {
let recents = Post::get_recents_for_blog(&*conn, &blog, 5);
let posts = Post::blog_page(&*conn, &blog, page.limits());
let authors = &blog.list_authors(&*conn);
Template::render("blogs/details", json!({
"blog": &blog,
"account": user,
"is_author": user.map(|x| x.is_author_in(&*conn, blog.clone())),
"recents": recents.into_iter().map(|p| p.to_json(&*conn)).collect::<Vec<serde_json::Value>>(),
"posts": posts.into_iter().map(|p| p.to_json(&*conn)).collect::<Vec<serde_json::Value>>(),
"authors": authors.into_iter().map(|u| u.to_json(&*conn)).collect::<Vec<serde_json::Value>>(),
"n_authors": authors.len()
}))
})
}
#[get("/~/<name>", rank = 3)]
fn details(name: String, conn: DbConn, user: Option<User>) -> Template {
paginated_details(name, conn, user, Page::first())
}
#[get("/~/<name>", rank = 1)]
fn activity_details(name: String, conn: DbConn, _ap: ApRequest) -> ActivityStream<CustomGroup> {
let blog = Blog::find_local(&*conn, name).unwrap();

View file

@ -1,5 +1,11 @@
use rocket::response::NamedFile;
use std::path::{Path, PathBuf};
use rocket::{
http::uri::{FromUriParam, UriDisplay},
response::NamedFile
};
use std::{
fmt,
path::{Path, PathBuf}
};
macro_rules! may_fail {
($account:expr, $expr:expr, $template:expr, $msg:expr, | $res:ident | $block:block) => {
@ -28,6 +34,38 @@ macro_rules! may_fail {
};
}
const ITEMS_PER_PAGE: i32 = 10;
#[derive(FromForm)]
pub struct Page {
page: i32
}
impl UriDisplay for Page {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "page={}", &self.page as &UriDisplay)
}
}
impl FromUriParam<i32> for Page {
type Target = Page;
fn from_uri_param(num: i32) -> Page {
Page { page: num }
}
}
impl Page {
pub fn first() -> Page {
Page {
page: 1
}
}
pub fn limits(&self) -> (i32, i32) {
((self.page - 1) * ITEMS_PER_PAGE, self.page * ITEMS_PER_PAGE)
}
}
pub mod blogs;
pub mod comments;
pub mod errors;

View file

@ -22,14 +22,14 @@
<section>
<h2>{{ "Latest articles" | _ }}</h2>
{% if recents | length < 1 %}
{% if posts | length < 1 %}
<p>{{ "No posts to see here yet." | _ }}</p>
{% endif %}
{% if is_author %}
<a href="new" class="button inline-block">{{ "New article" | _ }}</a>
{% endif %}
<div class="cards">
{% for article in recents %}
{% for article in posts %}
{{ macros::post_card(article=article) }}
{% endfor %}
</div>