Add a link to author's profile on article card

Use a macro to render those cards too
This commit is contained in:
Bat 2018-05-13 18:19:23 +01:00
parent 5e7d513a7e
commit 91b19bccb5
8 changed files with 36 additions and 19 deletions

View File

@ -227,8 +227,12 @@ impl User {
} }
pub fn get_fqn(&self, conn: &PgConnection) -> String { pub fn get_fqn(&self, conn: &PgConnection) -> String {
if self.instance_id == Instance::local_id(conn) {
self.username.clone()
} else {
format!("{}@{}", self.username, self.get_instance(conn).public_domain) format!("{}@{}", self.username, self.get_instance(conn).public_domain)
} }
}
pub fn get_followers(&self, conn: &PgConnection) -> Vec<User> { pub fn get_followers(&self, conn: &PgConnection) -> Vec<User> {
use schema::follows; use schema::follows;

View File

@ -24,7 +24,12 @@ fn details(name: String, conn: DbConn, user: Option<User>) -> Template {
"recents": recents.into_iter().map(|p| { "recents": recents.into_iter().map(|p| {
json!({ json!({
"post": p, "post": p,
"author": p.get_authors(&*conn)[0], "author": ({
let author = &p.get_authors(&*conn)[0];
let mut json = serde_json::to_value(author).unwrap();
json["fqn"] = serde_json::Value::String(author.get_fqn(&*conn));
json
}),
"url": format!("/~/{}/{}/", p.get_blog(&*conn).actor_id, p.slug), "url": format!("/~/{}/{}/", p.get_blog(&*conn).actor_id, p.slug),
"date": p.creation_date.timestamp() "date": p.creation_date.timestamp()
}) })

View File

@ -21,7 +21,12 @@ fn index(conn: DbConn, user: Option<User>) -> Template {
"recents": recents.into_iter().map(|p| { "recents": recents.into_iter().map(|p| {
json!({ json!({
"post": p, "post": p,
"author": p.get_authors(&*conn)[0], "author": ({
let author = &p.get_authors(&*conn)[0];
let mut json = serde_json::to_value(author).unwrap();
json["fqn"] = serde_json::Value::String(author.get_fqn(&*conn));
json
}),
"url": format!("/~/{}/{}/", p.get_blog(&*conn).actor_id, p.slug), "url": format!("/~/{}/{}/", p.get_blog(&*conn).actor_id, p.slug),
"date": p.creation_date.timestamp() "date": p.creation_date.timestamp()
}) })

View File

@ -31,7 +31,12 @@ fn details(name: String, conn: DbConn, account: Option<User>) -> Template {
"recents": recents.into_iter().map(|p| { "recents": recents.into_iter().map(|p| {
json!({ json!({
"post": p, "post": p,
"author": p.get_authors(&*conn)[0], "author": ({
let author = &p.get_authors(&*conn)[0];
let mut json = serde_json::to_value(author).unwrap();
json["fqn"] = serde_json::Value::String(author.get_fqn(&*conn));
json
}),
"url": format!("/~/{}/{}/", p.get_blog(&*conn).actor_id, p.slug), "url": format!("/~/{}/{}/", p.get_blog(&*conn).actor_id, p.slug),
"date": p.creation_date.timestamp() "date": p.creation_date.timestamp()
}) })

View File

@ -1,4 +1,5 @@
{% extends "base" %} {% extends "base" %}
{% import "macros" as macros %}
{% block title %} {% block title %}
{{ blog.title }} {{ blog.title }}
@ -10,10 +11,6 @@
<h2>Latest articles</h2> <h2>Latest articles</h2>
{% for article in recents %} {% for article in recents %}
<div> {{ macros::post_card(article=article) }}
<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 %} {% endfor %}
{% endblock content %} {% endblock content %}

View File

@ -1,4 +1,5 @@
{% extends "base" %} {% extends "base" %}
{% import "macros" as macros %}
{% block title %} {% block title %}
{{ instance.name }} {{ instance.name }}
@ -9,10 +10,6 @@
<h2>Latest articles</h2> <h2>Latest articles</h2>
{% for article in recents %} {% for article in recents %}
<div> {{ macros::post_card(article=article) }}
<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 %} {% endfor %}
{% endblock content %} {% endblock content %}

7
templates/macros.tera Normal file
View File

@ -0,0 +1,7 @@
{% macro post_card(article) %}
<div>
<h3><a href="{{ article.url }}">{{ article.post.title }}</a></h3>
<p>{{ article.post.content | escape | truncate(length=200) }}…</p>
<p>By <a href="/@/{{ article.author.fqn }}/">{{ article.author.display_name }}</a> ⋅ {{ article.date | date(format="%B %e") }}</p>
</div>
{% endmacro post_card %}

View File

@ -1,4 +1,5 @@
{% extends "base" %} {% extends "base" %}
{% import "macros" as macros %}
{% block title %} {% block title %}
{{ user.display_name }} {{ user.display_name }}
@ -32,10 +33,6 @@
<h2>Latest articles</h2> <h2>Latest articles</h2>
{% for article in recents %} {% for article in recents %}
<div> {{ macros::post_card(article=article) }}
<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 %} {% endfor %}
{% endblock content %} {% endblock content %}