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") .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> { pub fn get_authors(&self, conn: &PgConnection) -> Vec<User> {
use schema::users; use schema::users;
use schema::post_authors; use schema::post_authors;

View file

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

View file

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

View file

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

View file

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