Paginate followers too

This commit is contained in:
Bat 2018-07-25 15:50:29 +02:00
parent 4b0aba62f3
commit 4e07fdbd05
4 changed files with 26 additions and 4 deletions

View File

@ -275,6 +275,15 @@ impl User {
users::table.filter(users::id.eq(any(follows))).load::<User>(conn).unwrap()
}
pub fn get_followers_page(&self, conn: &PgConnection, (min, max): (i32, i32)) -> Vec<User> {
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::<User>(conn).unwrap()
}
pub fn get_following(&self, conn: &PgConnection) -> Vec<User> {
use schema::follows;
let follows = follows::table.filter(follows::follower_id.eq(self.id)).select(follows::following_id);

View File

@ -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,

View File

@ -24,6 +24,7 @@ use plume_models::{
users::*
};
use inbox::Inbox;
use routes::Page;
#[get("/me")]
fn me(user: Option<User>) -> Result<Redirect, Flash<Redirect>> {
@ -94,24 +95,33 @@ fn follow_auth(name: String) -> Flash<Redirect> {
utils::requires_login("You need to be logged in order to follow someone", uri!(follow: name = name))
}
#[get("/@/<name>/followers", rank = 2)]
fn followers(name: String, conn: DbConn, account: Option<User>) -> Template {
#[get("/@/<name>/followers?<page>")]
fn followers_paginated(name: String, conn: DbConn, account: Option<User>, 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::<Vec<serde_json::Value>>(),
"followers": user.get_followers_page(&*conn, page.limits()).into_iter().map(|f| f.to_json(&*conn)).collect::<Vec<serde_json::Value>>(),
"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("/@/<name>/followers", rank = 2)]
fn followers(name: String, conn: DbConn, account: Option<User>) -> Template {
followers_paginated(name, conn, account, Page::first())
}
#[get("/@/<name>", rank = 1)]
fn activity_details(name: String, conn: DbConn, _ap: ApRequest) -> ActivityStream<CustomPerson> {
let user = User::find_local(&*conn, name).unwrap();

View File

@ -1,4 +1,5 @@
{% extends "base" %}
{% import "macros" as macros %}
{% block title %}
{% if user.display_name %}
@ -27,4 +28,5 @@
</div>
{% endfor %}
</div>
{{ macros::paginate(page=page, total=n_pages) }}
{% endblock content %}