lemmy/crates/apub/src/objects/comment.rs

317 lines
9.9 KiB
Rust
Raw Normal View History

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},
check_apub_id_valid_with_strictness,
fetch_local_site_data,
local_instance,
mentions::collect_non_local_mentions,
objects::{read_from_string_or_source, verify_is_remote_object},
2022-08-22 20:55:10 +00:00
protocol::{
objects::{note::Note, LanguageTag},
InCommunity,
2022-08-22 20:55:10 +00:00
Source,
},
2021-11-05 00:24:10 +00:00
PostOrComment,
};
use activitypub_federation::{
core::object_id::ObjectId,
deser::values::MediaTypeMarkdownOrHtml,
traits::ApubObject,
utils::verify_domains_match,
};
use activitystreams_kinds::{object::NoteType, public};
use chrono::NaiveDateTime;
use lemmy_api_common::{context::LemmyContext, utils::local_site_opt_to_slur_regex};
use lemmy_db_schema::{
source::{
comment::{Comment, CommentInsertForm, CommentUpdateForm},
community::Community,
local_site::LocalSite,
person::Person,
post::Post,
},
2021-10-16 13:33:38 +00:00
traits::Crud,
};
use lemmy_utils::{
error::LemmyError,
2021-11-05 00:24:10 +00:00
utils::{convert_datetime, markdown_to_html, remove_slurs},
};
2021-11-05 00:24:10 +00:00
use std::ops::Deref;
use url::Url;
#[derive(Clone, Debug)]
pub struct ApubComment(pub(crate) Comment);
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)
}
}
#[async_trait::async_trait(?Send)]
impl ApubObject for ApubComment {
type DataType = LemmyContext;
type ApubType = Note;
type DbType = Comment;
type Error = LemmyError;
fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
None
}
#[tracing::instrument(skip_all)]
async fn read_from_apub_id(
object_id: Url,
context: &LemmyContext,
) -> Result<Option<Self>, LemmyError> {
Ok(
2022-11-09 10:05:00 +00:00
Comment::read_from_apub_id(context.pool(), object_id)
.await?
.map(Into::into),
)
}
#[tracing::instrument(skip_all)]
async fn delete(self, context: &LemmyContext) -> Result<(), LemmyError> {
if !self.deleted {
2022-11-09 10:05:00 +00:00
let form = CommentUpdateForm::builder().deleted(Some(true)).build();
Comment::update(context.pool(), self.id, &form).await?;
}
Ok(())
}
#[tracing::instrument(skip_all)]
async fn into_apub(self, context: &LemmyContext) -> Result<Note, LemmyError> {
let creator_id = self.creator_id;
2022-11-09 10:05:00 +00:00
let creator = Person::read(context.pool(), creator_id).await?;
let post_id = self.post_id;
2022-11-09 10:05:00 +00:00
let post = Post::read(context.pool(), post_id).await?;
let community_id = post.community_id;
2022-11-09 10:05:00 +00:00
let community = Community::read(context.pool(), community_id).await?;
let in_reply_to = if let Some(comment_id) = self.parent_comment_id() {
2022-11-09 10:05:00 +00:00
let parent_comment = Comment::read(context.pool(), comment_id).await?;
2021-11-05 00:24:10 +00:00
ObjectId::<PostOrComment>::new(parent_comment.ap_id)
} else {
2021-11-05 00:24:10 +00:00
ObjectId::<PostOrComment>::new(post.ap_id)
};
let language = LanguageTag::new_single(self.language_id, context.pool()).await?;
let maa = collect_non_local_mentions(
&self,
ObjectId::new(community.actor_id.clone()),
context,
&mut 0,
)
.await?;
let note = Note {
r#type: NoteType::Note,
id: ObjectId::new(self.ap_id.clone()),
attributed_to: ObjectId::new(creator.actor_id),
to: vec![public()],
cc: maa.ccs,
content: markdown_to_html(&self.content),
2022-05-06 23:53:33 +00:00
media_type: Some(MediaTypeMarkdownOrHtml::Html),
source: Some(Source::new(self.content.clone())),
in_reply_to,
published: Some(convert_datetime(self.published)),
updated: self.updated.map(convert_datetime),
tag: maa.tags,
distinguished: Some(self.distinguished),
language,
audience: Some(ObjectId::new(community.actor_id)),
};
Ok(note)
}
#[tracing::instrument(skip_all)]
2021-11-06 17:35:14 +00:00
async fn verify(
note: &Note,
expected_domain: &Url,
2021-11-06 17:35:14 +00:00
context: &LemmyContext,
request_counter: &mut i32,
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())?;
verify_is_public(&note.to, &note.cc)?;
let community = note.community(context, request_counter).await?;
2022-11-09 10:05:00 +00:00
let local_site_data = fetch_local_site_data(context.pool()).await?;
check_apub_id_valid_with_strictness(
note.id.inner(),
community.local,
&local_site_data,
context.settings(),
)?;
verify_is_remote_object(note.id.inner(), context.settings())?;
verify_person_in_community(&note.attributed_to, &community, context, request_counter).await?;
let (post, _) = note.get_parents(context, request_counter).await?;
if post.locked {
return Err(LemmyError::from_message("Post is locked"));
}
2021-11-06 17:35:14 +00:00
Ok(())
}
/// Converts a `Note` to `Comment`.
///
/// If the parent community, post and comment(s) are not known locally, these are also fetched.
#[tracing::instrument(skip_all)]
2021-11-06 17:35:14 +00:00
async fn from_apub(
note: Note,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<ApubComment, LemmyError> {
let creator = note
.attributed_to
2022-11-09 10:05:00 +00:00
.dereference(context, local_instance(context).await, request_counter)
2021-11-06 17:35:14 +00:00
.await?;
let (post, parent_comment) = note.get_parents(context, request_counter).await?;
2022-05-06 23:53:33 +00:00
let content = read_from_string_or_source(&note.content, &note.media_type, &note.source);
2022-11-09 10:05:00 +00:00
let local_site = LocalSite::read(context.pool()).await.ok();
let slur_regex = &local_site_opt_to_slur_regex(&local_site);
let content_slurs_removed = remove_slurs(&content, slur_regex);
let language_id = LanguageTag::to_language_id_single(note.language, context.pool()).await?;
2022-08-22 20:55:10 +00:00
let form = CommentInsertForm {
creator_id: creator.id,
post_id: post.id,
content: content_slurs_removed,
removed: None,
published: note.published.map(|u| u.naive_local()),
updated: note.updated.map(|u| u.naive_local()),
deleted: Some(false),
ap_id: Some(note.id.into()),
distinguished: note.distinguished,
2021-03-20 20:59:07 +00:00
local: Some(false),
language_id,
};
let parent_comment_path = parent_comment.map(|t| t.0.path);
2022-11-09 10:05:00 +00:00
let comment = Comment::create(context.pool(), &form, parent_comment_path.as_ref()).await?;
Ok(comment.into())
}
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
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,
};
use assert_json_diff::assert_json_include;
use html2md::parse_html;
use lemmy_db_schema::source::site::Site;
use serial_test::serial;
async fn prepare_comment_test(
url: &Url,
context: &LemmyContext,
) -> (ApubPerson, ApubCommunity, ApubPost, ApubSite) {
let (person, site) = parse_lemmy_person(context).await;
let community = parse_lemmy_community(context).await;
let post_json = file_to_json_object("assets/lemmy/objects/page.json").unwrap();
2021-11-06 17:35:14 +00:00
ApubPost::verify(&post_json, url, context, &mut 0)
.await
.unwrap();
let post = ApubPost::from_apub(post_json, context, &mut 0)
.await
.unwrap();
(person, community, post, site)
}
2022-11-09 10:05:00 +00:00
async fn cleanup(data: (ApubPerson, ApubCommunity, ApubPost, ApubSite), context: &LemmyContext) {
Post::delete(context.pool(), data.2.id).await.unwrap();
Community::delete(context.pool(), data.1.id).await.unwrap();
Person::delete(context.pool(), data.0.id).await.unwrap();
Site::delete(context.pool(), data.3.id).await.unwrap();
LocalSite::delete(context.pool()).await.unwrap();
}
#[actix_rt::test]
#[serial]
pub(crate) async fn test_parse_lemmy_comment() {
2022-11-09 10:05:00 +00:00
let context = init_context().await;
let url = Url::parse("https://enterprise.lemmy.ml/comment/38741").unwrap();
let data = prepare_comment_test(&url, &context).await;
let json: Note = file_to_json_object("assets/lemmy/objects/note.json").unwrap();
let mut request_counter = 0;
2021-11-06 17:35:14 +00:00
ApubComment::verify(&json, &url, &context, &mut request_counter)
.await
.unwrap();
let comment = ApubComment::from_apub(json.clone(), &context, &mut request_counter)
.await
.unwrap();
2021-11-05 00:24:10 +00:00
assert_eq!(comment.ap_id, url.into());
assert_eq!(comment.content.len(), 14);
assert!(!comment.local);
assert_eq!(request_counter, 0);
let comment_id = comment.id;
let to_apub = comment.into_apub(&context).await.unwrap();
assert_json_include!(actual: json, expected: to_apub);
2022-11-09 10:05:00 +00:00
Comment::delete(context.pool(), comment_id).await.unwrap();
cleanup(data, &context).await;
}
#[actix_rt::test]
#[serial]
async fn test_parse_pleroma_comment() {
2022-11-09 10:05:00 +00:00
let context = init_context().await;
let url = Url::parse("https://enterprise.lemmy.ml/comment/38741").unwrap();
let data = prepare_comment_test(&url, &context).await;
let pleroma_url =
Url::parse("https://queer.hacktivis.me/objects/8d4973f4-53de-49cd-8c27-df160e16a9c2")
.unwrap();
let person_json = file_to_json_object("assets/pleroma/objects/person.json").unwrap();
2021-11-06 17:35:14 +00:00
ApubPerson::verify(&person_json, &pleroma_url, &context, &mut 0)
.await
.unwrap();
ApubPerson::from_apub(person_json, &context, &mut 0)
.await
.unwrap();
let json = file_to_json_object("assets/pleroma/objects/note.json").unwrap();
let mut request_counter = 0;
2021-11-06 17:35:14 +00:00
ApubComment::verify(&json, &pleroma_url, &context, &mut request_counter)
.await
.unwrap();
let comment = ApubComment::from_apub(json, &context, &mut request_counter)
.await
.unwrap();
2021-11-05 00:24:10 +00:00
assert_eq!(comment.ap_id, pleroma_url.into());
assert_eq!(comment.content.len(), 64);
assert!(!comment.local);
assert_eq!(request_counter, 0);
2022-11-09 10:05:00 +00:00
Comment::delete(context.pool(), comment.id).await.unwrap();
cleanup(data, &context).await;
}
#[actix_rt::test]
#[serial]
async fn test_html_to_markdown_sanitize() {
let parsed = parse_html("<script></script><b>hello</b>");
assert_eq!(parsed, "**hello**");
}
}