Add some to_json functions to models for serialization in templates

This commit is contained in:
Bat
2018-06-18 17:34:29 +01:00
parent 58cc35691d
commit 7e3cdec0b6
7 changed files with 28 additions and 77 deletions
+6
View File
@@ -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 {
+9
View File
@@ -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 {
+6
View File
@@ -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 {