Federate comments

This commit is contained in:
Bat
2018-05-10 11:52:56 +01:00
parent a3d73cb2c4
commit a436f2da4b
7 changed files with 57 additions and 8 deletions
+9 -1
View File
@@ -4,7 +4,7 @@ use diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods};
use models::users::User;
use schema::comments;
#[derive(Queryable, Identifiable, Serialize)]
#[derive(Queryable, Identifiable, Serialize, Clone)]
pub struct Comment {
pub id: i32,
pub content: String,
@@ -51,6 +51,14 @@ impl Comment {
.expect("Error loading comment by post id")
}
pub fn get_by_ap_url(conn: &PgConnection, ap_url: String) -> Option<Comment> {
comments::table.filter(comments::ap_url.eq(ap_url))
.limit(1)
.load::<Comment>(conn)
.expect("Error loading comment by AP URL")
.into_iter().nth(0)
}
pub fn get_author(&self, conn: &PgConnection) -> User {
User::get(conn, self.author_id).unwrap()
}
+20 -2
View File
@@ -21,7 +21,8 @@ pub struct Post {
pub content: String,
pub published: bool,
pub license: String,
pub creation_date: NaiveDateTime
pub creation_date: NaiveDateTime,
pub ap_url: String
}
#[derive(Insertable)]
@@ -32,7 +33,8 @@ pub struct NewPost {
pub title: String,
pub content: String,
pub published: bool,
pub license: String
pub license: String,
pub ap_url: String
}
impl Post {
@@ -59,6 +61,14 @@ impl Post {
.into_iter().nth(0)
}
pub fn get_by_ap_url(conn: &PgConnection, ap_url: String) -> Option<Post> {
posts::table.filter(posts::ap_url.eq(ap_url))
.limit(1)
.load::<Post>(conn)
.expect("Error loading post by AP URL")
.into_iter().nth(0)
}
pub fn get_authors(&self, conn: &PgConnection) -> Vec<User> {
use schema::users;
use schema::post_authors;
@@ -74,6 +84,14 @@ impl Post {
.expect("Couldn't load blog associted to post")
.into_iter().nth(0).unwrap()
}
pub fn update_ap_url(&self, conn: &PgConnection) {
if self.ap_url.len() == 0 {
diesel::update(self)
.set(posts::ap_url.eq(self.compute_id(conn)))
.get_result::<Post>(conn).expect("Couldn't update AP URL");
}
}
}
impl Object for Post {