diff --git a/plume-models/src/posts.rs b/plume-models/src/posts.rs index 2e7bc1c..2601a75 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 2d442ba..b45843e 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 3da7d51..6163961 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 c58c2ae..a222daa 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 086f0fa..775c04e 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 c414e50..9fe7645 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 09304da..475d69c 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 985081e..3e9c6da 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 4ebec7a..8fbc1b0 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 %}