2018-06-10 11:13:07 +00:00
|
|
|
use activitypub::{
|
2018-05-18 22:04:30 +00:00
|
|
|
activity::Create,
|
2018-06-20 19:42:16 +00:00
|
|
|
link,
|
2018-06-21 10:38:07 +00:00
|
|
|
object::{Note}
|
2018-05-18 22:04:30 +00:00
|
|
|
};
|
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-06-23 16:36:11 +00:00
|
|
|
use plume_common::activity_pub::{
|
2018-06-26 14:21:58 +00:00
|
|
|
Id, IntoId, PUBLIC_VISIBILTY,
|
2018-06-20 09:01:25 +00:00
|
|
|
inbox::{FromActivity, Notify}
|
2018-05-19 07:39:59 +00:00
|
|
|
};
|
2018-06-23 16:36:11 +00:00
|
|
|
use plume_common::utils;
|
2018-09-26 15:22:42 +00:00
|
|
|
use {Connection, SqlDateTime};
|
2018-06-23 16:36:11 +00:00
|
|
|
use instance::Instance;
|
|
|
|
use mentions::Mention;
|
|
|
|
use notifications::*;
|
|
|
|
use posts::Post;
|
|
|
|
use 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,
|
2018-09-26 15:22:42 +00:00
|
|
|
pub creation_date: SqlDateTime,
|
2018-05-09 20:35:02 +00:00
|
|
|
pub ap_url: Option<String>,
|
|
|
|
pub sensitive: bool,
|
|
|
|
pub spoiler_text: String
|
|
|
|
}
|
|
|
|
|
2018-06-21 10:28:42 +00:00
|
|
|
#[derive(Insertable, Default)]
|
2018-05-09 20:35:02 +00:00
|
|
|
#[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-21 10:38:07 +00:00
|
|
|
list_by!(comments, list_by_post, post_id as i32);
|
2018-06-18 13:37:49 +00:00
|
|
|
find_by!(comments, find_by_ap_url, ap_url as String);
|
2018-05-10 10:52:56 +00:00
|
|
|
|
2018-09-26 15:22:42 +00:00
|
|
|
pub fn get_author(&self, conn: &Connection) -> User {
|
2018-05-10 09:44:57 +00:00
|
|
|
User::get(conn, self.author_id).unwrap()
|
|
|
|
}
|
2018-05-10 15:36:32 +00:00
|
|
|
|
2018-09-26 15:22:42 +00:00
|
|
|
pub fn get_post(&self, conn: &Connection) -> Post {
|
2018-09-07 22:29:50 +00:00
|
|
|
Post::get(conn, self.post_id).unwrap()
|
2018-05-10 15:36:32 +00:00
|
|
|
}
|
2018-05-18 22:04:30 +00:00
|
|
|
|
2018-09-26 15:22:42 +00:00
|
|
|
pub fn count_local(conn: &Connection) -> usize {
|
2018-06-10 19:33:42 +00:00
|
|
|
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-06-18 16:34:29 +00:00
|
|
|
|
2018-09-26 15:22:42 +00:00
|
|
|
pub fn to_json(&self, conn: &Connection, others: &Vec<Comment>) -> serde_json::Value {
|
2018-06-18 16:34:29 +00:00
|
|
|
let mut json = serde_json::to_value(self).unwrap();
|
|
|
|
json["author"] = self.get_author(conn).to_json(conn);
|
2018-06-21 13:05:35 +00:00
|
|
|
let mentions = Mention::list_for_comment(conn, self.id).into_iter()
|
|
|
|
.map(|m| m.get_mentioned(conn).map(|u| u.get_fqn(conn)).unwrap_or(String::new()))
|
|
|
|
.collect::<Vec<String>>();
|
|
|
|
json["mentions"] = serde_json::to_value(mentions).unwrap();
|
2018-07-25 16:18:41 +00:00
|
|
|
json["responses"] = json!(others.into_iter()
|
|
|
|
.filter(|c| c.in_response_to_id.map(|id| id == self.id).unwrap_or(false))
|
|
|
|
.map(|c| c.to_json(conn, others))
|
|
|
|
.collect::<Vec<_>>());
|
2018-06-18 16:34:29 +00:00
|
|
|
json
|
|
|
|
}
|
2018-06-20 09:01:25 +00:00
|
|
|
|
2018-09-26 15:22:42 +00:00
|
|
|
pub fn update_ap_url(&self, conn: &Connection) -> Comment {
|
2018-09-09 15:08:53 +00:00
|
|
|
if self.ap_url.is_none() {
|
|
|
|
diesel::update(self)
|
|
|
|
.set(comments::ap_url.eq(self.compute_id(conn)))
|
|
|
|
.get_result(conn)
|
|
|
|
.expect("Failed to update comment AP URL")
|
|
|
|
} else {
|
|
|
|
self.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 15:22:42 +00:00
|
|
|
pub fn compute_id(&self, conn: &Connection) -> String {
|
2018-09-09 15:22:58 +00:00
|
|
|
format!("{}comment/{}", self.get_post(conn).ap_url, self.id)
|
2018-09-09 15:08:53 +00:00
|
|
|
}
|
|
|
|
|
2018-09-26 15:22:42 +00:00
|
|
|
pub fn into_activity(&self, conn: &Connection) -> Note {
|
2018-09-09 15:08:53 +00:00
|
|
|
let (html, mentions) = utils::md_to_html(self.content.get().as_ref());
|
|
|
|
|
|
|
|
let author = User::get(conn, self.author_id).unwrap();
|
|
|
|
let mut note = Note::default();
|
|
|
|
let to = vec![Id::new(PUBLIC_VISIBILTY.to_string())];
|
|
|
|
|
|
|
|
note.object_props.set_id_string(self.ap_url.clone().unwrap_or(String::new())).expect("NewComment::create: note.id error");
|
|
|
|
note.object_props.set_summary_string(self.spoiler_text.clone()).expect("NewComment::create: note.summary error");
|
|
|
|
note.object_props.set_content_string(html).expect("NewComment::create: note.content error");
|
|
|
|
note.object_props.set_in_reply_to_link(Id::new(self.in_response_to_id.map_or_else(|| Post::get(conn, self.post_id).unwrap().ap_url, |id| {
|
|
|
|
let comm = Comment::get(conn, id).unwrap();
|
|
|
|
comm.ap_url.clone().unwrap_or(comm.compute_id(conn))
|
|
|
|
}))).expect("NewComment::create: note.in_reply_to error");
|
|
|
|
note.object_props.set_published_string(chrono::Utc::now().to_rfc3339()).expect("NewComment::create: note.published error");
|
|
|
|
note.object_props.set_attributed_to_link(author.clone().into_id()).expect("NewComment::create: note.attributed_to error");
|
|
|
|
note.object_props.set_to_link_vec(to.clone()).expect("NewComment::create: note.to error");
|
|
|
|
note.object_props.set_tag_link_vec(mentions.into_iter().map(|m| Mention::build_activity(conn, m)).collect::<Vec<link::Mention>>())
|
|
|
|
.expect("NewComment::create: note.tag error");
|
|
|
|
note
|
|
|
|
}
|
|
|
|
|
2018-09-26 15:22:42 +00:00
|
|
|
pub fn create_activity(&self, conn: &Connection) -> Create {
|
2018-09-09 15:08:53 +00:00
|
|
|
let author = User::get(conn, self.author_id).unwrap();
|
|
|
|
|
|
|
|
let note = self.into_activity(conn);
|
|
|
|
let mut act = Create::default();
|
|
|
|
act.create_props.set_actor_link(author.into_id()).expect("NewComment::create_acitivity: actor error");
|
|
|
|
act.create_props.set_object_object(note.clone()).expect("NewComment::create_acitivity: object error");
|
|
|
|
act.object_props.set_id_string(format!("{}/activity", self.ap_url.clone().unwrap())).expect("NewComment::create_acitivity: id error");
|
|
|
|
act.object_props.set_to_link_vec(note.object_props.to_link_vec::<Id>().expect("WTF")).expect("NewComment::create_acitivity: to error");
|
|
|
|
act.object_props.set_cc_link_vec::<Id>(vec![]).expect("NewComment::create_acitivity: cc error");
|
|
|
|
act
|
2018-06-20 09:01:25 +00:00
|
|
|
}
|
2018-05-10 15:36:32 +00:00
|
|
|
}
|
|
|
|
|
2018-06-23 16:36:11 +00:00
|
|
|
impl FromActivity<Note, PgConnection> for Comment {
|
2018-09-26 15:22:42 +00:00
|
|
|
fn from_activity(conn: &Connection, note: Note, actor: Id) -> Comment {
|
2018-06-12 19:10:08 +00:00
|
|
|
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-20 19:42:16 +00:00
|
|
|
|
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
|
|
|
});
|
2018-06-21 13:05:35 +00:00
|
|
|
|
2018-09-07 22:29:50 +00:00
|
|
|
// save mentions
|
2018-06-21 13:05:35 +00:00
|
|
|
if let Some(serde_json::Value::Array(tags)) = note.object_props.tag.clone() {
|
|
|
|
for tag in tags.into_iter() {
|
|
|
|
serde_json::from_value::<link::Mention>(tag)
|
2018-09-08 11:52:27 +00:00
|
|
|
.map(|m| {
|
|
|
|
let author = &Post::get(conn, comm.post_id).unwrap().get_authors(conn)[0];
|
|
|
|
let not_author = m.link_props.href_string().expect("Comment mention: no href") != author.ap_url.clone();
|
|
|
|
Mention::from_activity(conn, m, comm.id, false, not_author)
|
|
|
|
}).ok();
|
2018-06-21 13:05:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-20 21:51:47 +00:00
|
|
|
comm.notify(conn);
|
2018-06-17 19:37:10 +00:00
|
|
|
comm
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-23 16:36:11 +00:00
|
|
|
impl Notify<PgConnection> for Comment {
|
2018-09-26 15:22:42 +00:00
|
|
|
fn notify(&self, conn: &Connection) {
|
2018-06-20 21:51:47 +00:00
|
|
|
for author in self.get_post(conn).get_authors(conn) {
|
|
|
|
Notification::insert(conn, NewNotification {
|
2018-07-26 13:46:10 +00:00
|
|
|
kind: notification_kind::COMMENT.to_string(),
|
|
|
|
object_id: self.id,
|
2018-06-20 21:51:47 +00:00
|
|
|
user_id: author.id
|
|
|
|
});
|
|
|
|
}
|
2018-06-12 19:10:08 +00:00
|
|
|
}
|
|
|
|
}
|