From 397d25e431e35f45b9526fd66c5204581a0c2543 Mon Sep 17 00:00:00 2001 From: Bat Date: Sat, 12 May 2018 13:56:38 +0100 Subject: [PATCH] Display recent articles on the homepage --- src/models/posts.rs | 7 +++++++ src/routes/instance.rs | 16 ++++++++++++++-- templates/instance/index.tera | 11 ++++++++++- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/models/posts.rs b/src/models/posts.rs index 36cc54e..188197a 100644 --- a/src/models/posts.rs +++ b/src/models/posts.rs @@ -70,6 +70,13 @@ impl Post { .into_iter().nth(0) } + pub fn get_recents(conn: &PgConnection, limit: i64) -> Vec { + posts::table.order(posts::creation_date.desc()) + .limit(limit) + .load::(conn) + .expect("Error loading recent posts") + } + pub fn get_authors(&self, conn: &PgConnection) -> Vec { use schema::users; use schema::post_authors; diff --git a/src/routes/instance.rs b/src/routes/instance.rs index ca4f1d4..aaa38e5 100644 --- a/src/routes/instance.rs +++ b/src/routes/instance.rs @@ -1,10 +1,12 @@ use rocket::request::Form; use rocket::response::Redirect; use rocket_contrib::Template; -use std::collections::HashMap; +use serde_json; use BASE_URL; +use activity_pub::object::Object; use db_conn::DbConn; +use models::posts::Post; use models::users::User; use models::instance::*; @@ -12,9 +14,19 @@ use models::instance::*; fn index(conn: DbConn, user: Option) -> Template { match Instance::get_local(&*conn) { Some(inst) => { + let recents = Post::get_recents(&*conn, 5); + Template::render("instance/index", json!({ "instance": inst, - "account": user + "account": user, + "recents": recents.into_iter().map(|p| { + json!({ + "post": p, + "author": p.get_authors(&*conn)[0], + "url": p.compute_id(&*conn), + "date": p.creation_date.timestamp() + }) + }).collect::>() })) } None => { diff --git a/templates/instance/index.tera b/templates/instance/index.tera index 5a304ca..8090ead 100644 --- a/templates/instance/index.tera +++ b/templates/instance/index.tera @@ -5,5 +5,14 @@ {% endblock title %} {% block content %} -

Welcome on {{ instance.name }}

+

Welcome on {{ instance.name }}

+ +

Latest articles

+ {% for article in recents %} +
+

{{ article.post.title }}

+

{{ article.post.content | escape | truncate(length=200) }}…

+

By {{ article.author.display_name }} ⋅ {{ article.date | date(format="%B %e") }}

+
+ {% endfor %} {% endblock content %}