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

View File

@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
ALTER TABLE posts DROP COLUMN ap_url;

View File

@ -0,0 +1,2 @@
-- Your SQL goes here
ALTER TABLE posts ADD COLUMN ap_url VARCHAR NOT NULL DEFAULT '';

View File

@ -5,6 +5,7 @@ use activity_pub::activity;
use activity_pub::actor::Actor;
use activity_pub::sign::*;
use models::blogs::Blog;
use models::comments::*;
use models::follows::{Follow, NewFollow};
use models::posts::{Post, NewPost};
use models::users::User;
@ -18,14 +19,29 @@ pub trait Inbox: Actor + Sized {
match act["object"]["type"].as_str().unwrap() {
"Article" => {
Post::insert(conn, NewPost {
blog_id: 0,
slug: String::from(""),
title: String::from(""),
blog_id: 0, // TODO
slug: String::from(""), // TODO
title: String::from(""), // TODO
content: act["object"]["content"].as_str().unwrap().to_string(),
published: true,
license: String::from("CC-0")
license: String::from("CC-0"),
ap_url: act["object"]["url"].as_str().unwrap().to_string()
});
},
"Note" => {
let previous_comment = Comment::get_by_ap_url(conn, act["object"]["inReplyTo"].as_str().unwrap().to_string());
Comment::insert(conn, NewComment {
content: act["object"]["content"].as_str().unwrap().to_string(),
spoiler_text: act["object"]["summary"].as_str().unwrap_or("").to_string(),
ap_url: Some(act["object"]["id"].as_str().unwrap().to_string()),
in_response_to_id: previous_comment.clone().map(|c| c.id),
post_id: previous_comment
.map(|c| c.post_id)
.unwrap_or(Post::get_by_ap_url(conn, act["object"]["inReplyTo"].as_str().unwrap().to_string()).unwrap().id),
author_id: User::from_url(conn, act["actor"].as_str().unwrap().to_string()).unwrap().id,
sensitive: act["object"]["sensitive"].as_bool().unwrap_or(false)
});
}
x => println!("Received a new {}, but didn't saved it", x)
}
},

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()
}

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 {

View File

@ -72,8 +72,10 @@ fn create(blog_name: String, data: Form<NewPostForm>, user: User, conn: DbConn)
title: form.title.to_string(),
content: form.content.to_string(),
published: true,
license: form.license.to_string()
license: form.license.to_string(),
ap_url: "".to_string()
});
post.update_ap_url(&*conn);
PostAuthor::insert(&*conn, NewPostAuthor {
post_id: post.id,
author_id: user.id

View File

@ -74,6 +74,7 @@ table! {
published -> Bool,
license -> Varchar,
creation_date -> Timestamp,
ap_url -> Varchar,
}
}