From 4e07fdbd0555704e159b9698d5f0e017e991e572 Mon Sep 17 00:00:00 2001 From: Bat Date: Wed, 25 Jul 2018 15:50:29 +0200 Subject: [PATCH] Paginate followers too --- plume-models/src/users.rs | 9 +++++++++ src/main.rs | 1 + src/routes/user.rs | 18 ++++++++++++++---- templates/users/followers.html.tera | 2 ++ 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/plume-models/src/users.rs b/plume-models/src/users.rs index c43b15e8..ec74b1ca 100644 --- a/plume-models/src/users.rs +++ b/plume-models/src/users.rs @@ -275,6 +275,15 @@ impl User { users::table.filter(users::id.eq(any(follows))).load::(conn).unwrap() } + pub fn get_followers_page(&self, conn: &PgConnection, (min, max): (i32, i32)) -> Vec { + use schema::follows; + let follows = Follow::belonging_to(self).select(follows::follower_id); + users::table.filter(users::id.eq(any(follows))) + .offset(min.into()) + .limit((max - min).into()) + .load::(conn).unwrap() + } + pub fn get_following(&self, conn: &PgConnection) -> Vec { use schema::follows; let follows = follows::table.filter(follows::follower_id.eq(self.id)).select(follows::following_id); diff --git a/src/main.rs b/src/main.rs index a1c59e77..ceffc6e2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -79,6 +79,7 @@ fn main() { routes::user::details, routes::user::dashboard, routes::user::dashboard_auth, + routes::user::followers_paginated, routes::user::followers, routes::user::edit, routes::user::edit_auth, diff --git a/src/routes/user.rs b/src/routes/user.rs index 6b131304..67fb0215 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -24,6 +24,7 @@ use plume_models::{ users::* }; use inbox::Inbox; +use routes::Page; #[get("/me")] fn me(user: Option) -> Result> { @@ -94,24 +95,33 @@ fn follow_auth(name: String) -> Flash { utils::requires_login("You need to be logged in order to follow someone", uri!(follow: name = name)) } -#[get("/@//followers", rank = 2)] -fn followers(name: String, conn: DbConn, account: Option) -> Template { +#[get("/@//followers?")] +fn followers_paginated(name: String, conn: DbConn, account: Option, page: Page) -> Template { may_fail!(account, User::find_by_fqn(&*conn, name.clone()), "Couldn't find requested user", |user| { let user_id = user.id.clone(); + let followers_count = user.get_followers(&*conn).len(); Template::render("users/followers", json!({ "user": user.to_json(&*conn), "instance_url": user.get_instance(&*conn).public_domain, "is_remote": user.instance_id != Instance::local_id(&*conn), "follows": account.clone().map(|x| x.is_following(&*conn, user.id)).unwrap_or(false), - "followers": user.get_followers(&*conn).into_iter().map(|f| f.to_json(&*conn)).collect::>(), + "followers": user.get_followers_page(&*conn, page.limits()).into_iter().map(|f| f.to_json(&*conn)).collect::>(), "account": account, "is_self": account.map(|a| a.id == user_id).unwrap_or(false), - "n_followers": user.get_followers(&*conn).len() + "n_followers": followers_count, + "page": page.page, + "n_pages": Page::total(followers_count as i32) })) }) } +#[get("/@//followers", rank = 2)] +fn followers(name: String, conn: DbConn, account: Option) -> Template { + followers_paginated(name, conn, account, Page::first()) +} + + #[get("/@/", rank = 1)] fn activity_details(name: String, conn: DbConn, _ap: ApRequest) -> ActivityStream { let user = User::find_local(&*conn, name).unwrap(); diff --git a/templates/users/followers.html.tera b/templates/users/followers.html.tera index e748116e..8757857a 100644 --- a/templates/users/followers.html.tera +++ b/templates/users/followers.html.tera @@ -1,4 +1,5 @@ {% extends "base" %} +{% import "macros" as macros %} {% block title %} {% if user.display_name %} @@ -27,4 +28,5 @@ {% endfor %} + {{ macros::paginate(page=page, total=n_pages) }} {% endblock content %}