User feed

This commit is contained in:
Bat 2018-09-05 15:21:50 +01:00
parent 36f272b209
commit 1496598a45
4 changed files with 56 additions and 0 deletions

View File

@ -136,6 +136,20 @@ impl Post {
.expect("Error loading local posts page")
}
/// Give a page of customized user feed, based on a list of followed users
pub fn user_feed_page(conn: &PgConnection, followed: Vec<i32>, (min, max): (i32, i32)) -> Vec<Post> {
use schema::post_authors;
let post_ids = post_authors::table.filter(post_authors::author_id.eq(any(followed)))
.select(post_authors::post_id);
posts::table.order(posts::creation_date.desc())
.filter(posts::id.eq(any(post_ids)))
.offset(min.into())
.limit((max - min).into())
.load::<Post>(conn)
.expect("Error loading user feed page")
}
pub fn get_authors(&self, conn: &PgConnection) -> Vec<User> {
use schema::users;
use schema::post_authors;

View File

@ -59,6 +59,8 @@ fn main() {
routes::instance::index,
routes::instance::paginated_local,
routes::instance::local,
routes::instance::paginated_feed,
routes::instance::feed,
routes::instance::admin,
routes::instance::update_settings,
routes::instance::shared_inbox,

View File

@ -62,6 +62,23 @@ fn local(conn: DbConn, user: Option<User>) -> Template {
paginated_local(conn, user, Page::first())
}
#[get("/feed")]
fn feed(conn: DbConn, user: User) -> Template {
paginated_feed(conn, user, Page::first())
}
#[get("/feed?<page>")]
fn paginated_feed(conn: DbConn, user: User, page: Page) -> Template {
let followed = user.get_following(&*conn);
let articles = Post::user_feed_page(&*conn, followed.into_iter().map(|u| u.id).collect(), page.limits());
Template::render("instance/feed", json!({
"account": user.to_json(&*conn),
"page": page.page,
"n_pages": Page::total(Post::count_local(&*conn) as i32),
"articles": articles.into_iter().map(|p| p.to_json(&*conn)).collect::<Vec<serde_json::Value>>()
}))
}
#[get("/admin")]
fn admin(conn: DbConn, admin: Admin) -> Template {
Template::render("instance/admin", json!({

View File

@ -0,0 +1,23 @@
{% extends "base" %}
{% import "macros" as macros %}
{% block title %}
{{ "Your feed" | _ }}
{% endblock title %}
{% block content %}
<h1>
{{ "Your feed" | _ }}
</h1>
{% if articles | length > 0 %}
<div class="cards">
{% for article in articles %}
{{ macros::post_card(article=article) }}
{% endfor %}
</div>
{{ macros::paginate(page=page, total=n_pages) }}
{% else %}
<p class="center">{{ "Nothing to see here yet. Try to follow more people." | _ }}</p>
{% endif %}
{% endblock content %}