2020-01-21 07:02:03 +01:00
|
|
|
use crate::{
|
|
|
|
comment_seers::{CommentSeers, NewCommentSeers},
|
2021-01-30 11:24:16 +01:00
|
|
|
db_conn::DbConn,
|
2020-01-21 07:02:03 +01:00
|
|
|
instance::Instance,
|
|
|
|
medias::Media,
|
|
|
|
mentions::Mention,
|
|
|
|
notifications::*,
|
|
|
|
posts::Post,
|
|
|
|
safe_string::SafeString,
|
|
|
|
schema::comments,
|
|
|
|
users::User,
|
2021-01-30 11:24:16 +01:00
|
|
|
Connection, Error, Result, CONFIG,
|
2020-01-21 07:02:03 +01:00
|
|
|
};
|
2019-03-20 17:56:17 +01:00
|
|
|
use activitypub::{
|
|
|
|
activity::{Create, Delete},
|
|
|
|
link,
|
|
|
|
object::{Note, Tombstone},
|
|
|
|
};
|
2022-04-03 13:51:36 +02:00
|
|
|
use activitystreams::{
|
2022-04-03 14:44:21 +02:00
|
|
|
activity::{Create as Create07, Delete as Delete07},
|
2022-04-23 15:46:49 +02:00
|
|
|
base::{AnyBase, Base},
|
2022-04-03 14:44:21 +02:00
|
|
|
iri_string::types::IriString,
|
2022-04-23 15:46:49 +02:00
|
|
|
link::{self as link07, kind::MentionType},
|
2022-04-03 14:44:21 +02:00
|
|
|
object::{Note as Note07, Tombstone as Tombstone07},
|
|
|
|
prelude::*,
|
2022-04-23 15:46:49 +02:00
|
|
|
primitives::OneOrMany,
|
2022-04-03 14:44:21 +02:00
|
|
|
time::OffsetDateTime,
|
2022-04-03 13:51:36 +02:00
|
|
|
};
|
2022-01-09 05:10:54 +01:00
|
|
|
use chrono::{self, NaiveDateTime, TimeZone, Utc};
|
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::{
|
2022-05-02 10:43:03 +02:00
|
|
|
inbox::{AsActor, AsObject, FromId},
|
2021-11-24 14:50:16 +01:00
|
|
|
sign::Signer,
|
2022-04-23 15:46:49 +02:00
|
|
|
Id, IntoId, ToAsString, ToAsUri, PUBLIC_VISIBILITY,
|
2020-01-21 07:02:03 +01:00
|
|
|
},
|
|
|
|
utils,
|
|
|
|
};
|
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!(
|
2022-01-09 05:10:54 +01:00
|
|
|
"{}/comment/{}",
|
2019-03-20 17:56:17 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-30 11:24:16 +01:00
|
|
|
pub fn to_activity(&self, conn: &DbConn) -> Result<Note> {
|
|
|
|
let author = User::get(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,
|
2021-01-30 11:24:16 +01:00
|
|
|
Some(Media::get_media_processor(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(
|
2021-01-30 11:24:16 +01:00
|
|
|
|| Ok(Post::get(conn, self.post_id)?.ap_url),
|
|
|
|
|id| Ok(Comment::get(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
|
2022-01-09 05:10:54 +01:00
|
|
|
.set_published_utctime(Utc.from_utc_datetime(&self.creation_date))?;
|
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()
|
2021-01-30 11:24:16 +01:00
|
|
|
.filter_map(|m| Mention::build_activity(conn, &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
|
|
|
}
|
|
|
|
|
2022-04-03 13:51:36 +02:00
|
|
|
pub fn to_activity07(&self, conn: &DbConn) -> Result<Note07> {
|
|
|
|
let author = User::get(conn, self.author_id)?;
|
|
|
|
let (html, mentions, _hashtags) = utils::md_to_html(
|
|
|
|
self.content.get().as_ref(),
|
|
|
|
Some(&Instance::get_local()?.public_domain),
|
|
|
|
true,
|
|
|
|
Some(Media::get_media_processor(conn, vec![&author])),
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut note = Note07::new();
|
|
|
|
let to = vec![PUBLIC_VISIBILITY.parse::<IriString>()?];
|
|
|
|
|
|
|
|
note.set_id(
|
|
|
|
self.ap_url
|
|
|
|
.clone()
|
|
|
|
.unwrap_or_default()
|
|
|
|
.parse::<IriString>()?,
|
|
|
|
);
|
|
|
|
note.set_summary(self.spoiler_text.clone());
|
|
|
|
note.set_content(html);
|
|
|
|
note.set_in_reply_to(self.in_response_to_id.map_or_else(
|
|
|
|
|| Post::get(conn, self.post_id).map(|post| post.ap_url),
|
|
|
|
|id| Comment::get(conn, id).map(|comment| comment.ap_url.unwrap_or_default()),
|
|
|
|
)?);
|
|
|
|
note.set_published(
|
|
|
|
OffsetDateTime::from_unix_timestamp_nanos(self.creation_date.timestamp_nanos().into())
|
|
|
|
.expect("OffsetDateTime"),
|
|
|
|
);
|
|
|
|
note.set_attributed_to(author.into_id().parse::<IriString>()?);
|
|
|
|
note.set_many_tos(to);
|
|
|
|
note.set_many_tags(mentions.into_iter().filter_map(|m| {
|
|
|
|
Mention::build_activity07(conn, &m)
|
|
|
|
.map(|mention| mention.into_any_base().expect("Can convert"))
|
|
|
|
.ok()
|
|
|
|
}));
|
|
|
|
Ok(note)
|
|
|
|
}
|
|
|
|
|
2021-01-30 11:24:16 +01:00
|
|
|
pub fn create_activity(&self, conn: &DbConn) -> Result<Create> {
|
2021-11-27 23:53:13 +01:00
|
|
|
let author = User::get(conn, self.author_id)?;
|
2018-09-09 17:08:53 +02:00
|
|
|
|
2021-01-30 11:24:16 +01:00
|
|
|
let note = self.to_activity(conn)?;
|
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())?;
|
2021-11-27 23:53:13 +01:00
|
|
|
act.object_props.set_id_string(format!(
|
|
|
|
"{}/activity",
|
|
|
|
self.ap_url.clone().ok_or(Error::MissingApProperty)?,
|
|
|
|
))?;
|
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
|
2021-11-27 23:53:13 +01:00
|
|
|
.set_cc_link_vec(vec![Id::new(self.get_author(conn)?.followers_endpoint)])?;
|
2019-04-17 19:31:47 +02:00
|
|
|
Ok(act)
|
|
|
|
}
|
|
|
|
|
2022-04-03 14:33:19 +02:00
|
|
|
pub fn create_activity07(&self, conn: &DbConn) -> Result<Create07> {
|
|
|
|
let author = User::get(conn, self.author_id)?;
|
|
|
|
|
|
|
|
let note = self.to_activity07(conn)?;
|
2022-04-23 18:37:30 +02:00
|
|
|
let note_clone = note.clone();
|
2022-04-03 14:33:19 +02:00
|
|
|
|
|
|
|
let mut act = Create07::new(
|
|
|
|
author.into_id().parse::<IriString>()?,
|
|
|
|
Base::retract(note)?.into_generic()?,
|
|
|
|
);
|
|
|
|
act.set_id(
|
|
|
|
format!(
|
|
|
|
"{}/activity",
|
|
|
|
self.ap_url.clone().ok_or(Error::MissingApProperty)?,
|
|
|
|
)
|
|
|
|
.parse::<IriString>()?,
|
|
|
|
);
|
2022-04-23 18:37:30 +02:00
|
|
|
act.set_many_tos(
|
|
|
|
note_clone
|
|
|
|
.to()
|
|
|
|
.iter()
|
|
|
|
.flat_map(|tos| tos.iter().map(|to| to.to_owned())),
|
|
|
|
);
|
|
|
|
act.set_many_ccs(vec![self.get_author(conn)?.followers_endpoint]);
|
2022-04-03 14:33:19 +02:00
|
|
|
Ok(act)
|
|
|
|
}
|
|
|
|
|
2019-04-17 19:31:47 +02:00
|
|
|
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();
|
2021-11-27 23:53:13 +01:00
|
|
|
tombstone
|
|
|
|
.object_props
|
|
|
|
.set_id_string(self.ap_url.clone().ok_or(Error::MissingApProperty)?)?;
|
2019-04-17 19:31:47 +02:00
|
|
|
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
|
|
|
}
|
2022-04-03 14:44:21 +02:00
|
|
|
|
|
|
|
pub fn build_delete07(&self, conn: &Connection) -> Result<Delete07> {
|
|
|
|
let mut tombstone = Tombstone07::new();
|
|
|
|
tombstone.set_id(
|
|
|
|
self.ap_url
|
|
|
|
.as_ref()
|
|
|
|
.ok_or(Error::MissingApProperty)?
|
|
|
|
.parse::<IriString>()?,
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut act = Delete07::new(
|
|
|
|
self.get_author(conn)?.into_id().parse::<IriString>()?,
|
|
|
|
Base::retract(tombstone)?.into_generic()?,
|
|
|
|
);
|
|
|
|
|
|
|
|
act.set_id(format!("{}#delete", self.ap_url.clone().unwrap()).parse::<IriString>()?);
|
|
|
|
act.set_many_tos(vec![PUBLIC_VISIBILITY.parse::<IriString>()?]);
|
|
|
|
|
|
|
|
Ok(act)
|
|
|
|
}
|
2018-05-10 17:36:32 +02:00
|
|
|
}
|
|
|
|
|
2022-05-02 09:07:08 +02:00
|
|
|
impl FromId<DbConn> for Comment {
|
2022-04-23 15:46:49 +02:00
|
|
|
type Error = Error;
|
|
|
|
type Object = Note07;
|
|
|
|
|
|
|
|
fn from_db07(conn: &DbConn, id: &str) -> Result<Self> {
|
|
|
|
Self::find_by_ap_url(conn, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_activity07(conn: &DbConn, note: Note07) -> Result<Self> {
|
|
|
|
let comm = {
|
|
|
|
let previous_url = note
|
|
|
|
.in_reply_to()
|
|
|
|
.ok_or(Error::MissingApProperty)?
|
|
|
|
.iter()
|
|
|
|
.next()
|
|
|
|
.ok_or(Error::MissingApProperty)?
|
2022-04-30 21:45:56 +02:00
|
|
|
.id()
|
2022-04-23 15:46:49 +02:00
|
|
|
.ok_or(Error::MissingApProperty)?;
|
2022-04-30 21:45:56 +02:00
|
|
|
let previous_comment = Comment::find_by_ap_url(conn, previous_url.as_str());
|
2022-04-23 15:46:49 +02:00
|
|
|
|
|
|
|
let is_public = |v: &Option<&OneOrMany<AnyBase>>| match v {
|
|
|
|
Some(one_or_many) => one_or_many.iter().any(|any_base| {
|
2022-04-30 21:45:56 +02:00
|
|
|
let id = any_base.id();
|
|
|
|
id.is_some() && id.unwrap() == PUBLIC_VISIBILITY
|
2022-04-23 15:46:49 +02:00
|
|
|
}),
|
|
|
|
None => false,
|
|
|
|
};
|
|
|
|
|
|
|
|
let public_visibility = is_public(¬e.to())
|
|
|
|
|| is_public(¬e.bto())
|
|
|
|
|| is_public(¬e.cc())
|
|
|
|
|| is_public(¬e.bcc());
|
|
|
|
|
|
|
|
let summary = note.summary().and_then(|summary| summary.to_as_string());
|
|
|
|
let sensitive = summary.is_some();
|
|
|
|
let comm = Comment::insert(
|
|
|
|
conn,
|
|
|
|
NewComment {
|
|
|
|
content: SafeString::new(
|
|
|
|
¬e
|
|
|
|
.content()
|
|
|
|
.ok_or(Error::MissingApProperty)?
|
|
|
|
.to_as_string()
|
|
|
|
.ok_or(Error::InvalidValue)?,
|
|
|
|
),
|
|
|
|
spoiler_text: summary.unwrap_or_default(),
|
|
|
|
ap_url: Some(
|
|
|
|
note.id_unchecked()
|
|
|
|
.ok_or(Error::MissingApProperty)?
|
|
|
|
.to_string(),
|
|
|
|
),
|
|
|
|
in_response_to_id: previous_comment.iter().map(|c| c.id).next(),
|
|
|
|
post_id: previous_comment.map(|c| c.post_id).or_else(|_| {
|
2022-04-30 21:45:56 +02:00
|
|
|
Ok(Post::find_by_ap_url(conn, previous_url.as_str())?.id) as Result<i32>
|
2022-04-23 15:46:49 +02:00
|
|
|
})?,
|
2022-05-02 12:24:36 +02:00
|
|
|
author_id: User::from_id(
|
2022-04-23 15:46:49 +02:00
|
|
|
conn,
|
|
|
|
¬e
|
|
|
|
.attributed_to()
|
|
|
|
.ok_or(Error::MissingApProperty)?
|
|
|
|
.to_as_uri()
|
|
|
|
.ok_or(Error::MissingApProperty)?,
|
|
|
|
None,
|
|
|
|
CONFIG.proxy(),
|
|
|
|
)
|
|
|
|
.map_err(|(_, e)| e)?
|
|
|
|
.id,
|
|
|
|
sensitive,
|
|
|
|
public_visibility,
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// save mentions
|
|
|
|
if let Some(tags) = note.tag() {
|
|
|
|
let author_url = &Post::get(conn, comm.post_id)?.get_authors(conn)?[0].ap_url;
|
|
|
|
for tag in tags.iter() {
|
|
|
|
let m = tag.clone().extend::<link07::Mention, MentionType>()?; // FIXME: Don't clone
|
|
|
|
if m.is_none() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let m = m.unwrap();
|
|
|
|
let not_author = m.href().ok_or(Error::MissingApProperty)? != author_url;
|
|
|
|
let _ = Mention::from_activity07(conn, &m, comm.id, false, not_author);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
comm
|
|
|
|
};
|
|
|
|
|
|
|
|
if !comm.public_visibility {
|
|
|
|
let mut receiver_ids = HashSet::new();
|
|
|
|
let mut receivers_id = |v: Option<&'_ OneOrMany<AnyBase>>| {
|
|
|
|
if let Some(one_or_many) = v {
|
|
|
|
for any_base in one_or_many.iter() {
|
|
|
|
if let Some(id) = any_base.id() {
|
|
|
|
receiver_ids.insert(id.to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
receivers_id(note.to());
|
|
|
|
receivers_id(note.cc());
|
|
|
|
receivers_id(note.bto());
|
|
|
|
receivers_id(note.bcc());
|
|
|
|
|
|
|
|
let receivers_ap_url = receiver_ids
|
|
|
|
.into_iter()
|
|
|
|
.flat_map(|v| {
|
2022-05-02 12:24:36 +02:00
|
|
|
if let Ok(user) = User::from_id(conn, v.as_ref(), None, CONFIG.proxy()) {
|
2022-04-23 15:46:49 +02:00
|
|
|
vec![user]
|
|
|
|
} else {
|
|
|
|
vec![] // TODO try to fetch collection
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.filter(|u| u.get_instance(conn).map(|i| i.local).unwrap_or(false))
|
|
|
|
.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,
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
comm.notify(conn)?;
|
|
|
|
Ok(comm)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_sender07() -> &'static dyn Signer {
|
|
|
|
Instance::get_local_instance_user().expect("Failed to local instance user")
|
2021-11-24 14:50:16 +01:00
|
|
|
}
|
2018-06-17 21:37:10 +02:00
|
|
|
}
|
|
|
|
|
2022-05-02 10:43:03 +02:00
|
|
|
impl AsObject<User, Create07, &DbConn> for Comment {
|
2022-04-23 18:49:27 +02:00
|
|
|
type Error = Error;
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn activity07(self, _conn: &DbConn, _actor: User, _id: &str) -> Result<Self> {
|
|
|
|
// The actual creation takes place in the FromId impl
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-02 10:43:03 +02:00
|
|
|
impl AsObject<User, Delete07, &DbConn> for Comment {
|
2022-04-23 18:49:27 +02:00
|
|
|
type Error = Error;
|
|
|
|
type Output = ();
|
|
|
|
|
|
|
|
fn activity07(self, conn: &DbConn, actor: User, _id: &str) -> Result<()> {
|
|
|
|
if self.author_id != actor.id {
|
|
|
|
return Err(Error::Unauthorized);
|
|
|
|
}
|
|
|
|
|
|
|
|
for m in Mention::list_for_comment(conn, self.id)? {
|
|
|
|
for n in Notification::find_for_mention(conn, &m)? {
|
|
|
|
n.delete(conn)?;
|
|
|
|
}
|
|
|
|
m.delete(conn)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
for n in Notification::find_for_comment(conn, &self)? {
|
|
|
|
n.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)?;
|
|
|
|
diesel::delete(&self).execute(&**conn)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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::*;
|
2022-01-09 12:43:38 +01:00
|
|
|
use crate::blogs::Blog;
|
2019-04-17 19:31:47 +02:00
|
|
|
use crate::inbox::{inbox, tests::fill_database, InboxResult};
|
|
|
|
use crate::safe_string::SafeString;
|
2022-01-09 05:10:38 +01:00
|
|
|
use crate::tests::{db, format_datetime};
|
|
|
|
use assert_json_diff::assert_json_eq;
|
2019-04-17 19:31:47 +02:00
|
|
|
use diesel::Connection;
|
2022-01-09 05:10:38 +01:00
|
|
|
use serde_json::{json, to_value};
|
2019-04-17 19:31:47 +02:00
|
|
|
|
2022-01-09 12:43:38 +01:00
|
|
|
fn prepare_activity(conn: &DbConn) -> (Comment, Vec<Post>, Vec<User>, Vec<Blog>) {
|
|
|
|
let (posts, users, blogs) = fill_database(&conn);
|
|
|
|
|
|
|
|
let comment = Comment::insert(
|
|
|
|
conn,
|
|
|
|
NewComment {
|
|
|
|
content: SafeString::new("My comment, mentioning to @user"),
|
|
|
|
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();
|
|
|
|
|
|
|
|
(comment, posts, users, blogs)
|
|
|
|
}
|
|
|
|
|
2019-04-17 19:31:47 +02:00
|
|
|
// 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() {
|
2021-01-30 15:15:07 +01:00
|
|
|
let conn = &db();
|
2019-04-17 19:31:47 +02:00
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2022-01-09 12:43:38 +01:00
|
|
|
let (original_comm, posts, users, _blogs) = prepare_activity(&conn);
|
2021-01-30 15:15:07 +01:00
|
|
|
let act = original_comm.create_activity(&conn).unwrap();
|
2022-01-09 05:10:38 +01:00
|
|
|
|
|
|
|
assert_json_eq!(to_value(&act).unwrap(), json!({
|
|
|
|
"actor": "https://plu.me/@/admin/",
|
|
|
|
"cc": ["https://plu.me/@/admin/followers"],
|
|
|
|
"id": format!("https://plu.me/~/BlogName/testing/comment/{}/activity", original_comm.id),
|
|
|
|
"object": {
|
|
|
|
"attributedTo": "https://plu.me/@/admin/",
|
|
|
|
"content": r###"<p dir="auto">My comment, mentioning to <a href="https://plu.me/@/user/" title="user">@user</a></p>
|
|
|
|
"###,
|
|
|
|
"id": format!("https://plu.me/~/BlogName/testing/comment/{}", original_comm.id),
|
|
|
|
"inReplyTo": "https://plu.me/~/BlogName/testing",
|
|
|
|
"published": format_datetime(&original_comm.creation_date),
|
|
|
|
"summary": "My CW",
|
|
|
|
"tag": [
|
|
|
|
{
|
|
|
|
"href": "https://plu.me/@/user/",
|
|
|
|
"name": "@user",
|
|
|
|
"type": "Mention"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"type": "Note"
|
|
|
|
},
|
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"type": "Create",
|
|
|
|
}));
|
|
|
|
|
2022-01-09 12:36:38 +01:00
|
|
|
let reply = Comment::insert(
|
|
|
|
conn,
|
|
|
|
NewComment {
|
|
|
|
content: SafeString::new(""),
|
|
|
|
in_response_to_id: Some(original_comm.id),
|
|
|
|
post_id: posts[0].id,
|
|
|
|
author_id: users[1].id,
|
|
|
|
ap_url: None,
|
|
|
|
sensitive: false,
|
|
|
|
spoiler_text: "".into(),
|
|
|
|
public_visibility: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let reply_act = reply.create_activity(&conn).unwrap();
|
|
|
|
|
|
|
|
assert_json_eq!(to_value(&reply_act).unwrap(), json!({
|
|
|
|
"actor": "https://plu.me/@/user/",
|
|
|
|
"cc": ["https://plu.me/@/user/followers"],
|
|
|
|
"id": format!("https://plu.me/~/BlogName/testing/comment/{}/activity", reply.id),
|
|
|
|
"object": {
|
|
|
|
"attributedTo": "https://plu.me/@/user/",
|
|
|
|
"content": "",
|
|
|
|
"id": format!("https://plu.me/~/BlogName/testing/comment/{}", reply.id),
|
|
|
|
"inReplyTo": format!("https://plu.me/~/BlogName/testing/comment/{}", original_comm.id),
|
|
|
|
"published": format_datetime(&reply.creation_date),
|
|
|
|
"summary": "",
|
|
|
|
"tag": [],
|
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"type": "Note"
|
|
|
|
},
|
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"type": "Create"
|
|
|
|
}));
|
|
|
|
|
2019-04-17 19:31:47 +02:00
|
|
|
inbox(
|
2021-01-30 15:15:07 +01:00
|
|
|
&conn,
|
|
|
|
serde_json::to_value(original_comm.build_delete(&conn).unwrap()).unwrap(),
|
2019-04-17 19:31:47 +02:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2022-04-23 18:37:03 +02:00
|
|
|
match inbox(&conn, 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(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// creates a post, get it's Create activity, delete the post,
|
|
|
|
// "send" the Create to the inbox, and check it works
|
|
|
|
#[test]
|
|
|
|
fn self_federation07() {
|
|
|
|
let conn = &db();
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
|
|
|
let (original_comm, posts, users, _blogs) = prepare_activity(&conn);
|
|
|
|
let act = original_comm.create_activity07(&conn).unwrap();
|
|
|
|
|
|
|
|
assert_json_eq!(to_value(&act).unwrap(), json!({
|
|
|
|
"actor": "https://plu.me/@/admin/",
|
|
|
|
"cc": ["https://plu.me/@/admin/followers"],
|
|
|
|
"id": format!("https://plu.me/~/BlogName/testing/comment/{}/activity", original_comm.id),
|
|
|
|
"object": {
|
|
|
|
"attributedTo": "https://plu.me/@/admin/",
|
|
|
|
"content": r###"<p dir="auto">My comment, mentioning to <a href="https://plu.me/@/user/" title="user">@user</a></p>
|
|
|
|
"###,
|
|
|
|
"id": format!("https://plu.me/~/BlogName/testing/comment/{}", original_comm.id),
|
|
|
|
"inReplyTo": "https://plu.me/~/BlogName/testing",
|
|
|
|
"published": format_datetime(&original_comm.creation_date),
|
|
|
|
"summary": "My CW",
|
|
|
|
"tag": [
|
|
|
|
{
|
|
|
|
"href": "https://plu.me/@/user/",
|
|
|
|
"name": "@user",
|
|
|
|
"type": "Mention"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"type": "Note"
|
|
|
|
},
|
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"type": "Create",
|
|
|
|
}));
|
|
|
|
|
|
|
|
let reply = Comment::insert(
|
|
|
|
conn,
|
|
|
|
NewComment {
|
|
|
|
content: SafeString::new(""),
|
|
|
|
in_response_to_id: Some(original_comm.id),
|
|
|
|
post_id: posts[0].id,
|
|
|
|
author_id: users[1].id,
|
|
|
|
ap_url: None,
|
|
|
|
sensitive: false,
|
|
|
|
spoiler_text: "".into(),
|
|
|
|
public_visibility: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let reply_act = reply.create_activity07(&conn).unwrap();
|
|
|
|
|
|
|
|
assert_json_eq!(to_value(&reply_act).unwrap(), json!({
|
|
|
|
"actor": "https://plu.me/@/user/",
|
|
|
|
"cc": ["https://plu.me/@/user/followers"],
|
|
|
|
"id": format!("https://plu.me/~/BlogName/testing/comment/{}/activity", reply.id),
|
|
|
|
"object": {
|
|
|
|
"attributedTo": "https://plu.me/@/user/",
|
|
|
|
"content": "",
|
|
|
|
"id": format!("https://plu.me/~/BlogName/testing/comment/{}", reply.id),
|
|
|
|
"inReplyTo": format!("https://plu.me/~/BlogName/testing/comment/{}", original_comm.id),
|
|
|
|
"published": format_datetime(&reply.creation_date),
|
|
|
|
"summary": "",
|
|
|
|
"tag": [],
|
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"type": "Note"
|
|
|
|
},
|
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"type": "Create"
|
|
|
|
}));
|
|
|
|
|
|
|
|
inbox(
|
|
|
|
&conn,
|
|
|
|
serde_json::to_value(original_comm.build_delete07(&conn).unwrap()).unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2022-01-09 05:10:38 +01:00
|
|
|
match inbox(&conn, to_value(act).unwrap()).unwrap() {
|
2019-04-17 19:31:47 +02:00
|
|
|
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
|
|
|
}
|
2022-01-09 13:05:47 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn to_activity() {
|
|
|
|
let conn = db();
|
|
|
|
conn.test_transaction::<_, Error, _>(|| {
|
|
|
|
let (comment, _posts, _users, _blogs) = prepare_activity(&conn);
|
|
|
|
let act = comment.to_activity(&conn)?;
|
|
|
|
|
|
|
|
let expected = json!({
|
|
|
|
"attributedTo": "https://plu.me/@/admin/",
|
|
|
|
"content": r###"<p dir="auto">My comment, mentioning to <a href="https://plu.me/@/user/" title="user">@user</a></p>
|
|
|
|
"###,
|
|
|
|
"id": format!("https://plu.me/~/BlogName/testing/comment/{}", comment.id),
|
|
|
|
"inReplyTo": "https://plu.me/~/BlogName/testing",
|
|
|
|
"published": format_datetime(&comment.creation_date),
|
|
|
|
"summary": "My CW",
|
|
|
|
"tag": [
|
|
|
|
{
|
|
|
|
"href": "https://plu.me/@/user/",
|
|
|
|
"name": "@user",
|
|
|
|
"type": "Mention"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#Public"],
|
2022-04-03 13:51:47 +02:00
|
|
|
"type": "Note"
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_json_eq!(to_value(act)?, expected);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn to_activity07() {
|
|
|
|
let conn = db();
|
|
|
|
conn.test_transaction::<_, Error, _>(|| {
|
|
|
|
let (comment, _posts, _users, _blogs) = prepare_activity(&conn);
|
|
|
|
let act = comment.to_activity07(&conn)?;
|
|
|
|
|
|
|
|
let expected = json!({
|
|
|
|
"attributedTo": "https://plu.me/@/admin/",
|
|
|
|
"content": r###"<p dir="auto">My comment, mentioning to <a href="https://plu.me/@/user/" title="user">@user</a></p>
|
|
|
|
"###,
|
|
|
|
"id": format!("https://plu.me/~/BlogName/testing/comment/{}", comment.id),
|
|
|
|
"inReplyTo": "https://plu.me/~/BlogName/testing",
|
|
|
|
"published": format_datetime(&comment.creation_date),
|
|
|
|
"summary": "My CW",
|
|
|
|
"tag": [
|
|
|
|
{
|
|
|
|
"href": "https://plu.me/@/user/",
|
|
|
|
"name": "@user",
|
|
|
|
"type": "Mention"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#Public"],
|
2022-01-09 13:05:47 +01:00
|
|
|
"type": "Note"
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_json_eq!(to_value(act)?, expected);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn build_delete() {
|
|
|
|
let conn = db();
|
|
|
|
conn.test_transaction::<_, Error, _>(|| {
|
|
|
|
let (comment, _posts, _users, _blogs) = prepare_activity(&conn);
|
|
|
|
let act = comment.build_delete(&conn)?;
|
|
|
|
|
|
|
|
let expected = json!({
|
|
|
|
"actor": "https://plu.me/@/admin/",
|
|
|
|
"id": format!("https://plu.me/~/BlogName/testing/comment/{}#delete", comment.id),
|
|
|
|
"object": {
|
|
|
|
"id": format!("https://plu.me/~/BlogName/testing/comment/{}", comment.id),
|
|
|
|
"type": "Tombstone"
|
|
|
|
},
|
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"type": "Delete"
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_json_eq!(to_value(act)?, expected);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
2022-04-03 14:44:32 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn build_delete07() {
|
|
|
|
let conn = db();
|
|
|
|
conn.test_transaction::<_, Error, _>(|| {
|
|
|
|
let (comment, _posts, _users, _blogs) = prepare_activity(&conn);
|
|
|
|
let act = comment.build_delete07(&conn)?;
|
|
|
|
|
|
|
|
let expected = json!({
|
|
|
|
"actor": "https://plu.me/@/admin/",
|
|
|
|
"id": format!("https://plu.me/~/BlogName/testing/comment/{}#delete", comment.id),
|
|
|
|
"object": {
|
|
|
|
"id": format!("https://plu.me/~/BlogName/testing/comment/{}", comment.id),
|
|
|
|
"type": "Tombstone"
|
|
|
|
},
|
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"type": "Delete"
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_json_eq!(to_value(act)?, expected);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
2018-12-23 11:13:36 +01:00
|
|
|
}
|