diff --git a/plume-models/src/notifications.rs b/plume-models/src/notifications.rs index 10944359..07f0bdb2 100644 --- a/plume-models/src/notifications.rs +++ b/plume-models/src/notifications.rs @@ -35,4 +35,13 @@ impl Notification { .load::(conn) .expect("Couldn't load user notifications") } + + pub fn page_for_user(conn: &PgConnection, user: &User, (min, max): (i32, i32)) -> Vec { + notifications::table.filter(notifications::user_id.eq(user.id)) + .order_by(notifications::creation_date.desc()) + .offset(min.into()) + .limit((max - min).into()) + .load::(conn) + .expect("Couldn't load user notifications page") + } } diff --git a/src/main.rs b/src/main.rs index 9fe76452..a1c59e77 100644 --- a/src/main.rs +++ b/src/main.rs @@ -54,6 +54,7 @@ fn main() { routes::likes::create, routes::likes::create_auth, + routes::notifications::paginated_notifications, routes::notifications::notifications, routes::notifications::notifications_auth, diff --git a/src/routes/notifications.rs b/src/routes/notifications.rs index 14cf6a74..de414944 100644 --- a/src/routes/notifications.rs +++ b/src/routes/notifications.rs @@ -3,13 +3,21 @@ use rocket_contrib::Template; use plume_common::utils; use plume_models::{db_conn::DbConn, notifications::Notification, users::User}; +use routes::Page; + +#[get("/notifications?")] +fn paginated_notifications(conn: DbConn, user: User, page: Page) -> Template { + Template::render("notifications/index", json!({ + "account": user, + "notifications": Notification::page_for_user(&*conn, &user, page.limits()), + "page": page.page, + "n_pages": Page::total(Notification::find_for_user(&*conn, &user).len() as i32) + })) +} #[get("/notifications")] fn notifications(conn: DbConn, user: User) -> Template { - Template::render("notifications/index", json!({ - "account": user, - "notifications": Notification::find_for_user(&*conn, &user) - })) + paginated_notifications(conn, user, Page::first()) } #[get("/notifications", rank = 2)] diff --git a/templates/notifications/index.html.tera b/templates/notifications/index.html.tera index 50752282..97034a50 100644 --- a/templates/notifications/index.html.tera +++ b/templates/notifications/index.html.tera @@ -1,4 +1,5 @@ {% extends "base" %} +{% import "macros" as macros %} {% block title %} {{ "Notifications" | _ }} @@ -9,11 +10,12 @@
{% for notification in notifications %}
-

{{ notification.title | _(data=notification.data) }}

+

{{ notification.title | _(data=notification.data) }}

{% if notification.content %}

{{ notification.content }}

{% endif %}
{% endfor %}
+ {{ macros::paginate(page=page, total=n_pages) }} {% endblock content %}