diff --git a/src/models/comments.rs b/src/models/comments.rs index e4788925..79848332 100644 --- a/src/models/comments.rs +++ b/src/models/comments.rs @@ -99,6 +99,12 @@ impl Comment { .expect("Couldn't load local comments") .len() } + + pub fn to_json(&self, conn: &PgConnection) -> serde_json::Value { + let mut json = serde_json::to_value(self).unwrap(); + json["author"] = self.get_author(conn).to_json(conn); + json + } } impl FromActivity for Comment { diff --git a/src/models/posts.rs b/src/models/posts.rs index 6b71928b..8d12bc8f 100644 --- a/src/models/posts.rs +++ b/src/models/posts.rs @@ -168,6 +168,15 @@ impl Post { act.create_props.set_object_object(self.into_activity(conn)).unwrap(); act } + + pub fn to_json(&self, conn: &PgConnection) -> serde_json::Value { + json!({ + "post": self, + "author": self.get_authors(conn)[0].to_json(conn), + "url": format!("/~/{}/{}/", self.get_blog(conn).actor_id, self.slug), + "date": self.creation_date.timestamp() + }) + } } impl FromActivity
for Post { diff --git a/src/models/users.rs b/src/models/users.rs index 88bf979a..dc37ff95 100644 --- a/src/models/users.rs +++ b/src/models/users.rs @@ -330,6 +330,12 @@ impl User { }; actor } + + pub fn to_json(&self, conn: &PgConnection) -> serde_json::Value { + let mut json = serde_json::to_value(self).unwrap(); + json["fqn"] = serde_json::Value::String(self.get_fqn(conn)); + json + } } impl<'a, 'r> FromRequest<'a, 'r> for User { diff --git a/src/routes/blogs.rs b/src/routes/blogs.rs index 0a64e9e6..ecd01394 100644 --- a/src/routes/blogs.rs +++ b/src/routes/blogs.rs @@ -25,19 +25,7 @@ fn details(name: String, conn: DbConn, user: Option) -> Template { "blog": blog, "account": user, "is_author": user.map(|x| x.is_author_in(&*conn, blog)), - "recents": recents.into_iter().map(|p| { - 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::>() + "recents": recents.into_iter().map(|p| p.to_json(&*conn)).collect::>() })) } diff --git a/src/routes/instance.rs b/src/routes/instance.rs index a5dd54df..be1deb42 100644 --- a/src/routes/instance.rs +++ b/src/routes/instance.rs @@ -22,19 +22,7 @@ fn index(conn: DbConn, user: Option) -> Template { Template::render("instance/index", json!({ "instance": inst, "account": user, - "recents": recents.into_iter().map(|p| { - 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::>() + "recents": recents.into_iter().map(|p| p.to_json(&*conn)).collect::>() })) } None => { diff --git a/src/routes/posts.rs b/src/routes/posts.rs index fef302ab..c0b7663b 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -24,26 +24,10 @@ fn details(blog: String, slug: String, conn: DbConn, user: Option) -> Temp let comments = Comment::find_by_post(&*conn, post.id); Template::render("posts/details", json!({ - "author": ({ - let author = &post.get_authors(&*conn)[0]; - let mut json = serde_json::to_value(author).unwrap(); - json["fqn"] = serde_json::Value::String(author.get_fqn(&*conn)); - json - }), + "author": post.get_authors(&*conn)[0].to_json(&*conn), "post": post, "blog": blog, - "comments": comments.into_iter().map(|c| { - json!({ - "id": c.id, - "content": c.content, - "author": ({ - let author = &c.get_author(&*conn); - let mut json = serde_json::to_value(author).unwrap(); - json["fqn"] = serde_json::Value::String(author.get_fqn(&*conn)); - json - }) - }) - }).collect::>(), + "comments": comments.into_iter().map(|c| c.to_json(&*conn)).collect::>(), "n_likes": post.get_likes(&*conn).len(), "has_liked": user.clone().map(|u| u.has_liked(&*conn, &post)).unwrap_or(false), "n_reshares": post.get_reshares(&*conn).len(), diff --git a/src/routes/user.rs b/src/routes/user.rs index 50258360..d485df1e 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -46,33 +46,8 @@ fn details(name: String, conn: DbConn, account: Option) -> Template { "is_remote": user.instance_id != Instance::local_id(&*conn), "follows": account.clone().map(|x| x.is_following(&*conn, user.id)).unwrap_or(false), "account": account, - "recents": recents.into_iter().map(|p| { - 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::>(), - "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::>(), + "recents": recents.into_iter().map(|p| p.to_json(&*conn)).collect::>(), + "reshares": reshares.into_iter().map(|r| r.get_post(&*conn).unwrap().to_json(&*conn)).collect::>(), "is_self": account.map(|a| a.id == user_id).unwrap_or(false), "n_followers": n_followers })) @@ -124,12 +99,7 @@ fn followers(name: String, conn: DbConn, account: Option) -> Template { "instance_url": user.get_instance(&*conn).public_domain, "is_remote": user.instance_id != Instance::local_id(&*conn), "follows": account.clone().map(|x| x.is_following(&*conn, user.id)).unwrap_or(false), - "followers": user.get_followers(&*conn).into_iter().map(|f| { - let fqn = f.get_fqn(&*conn); - let mut json = serde_json::to_value(f).unwrap(); - json["fqn"] = serde_json::Value::String(fqn); - json - }).collect::>(), + "followers": user.get_followers(&*conn).into_iter().map(|f| f.to_json(&*conn)).collect::>(), "account": account, "is_self": account.map(|a| a.id == user_id).unwrap_or(false), "n_followers": user.get_followers(&*conn).len()