Add pagination to the blog page
No UI to control it yet
This commit is contained in:
parent
3764e295b8
commit
67eb41add1
@ -91,6 +91,15 @@ impl Post {
|
|||||||
.expect("Error loading recent posts for blog")
|
.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> {
|
pub fn get_authors(&self, conn: &PgConnection) -> Vec<User> {
|
||||||
use schema::users;
|
use schema::users;
|
||||||
use schema::post_authors;
|
use schema::post_authors;
|
||||||
|
@ -36,6 +36,7 @@ fn main() {
|
|||||||
let pool = setup::check();
|
let pool = setup::check();
|
||||||
rocket::ignite()
|
rocket::ignite()
|
||||||
.mount("/", routes![
|
.mount("/", routes![
|
||||||
|
routes::blogs::paginated_details,
|
||||||
routes::blogs::details,
|
routes::blogs::details,
|
||||||
routes::blogs::activity_details,
|
routes::blogs::activity_details,
|
||||||
routes::blogs::outbox,
|
routes::blogs::outbox,
|
||||||
|
@ -18,24 +18,30 @@ use plume_models::{
|
|||||||
posts::Post,
|
posts::Post,
|
||||||
users::User
|
users::User
|
||||||
};
|
};
|
||||||
|
use routes::Page;
|
||||||
|
|
||||||
#[get("/~/<name>", rank = 2)]
|
#[get("/~/<name>?<page>", rank = 2)]
|
||||||
fn details(name: String, conn: DbConn, user: Option<User>) -> Template {
|
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| {
|
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);
|
let authors = &blog.list_authors(&*conn);
|
||||||
|
|
||||||
Template::render("blogs/details", json!({
|
Template::render("blogs/details", json!({
|
||||||
"blog": &blog,
|
"blog": &blog,
|
||||||
"account": user,
|
"account": user,
|
||||||
"is_author": user.map(|x| x.is_author_in(&*conn, blog.clone())),
|
"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>>(),
|
"authors": authors.into_iter().map(|u| u.to_json(&*conn)).collect::<Vec<serde_json::Value>>(),
|
||||||
"n_authors": authors.len()
|
"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)]
|
#[get("/~/<name>", rank = 1)]
|
||||||
fn activity_details(name: String, conn: DbConn, _ap: ApRequest) -> ActivityStream<CustomGroup> {
|
fn activity_details(name: String, conn: DbConn, _ap: ApRequest) -> ActivityStream<CustomGroup> {
|
||||||
let blog = Blog::find_local(&*conn, name).unwrap();
|
let blog = Blog::find_local(&*conn, name).unwrap();
|
||||||
|
@ -1,5 +1,11 @@
|
|||||||
use rocket::response::NamedFile;
|
use rocket::{
|
||||||
use std::path::{Path, PathBuf};
|
http::uri::{FromUriParam, UriDisplay},
|
||||||
|
response::NamedFile
|
||||||
|
};
|
||||||
|
use std::{
|
||||||
|
fmt,
|
||||||
|
path::{Path, PathBuf}
|
||||||
|
};
|
||||||
|
|
||||||
macro_rules! may_fail {
|
macro_rules! may_fail {
|
||||||
($account:expr, $expr:expr, $template:expr, $msg:expr, | $res:ident | $block:block) => {
|
($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 blogs;
|
||||||
pub mod comments;
|
pub mod comments;
|
||||||
pub mod errors;
|
pub mod errors;
|
||||||
|
@ -22,14 +22,14 @@
|
|||||||
|
|
||||||
<section>
|
<section>
|
||||||
<h2>{{ "Latest articles" | _ }}</h2>
|
<h2>{{ "Latest articles" | _ }}</h2>
|
||||||
{% if recents | length < 1 %}
|
{% if posts | length < 1 %}
|
||||||
<p>{{ "No posts to see here yet." | _ }}</p>
|
<p>{{ "No posts to see here yet." | _ }}</p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if is_author %}
|
{% if is_author %}
|
||||||
<a href="new" class="button inline-block">{{ "New article" | _ }}</a>
|
<a href="new" class="button inline-block">{{ "New article" | _ }}</a>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<div class="cards">
|
<div class="cards">
|
||||||
{% for article in recents %}
|
{% for article in posts %}
|
||||||
{{ macros::post_card(article=article) }}
|
{{ macros::post_card(article=article) }}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
Reference in New Issue
Block a user