Articles listings on blogs and users pages

This commit is contained in:
Bat 2018-05-12 14:31:09 +01:00
parent 397d25e431
commit 6de9d5b82a
7 changed files with 65 additions and 9 deletions

View File

@ -77,6 +77,25 @@ impl Post {
.expect("Error loading recent posts")
}
pub fn get_recents_for_author(conn: &PgConnection, author: &User, limit: i64) -> Vec<Post> {
use schema::post_authors;
let posts = PostAuthor::belonging_to(author).select(post_authors::post_id);
posts::table.filter(posts::id.eq(any(posts)))
.order(posts::creation_date.desc())
.limit(limit)
.load::<Post>(conn)
.expect("Error loading recent posts for author")
}
pub fn get_recents_for_blog(conn: &PgConnection, blog: &Blog, limit: i64) -> Vec<Post> {
posts::table.filter(posts::blog_id.eq(blog.id))
.order(posts::creation_date.desc())
.limit(limit)
.load::<Post>(conn)
.expect("Error loading recent posts for blog")
}
pub fn get_authors(&self, conn: &PgConnection) -> Vec<User> {
use schema::users;
use schema::post_authors;

View File

@ -1,24 +1,35 @@
use rocket::request::Form;
use rocket::response::Redirect;
use rocket_contrib::Template;
use std::collections::HashMap;
use serde_json;
use activity_pub::ActivityPub;
use activity_pub::actor::Actor;
use activity_pub::object::Object;
use activity_pub::outbox::Outbox;
use db_conn::DbConn;
use models::blog_authors::*;
use models::blogs::*;
use models::instance::Instance;
use models::posts::Post;
use models::users::User;
use utils;
#[get("/~/<name>", rank = 2)]
fn details(name: String, conn: DbConn, user: Option<User>) -> Template {
let blog = Blog::find_by_actor_id(&*conn, name).unwrap();
let recents = Post::get_recents_for_blog(&*conn, &blog, 5);
Template::render("blogs/details", json!({
"blog": blog,
"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>>()
}))
}

View File

@ -3,7 +3,6 @@ use rocket::request::Form;
use rocket::response::Redirect;
use rocket_contrib::Template;
use serde_json;
use std::collections::HashMap;
use activity_pub::{context, activity_pub, ActivityPub};
use activity_pub::activity::Create;

View File

@ -3,7 +3,6 @@ use rocket::response::Redirect;
use rocket::response::status::NotFound;
use rocket::request::Form;
use rocket_contrib::Template;
use std::collections::HashMap;
use db_conn::DbConn;
use models::users::{User, AUTH_COOKIE};

View File

@ -2,15 +2,16 @@ use rocket::request::Form;
use rocket::response::Redirect;
use rocket_contrib::Template;
use serde_json;
use std::collections::HashMap;
use activity_pub::{activity, activity_pub, ActivityPub, context};
use activity_pub::actor::Actor;
use activity_pub::inbox::Inbox;
use activity_pub::object::Object;
use activity_pub::outbox::Outbox;
use db_conn::DbConn;
use models::follows::*;
use models::instance::Instance;
use models::posts::Post;
use models::users::*;
#[get("/me")]
@ -21,9 +22,18 @@ fn me(user: User) -> Redirect {
#[get("/@/<name>", rank = 2)]
fn details(name: String, conn: DbConn, account: Option<User>) -> Template {
let user = User::find_by_fqn(&*conn, name).unwrap();
let recents = Post::get_recents_for_author(&*conn, &user, 5);
Template::render("users/details", json!({
"user": serde_json::to_value(user).unwrap(),
"account": account
"account": account,
"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>>()
}))
}

View File

@ -5,6 +5,15 @@
{% endblock title %}
{% block content %}
<h1>{{ blog.title }} (~{{ blog.actor_id }})</h1>
<p>{{ blog.summary }}</p>
<h1>{{ blog.title }} (~{{ blog.actor_id }})</h1>
<p>{{ blog.summary }}</p>
<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 %}

View File

@ -9,4 +9,13 @@
<div>
{{ user.summary | safe }}
</div>
<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 %}