Add some to_json functions to models for serialization in templates
This commit is contained in:
parent
58cc35691d
commit
7e3cdec0b6
@ -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<Note> for Comment {
|
||||
|
@ -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<Article> for Post {
|
||||
|
@ -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 {
|
||||
|
@ -25,19 +25,7 @@ fn details(name: String, conn: DbConn, user: Option<User>) -> 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::<Vec<serde_json::Value>>()
|
||||
"recents": recents.into_iter().map(|p| p.to_json(&*conn)).collect::<Vec<serde_json::Value>>()
|
||||
}))
|
||||
}
|
||||
|
||||
|
@ -22,19 +22,7 @@ fn index(conn: DbConn, user: Option<User>) -> 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::<Vec<serde_json::Value>>()
|
||||
"recents": recents.into_iter().map(|p| p.to_json(&*conn)).collect::<Vec<serde_json::Value>>()
|
||||
}))
|
||||
}
|
||||
None => {
|
||||
|
@ -24,26 +24,10 @@ fn details(blog: String, slug: String, conn: DbConn, user: Option<User>) -> 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::<Vec<serde_json::Value>>(),
|
||||
"comments": comments.into_iter().map(|c| c.to_json(&*conn)).collect::<Vec<serde_json::Value>>(),
|
||||
"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(),
|
||||
|
@ -46,33 +46,8 @@ fn details(name: String, conn: DbConn, account: Option<User>) -> 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::<Vec<serde_json::Value>>(),
|
||||
"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::<Vec<serde_json::Value>>(),
|
||||
"recents": recents.into_iter().map(|p| p.to_json(&*conn)).collect::<Vec<serde_json::Value>>(),
|
||||
"reshares": reshares.into_iter().map(|r| r.get_post(&*conn).unwrap().to_json(&*conn)).collect::<Vec<serde_json::Value>>(),
|
||||
"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<User>) -> 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::<Vec<serde_json::Value>>(),
|
||||
"followers": user.get_followers(&*conn).into_iter().map(|f| f.to_json(&*conn)).collect::<Vec<serde_json::Value>>(),
|
||||
"account": account,
|
||||
"is_self": account.map(|a| a.id == user_id).unwrap_or(false),
|
||||
"n_followers": user.get_followers(&*conn).len()
|
||||
|
Loading…
Reference in New Issue
Block a user