Display recent articles on the homepage

This commit is contained in:
Bat 2018-05-12 13:56:38 +01:00
parent a74215ef07
commit 397d25e431
3 changed files with 31 additions and 3 deletions

View file

@ -70,6 +70,13 @@ impl Post {
.into_iter().nth(0)
}
pub fn get_recents(conn: &PgConnection, limit: i64) -> Vec<Post> {
posts::table.order(posts::creation_date.desc())
.limit(limit)
.load::<Post>(conn)
.expect("Error loading recent posts")
}
pub fn get_authors(&self, conn: &PgConnection) -> Vec<User> {
use schema::users;
use schema::post_authors;

View file

@ -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<User>) -> 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::<Vec<serde_json::Value>>()
}))
}
None => {

View file

@ -6,4 +6,13 @@
{% block content %}
<h1>Welcome on {{ instance.name }}</h1>
<h2>Latest articles</h2>
{% for article in recents %}
<div>
<h3><a href="{{ article.url }}">{{ article.post.title }}</a></h3>
<p>{{ article.post.content | escape | truncate(length=200) }}…</p>
<p>By {{ article.author.display_name }} ⋅ {{ article.date | date(format="%B %e") }}</p>
</div>
{% endfor %}
{% endblock content %}