From e9cd48eccae7af7d847561a3b2c83a2fd857a956 Mon Sep 17 00:00:00 2001 From: Bat Date: Thu, 24 May 2018 10:45:36 +0100 Subject: [PATCH] Display reshares on profile page --- src/models/reshares.rs | 12 ++++++++++++ src/routes/user.rs | 16 ++++++++++++++++ templates/users/details.tera | 7 +++++++ 3 files changed, 35 insertions(+) diff --git a/src/models/reshares.rs b/src/models/reshares.rs index 8d50ff5b..6d8c138e 100644 --- a/src/models/reshares.rs +++ b/src/models/reshares.rs @@ -68,6 +68,18 @@ impl Reshare { .into_iter().nth(0) } + pub fn get_recents_for_author(conn: &PgConnection, user: &User, limit: i64) -> Vec { + reshares::table.filter(reshares::user_id.eq(user.id)) + .order(reshares::creation_date.desc()) + .limit(limit) + .load::(conn) + .expect("Error loading recent reshares for user") + } + + pub fn get_post(&self, conn: &PgConnection) -> Option { + Post::get(conn, self.post_id) + } + pub fn delete(&self, conn: &PgConnection) -> activity::Undo { diesel::delete(self).execute(conn).unwrap(); diff --git a/src/routes/user.rs b/src/routes/user.rs index 6f4bb615..606b0cf5 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -16,6 +16,7 @@ use models::{ follows, instance::Instance, posts::Post, + reshares::Reshare, users::* }; @@ -28,6 +29,7 @@ fn me(user: User) -> Redirect { fn details(name: String, conn: DbConn, account: Option) -> Template { let user = User::find_by_fqn(&*conn, name).unwrap(); let recents = Post::get_recents_for_author(&*conn, &user, 5); + let reshares = Reshare::get_recents_for_author(&*conn, &user, 5); let user_id = user.id.clone(); let n_followers = user.get_followers(&*conn).len(); @@ -47,6 +49,20 @@ fn details(name: String, conn: DbConn, account: Option) -> Template { "date": p.creation_date.timestamp() }) }).collect::>(), + "reshares": reshares.into_iter().map(|r| { + let p = r.get_post(&*conn).unwrap(); + json!({ + "post": p, + "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), + "date": p.creation_date.timestamp() + }) + }).collect::>(), "is_self": account.map(|a| a.id == user_id).unwrap_or(false), "n_followers": n_followers })) diff --git a/templates/users/details.tera b/templates/users/details.tera index c78fe935..33982214 100644 --- a/templates/users/details.tera +++ b/templates/users/details.tera @@ -37,4 +37,11 @@ {{ macros::post_card(article=article) }} {% endfor %} + +

Recently reshared

+
+ {% for article in reshares %} + {{ macros::post_card(article=article) }} + {% endfor %} +
{% endblock content %}