From babb3a81f52c7c566683e596e10ed3b12defb301 Mon Sep 17 00:00:00 2001 From: Bat Date: Tue, 4 Sep 2018 20:56:27 +0100 Subject: [PATCH] Local timeline --- plume-models/src/posts.rs | 15 +++++++++++++++ src/main.rs | 2 ++ src/routes/instance.rs | 18 ++++++++++++++++++ templates/instance/local.html.tera | 17 +++++++++++++++++ 4 files changed, 52 insertions(+) create mode 100644 templates/instance/local.html.tera diff --git a/plume-models/src/posts.rs b/plume-models/src/posts.rs index 73c8c98..5305142 100644 --- a/plume-models/src/posts.rs +++ b/plume-models/src/posts.rs @@ -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 { 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 { + 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::(conn) + .expect("Error loading local posts page") + } + pub fn get_authors(&self, conn: &PgConnection) -> Vec { use schema::users; use schema::post_authors; diff --git a/src/main.rs b/src/main.rs index f0f9fce..c00c457 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, diff --git a/src/routes/instance.rs b/src/routes/instance.rs index cbf710c..159fc6c 100644 --- a/src/routes/instance.rs +++ b/src/routes/instance.rs @@ -44,6 +44,24 @@ fn index(conn: DbConn, user: Option) -> Template { paginated_index(conn, user, Page::first()) } +#[get("/local?")] +fn paginated_local(conn: DbConn, user: Option, 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::>() + })) +} + +#[get("/local")] +fn local(conn: DbConn, user: Option) -> Template { + paginated_local(conn, user, Page::first()) +} + #[get("/admin")] fn admin(conn: DbConn, admin: Admin) -> Template { Template::render("instance/admin", json!({ diff --git a/templates/instance/local.html.tera b/templates/instance/local.html.tera new file mode 100644 index 0000000..9c550f4 --- /dev/null +++ b/templates/instance/local.html.tera @@ -0,0 +1,17 @@ +{% extends "base" %} +{% import "macros" as macros %} + +{% block title %} +{{ "Articles from {{ instance.name }}" | _(instance=instance) }} +{% endblock title %} + +{% block content %} +

{{ "Articles from {{ instance.name }}" | _(instance=instance) }}

+ +
+ {% for article in articles %} + {{ macros::post_card(article=article) }} + {% endfor %} +
+ {{ macros::paginate(page=page, total=n_pages) }} +{% endblock content %}