2021-11-05 00:24:10 +00:00
|
|
|
use crate::{
|
2021-11-06 17:35:14 +00:00
|
|
|
activities::{verify_is_public, verify_person_in_community},
|
2022-06-02 14:33:41 +00:00
|
|
|
check_apub_id_valid_with_strictness,
|
2021-11-15 22:54:25 +00:00
|
|
|
mentions::collect_non_local_mentions,
|
2022-04-25 21:11:34 +00:00
|
|
|
objects::{read_from_string_or_source, verify_is_remote_object},
|
2022-08-22 20:55:10 +00:00
|
|
|
protocol::{
|
|
|
|
objects::{note::Note, LanguageTag},
|
2022-12-01 20:52:49 +00:00
|
|
|
InCommunity,
|
2022-08-22 20:55:10 +00:00
|
|
|
Source,
|
|
|
|
},
|
2021-11-05 00:24:10 +00:00
|
|
|
};
|
2022-06-02 14:33:41 +00:00
|
|
|
use activitypub_federation::{
|
2023-03-21 15:03:05 +00:00
|
|
|
config::Data,
|
|
|
|
kinds::{object::NoteType, public},
|
|
|
|
protocol::{values::MediaTypeMarkdownOrHtml, verification::verify_domains_match},
|
|
|
|
traits::Object,
|
2022-06-02 14:33:41 +00:00
|
|
|
};
|
2023-08-24 15:27:00 +00:00
|
|
|
use chrono::{DateTime, Utc};
|
2023-07-26 18:01:15 +00:00
|
|
|
use lemmy_api_common::{
|
|
|
|
context::LemmyContext,
|
2023-09-06 14:56:26 +00:00
|
|
|
utils::{local_site_opt_to_slur_regex, sanitize_html_federation},
|
2023-07-26 18:01:15 +00:00
|
|
|
};
|
2021-03-18 20:25:21 +00:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::{
|
2022-10-27 09:24:07 +00:00
|
|
|
comment::{Comment, CommentInsertForm, CommentUpdateForm},
|
2021-03-22 14:08:06 +00:00
|
|
|
community::Community,
|
2022-10-27 09:24:07 +00:00
|
|
|
local_site::LocalSite,
|
2021-03-18 20:25:21 +00:00
|
|
|
person::Person,
|
|
|
|
post::Post,
|
|
|
|
},
|
2021-10-16 13:33:38 +00:00
|
|
|
traits::Crud,
|
2020-10-12 14:10:09 +00:00
|
|
|
};
|
|
|
|
use lemmy_utils::{
|
2023-07-10 14:50:07 +00:00
|
|
|
error::{LemmyError, LemmyErrorType},
|
2023-02-16 04:05:14 +00:00
|
|
|
utils::{markdown::markdown_to_html, slurs::remove_slurs, time::convert_datetime},
|
2020-10-12 14:10:09 +00:00
|
|
|
};
|
2021-11-05 00:24:10 +00:00
|
|
|
use std::ops::Deref;
|
|
|
|
use url::Url;
|
2021-07-31 14:57:37 +00:00
|
|
|
|
2021-10-18 21:36:44 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2022-11-28 14:29:33 +00:00
|
|
|
pub struct ApubComment(pub(crate) Comment);
|
2021-10-18 21:36:44 +00:00
|
|
|
|
|
|
|
impl Deref for ApubComment {
|
|
|
|
type Target = Comment;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Comment> for ApubComment {
|
|
|
|
fn from(c: Comment) -> Self {
|
2022-03-30 14:58:03 +00:00
|
|
|
ApubComment(c)
|
2021-10-18 21:36:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl Object for ApubComment {
|
2021-10-18 21:36:44 +00:00
|
|
|
type DataType = LemmyContext;
|
2023-03-21 15:03:05 +00:00
|
|
|
type Kind = Note;
|
2022-06-02 14:33:41 +00:00
|
|
|
type Error = LemmyError;
|
2021-10-18 21:36:44 +00:00
|
|
|
|
2023-08-24 15:27:00 +00:00
|
|
|
fn last_refreshed_at(&self) -> Option<DateTime<Utc>> {
|
2021-10-18 21:36:44 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn read_from_id(
|
2021-10-18 21:36:44 +00:00
|
|
|
object_id: Url,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: &Data<Self::DataType>,
|
2021-10-18 21:36:44 +00:00
|
|
|
) -> Result<Option<Self>, LemmyError> {
|
|
|
|
Ok(
|
2023-07-11 13:09:59 +00:00
|
|
|
Comment::read_from_apub_id(&mut context.pool(), object_id)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await?
|
|
|
|
.map(Into::into),
|
2021-10-18 21:36:44 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn delete(self, context: &Data<Self::DataType>) -> Result<(), LemmyError> {
|
2021-11-03 17:47:24 +00:00
|
|
|
if !self.deleted {
|
2023-08-08 09:41:41 +00:00
|
|
|
let form = CommentUpdateForm {
|
|
|
|
deleted: Some(true),
|
|
|
|
..Default::default()
|
|
|
|
};
|
2023-07-11 13:09:59 +00:00
|
|
|
Comment::update(&mut context.pool(), self.id, &form).await?;
|
2021-11-03 17:47:24 +00:00
|
|
|
}
|
2021-10-18 21:36:44 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn into_json(self, context: &Data<Self::DataType>) -> Result<Note, LemmyError> {
|
2020-10-12 14:10:09 +00:00
|
|
|
let creator_id = self.creator_id;
|
2023-07-11 13:09:59 +00:00
|
|
|
let creator = Person::read(&mut context.pool(), creator_id).await?;
|
2020-10-12 14:10:09 +00:00
|
|
|
|
|
|
|
let post_id = self.post_id;
|
2023-07-11 13:09:59 +00:00
|
|
|
let post = Post::read(&mut context.pool(), post_id).await?;
|
2021-11-15 22:54:25 +00:00
|
|
|
let community_id = post.community_id;
|
2023-07-11 13:09:59 +00:00
|
|
|
let community = Community::read(&mut context.pool(), community_id).await?;
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2022-07-30 03:55:59 +00:00
|
|
|
let in_reply_to = if let Some(comment_id) = self.parent_comment_id() {
|
2023-07-11 13:09:59 +00:00
|
|
|
let parent_comment = Comment::read(&mut context.pool(), comment_id).await?;
|
2023-03-21 15:03:05 +00:00
|
|
|
parent_comment.ap_id.into()
|
2021-10-22 16:21:26 +00:00
|
|
|
} else {
|
2023-03-21 15:03:05 +00:00
|
|
|
post.ap_id.into()
|
2021-10-22 16:21:26 +00:00
|
|
|
};
|
2023-07-11 13:09:59 +00:00
|
|
|
let language = LanguageTag::new_single(self.language_id, &mut context.pool()).await?;
|
2023-03-21 15:03:05 +00:00
|
|
|
let maa = collect_non_local_mentions(&self, community.actor_id.clone().into(), context).await?;
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2021-07-31 14:57:37 +00:00
|
|
|
let note = Note {
|
|
|
|
r#type: NoteType::Note,
|
2023-03-21 15:03:05 +00:00
|
|
|
id: self.ap_id.clone().into(),
|
|
|
|
attributed_to: creator.actor_id.into(),
|
2021-10-21 17:25:35 +00:00
|
|
|
to: vec![public()],
|
2021-11-15 22:54:25 +00:00
|
|
|
cc: maa.ccs,
|
2021-11-02 11:49:28 +00:00
|
|
|
content: markdown_to_html(&self.content),
|
2022-05-06 23:53:33 +00:00
|
|
|
media_type: Some(MediaTypeMarkdownOrHtml::Html),
|
2022-04-01 18:25:19 +00:00
|
|
|
source: Some(Source::new(self.content.clone())),
|
2021-10-22 16:21:26 +00:00
|
|
|
in_reply_to,
|
2021-10-21 17:25:35 +00:00
|
|
|
published: Some(convert_datetime(self.published)),
|
2021-07-31 14:57:37 +00:00
|
|
|
updated: self.updated.map(convert_datetime),
|
2021-11-15 22:54:25 +00:00
|
|
|
tag: maa.tags,
|
2022-08-17 11:38:52 +00:00
|
|
|
distinguished: Some(self.distinguished),
|
2022-10-06 18:27:58 +00:00
|
|
|
language,
|
2023-03-21 15:03:05 +00:00
|
|
|
audience: Some(community.actor_id.into()),
|
2021-07-31 14:57:37 +00:00
|
|
|
};
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2021-07-31 14:57:37 +00:00
|
|
|
Ok(note)
|
2020-10-12 14:10:09 +00:00
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2021-11-06 17:35:14 +00:00
|
|
|
async fn verify(
|
|
|
|
note: &Note,
|
2021-08-12 12:48:09 +00:00
|
|
|
expected_domain: &Url,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2021-11-06 17:35:14 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
2021-11-03 16:26:09 +00:00
|
|
|
verify_domains_match(note.id.inner(), expected_domain)?;
|
2021-11-06 17:35:14 +00:00
|
|
|
verify_domains_match(note.attributed_to.inner(), note.id.inner())?;
|
2021-11-06 17:44:34 +00:00
|
|
|
verify_is_public(¬e.to, ¬e.cc)?;
|
2023-03-21 15:03:05 +00:00
|
|
|
let community = note.community(context).await?;
|
2022-10-27 09:24:07 +00:00
|
|
|
|
2023-07-05 15:08:02 +00:00
|
|
|
check_apub_id_valid_with_strictness(note.id.inner(), community.local, context).await?;
|
2022-06-22 20:24:54 +00:00
|
|
|
verify_is_remote_object(note.id.inner(), context.settings())?;
|
2023-03-21 15:03:05 +00:00
|
|
|
verify_person_in_community(¬e.attributed_to, &community, context).await?;
|
|
|
|
let (post, _) = note.get_parents(context).await?;
|
2021-09-25 15:44:52 +00:00
|
|
|
if post.locked {
|
2023-08-31 13:01:08 +00:00
|
|
|
Err(LemmyErrorType::PostIsLocked)?
|
|
|
|
} else {
|
|
|
|
Ok(())
|
2021-09-25 15:44:52 +00:00
|
|
|
}
|
2021-11-06 17:35:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts a `Note` to `Comment`.
|
|
|
|
///
|
|
|
|
/// If the parent community, post and comment(s) are not known locally, these are also fetched.
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn from_json(note: Note, context: &Data<LemmyContext>) -> Result<ApubComment, LemmyError> {
|
|
|
|
let creator = note.attributed_to.dereference(context).await?;
|
|
|
|
let (post, parent_comment) = note.get_parents(context).await?;
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2022-05-06 23:53:33 +00:00
|
|
|
let content = read_from_string_or_source(¬e.content, ¬e.media_type, ¬e.source);
|
2022-10-27 09:24:07 +00:00
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
let local_site = LocalSite::read(&mut context.pool()).await.ok();
|
2022-10-27 09:24:07 +00:00
|
|
|
let slur_regex = &local_site_opt_to_slur_regex(&local_site);
|
2023-07-26 18:01:15 +00:00
|
|
|
let content = remove_slurs(&content, slur_regex);
|
2023-09-06 14:56:26 +00:00
|
|
|
let content = sanitize_html_federation(&content);
|
2023-07-11 13:09:59 +00:00
|
|
|
let language_id =
|
|
|
|
LanguageTag::to_language_id_single(note.language, &mut context.pool()).await?;
|
2022-08-22 20:55:10 +00:00
|
|
|
|
2022-10-27 09:24:07 +00:00
|
|
|
let form = CommentInsertForm {
|
2020-10-12 14:10:09 +00:00
|
|
|
creator_id: creator.id,
|
|
|
|
post_id: post.id,
|
2023-07-26 18:01:15 +00:00
|
|
|
content,
|
2020-10-12 14:10:09 +00:00
|
|
|
removed: None,
|
2023-08-24 15:27:00 +00:00
|
|
|
published: note.published.map(Into::into),
|
|
|
|
updated: note.updated.map(Into::into),
|
2022-11-03 13:39:30 +00:00
|
|
|
deleted: Some(false),
|
2021-11-06 12:37:55 +00:00
|
|
|
ap_id: Some(note.id.into()),
|
2022-08-17 11:38:52 +00:00
|
|
|
distinguished: note.distinguished,
|
2021-03-20 20:59:07 +00:00
|
|
|
local: Some(false),
|
2022-10-06 18:27:58 +00:00
|
|
|
language_id,
|
2021-07-31 14:57:37 +00:00
|
|
|
};
|
2022-07-30 03:55:59 +00:00
|
|
|
let parent_comment_path = parent_comment.map(|t| t.0.path);
|
2023-07-11 13:09:59 +00:00
|
|
|
let comment = Comment::create(&mut context.pool(), &form, parent_comment_path.as_ref()).await?;
|
2021-10-18 21:36:44 +00:00
|
|
|
Ok(comment.into())
|
2020-10-12 14:10:09 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-21 17:25:35 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
2021-10-26 10:47:26 +00:00
|
|
|
pub(crate) mod tests {
|
2023-07-17 15:04:14 +00:00
|
|
|
#![allow(clippy::unwrap_used)]
|
|
|
|
#![allow(clippy::indexing_slicing)]
|
|
|
|
|
2021-10-29 14:54:19 +00:00
|
|
|
use super::*;
|
2022-02-17 22:04:01 +00:00
|
|
|
use crate::{
|
|
|
|
objects::{
|
|
|
|
community::{tests::parse_lemmy_community, ApubCommunity},
|
|
|
|
instance::ApubSite,
|
|
|
|
person::{tests::parse_lemmy_person, ApubPerson},
|
|
|
|
post::ApubPost,
|
|
|
|
tests::init_context,
|
|
|
|
},
|
|
|
|
protocol::tests::file_to_json_object,
|
2021-10-21 17:25:35 +00:00
|
|
|
};
|
2021-10-29 14:54:19 +00:00
|
|
|
use assert_json_diff::assert_json_include;
|
2022-03-24 16:33:42 +00:00
|
|
|
use html2md::parse_html;
|
2022-02-07 19:23:12 +00:00
|
|
|
use lemmy_db_schema::source::site::Site;
|
2021-10-29 14:54:19 +00:00
|
|
|
use serial_test::serial;
|
2021-10-21 17:25:35 +00:00
|
|
|
|
2021-11-01 13:05:20 +00:00
|
|
|
async fn prepare_comment_test(
|
2021-10-21 17:25:35 +00:00
|
|
|
url: &Url,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2022-02-07 19:23:12 +00:00
|
|
|
) -> (ApubPerson, ApubCommunity, ApubPost, ApubSite) {
|
2023-03-21 15:03:05 +00:00
|
|
|
// use separate counter so this doesnt affect tests
|
|
|
|
let context2 = context.reset_request_count();
|
|
|
|
let (person, site) = parse_lemmy_person(&context2).await;
|
|
|
|
let community = parse_lemmy_community(&context2).await;
|
2022-01-17 14:40:47 +00:00
|
|
|
let post_json = file_to_json_object("assets/lemmy/objects/page.json").unwrap();
|
2023-03-21 15:03:05 +00:00
|
|
|
ApubPost::verify(&post_json, url, &context2).await.unwrap();
|
|
|
|
let post = ApubPost::from_json(post_json, &context2).await.unwrap();
|
2022-02-07 19:23:12 +00:00
|
|
|
(person, community, post, site)
|
2021-10-21 17:25:35 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn cleanup(data: (ApubPerson, ApubCommunity, ApubPost, ApubSite), context: &LemmyContext) {
|
2023-07-11 13:09:59 +00:00
|
|
|
Post::delete(&mut context.pool(), data.2.id).await.unwrap();
|
|
|
|
Community::delete(&mut context.pool(), data.1.id)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
Person::delete(&mut context.pool(), data.0.id)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
Site::delete(&mut context.pool(), data.3.id).await.unwrap();
|
|
|
|
LocalSite::delete(&mut context.pool()).await.unwrap();
|
2021-10-21 17:25:35 +00:00
|
|
|
}
|
|
|
|
|
2023-06-26 08:24:11 +00:00
|
|
|
#[tokio::test]
|
2021-10-21 17:25:35 +00:00
|
|
|
#[serial]
|
2021-10-26 10:47:26 +00:00
|
|
|
pub(crate) async fn test_parse_lemmy_comment() {
|
2022-11-09 10:05:00 +00:00
|
|
|
let context = init_context().await;
|
2021-10-22 16:21:26 +00:00
|
|
|
let url = Url::parse("https://enterprise.lemmy.ml/comment/38741").unwrap();
|
2021-10-21 17:25:35 +00:00
|
|
|
let data = prepare_comment_test(&url, &context).await;
|
|
|
|
|
2022-01-17 14:40:47 +00:00
|
|
|
let json: Note = file_to_json_object("assets/lemmy/objects/note.json").unwrap();
|
2023-03-21 15:03:05 +00:00
|
|
|
ApubComment::verify(&json, &url, &context).await.unwrap();
|
|
|
|
let comment = ApubComment::from_json(json.clone(), &context)
|
2021-10-21 17:25:35 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2021-11-05 00:24:10 +00:00
|
|
|
assert_eq!(comment.ap_id, url.into());
|
2021-10-22 16:21:26 +00:00
|
|
|
assert_eq!(comment.content.len(), 14);
|
2021-10-21 17:25:35 +00:00
|
|
|
assert!(!comment.local);
|
2023-03-21 15:03:05 +00:00
|
|
|
assert_eq!(context.request_count(), 0);
|
2021-10-21 17:25:35 +00:00
|
|
|
|
2021-11-06 12:37:55 +00:00
|
|
|
let comment_id = comment.id;
|
2023-03-21 15:03:05 +00:00
|
|
|
let to_apub = comment.into_json(&context).await.unwrap();
|
2021-10-21 17:25:35 +00:00
|
|
|
assert_json_include!(actual: json, expected: to_apub);
|
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
Comment::delete(&mut context.pool(), comment_id)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2022-11-09 10:05:00 +00:00
|
|
|
cleanup(data, &context).await;
|
2021-10-21 17:25:35 +00:00
|
|
|
}
|
|
|
|
|
2023-06-26 08:24:11 +00:00
|
|
|
#[tokio::test]
|
2021-10-21 17:25:35 +00:00
|
|
|
#[serial]
|
2021-10-22 16:21:26 +00:00
|
|
|
async fn test_parse_pleroma_comment() {
|
2022-11-09 10:05:00 +00:00
|
|
|
let context = init_context().await;
|
2021-10-22 16:21:26 +00:00
|
|
|
let url = Url::parse("https://enterprise.lemmy.ml/comment/38741").unwrap();
|
2021-10-21 17:25:35 +00:00
|
|
|
let data = prepare_comment_test(&url, &context).await;
|
|
|
|
|
|
|
|
let pleroma_url =
|
|
|
|
Url::parse("https://queer.hacktivis.me/objects/8d4973f4-53de-49cd-8c27-df160e16a9c2")
|
|
|
|
.unwrap();
|
2022-01-17 14:40:47 +00:00
|
|
|
let person_json = file_to_json_object("assets/pleroma/objects/person.json").unwrap();
|
2023-03-21 15:03:05 +00:00
|
|
|
ApubPerson::verify(&person_json, &pleroma_url, &context)
|
2021-10-21 17:25:35 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2023-03-21 15:03:05 +00:00
|
|
|
ApubPerson::from_json(person_json, &context).await.unwrap();
|
2022-01-17 14:40:47 +00:00
|
|
|
let json = file_to_json_object("assets/pleroma/objects/note.json").unwrap();
|
2023-03-21 15:03:05 +00:00
|
|
|
ApubComment::verify(&json, &pleroma_url, &context)
|
2021-10-21 17:25:35 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2023-03-21 15:03:05 +00:00
|
|
|
let comment = ApubComment::from_json(json, &context).await.unwrap();
|
2021-10-21 17:25:35 +00:00
|
|
|
|
2021-11-05 00:24:10 +00:00
|
|
|
assert_eq!(comment.ap_id, pleroma_url.into());
|
2021-10-21 17:25:35 +00:00
|
|
|
assert_eq!(comment.content.len(), 64);
|
|
|
|
assert!(!comment.local);
|
2023-03-21 15:03:05 +00:00
|
|
|
assert_eq!(context.request_count(), 1);
|
2021-10-21 17:25:35 +00:00
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
Comment::delete(&mut context.pool(), comment.id)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2022-11-09 10:05:00 +00:00
|
|
|
cleanup(data, &context).await;
|
2021-10-21 17:25:35 +00:00
|
|
|
}
|
|
|
|
|
2023-06-26 08:24:11 +00:00
|
|
|
#[tokio::test]
|
2021-10-21 17:25:35 +00:00
|
|
|
#[serial]
|
|
|
|
async fn test_html_to_markdown_sanitize() {
|
|
|
|
let parsed = parse_html("<script></script><b>hello</b>");
|
|
|
|
assert_eq!(parsed, "**hello**");
|
|
|
|
}
|
|
|
|
}
|