From 18125ab398e5660e194fe52a18190991aca9369e Mon Sep 17 00:00:00 2001 From: Bat Date: Wed, 25 Jul 2018 15:20:09 +0200 Subject: [PATCH] Paginate the homepage --- plume-models/src/posts.rs | 12 ++++++++++++ po/de.po | 6 ++++++ po/en.po | 6 ++++++ po/fr.po | 6 ++++++ po/pl.po | 6 ++++++ src/main.rs | 1 + src/routes/instance.rs | 16 ++++++++++++---- static/main.css | 1 - templates/instance/index.html.tera | 1 + 9 files changed, 50 insertions(+), 5 deletions(-) diff --git a/plume-models/src/posts.rs b/plume-models/src/posts.rs index 2e7bc1c2..2601a752 100644 --- a/plume-models/src/posts.rs +++ b/plume-models/src/posts.rs @@ -65,6 +65,10 @@ impl Post { .len() } + pub fn count(conn: &PgConnection) -> i64 { + posts::table.count().get_result(conn).expect("Couldn't count posts") + } + pub fn get_recents(conn: &PgConnection, limit: i64) -> Vec { posts::table.order(posts::creation_date.desc()) .limit(limit) @@ -106,6 +110,14 @@ impl Post { .expect("Error loading a page of posts for blog") } + pub fn get_recents_page(conn: &PgConnection, (min, max): (i32, i32)) -> Vec { + posts::table.order(posts::creation_date.desc()) + .offset(min.into()) + .limit((max - min).into()) + .load::(conn) + .expect("Error loading recent posts page") + } + pub fn get_authors(&self, conn: &PgConnection) -> Vec { use schema::users; use schema::post_authors; diff --git a/po/de.po b/po/de.po index 2d442bad..b45843e9 100644 --- a/po/de.po +++ b/po/de.po @@ -345,3 +345,9 @@ msgid "One article in this blog" msgid_plural "{{ count }} articles in this blog" msgstr[0] "Du bist kein Autor in diesem Blog." msgstr[1] "Du bist kein Autor in diesem Blog." + +msgid "Previous page" +msgstr "" + +msgid "Next page" +msgstr "" diff --git a/po/en.po b/po/en.po index 3da7d51e..61639615 100644 --- a/po/en.po +++ b/po/en.po @@ -337,3 +337,9 @@ msgid "One article in this blog" msgid_plural "{{ count }} articles in this blog" msgstr[0] "" msgstr[1] "" + +msgid "Previous page" +msgstr "" + +msgid "Next page" +msgstr "" diff --git a/po/fr.po b/po/fr.po index c58c2ae3..a222daae 100644 --- a/po/fr.po +++ b/po/fr.po @@ -341,3 +341,9 @@ msgid "One article in this blog" msgid_plural "{{ count }} articles in this blog" msgstr[0] "{{ count }} aut⋅eur⋅rice dans ce blog : " msgstr[1] "{{ count }} aut⋅eur⋅rice⋅s dans ce blog : " + +msgid "Previous page" +msgstr "" + +msgid "Next page" +msgstr "" diff --git a/po/pl.po b/po/pl.po index 086f0fa4..775c04e8 100644 --- a/po/pl.po +++ b/po/pl.po @@ -350,5 +350,11 @@ msgstr[0] "Ten blog ma jednego autora: " msgstr[1] "Ten blog ma {{ count }} autorów: " msgstr[2] "Ten blog ma {{ count }} autorów: " +msgid "Previous page" +msgstr "" + +msgid "Next page" +msgstr "" + #~ msgid "Logowanie" #~ msgstr "Zaloguj się" diff --git a/src/main.rs b/src/main.rs index c414e503..9fe76452 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,6 +46,7 @@ fn main() { routes::comments::create, + routes::instance::paginated_index, routes::instance::index, routes::instance::shared_inbox, routes::instance::nodeinfo, diff --git a/src/routes/instance.rs b/src/routes/instance.rs index 09304dac..475d69cf 100644 --- a/src/routes/instance.rs +++ b/src/routes/instance.rs @@ -10,17 +10,20 @@ use plume_models::{ instance::* }; use inbox::Inbox; +use routes::Page; -#[get("/")] -fn index(conn: DbConn, user: Option) -> Template { +#[get("/?")] +fn paginated_index(conn: DbConn, user: Option, page: Page) -> Template { match Instance::get_local(&*conn) { Some(inst) => { - let recents = Post::get_recents(&*conn, 6); + let recents = Post::get_recents_page(&*conn, page.limits()); Template::render("instance/index", json!({ "instance": inst, "account": user, - "recents": recents.into_iter().map(|p| p.to_json(&*conn)).collect::>() + "recents": recents.into_iter().map(|p| p.to_json(&*conn)).collect::>(), + "page": page.page, + "n_pages": Page::total(Post::count(&*conn) as i32) })) } None => { @@ -31,6 +34,11 @@ fn index(conn: DbConn, user: Option) -> Template { } } +#[get("/")] +fn index(conn: DbConn, user: Option) -> Template { + paginated_index(conn, user, Page::first()) +} + #[post("/inbox", data = "")] fn shared_inbox(conn: DbConn, data: String) -> String { let act: serde_json::Value = serde_json::from_str(&data[..]).unwrap(); diff --git a/static/main.css b/static/main.css index 985081ea..3e9c6da1 100644 --- a/static/main.css +++ b/static/main.css @@ -544,7 +544,6 @@ form.new-post input[type="submit"]:hover { background: #DADADA; } /*== Pagination ==*/ .pagination { display: flex; - width: 100%; justify-content: space-evenly; } diff --git a/templates/instance/index.html.tera b/templates/instance/index.html.tera index 4ebec7a5..8fbc1b08 100644 --- a/templates/instance/index.html.tera +++ b/templates/instance/index.html.tera @@ -14,4 +14,5 @@ {{ macros::post_card(article=article) }} {% endfor %} + {{ macros::paginate(page=page, total=n_pages) }} {% endblock content %}