2018-06-10 11:13:07 +00:00
|
|
|
use activitypub::{
|
2018-05-18 22:04:30 +00:00
|
|
|
activity::Create,
|
|
|
|
object::{Note, properties::ObjectProperties}
|
|
|
|
};
|
2018-05-09 20:35:02 +00:00
|
|
|
use chrono;
|
2018-06-10 19:33:42 +00:00
|
|
|
use diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods, dsl::any};
|
2018-05-10 15:36:32 +00:00
|
|
|
use serde_json;
|
2018-05-09 20:35:02 +00:00
|
|
|
|
2018-05-19 07:39:59 +00:00
|
|
|
use activity_pub::{
|
2018-06-12 19:10:08 +00:00
|
|
|
ap_url, Id, IntoId, PUBLIC_VISIBILTY,
|
2018-05-19 07:39:59 +00:00
|
|
|
actor::Actor,
|
2018-06-17 19:37:10 +00:00
|
|
|
inbox::{FromActivity, Notify},
|
2018-05-19 07:39:59 +00:00
|
|
|
object::Object
|
|
|
|
};
|
|
|
|
use models::{
|
2018-06-10 19:33:42 +00:00
|
|
|
instance::Instance,
|
2018-06-17 19:37:10 +00:00
|
|
|
notifications::*,
|
2018-05-19 07:39:59 +00:00
|
|
|
posts::Post,
|
|
|
|
users::User
|
|
|
|
};
|
2018-05-09 20:35:02 +00:00
|
|
|
use schema::comments;
|
2018-06-11 10:21:34 +00:00
|
|
|
use safe_string::SafeString;
|
2018-05-09 20:35:02 +00:00
|
|
|
|
2018-05-10 10:52:56 +00:00
|
|
|
#[derive(Queryable, Identifiable, Serialize, Clone)]
|
2018-05-09 20:35:02 +00:00
|
|
|
pub struct Comment {
|
|
|
|
pub id: i32,
|
2018-06-11 10:21:34 +00:00
|
|
|
pub content: SafeString,
|
2018-05-09 20:35:02 +00:00
|
|
|
pub in_response_to_id: Option<i32>,
|
|
|
|
pub post_id: i32,
|
|
|
|
pub author_id: i32,
|
|
|
|
pub creation_date: chrono::NaiveDateTime,
|
|
|
|
pub ap_url: Option<String>,
|
|
|
|
pub sensitive: bool,
|
|
|
|
pub spoiler_text: String
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Insertable)]
|
|
|
|
#[table_name = "comments"]
|
|
|
|
pub struct NewComment {
|
2018-06-11 10:21:34 +00:00
|
|
|
pub content: SafeString,
|
2018-05-09 20:35:02 +00:00
|
|
|
pub in_response_to_id: Option<i32>,
|
|
|
|
pub post_id: i32,
|
|
|
|
pub author_id: i32,
|
|
|
|
pub ap_url: Option<String>,
|
|
|
|
pub sensitive: bool,
|
|
|
|
pub spoiler_text: String
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Comment {
|
2018-06-18 13:57:38 +00:00
|
|
|
insert!(comments, NewComment);
|
2018-06-18 13:44:23 +00:00
|
|
|
get!(comments);
|
2018-06-18 13:37:49 +00:00
|
|
|
find_by!(comments, find_by_post, post_id as i32);
|
|
|
|
find_by!(comments, find_by_ap_url, ap_url as String);
|
2018-05-10 10:52:56 +00:00
|
|
|
|
2018-05-10 09:44:57 +00:00
|
|
|
pub fn get_author(&self, conn: &PgConnection) -> User {
|
|
|
|
User::get(conn, self.author_id).unwrap()
|
|
|
|
}
|
2018-05-10 15:36:32 +00:00
|
|
|
|
|
|
|
pub fn get_post(&self, conn: &PgConnection) -> Post {
|
|
|
|
Post::get(conn, self.post_id).unwrap()
|
|
|
|
}
|
2018-05-18 22:04:30 +00:00
|
|
|
|
|
|
|
pub fn into_activity(&self, conn: &PgConnection) -> Note {
|
|
|
|
let mut to = self.get_author(conn).get_followers(conn).into_iter().map(|f| f.ap_url).collect::<Vec<String>>();
|
|
|
|
to.append(&mut self.get_post(conn).get_receivers_urls(conn));
|
|
|
|
to.push(PUBLIC_VISIBILTY.to_string());
|
|
|
|
|
|
|
|
let mut comment = Note::default();
|
|
|
|
comment.object_props = ObjectProperties {
|
|
|
|
id: Some(serde_json::to_value(self.ap_url.clone()).unwrap()),
|
|
|
|
summary: Some(serde_json::to_value(self.spoiler_text.clone()).unwrap()),
|
|
|
|
content: Some(serde_json::to_value(self.content.clone()).unwrap()),
|
|
|
|
in_reply_to: Some(serde_json::to_value(self.in_response_to_id.map_or_else(|| self.get_post(conn).ap_url, |id| {
|
|
|
|
let comm = Comment::get(conn, id).unwrap();
|
|
|
|
comm.ap_url.clone().unwrap_or(comm.compute_id(conn))
|
|
|
|
})).unwrap()),
|
|
|
|
published: Some(serde_json::to_value(self.creation_date).unwrap()),
|
|
|
|
attributed_to: Some(serde_json::to_value(self.get_author(conn).compute_id(conn)).unwrap()),
|
|
|
|
to: Some(serde_json::to_value(to).unwrap()),
|
|
|
|
cc: Some(serde_json::to_value(Vec::<serde_json::Value>::new()).unwrap()),
|
|
|
|
..ObjectProperties::default()
|
|
|
|
};
|
|
|
|
comment
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_activity(&self, conn: &PgConnection) -> Create {
|
|
|
|
let mut act = Create::default();
|
2018-06-10 11:13:07 +00:00
|
|
|
act.create_props.set_actor_link(self.get_author(conn).into_id()).unwrap();
|
|
|
|
act.create_props.set_object_object(self.into_activity(conn)).unwrap();
|
2018-05-18 22:04:30 +00:00
|
|
|
act.object_props.set_id_string(format!("{}/activity", self.ap_url.clone().unwrap())).unwrap();
|
|
|
|
act
|
|
|
|
}
|
2018-06-10 19:33:42 +00:00
|
|
|
|
|
|
|
pub fn count_local(conn: &PgConnection) -> usize {
|
|
|
|
use schema::users;
|
|
|
|
let local_authors = users::table.filter(users::instance_id.eq(Instance::local_id(conn))).select(users::id);
|
|
|
|
comments::table.filter(comments::author_id.eq(any(local_authors)))
|
|
|
|
.load::<Comment>(conn)
|
|
|
|
.expect("Couldn't load local comments")
|
|
|
|
.len()
|
|
|
|
}
|
2018-05-10 15:36:32 +00:00
|
|
|
}
|
|
|
|
|
2018-06-12 19:10:08 +00:00
|
|
|
impl FromActivity<Note> for Comment {
|
|
|
|
fn from_activity(conn: &PgConnection, note: Note, actor: Id) -> Comment {
|
|
|
|
let previous_url = note.object_props.in_reply_to.clone().unwrap().as_str().unwrap().to_string();
|
|
|
|
let previous_comment = Comment::find_by_ap_url(conn, previous_url.clone());
|
2018-06-17 19:37:10 +00:00
|
|
|
let comm = Comment::insert(conn, NewComment {
|
2018-06-12 19:10:08 +00:00
|
|
|
content: SafeString::new(¬e.object_props.content_string().unwrap()),
|
|
|
|
spoiler_text: note.object_props.summary_string().unwrap_or(String::from("")),
|
|
|
|
ap_url: note.object_props.id_string().ok(),
|
|
|
|
in_response_to_id: previous_comment.clone().map(|c| c.id),
|
|
|
|
post_id: previous_comment
|
|
|
|
.map(|c| c.post_id)
|
|
|
|
.unwrap_or_else(|| Post::find_by_ap_url(conn, previous_url).unwrap().id),
|
2018-06-17 19:37:10 +00:00
|
|
|
author_id: User::from_url(conn, actor.clone().into()).unwrap().id,
|
2018-06-12 19:10:08 +00:00
|
|
|
sensitive: false // "sensitive" is not a standard property, we need to think about how to support it with the activitypub crate
|
2018-06-17 19:37:10 +00:00
|
|
|
});
|
|
|
|
Comment::notify(conn, note, actor);
|
|
|
|
comm
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Notify<Note> for Comment {
|
|
|
|
fn notify(conn: &PgConnection, note: Note, _actor: Id) {
|
|
|
|
match Comment::find_by_ap_url(conn, note.object_props.id_string().unwrap()) {
|
|
|
|
Some(comment) => {
|
|
|
|
for author in comment.clone().get_post(conn).get_authors(conn) {
|
|
|
|
let comment = comment.clone();
|
|
|
|
Notification::insert(conn, NewNotification {
|
2018-06-17 20:19:27 +00:00
|
|
|
title: "{{ data }} commented your article".to_string(),
|
|
|
|
data: Some(comment.get_author(conn).display_name.clone()),
|
2018-06-17 19:37:10 +00:00
|
|
|
content: Some(comment.get_post(conn).title),
|
|
|
|
link: comment.ap_url,
|
|
|
|
user_id: author.id
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => println!("Couldn't find comment by AP id, to create a new notification")
|
|
|
|
};
|
2018-06-12 19:10:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-10 15:36:32 +00:00
|
|
|
impl Object for Comment {
|
|
|
|
fn serialize(&self, conn: &PgConnection) -> serde_json::Value {
|
|
|
|
let mut to = self.get_author(conn).get_followers(conn).into_iter().map(|f| f.ap_url).collect::<Vec<String>>();
|
|
|
|
to.append(&mut self.get_post(conn).get_receivers_urls(conn));
|
|
|
|
to.push(PUBLIC_VISIBILTY.to_string());
|
|
|
|
|
|
|
|
json!({
|
|
|
|
"id": self.compute_id(conn),
|
|
|
|
"type": "Note",
|
|
|
|
"summary": self.spoiler_text,
|
|
|
|
"content": self.content,
|
|
|
|
"inReplyTo": self.in_response_to_id.map_or_else(|| self.get_post(conn).ap_url, |id| {
|
|
|
|
let comm = Comment::get(conn, id).unwrap();
|
|
|
|
comm.ap_url.clone().unwrap_or(comm.compute_id(conn))
|
|
|
|
}),
|
|
|
|
"published": self.creation_date,
|
|
|
|
"attributedTo": self.get_author(conn).compute_id(conn),
|
|
|
|
"to": to,
|
|
|
|
"cc": [],
|
|
|
|
"sensitive": self.sensitive,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compute_id(&self, conn: &PgConnection) -> String {
|
|
|
|
ap_url(format!("{}#comment-{}", self.get_post(conn).compute_id(conn), self.id))
|
|
|
|
}
|
2018-05-09 20:35:02 +00:00
|
|
|
}
|