Add pagination to the blog page
No UI to control it yet
This commit is contained in:
@@ -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,
|
||||
|
||||
+10
-4
@@ -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();
|
||||
|
||||
+40
-2
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user