Local timeline

This commit is contained in:
Bat 2018-09-04 20:56:27 +01:00
parent adcfd88471
commit babb3a81f5
4 changed files with 52 additions and 0 deletions

View file

@ -113,6 +113,7 @@ impl Post {
.expect("Error loading a page of posts for blog")
}
/// Give a page of all the recent posts known to this instance (= federated timeline)
pub fn get_recents_page(conn: &PgConnection, (min, max): (i32, i32)) -> Vec<Post> {
posts::table.order(posts::creation_date.desc())
.offset(min.into())
@ -121,6 +122,20 @@ impl Post {
.expect("Error loading recent posts page")
}
/// Give a page of posts from a specific instance
pub fn get_instance_page(conn: &PgConnection, instance_id: i32, (min, max): (i32, i32)) -> Vec<Post> {
use schema::blogs;
let blog_ids = blogs::table.filter(blogs::instance_id.eq(instance_id)).select(blogs::id);
posts::table.order(posts::creation_date.desc())
.filter(posts::blog_id.eq(any(blog_ids)))
.offset(min.into())
.limit((max - min).into())
.load::<Post>(conn)
.expect("Error loading local posts page")
}
pub fn get_authors(&self, conn: &PgConnection) -> Vec<User> {
use schema::users;
use schema::post_authors;

View file

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

View file

@ -44,6 +44,24 @@ fn index(conn: DbConn, user: Option<User>) -> Template {
paginated_index(conn, user, Page::first())
}
#[get("/local?<page>")]
fn paginated_local(conn: DbConn, user: Option<User>, page: Page) -> Template {
let instance = Instance::get_local(&*conn).unwrap();
let articles = Post::get_instance_page(&*conn, instance.id, page.limits());
Template::render("instance/local", json!({
"account": user.map(|u| u.to_json(&*conn)),
"instance": instance,
"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("/local")]
fn local(conn: DbConn, user: Option<User>) -> Template {
paginated_local(conn, user, Page::first())
}
#[get("/admin")]
fn admin(conn: DbConn, admin: Admin) -> Template {
Template::render("instance/admin", json!({

View file

@ -0,0 +1,17 @@
{% extends "base" %}
{% import "macros" as macros %}
{% block title %}
{{ "Articles from {{ instance.name }}" | _(instance=instance) }}
{% endblock title %}
{% block content %}
<h1>{{ "Articles from {{ instance.name }}" | _(instance=instance) }}</h1>
<div class="cards">
{% for article in articles %}
{{ macros::post_card(article=article) }}
{% endfor %}
</div>
{{ macros::paginate(page=page, total=n_pages) }}
{% endblock content %}