2018-12-23 11:13:36 +01:00
|
|
|
use activitypub::{activity::{Create, Delete}, link, object::{Note, Tombstone}};
|
2018-09-27 23:06:40 +02:00
|
|
|
use chrono::{self, NaiveDateTime};
|
2018-11-24 12:44:17 +01:00
|
|
|
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
|
2018-05-10 17:36:32 +02:00
|
|
|
use serde_json;
|
2018-05-09 22:35:02 +02:00
|
|
|
|
2018-12-24 11:23:04 +01:00
|
|
|
use std::collections::HashSet;
|
|
|
|
|
2018-11-24 12:44:17 +01:00
|
|
|
use instance::Instance;
|
|
|
|
use mentions::Mention;
|
|
|
|
use notifications::*;
|
2018-06-23 18:36:11 +02:00
|
|
|
use plume_common::activity_pub::{
|
2018-12-23 11:13:36 +01:00
|
|
|
inbox::{FromActivity, Notify, Deletable},
|
2018-06-26 16:21:58 +02:00
|
|
|
Id, IntoId, PUBLIC_VISIBILTY,
|
2018-05-19 09:39:59 +02:00
|
|
|
};
|
2018-06-23 18:36:11 +02:00
|
|
|
use plume_common::utils;
|
2018-12-24 11:23:04 +01:00
|
|
|
use comment_seers::{CommentSeers, NewCommentSeers};
|
2018-06-23 18:36:11 +02:00
|
|
|
use posts::Post;
|
2018-06-11 12:21:34 +02:00
|
|
|
use safe_string::SafeString;
|
2018-11-24 12:44:17 +01:00
|
|
|
use schema::comments;
|
|
|
|
use users::User;
|
|
|
|
use Connection;
|
2018-05-09 22:35:02 +02:00
|
|
|
|
2018-05-10 12:52:56 +02:00
|
|
|
#[derive(Queryable, Identifiable, Serialize, Clone)]
|
2018-05-09 22:35:02 +02:00
|
|
|
pub struct Comment {
|
|
|
|
pub id: i32,
|
2018-06-11 12:21:34 +02:00
|
|
|
pub content: SafeString,
|
2018-05-09 22:35:02 +02:00
|
|
|
pub in_response_to_id: Option<i32>,
|
|
|
|
pub post_id: i32,
|
|
|
|
pub author_id: i32,
|
2018-09-27 23:06:40 +02:00
|
|
|
pub creation_date: NaiveDateTime,
|
2018-05-09 22:35:02 +02:00
|
|
|
pub ap_url: Option<String>,
|
|
|
|
pub sensitive: bool,
|
2018-11-24 12:44:17 +01:00
|
|
|
pub spoiler_text: String,
|
2018-12-24 11:23:04 +01:00
|
|
|
pub public_visibility: bool,
|
2018-05-09 22:35:02 +02:00
|
|
|
}
|
|
|
|
|
2018-06-21 12:28:42 +02:00
|
|
|
#[derive(Insertable, Default)]
|
2018-05-09 22:35:02 +02:00
|
|
|
#[table_name = "comments"]
|
|
|
|
pub struct NewComment {
|
2018-06-11 12:21:34 +02:00
|
|
|
pub content: SafeString,
|
2018-05-09 22:35:02 +02:00
|
|
|
pub in_response_to_id: Option<i32>,
|
|
|
|
pub post_id: i32,
|
|
|
|
pub author_id: i32,
|
|
|
|
pub ap_url: Option<String>,
|
|
|
|
pub sensitive: bool,
|
2018-11-24 12:44:17 +01:00
|
|
|
pub spoiler_text: String,
|
2018-12-24 11:23:04 +01:00
|
|
|
pub public_visibility: bool,
|
2018-05-09 22:35:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Comment {
|
2018-06-18 15:57:38 +02:00
|
|
|
insert!(comments, NewComment);
|
2018-06-18 15:44:23 +02:00
|
|
|
get!(comments);
|
2018-06-21 12:38:07 +02:00
|
|
|
list_by!(comments, list_by_post, post_id as i32);
|
2018-11-26 10:21:52 +01:00
|
|
|
find_by!(comments, find_by_ap_url, ap_url as &str);
|
2018-05-10 12:52:56 +02:00
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn get_author(&self, conn: &Connection) -> User {
|
2018-10-20 08:44:33 +02:00
|
|
|
User::get(conn, self.author_id).expect("Comment::get_author: author error")
|
2018-05-10 11:44:57 +02:00
|
|
|
}
|
2018-05-10 17:36:32 +02:00
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn get_post(&self, conn: &Connection) -> Post {
|
2018-10-20 08:44:33 +02:00
|
|
|
Post::get(conn, self.post_id).expect("Comment::get_post: post error")
|
2018-05-10 17:36:32 +02:00
|
|
|
}
|
2018-05-19 00:04:30 +02:00
|
|
|
|
2018-12-14 23:16:18 +01:00
|
|
|
pub fn count_local(conn: &Connection) -> i64 {
|
2018-06-10 21:33:42 +02:00
|
|
|
use schema::users;
|
2018-11-24 12:44:17 +01:00
|
|
|
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))
|
2018-12-14 23:16:18 +01:00
|
|
|
.count()
|
|
|
|
.get_result(conn)
|
2018-10-20 08:44:33 +02:00
|
|
|
.expect("Comment::count_local: loading error")
|
2018-06-10 21:33:42 +02:00
|
|
|
}
|
2018-06-18 18:34:29 +02:00
|
|
|
|
2018-12-06 18:54:16 +01:00
|
|
|
pub fn get_responses(&self, conn: &Connection) -> Vec<Comment> {
|
|
|
|
comments::table.filter(comments::in_response_to_id.eq(self.id))
|
|
|
|
.load::<Comment>(conn)
|
|
|
|
.expect("Comment::get_responses: loading error")
|
2018-06-18 18:34:29 +02:00
|
|
|
}
|
2018-06-20 11:01:25 +02:00
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn update_ap_url(&self, conn: &Connection) -> Comment {
|
2018-09-09 17:08:53 +02:00
|
|
|
if self.ap_url.is_none() {
|
|
|
|
diesel::update(self)
|
|
|
|
.set(comments::ap_url.eq(self.compute_id(conn)))
|
2018-09-27 23:06:40 +02:00
|
|
|
.execute(conn)
|
2018-10-20 08:44:33 +02:00
|
|
|
.expect("Comment::update_ap_url: update error");
|
|
|
|
Comment::get(conn, self.id).expect("Comment::update_ap_url: get error")
|
2018-09-09 17:08:53 +02:00
|
|
|
} else {
|
|
|
|
self.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn compute_id(&self, conn: &Connection) -> String {
|
2018-09-09 17:22:58 +02:00
|
|
|
format!("{}comment/{}", self.get_post(conn).ap_url, self.id)
|
2018-09-09 17:08:53 +02:00
|
|
|
}
|
|
|
|
|
2018-12-24 11:23:04 +01:00
|
|
|
pub fn can_see(&self, conn: &Connection, user: Option<&User>) -> bool {
|
|
|
|
self.public_visibility ||
|
|
|
|
user.as_ref().map(|u| CommentSeers::can_see(conn, self, u)).unwrap_or(false)
|
|
|
|
}
|
|
|
|
|
2018-11-26 10:21:52 +01:00
|
|
|
pub fn to_activity(&self, conn: &Connection) -> Note {
|
2018-12-23 11:12:15 +01:00
|
|
|
let (html, mentions, _hashtags) = utils::md_to_html(self.content.get().as_ref(),
|
|
|
|
&Instance::get_local(conn)
|
|
|
|
.expect("Comment::to_activity: instance error")
|
|
|
|
.public_domain);
|
2018-09-09 17:08:53 +02:00
|
|
|
|
2018-11-26 10:21:52 +01:00
|
|
|
let author = User::get(conn, self.author_id).expect("Comment::to_activity: author error");
|
2018-09-09 17:08:53 +02:00
|
|
|
let mut note = Note::default();
|
|
|
|
let to = vec![Id::new(PUBLIC_VISIBILTY.to_string())];
|
|
|
|
|
2018-11-24 12:44:17 +01:00
|
|
|
note.object_props
|
2018-11-26 10:21:52 +01:00
|
|
|
.set_id_string(self.ap_url.clone().unwrap_or_default())
|
|
|
|
.expect("Comment::to_activity: id error");
|
2018-11-24 12:44:17 +01:00
|
|
|
note.object_props
|
|
|
|
.set_summary_string(self.spoiler_text.clone())
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("Comment::to_activity: summary error");
|
2018-11-24 12:44:17 +01:00
|
|
|
note.object_props
|
|
|
|
.set_content_string(html)
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("Comment::to_activity: content error");
|
2018-11-24 12:44:17 +01:00
|
|
|
note.object_props
|
|
|
|
.set_in_reply_to_link(Id::new(self.in_response_to_id.map_or_else(
|
|
|
|
|| {
|
|
|
|
Post::get(conn, self.post_id)
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("Comment::to_activity: post error")
|
2018-11-24 12:44:17 +01:00
|
|
|
.ap_url
|
|
|
|
},
|
|
|
|
|id| {
|
|
|
|
let comm =
|
2018-11-26 10:21:52 +01:00
|
|
|
Comment::get(conn, id).expect("Comment::to_activity: comment error");
|
|
|
|
comm.ap_url.clone().unwrap_or_else(|| comm.compute_id(conn))
|
2018-11-24 12:44:17 +01:00
|
|
|
},
|
|
|
|
)))
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("Comment::to_activity: in_reply_to error");
|
2018-11-24 12:44:17 +01:00
|
|
|
note.object_props
|
|
|
|
.set_published_string(chrono::Utc::now().to_rfc3339())
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("Comment::to_activity: published error");
|
2018-11-24 12:44:17 +01:00
|
|
|
note.object_props
|
|
|
|
.set_attributed_to_link(author.clone().into_id())
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("Comment::to_activity: attributed_to error");
|
2018-11-24 12:44:17 +01:00
|
|
|
note.object_props
|
|
|
|
.set_to_link_vec(to.clone())
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("Comment::to_activity: to error");
|
2018-11-24 12:44:17 +01:00
|
|
|
note.object_props
|
|
|
|
.set_tag_link_vec(
|
|
|
|
mentions
|
|
|
|
.into_iter()
|
2018-11-26 10:21:52 +01:00
|
|
|
.map(|m| Mention::build_activity(conn, &m))
|
2018-11-24 12:44:17 +01:00
|
|
|
.collect::<Vec<link::Mention>>(),
|
|
|
|
)
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("Comment::to_activity: tag error");
|
2018-09-09 17:08:53 +02:00
|
|
|
note
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn create_activity(&self, conn: &Connection) -> Create {
|
2018-11-24 12:44:17 +01:00
|
|
|
let author =
|
|
|
|
User::get(conn, self.author_id).expect("Comment::create_activity: author error");
|
2018-09-09 17:08:53 +02:00
|
|
|
|
2018-11-26 10:21:52 +01:00
|
|
|
let note = self.to_activity(conn);
|
2018-09-09 17:08:53 +02:00
|
|
|
let mut act = Create::default();
|
2018-11-24 12:44:17 +01:00
|
|
|
act.create_props
|
|
|
|
.set_actor_link(author.into_id())
|
|
|
|
.expect("Comment::create_activity: actor error");
|
|
|
|
act.create_props
|
|
|
|
.set_object_object(note.clone())
|
|
|
|
.expect("Comment::create_activity: object error");
|
|
|
|
act.object_props
|
|
|
|
.set_id_string(format!(
|
|
|
|
"{}/activity",
|
|
|
|
self.ap_url
|
|
|
|
.clone()
|
|
|
|
.expect("Comment::create_activity: ap_url error")
|
|
|
|
))
|
|
|
|
.expect("Comment::create_activity: id error");
|
|
|
|
act.object_props
|
|
|
|
.set_to_link_vec(
|
|
|
|
note.object_props
|
|
|
|
.to_link_vec::<Id>()
|
|
|
|
.expect("Comment::create_activity: id error"),
|
|
|
|
)
|
|
|
|
.expect("Comment::create_activity: to error");
|
|
|
|
act.object_props
|
|
|
|
.set_cc_link_vec::<Id>(vec![])
|
|
|
|
.expect("Comment::create_activity: cc error");
|
2018-09-09 17:08:53 +02:00
|
|
|
act
|
2018-06-20 11:01:25 +02:00
|
|
|
}
|
2018-05-10 17:36:32 +02:00
|
|
|
}
|
|
|
|
|
2018-09-27 23:06:40 +02:00
|
|
|
impl FromActivity<Note, Connection> for Comment {
|
2018-09-26 17:22:42 +02:00
|
|
|
fn from_activity(conn: &Connection, note: Note, actor: Id) -> Comment {
|
2018-12-24 11:23:04 +01:00
|
|
|
let comm = {
|
|
|
|
let previous_url = note
|
|
|
|
.object_props
|
|
|
|
.in_reply_to
|
|
|
|
.as_ref()
|
|
|
|
.expect("Comment::from_activity: not an answer error")
|
|
|
|
.as_str()
|
|
|
|
.expect("Comment::from_activity: in_reply_to parsing error");
|
|
|
|
let previous_comment = Comment::find_by_ap_url(conn, previous_url);
|
|
|
|
|
|
|
|
let is_public = |v: &Option<serde_json::Value>| match v.as_ref().unwrap_or(&serde_json::Value::Null) {
|
|
|
|
serde_json::Value::Array(v) => v.iter().filter_map(serde_json::Value::as_str).any(|s| s==PUBLIC_VISIBILTY),
|
|
|
|
serde_json::Value::String(s) => s == PUBLIC_VISIBILTY,
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
|
|
|
|
let public_visibility = is_public(¬e.object_props.to) ||
|
|
|
|
is_public(¬e.object_props.bto) ||
|
|
|
|
is_public(¬e.object_props.cc) ||
|
|
|
|
is_public(¬e.object_props.bcc);
|
|
|
|
|
|
|
|
let comm = Comment::insert(
|
|
|
|
conn,
|
|
|
|
NewComment {
|
|
|
|
content: SafeString::new(
|
|
|
|
¬e
|
|
|
|
.object_props
|
|
|
|
.content_string()
|
|
|
|
.expect("Comment::from_activity: content deserialization error"),
|
|
|
|
),
|
|
|
|
spoiler_text: note
|
2018-11-24 12:44:17 +01:00
|
|
|
.object_props
|
2018-12-24 11:23:04 +01:00
|
|
|
.summary_string()
|
|
|
|
.unwrap_or_default(),
|
|
|
|
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)
|
|
|
|
.expect("Comment::from_activity: post error")
|
|
|
|
.id
|
|
|
|
}),
|
|
|
|
author_id: User::from_url(conn, actor.as_ref())
|
|
|
|
.expect("Comment::from_activity: author error")
|
|
|
|
.id,
|
|
|
|
sensitive: false, // "sensitive" is not a standard property, we need to think about how to support it with the activitypub crate
|
|
|
|
public_visibility
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
// save mentions
|
|
|
|
if let Some(serde_json::Value::Array(tags)) = note.object_props.tag.clone() {
|
|
|
|
for tag in tags {
|
|
|
|
serde_json::from_value::<link::Mention>(tag)
|
|
|
|
.map(|m| {
|
|
|
|
let author = &Post::get(conn, comm.post_id)
|
|
|
|
.expect("Comment::from_activity: error")
|
|
|
|
.get_authors(conn)[0];
|
|
|
|
let not_author = m
|
|
|
|
.link_props
|
|
|
|
.href_string()
|
|
|
|
.expect("Comment::from_activity: no href error")
|
|
|
|
!= author.ap_url.clone();
|
|
|
|
Mention::from_activity(conn, &m, comm.id, false, not_author)
|
|
|
|
})
|
|
|
|
.ok();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
comm
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if !comm.public_visibility {
|
|
|
|
let receivers_ap_url = |v: Option<serde_json::Value>| {
|
|
|
|
let filter = |e: serde_json::Value| if let serde_json::Value::String(s) = e { Some(s) } else { None };
|
|
|
|
match v.unwrap_or(serde_json::Value::Null) {
|
|
|
|
serde_json::Value::Array(v) => v,
|
|
|
|
v => vec![v],
|
|
|
|
}.into_iter().filter_map(filter)
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut note = note;
|
|
|
|
|
|
|
|
let to = receivers_ap_url(note.object_props.to.take());
|
|
|
|
let cc = receivers_ap_url(note.object_props.cc.take());
|
|
|
|
let bto = receivers_ap_url(note.object_props.bto.take());
|
|
|
|
let bcc = receivers_ap_url(note.object_props.bcc.take());
|
|
|
|
|
|
|
|
let receivers_ap_url = to.chain(cc).chain(bto).chain(bcc)
|
|
|
|
.collect::<HashSet<_>>()//remove duplicates (don't do a query more than once)
|
|
|
|
.into_iter()
|
|
|
|
.map(|v| if let Some(user) = User::from_url(conn,&v) {
|
|
|
|
vec![user]
|
|
|
|
} else {
|
|
|
|
vec![]// TODO try to fetch collection
|
|
|
|
})
|
|
|
|
.flatten()
|
|
|
|
.filter(|u| u.get_instance(conn).local)
|
|
|
|
.collect::<HashSet<User>>();//remove duplicates (prevent db error)
|
|
|
|
|
|
|
|
for user in &receivers_ap_url {
|
|
|
|
CommentSeers::insert(
|
|
|
|
conn,
|
|
|
|
NewCommentSeers {
|
|
|
|
comment_id: comm.id,
|
|
|
|
user_id: user.id
|
|
|
|
}
|
|
|
|
);
|
2018-06-21 15:05:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-20 23:51:47 +02:00
|
|
|
comm.notify(conn);
|
2018-06-17 21:37:10 +02:00
|
|
|
comm
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-27 23:06:40 +02:00
|
|
|
impl Notify<Connection> for Comment {
|
2018-09-26 17:22:42 +02:00
|
|
|
fn notify(&self, conn: &Connection) {
|
2018-06-20 23:51:47 +02:00
|
|
|
for author in self.get_post(conn).get_authors(conn) {
|
2018-11-24 12:44:17 +01:00
|
|
|
Notification::insert(
|
|
|
|
conn,
|
|
|
|
NewNotification {
|
|
|
|
kind: notification_kind::COMMENT.to_string(),
|
|
|
|
object_id: self.id,
|
|
|
|
user_id: author.id,
|
|
|
|
},
|
|
|
|
);
|
2018-06-20 23:51:47 +02:00
|
|
|
}
|
2018-06-12 21:10:08 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-23 11:13:36 +01:00
|
|
|
|
2018-12-24 11:23:04 +01:00
|
|
|
pub struct CommentTree {
|
|
|
|
pub comment: Comment,
|
|
|
|
pub responses: Vec<CommentTree>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CommentTree {
|
|
|
|
pub fn from_post(conn: &Connection, p: &Post, user: Option<&User>) -> Vec<Self> {
|
|
|
|
Comment::list_by_post(conn, p.id).into_iter()
|
|
|
|
.filter(|c| c.in_response_to_id.is_none())
|
|
|
|
.filter(|c| c.can_see(conn, user))
|
|
|
|
.map(|c| Self::from_comment(conn, c, user))
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_comment(conn: &Connection, comment: Comment, user: Option<&User>) -> Self {
|
|
|
|
let responses = comment.get_responses(conn).into_iter()
|
|
|
|
.filter(|c| c.can_see(conn, user))
|
|
|
|
.map(|c| Self::from_comment(conn, c, user))
|
|
|
|
.collect();
|
|
|
|
CommentTree {
|
|
|
|
comment,
|
|
|
|
responses,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-23 11:13:36 +01:00
|
|
|
|
|
|
|
impl<'a> Deletable<Connection, Delete> for Comment {
|
|
|
|
fn delete(&self, conn: &Connection) -> Delete {
|
|
|
|
let mut act = Delete::default();
|
|
|
|
act.delete_props
|
|
|
|
.set_actor_link(self.get_author(conn).into_id())
|
|
|
|
.expect("Comment::delete: actor error");
|
|
|
|
|
|
|
|
let mut tombstone = Tombstone::default();
|
|
|
|
tombstone
|
|
|
|
.object_props
|
|
|
|
.set_id_string(self.ap_url.clone().expect("Comment::delete: no ap_url"))
|
|
|
|
.expect("Comment::delete: object.id error");
|
|
|
|
act.delete_props
|
|
|
|
.set_object_object(tombstone)
|
|
|
|
.expect("Comment::delete: object error");
|
|
|
|
|
|
|
|
act.object_props
|
|
|
|
.set_id_string(format!("{}#delete", self.ap_url.clone().unwrap()))
|
|
|
|
.expect("Comment::delete: id error");
|
|
|
|
act.object_props
|
|
|
|
.set_to_link_vec(vec![Id::new(PUBLIC_VISIBILTY)])
|
|
|
|
.expect("Comment::delete: to error");
|
|
|
|
|
|
|
|
for m in Mention::list_for_comment(&conn, self.id) {
|
|
|
|
m.delete(conn);
|
|
|
|
}
|
|
|
|
diesel::update(comments::table).filter(comments::in_response_to_id.eq(self.id))
|
|
|
|
.set(comments::in_response_to_id.eq(self.in_response_to_id))
|
|
|
|
.execute(conn)
|
|
|
|
.expect("Comment::delete: DB error could not update other comments");
|
|
|
|
diesel::delete(self)
|
|
|
|
.execute(conn)
|
|
|
|
.expect("Comment::delete: DB error");
|
|
|
|
act
|
|
|
|
}
|
|
|
|
|
|
|
|
fn delete_id(id: &str, actor_id: &str, conn: &Connection) {
|
|
|
|
let actor = User::find_by_ap_url(conn, actor_id);
|
|
|
|
let comment = Comment::find_by_ap_url(conn, id);
|
|
|
|
if let Some(comment) = comment.filter(|c| c.author_id == actor.unwrap().id) {
|
|
|
|
comment.delete(conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|