2020-01-21 07:02:03 +01:00
|
|
|
use crate::{
|
|
|
|
comment_seers::{CommentSeers, NewCommentSeers},
|
|
|
|
instance::Instance,
|
|
|
|
medias::Media,
|
|
|
|
mentions::Mention,
|
|
|
|
notifications::*,
|
|
|
|
posts::Post,
|
|
|
|
safe_string::SafeString,
|
|
|
|
schema::comments,
|
|
|
|
users::User,
|
|
|
|
Connection, Error, PlumeRocket, Result,
|
|
|
|
};
|
2019-03-20 17:56:17 +01:00
|
|
|
use activitypub::{
|
|
|
|
activity::{Create, Delete},
|
|
|
|
link,
|
|
|
|
object::{Note, Tombstone},
|
|
|
|
};
|
2018-09-27 23:06:40 +02:00
|
|
|
use chrono::{self, NaiveDateTime};
|
2019-03-04 21:35:03 +01:00
|
|
|
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl, SaveChangesDsl};
|
2020-01-21 07:02:03 +01:00
|
|
|
use plume_common::{
|
|
|
|
activity_pub::{
|
|
|
|
inbox::{AsActor, AsObject, FromId},
|
|
|
|
Id, IntoId, PUBLIC_VISIBILITY,
|
|
|
|
},
|
|
|
|
utils,
|
|
|
|
};
|
2018-05-10 17:36:32 +02:00
|
|
|
use serde_json;
|
2018-12-24 11:23:04 +01:00
|
|
|
use std::collections::HashSet;
|
|
|
|
|
2019-03-12 19:40:54 +01:00
|
|
|
#[derive(Queryable, Identifiable, Clone, AsChangeset)]
|
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 {
|
2019-03-04 21:35:03 +01:00
|
|
|
insert!(comments, NewComment, |inserted, conn| {
|
|
|
|
if inserted.ap_url.is_none() {
|
2019-03-20 17:56:17 +01:00
|
|
|
inserted.ap_url = Some(format!(
|
|
|
|
"{}comment/{}",
|
|
|
|
inserted.get_post(conn)?.ap_url,
|
|
|
|
inserted.id
|
|
|
|
));
|
2019-03-04 21:35:03 +01:00
|
|
|
let _: Comment = inserted.save_changes(conn)?;
|
|
|
|
}
|
|
|
|
Ok(inserted)
|
|
|
|
});
|
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-12-29 09:36:07 +01:00
|
|
|
pub fn get_author(&self, conn: &Connection) -> Result<User> {
|
|
|
|
User::get(conn, self.author_id)
|
2018-05-10 11:44:57 +02:00
|
|
|
}
|
2018-05-10 17:36:32 +02:00
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn get_post(&self, conn: &Connection) -> Result<Post> {
|
|
|
|
Post::get(conn, self.post_id)
|
2018-05-10 17:36:32 +02:00
|
|
|
}
|
2018-05-19 00:04:30 +02:00
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn count_local(conn: &Connection) -> Result<i64> {
|
2020-01-21 07:02:03 +01:00
|
|
|
use crate::schema::users;
|
2018-11-24 12:44:17 +01:00
|
|
|
let local_authors = users::table
|
2019-05-10 22:59:34 +02:00
|
|
|
.filter(users::instance_id.eq(Instance::get_local()?.id))
|
2018-11-24 12:44:17 +01:00
|
|
|
.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-12-29 09:36:07 +01:00
|
|
|
.map_err(Error::from)
|
2018-06-10 21:33:42 +02:00
|
|
|
}
|
2018-06-18 18:34:29 +02:00
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn get_responses(&self, conn: &Connection) -> Result<Vec<Comment>> {
|
2019-03-20 17:56:17 +01:00
|
|
|
comments::table
|
|
|
|
.filter(comments::in_response_to_id.eq(self.id))
|
2018-12-06 18:54:16 +01:00
|
|
|
.load::<Comment>(conn)
|
2018-12-29 09:36:07 +01:00
|
|
|
.map_err(Error::from)
|
2018-06-18 18:34:29 +02:00
|
|
|
}
|
2018-06-20 11:01:25 +02:00
|
|
|
|
2018-12-24 11:23:04 +01:00
|
|
|
pub fn can_see(&self, conn: &Connection, user: Option<&User>) -> bool {
|
2019-03-20 17:56:17 +01:00
|
|
|
self.public_visibility
|
|
|
|
|| user
|
|
|
|
.as_ref()
|
|
|
|
.map(|u| CommentSeers::can_see(conn, self, u).unwrap_or(false))
|
2018-12-29 09:36:07 +01:00
|
|
|
.unwrap_or(false)
|
2018-12-24 11:23:04 +01:00
|
|
|
}
|
|
|
|
|
2019-04-17 19:31:47 +02:00
|
|
|
pub fn to_activity(&self, c: &PlumeRocket) -> Result<Note> {
|
|
|
|
let author = User::get(&c.conn, self.author_id)?;
|
2019-03-20 17:56:17 +01:00
|
|
|
let (html, mentions, _hashtags) = utils::md_to_html(
|
|
|
|
self.content.get().as_ref(),
|
2019-05-10 22:59:34 +02:00
|
|
|
Some(&Instance::get_local()?.public_domain),
|
2019-03-22 19:51:36 +01:00
|
|
|
true,
|
2019-04-17 19:31:47 +02:00
|
|
|
Some(Media::get_media_processor(&c.conn, vec![&author])),
|
2019-03-20 17:56:17 +01:00
|
|
|
);
|
2018-09-09 17:08:53 +02:00
|
|
|
|
|
|
|
let mut note = Note::default();
|
2019-04-17 19:31:47 +02:00
|
|
|
let to = vec![Id::new(PUBLIC_VISIBILITY.to_string())];
|
2018-09-09 17:08:53 +02:00
|
|
|
|
2018-11-24 12:44:17 +01:00
|
|
|
note.object_props
|
2018-12-29 09:36:07 +01:00
|
|
|
.set_id_string(self.ap_url.clone().unwrap_or_default())?;
|
2018-11-24 12:44:17 +01:00
|
|
|
note.object_props
|
2018-12-29 09:36:07 +01:00
|
|
|
.set_summary_string(self.spoiler_text.clone())?;
|
2019-03-20 17:56:17 +01:00
|
|
|
note.object_props.set_content_string(html)?;
|
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(
|
2019-04-17 19:31:47 +02:00
|
|
|
|| Ok(Post::get(&c.conn, self.post_id)?.ap_url),
|
|
|
|
|id| Ok(Comment::get(&c.conn, id)?.ap_url.unwrap_or_default()) as Result<String>,
|
2018-12-29 09:36:07 +01:00
|
|
|
)?))?;
|
2018-11-24 12:44:17 +01:00
|
|
|
note.object_props
|
2018-12-29 09:36:07 +01:00
|
|
|
.set_published_string(chrono::Utc::now().to_rfc3339())?;
|
2020-01-19 12:52:32 +01:00
|
|
|
note.object_props.set_attributed_to_link(author.into_id())?;
|
|
|
|
note.object_props.set_to_link_vec(to)?;
|
2019-03-20 17:56:17 +01:00
|
|
|
note.object_props.set_tag_link_vec(
|
|
|
|
mentions
|
|
|
|
.into_iter()
|
2019-04-17 19:31:47 +02:00
|
|
|
.filter_map(|m| Mention::build_activity(c, &m).ok())
|
2019-03-20 17:56:17 +01:00
|
|
|
.collect::<Vec<link::Mention>>(),
|
|
|
|
)?;
|
2018-12-29 09:36:07 +01:00
|
|
|
Ok(note)
|
2018-09-09 17:08:53 +02:00
|
|
|
}
|
|
|
|
|
2019-04-17 19:31:47 +02:00
|
|
|
pub fn create_activity(&self, c: &PlumeRocket) -> Result<Create> {
|
|
|
|
let author = User::get(&c.conn, self.author_id)?;
|
2018-09-09 17:08:53 +02:00
|
|
|
|
2019-04-17 19:31:47 +02:00
|
|
|
let note = self.to_activity(c)?;
|
2018-09-09 17:08:53 +02:00
|
|
|
let mut act = Create::default();
|
2019-03-20 17:56:17 +01:00
|
|
|
act.create_props.set_actor_link(author.into_id())?;
|
|
|
|
act.create_props.set_object_object(note.clone())?;
|
2018-11-24 12:44:17 +01:00
|
|
|
act.object_props
|
2019-03-20 17:56:17 +01:00
|
|
|
.set_id_string(format!("{}/activity", self.ap_url.clone()?,))?;
|
2018-11-24 12:44:17 +01:00
|
|
|
act.object_props
|
2019-03-20 17:56:17 +01:00
|
|
|
.set_to_link_vec(note.object_props.to_link_vec::<Id>()?)?;
|
2019-04-17 19:31:47 +02:00
|
|
|
act.object_props
|
|
|
|
.set_cc_link_vec(vec![Id::new(self.get_author(&c.conn)?.followers_endpoint)])?;
|
|
|
|
Ok(act)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn notify(&self, conn: &Connection) -> Result<()> {
|
|
|
|
for author in self.get_post(conn)?.get_authors(conn)? {
|
2019-05-04 17:15:41 +02:00
|
|
|
if Mention::list_for_comment(conn, self.id)?
|
|
|
|
.iter()
|
|
|
|
.all(|m| m.get_mentioned(conn).map(|u| u != author).unwrap_or(true))
|
|
|
|
&& author.is_local()
|
|
|
|
{
|
|
|
|
Notification::insert(
|
|
|
|
conn,
|
|
|
|
NewNotification {
|
|
|
|
kind: notification_kind::COMMENT.to_string(),
|
|
|
|
object_id: self.id,
|
|
|
|
user_id: author.id,
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
}
|
2019-04-17 19:31:47 +02:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn build_delete(&self, conn: &Connection) -> Result<Delete> {
|
|
|
|
let mut act = Delete::default();
|
|
|
|
act.delete_props
|
|
|
|
.set_actor_link(self.get_author(conn)?.into_id())?;
|
|
|
|
|
|
|
|
let mut tombstone = Tombstone::default();
|
|
|
|
tombstone.object_props.set_id_string(self.ap_url.clone()?)?;
|
|
|
|
act.delete_props.set_object_object(tombstone)?;
|
|
|
|
|
|
|
|
act.object_props
|
|
|
|
.set_id_string(format!("{}#delete", self.ap_url.clone().unwrap()))?;
|
|
|
|
act.object_props
|
|
|
|
.set_to_link_vec(vec![Id::new(PUBLIC_VISIBILITY)])?;
|
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
Ok(act)
|
2018-06-20 11:01:25 +02:00
|
|
|
}
|
2018-05-10 17:36:32 +02:00
|
|
|
}
|
|
|
|
|
2019-04-17 19:31:47 +02:00
|
|
|
impl FromId<PlumeRocket> for Comment {
|
2018-12-29 09:36:07 +01:00
|
|
|
type Error = Error;
|
2019-04-17 19:31:47 +02:00
|
|
|
type Object = Note;
|
2018-12-29 09:36:07 +01:00
|
|
|
|
2019-04-17 19:31:47 +02:00
|
|
|
fn from_db(c: &PlumeRocket, id: &str) -> Result<Self> {
|
|
|
|
Self::find_by_ap_url(&c.conn, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_activity(c: &PlumeRocket, note: Note) -> Result<Self> {
|
|
|
|
let conn = &*c.conn;
|
2018-12-24 11:23:04 +01:00
|
|
|
let comm = {
|
2019-03-20 17:56:17 +01:00
|
|
|
let previous_url = note.object_props.in_reply_to.as_ref()?.as_str()?;
|
2018-12-24 11:23:04 +01:00
|
|
|
let previous_comment = Comment::find_by_ap_url(conn, previous_url);
|
|
|
|
|
2019-03-20 17:56:17 +01:00
|
|
|
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)
|
2019-04-17 19:31:47 +02:00
|
|
|
.any(|s| s == PUBLIC_VISIBILITY),
|
|
|
|
serde_json::Value::String(s) => s == PUBLIC_VISIBILITY,
|
2018-12-24 11:23:04 +01:00
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
|
2019-03-20 17:56:17 +01:00
|
|
|
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);
|
2018-12-24 11:23:04 +01:00
|
|
|
|
|
|
|
let comm = Comment::insert(
|
|
|
|
conn,
|
|
|
|
NewComment {
|
2019-03-20 17:56:17 +01:00
|
|
|
content: SafeString::new(¬e.object_props.content_string()?),
|
|
|
|
spoiler_text: note.object_props.summary_string().unwrap_or_default(),
|
2018-12-24 11:23:04 +01:00
|
|
|
ap_url: note.object_props.id_string().ok(),
|
2018-12-29 09:36:07 +01:00
|
|
|
in_response_to_id: previous_comment.iter().map(|c| c.id).next(),
|
2019-03-20 17:56:17 +01:00
|
|
|
post_id: previous_comment.map(|c| c.post_id).or_else(|_| {
|
|
|
|
Ok(Post::find_by_ap_url(conn, previous_url)?.id) as Result<i32>
|
|
|
|
})?,
|
2019-04-17 19:31:47 +02:00
|
|
|
author_id: User::from_id(
|
|
|
|
c,
|
2019-05-25 20:23:45 +02:00
|
|
|
¬e.object_props.attributed_to_link::<Id>()?,
|
2019-04-17 19:31:47 +02:00
|
|
|
None,
|
|
|
|
)
|
|
|
|
.map_err(|(_, e)| e)?
|
|
|
|
.id,
|
|
|
|
sensitive: note.object_props.summary_string().is_ok(),
|
2019-03-20 17:56:17 +01:00
|
|
|
public_visibility,
|
2018-12-24 11:23:04 +01:00
|
|
|
},
|
2018-12-29 09:36:07 +01:00
|
|
|
)?;
|
2018-12-24 11:23:04 +01:00
|
|
|
|
|
|
|
// 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)
|
2018-12-29 09:36:07 +01:00
|
|
|
.map_err(Error::from)
|
|
|
|
.and_then(|m| {
|
2019-03-20 17:56:17 +01:00
|
|
|
let author = &Post::get(conn, comm.post_id)?.get_authors(conn)?[0];
|
|
|
|
let not_author = m.link_props.href_string()? != author.ap_url.clone();
|
|
|
|
Ok(Mention::from_activity(
|
|
|
|
conn, &m, comm.id, false, not_author,
|
|
|
|
)?)
|
2018-12-24 11:23:04 +01:00
|
|
|
})
|
|
|
|
.ok();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
comm
|
|
|
|
};
|
|
|
|
|
|
|
|
if !comm.public_visibility {
|
|
|
|
let receivers_ap_url = |v: Option<serde_json::Value>| {
|
2019-03-20 17:56:17 +01:00
|
|
|
let filter = |e: serde_json::Value| {
|
|
|
|
if let serde_json::Value::String(s) = e {
|
|
|
|
Some(s)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
2018-12-24 11:23:04 +01:00
|
|
|
match v.unwrap_or(serde_json::Value::Null) {
|
|
|
|
serde_json::Value::Array(v) => v,
|
|
|
|
v => vec![v],
|
2019-03-20 17:56:17 +01:00
|
|
|
}
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(filter)
|
2018-12-24 11:23:04 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
2019-03-20 17:56:17 +01:00
|
|
|
let receivers_ap_url = to
|
|
|
|
.chain(cc)
|
|
|
|
.chain(bto)
|
|
|
|
.chain(bcc)
|
2019-04-17 19:31:47 +02:00
|
|
|
.collect::<HashSet<_>>() // remove duplicates (don't do a query more than once)
|
2018-12-24 11:23:04 +01:00
|
|
|
.into_iter()
|
2019-03-20 17:56:17 +01:00
|
|
|
.map(|v| {
|
2019-04-17 19:31:47 +02:00
|
|
|
if let Ok(user) = User::from_id(c, &v, None) {
|
2019-03-20 17:56:17 +01:00
|
|
|
vec![user]
|
|
|
|
} else {
|
|
|
|
vec![] // TODO try to fetch collection
|
|
|
|
}
|
2018-12-24 11:23:04 +01:00
|
|
|
})
|
|
|
|
.flatten()
|
2018-12-29 09:36:07 +01:00
|
|
|
.filter(|u| u.get_instance(conn).map(|i| i.local).unwrap_or(false))
|
2019-03-20 17:56:17 +01:00
|
|
|
.collect::<HashSet<User>>(); //remove duplicates (prevent db error)
|
2018-12-24 11:23:04 +01:00
|
|
|
|
|
|
|
for user in &receivers_ap_url {
|
|
|
|
CommentSeers::insert(
|
|
|
|
conn,
|
|
|
|
NewCommentSeers {
|
|
|
|
comment_id: comm.id,
|
2019-03-20 17:56:17 +01:00
|
|
|
user_id: user.id,
|
|
|
|
},
|
2018-12-29 09:36:07 +01:00
|
|
|
)?;
|
2018-06-21 15:05:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
comm.notify(conn)?;
|
|
|
|
Ok(comm)
|
2018-06-17 21:37:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 19:31:47 +02:00
|
|
|
impl AsObject<User, Create, &PlumeRocket> for Comment {
|
2018-12-29 09:36:07 +01:00
|
|
|
type Error = Error;
|
2019-04-17 19:31:47 +02:00
|
|
|
type Output = Self;
|
2018-12-29 09:36:07 +01:00
|
|
|
|
2019-04-17 19:31:47 +02:00
|
|
|
fn activity(self, _c: &PlumeRocket, _actor: User, _id: &str) -> Result<Self> {
|
|
|
|
// The actual creation takes place in the FromId impl
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsObject<User, Delete, &PlumeRocket> for Comment {
|
|
|
|
type Error = Error;
|
|
|
|
type Output = ();
|
|
|
|
|
|
|
|
fn activity(self, c: &PlumeRocket, actor: User, _id: &str) -> Result<()> {
|
|
|
|
if self.author_id != actor.id {
|
|
|
|
return Err(Error::Unauthorized);
|
|
|
|
}
|
|
|
|
|
|
|
|
for m in Mention::list_for_comment(&c.conn, self.id)? {
|
|
|
|
for n in Notification::find_for_mention(&c.conn, &m)? {
|
|
|
|
n.delete(&c.conn)?;
|
|
|
|
}
|
|
|
|
m.delete(&c.conn)?;
|
2018-06-20 23:51:47 +02:00
|
|
|
}
|
2019-04-17 19:31:47 +02:00
|
|
|
|
|
|
|
for n in Notification::find_for_comment(&c.conn, &self)? {
|
|
|
|
n.delete(&c.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(&*c.conn)?;
|
|
|
|
diesel::delete(&self).execute(&*c.conn)?;
|
2018-12-29 09:36:07 +01:00
|
|
|
Ok(())
|
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 {
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn from_post(conn: &Connection, p: &Post, user: Option<&User>) -> Result<Vec<Self>> {
|
2019-03-20 17:56:17 +01:00
|
|
|
Ok(Comment::list_by_post(conn, p.id)?
|
|
|
|
.into_iter()
|
2018-12-24 11:23:04 +01:00
|
|
|
.filter(|c| c.in_response_to_id.is_none())
|
|
|
|
.filter(|c| c.can_see(conn, user))
|
2018-12-29 09:36:07 +01:00
|
|
|
.filter_map(|c| Self::from_comment(conn, c, user).ok())
|
|
|
|
.collect())
|
2018-12-24 11:23:04 +01:00
|
|
|
}
|
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn from_comment(conn: &Connection, comment: Comment, user: Option<&User>) -> Result<Self> {
|
2019-03-20 17:56:17 +01:00
|
|
|
let responses = comment
|
|
|
|
.get_responses(conn)?
|
|
|
|
.into_iter()
|
2018-12-24 11:23:04 +01:00
|
|
|
.filter(|c| c.can_see(conn, user))
|
2018-12-29 09:36:07 +01:00
|
|
|
.filter_map(|c| Self::from_comment(conn, c, user).ok())
|
2018-12-24 11:23:04 +01:00
|
|
|
.collect();
|
2019-03-20 17:56:17 +01:00
|
|
|
Ok(CommentTree { comment, responses })
|
2018-12-24 11:23:04 +01:00
|
|
|
}
|
|
|
|
}
|
2018-12-23 11:13:36 +01:00
|
|
|
|
2019-04-17 19:31:47 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use crate::inbox::{inbox, tests::fill_database, InboxResult};
|
|
|
|
use crate::safe_string::SafeString;
|
|
|
|
use crate::tests::rockets;
|
|
|
|
use diesel::Connection;
|
|
|
|
|
|
|
|
// creates a post, get it's Create activity, delete the post,
|
|
|
|
// "send" the Create to the inbox, and check it works
|
|
|
|
#[test]
|
|
|
|
fn self_federation() {
|
|
|
|
let r = rockets();
|
|
|
|
let conn = &*r.conn;
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
|
|
|
let (posts, users, _) = fill_database(&r);
|
|
|
|
|
|
|
|
let original_comm = Comment::insert(
|
|
|
|
conn,
|
|
|
|
NewComment {
|
|
|
|
content: SafeString::new("My comment"),
|
|
|
|
in_response_to_id: None,
|
|
|
|
post_id: posts[0].id,
|
|
|
|
author_id: users[0].id,
|
|
|
|
ap_url: None,
|
|
|
|
sensitive: true,
|
|
|
|
spoiler_text: "My CW".into(),
|
|
|
|
public_visibility: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let act = original_comm.create_activity(&r).unwrap();
|
|
|
|
inbox(
|
|
|
|
&r,
|
|
|
|
serde_json::to_value(original_comm.build_delete(conn).unwrap()).unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
match inbox(&r, serde_json::to_value(act).unwrap()).unwrap() {
|
|
|
|
InboxResult::Commented(c) => {
|
|
|
|
// TODO: one is HTML, the other markdown: assert_eq!(c.content, original_comm.content);
|
|
|
|
assert_eq!(c.in_response_to_id, original_comm.in_response_to_id);
|
|
|
|
assert_eq!(c.post_id, original_comm.post_id);
|
|
|
|
assert_eq!(c.author_id, original_comm.author_id);
|
|
|
|
assert_eq!(c.ap_url, original_comm.ap_url);
|
|
|
|
assert_eq!(c.spoiler_text, original_comm.spoiler_text);
|
|
|
|
assert_eq!(c.public_visibility, original_comm.public_visibility);
|
|
|
|
}
|
|
|
|
_ => panic!("Unexpected result"),
|
|
|
|
};
|
|
|
|
Ok(())
|
2019-10-07 19:08:20 +02:00
|
|
|
})
|
2018-12-23 11:13:36 +01:00
|
|
|
}
|
|
|
|
}
|