diff --git a/src/models/posts.rs b/src/models/posts.rs index 36cc54ee..188197aa 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 ca4f1d4f..aaa38e5d 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 5a304ca0..8090ead0 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 %}