2020-10-12 14:10:09 +00:00
|
|
|
use crate::{
|
2021-10-21 17:25:35 +00:00
|
|
|
activities::{verify_is_public, verify_person_in_community},
|
2021-10-06 20:20:05 +00:00
|
|
|
context::lemmy_context,
|
2021-09-25 15:44:52 +00:00
|
|
|
fetcher::object_id::ObjectId,
|
2021-10-18 21:36:44 +00:00
|
|
|
objects::{create_tombstone, person::ApubPerson, post::ApubPost, Source},
|
2021-08-05 11:00:29 +00:00
|
|
|
PostOrComment,
|
2020-10-12 14:10:09 +00:00
|
|
|
};
|
|
|
|
use activitystreams::{
|
2021-07-31 14:57:37 +00:00
|
|
|
base::AnyBase,
|
2021-10-18 21:36:44 +00:00
|
|
|
chrono::NaiveDateTime,
|
2021-07-31 14:57:37 +00:00
|
|
|
object::{kind::NoteType, Tombstone},
|
|
|
|
primitives::OneOrMany,
|
2021-10-21 17:25:35 +00:00
|
|
|
public,
|
2021-07-31 14:57:37 +00:00
|
|
|
unparsed::Unparsed,
|
2020-10-12 14:10:09 +00:00
|
|
|
};
|
2021-10-22 16:21:26 +00:00
|
|
|
use anyhow::anyhow;
|
2021-07-31 14:57:37 +00:00
|
|
|
use chrono::{DateTime, FixedOffset};
|
2021-10-21 17:25:35 +00:00
|
|
|
use html2md::parse_html;
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_api_common::blocking;
|
2021-07-31 14:57:37 +00:00
|
|
|
use lemmy_apub_lib::{
|
2021-10-18 21:36:44 +00:00
|
|
|
traits::{ApubObject, FromApub, ToApub},
|
2021-10-21 17:25:35 +00:00
|
|
|
values::{MediaTypeHtml, MediaTypeMarkdown},
|
2021-10-06 20:20:05 +00:00
|
|
|
verify::verify_domains_match,
|
2021-07-31 14:57:37 +00:00
|
|
|
};
|
2021-03-18 20:25:21 +00:00
|
|
|
use lemmy_db_schema::{
|
2021-10-16 13:33:38 +00:00
|
|
|
newtypes::CommentId,
|
2021-03-18 20:25:21 +00:00
|
|
|
source::{
|
|
|
|
comment::{Comment, CommentForm},
|
2021-03-22 14:08:06 +00:00
|
|
|
community::Community,
|
2021-03-18 20:25:21 +00:00
|
|
|
person::Person,
|
|
|
|
post::Post,
|
|
|
|
},
|
2021-10-16 13:33:38 +00:00
|
|
|
traits::Crud,
|
|
|
|
DbPool,
|
2020-10-12 14:10:09 +00:00
|
|
|
};
|
|
|
|
use lemmy_utils::{
|
|
|
|
utils::{convert_datetime, remove_slurs},
|
|
|
|
LemmyError,
|
|
|
|
};
|
|
|
|
use lemmy_websocket::LemmyContext;
|
2021-07-31 14:57:37 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-08-12 12:48:09 +00:00
|
|
|
use serde_with::skip_serializing_none;
|
2021-08-05 11:00:29 +00:00
|
|
|
use std::ops::Deref;
|
2020-10-12 14:10:09 +00:00
|
|
|
use url::Url;
|
|
|
|
|
2021-08-12 12:48:09 +00:00
|
|
|
#[skip_serializing_none]
|
2021-07-31 14:57:37 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct Note {
|
|
|
|
#[serde(rename = "@context")]
|
|
|
|
context: OneOrMany<AnyBase>,
|
|
|
|
r#type: NoteType,
|
2021-08-12 12:48:09 +00:00
|
|
|
id: Url,
|
2021-10-18 21:36:44 +00:00
|
|
|
pub(crate) attributed_to: ObjectId<ApubPerson>,
|
2021-07-31 14:57:37 +00:00
|
|
|
/// Indicates that the object is publicly readable. Unlike [`Post.to`], this one doesn't contain
|
|
|
|
/// the community ID, as it would be incompatible with Pleroma (and we can get the community from
|
|
|
|
/// the post in [`in_reply_to`]).
|
2021-10-21 17:25:35 +00:00
|
|
|
to: Vec<Url>,
|
2021-07-31 14:57:37 +00:00
|
|
|
content: String,
|
2021-10-21 17:25:35 +00:00
|
|
|
media_type: Option<MediaTypeHtml>,
|
|
|
|
source: SourceCompat,
|
2021-10-22 16:21:26 +00:00
|
|
|
in_reply_to: ObjectId<PostOrComment>,
|
2021-10-21 17:25:35 +00:00
|
|
|
published: Option<DateTime<FixedOffset>>,
|
2021-07-31 14:57:37 +00:00
|
|
|
updated: Option<DateTime<FixedOffset>>,
|
|
|
|
#[serde(flatten)]
|
|
|
|
unparsed: Unparsed,
|
|
|
|
}
|
|
|
|
|
2021-10-21 17:25:35 +00:00
|
|
|
/// Pleroma puts a raw string in the source, so we have to handle it here for deserialization to work
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
#[serde(untagged)]
|
|
|
|
enum SourceCompat {
|
|
|
|
Lemmy(Source),
|
|
|
|
Pleroma(String),
|
|
|
|
}
|
|
|
|
|
2021-07-31 14:57:37 +00:00
|
|
|
impl Note {
|
2021-08-12 12:48:09 +00:00
|
|
|
pub(crate) fn id_unchecked(&self) -> &Url {
|
|
|
|
&self.id
|
|
|
|
}
|
|
|
|
pub(crate) fn id(&self, expected_domain: &Url) -> Result<&Url, LemmyError> {
|
|
|
|
verify_domains_match(&self.id, expected_domain)?;
|
|
|
|
Ok(&self.id)
|
|
|
|
}
|
|
|
|
|
2021-10-14 16:33:19 +00:00
|
|
|
pub(crate) async fn get_parents(
|
2021-07-31 14:57:37 +00:00
|
|
|
&self,
|
|
|
|
context: &LemmyContext,
|
|
|
|
request_counter: &mut i32,
|
2021-10-18 21:36:44 +00:00
|
|
|
) -> Result<(ApubPost, Option<CommentId>), LemmyError> {
|
2021-10-22 16:21:26 +00:00
|
|
|
// Fetch parent comment chain in a box, otherwise it can cause a stack overflow.
|
|
|
|
let parent = Box::pin(
|
|
|
|
self
|
|
|
|
.in_reply_to
|
|
|
|
.dereference(context, request_counter)
|
|
|
|
.await?,
|
|
|
|
);
|
|
|
|
match parent.deref() {
|
|
|
|
PostOrComment::Post(p) => {
|
|
|
|
// Workaround because I cant figure out how to get the post out of the box (and we dont
|
|
|
|
// want to stackoverflow in a deep comment hierarchy).
|
|
|
|
let post_id = p.id;
|
|
|
|
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
|
|
|
|
Ok((post.into(), None))
|
2021-07-31 14:57:37 +00:00
|
|
|
}
|
2021-10-22 16:21:26 +00:00
|
|
|
PostOrComment::Comment(c) => {
|
|
|
|
let post_id = c.post_id;
|
|
|
|
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
|
|
|
|
Ok((post.into(), Some(c.id)))
|
2021-08-05 11:00:29 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-31 14:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn verify(
|
|
|
|
&self,
|
|
|
|
context: &LemmyContext,
|
|
|
|
request_counter: &mut i32,
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
let (post, _parent_comment_id) = self.get_parents(context, request_counter).await?;
|
|
|
|
let community_id = post.community_id;
|
|
|
|
let community = blocking(context.pool(), move |conn| {
|
|
|
|
Community::read(conn, community_id)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
if post.locked {
|
|
|
|
return Err(anyhow!("Post is locked").into());
|
|
|
|
}
|
2021-09-25 15:44:52 +00:00
|
|
|
verify_domains_match(self.attributed_to.inner(), &self.id)?;
|
2021-07-31 14:57:37 +00:00
|
|
|
verify_person_in_community(
|
|
|
|
&self.attributed_to,
|
2021-10-18 21:36:44 +00:00
|
|
|
&ObjectId::new(community.actor_id),
|
2021-07-31 14:57:37 +00:00
|
|
|
context,
|
|
|
|
request_counter,
|
|
|
|
)
|
|
|
|
.await?;
|
2021-10-21 17:25:35 +00:00
|
|
|
verify_is_public(&self.to)?;
|
2021-07-31 14:57:37 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 21:36:44 +00:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct ApubComment(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 {
|
|
|
|
ApubComment { 0: c }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl ApubObject for ApubComment {
|
|
|
|
type DataType = LemmyContext;
|
|
|
|
|
|
|
|
fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn read_from_apub_id(
|
|
|
|
object_id: Url,
|
|
|
|
context: &LemmyContext,
|
|
|
|
) -> Result<Option<Self>, LemmyError> {
|
|
|
|
Ok(
|
|
|
|
blocking(context.pool(), move |conn| {
|
|
|
|
Comment::read_from_apub_id(conn, object_id)
|
|
|
|
})
|
|
|
|
.await??
|
|
|
|
.map(Into::into),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn delete(self, context: &LemmyContext) -> Result<(), LemmyError> {
|
|
|
|
blocking(context.pool(), move |conn| {
|
|
|
|
Comment::update_deleted(conn, self.id, true)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:10:09 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2021-10-18 21:36:44 +00:00
|
|
|
impl ToApub for ApubComment {
|
2021-07-31 14:57:37 +00:00
|
|
|
type ApubType = Note;
|
2021-10-18 21:36:44 +00:00
|
|
|
type TombstoneType = Tombstone;
|
|
|
|
type DataType = DbPool;
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2021-07-31 14:57:37 +00:00
|
|
|
async fn to_apub(&self, pool: &DbPool) -> Result<Note, LemmyError> {
|
2020-10-12 14:10:09 +00:00
|
|
|
let creator_id = self.creator_id;
|
2021-03-10 22:33:55 +00:00
|
|
|
let creator = blocking(pool, move |conn| Person::read(conn, creator_id)).await??;
|
2020-10-12 14:10:09 +00:00
|
|
|
|
|
|
|
let post_id = self.post_id;
|
|
|
|
let post = blocking(pool, move |conn| Post::read(conn, post_id)).await??;
|
|
|
|
|
2021-10-22 16:21:26 +00:00
|
|
|
let in_reply_to = if let Some(comment_id) = self.parent_id {
|
|
|
|
let parent_comment = blocking(pool, move |conn| Comment::read(conn, comment_id)).await??;
|
|
|
|
ObjectId::<PostOrComment>::new(parent_comment.ap_id.into_inner())
|
|
|
|
} else {
|
|
|
|
ObjectId::<PostOrComment>::new(post.ap_id.into_inner())
|
|
|
|
};
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2021-07-31 14:57:37 +00:00
|
|
|
let note = Note {
|
|
|
|
context: lemmy_context(),
|
|
|
|
r#type: NoteType::Note,
|
|
|
|
id: self.ap_id.to_owned().into_inner(),
|
2021-09-25 15:44:52 +00:00
|
|
|
attributed_to: ObjectId::new(creator.actor_id),
|
2021-10-21 17:25:35 +00:00
|
|
|
to: vec![public()],
|
2021-07-31 14:57:37 +00:00
|
|
|
content: self.content.clone(),
|
2021-10-21 17:25:35 +00:00
|
|
|
media_type: Some(MediaTypeHtml::Html),
|
|
|
|
source: SourceCompat::Lemmy(Source {
|
2021-07-31 14:57:37 +00:00
|
|
|
content: self.content.clone(),
|
|
|
|
media_type: MediaTypeMarkdown::Markdown,
|
2021-10-21 17:25:35 +00:00
|
|
|
}),
|
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),
|
|
|
|
unparsed: Default::default(),
|
|
|
|
};
|
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
|
|
|
}
|
|
|
|
|
|
|
|
fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
|
2021-01-27 16:42:23 +00:00
|
|
|
create_tombstone(
|
|
|
|
self.deleted,
|
|
|
|
self.ap_id.to_owned().into(),
|
|
|
|
self.updated,
|
|
|
|
NoteType::Note,
|
|
|
|
)
|
2020-10-12 14:10:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
2021-10-18 21:36:44 +00:00
|
|
|
impl FromApub for ApubComment {
|
2021-07-31 14:57:37 +00:00
|
|
|
type ApubType = Note;
|
2021-10-18 21:36:44 +00:00
|
|
|
type DataType = LemmyContext;
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2020-12-08 17:38:48 +00:00
|
|
|
/// Converts a `Note` to `Comment`.
|
2020-10-19 14:29:35 +00:00
|
|
|
///
|
|
|
|
/// If the parent community, post and comment(s) are not known locally, these are also fetched.
|
2020-10-12 14:10:09 +00:00
|
|
|
async fn from_apub(
|
2021-07-31 14:57:37 +00:00
|
|
|
note: &Note,
|
2020-10-12 14:10:09 +00:00
|
|
|
context: &LemmyContext,
|
2021-08-12 12:48:09 +00:00
|
|
|
expected_domain: &Url,
|
2020-10-22 18:27:32 +00:00
|
|
|
request_counter: &mut i32,
|
2021-10-18 21:36:44 +00:00
|
|
|
) -> Result<ApubComment, LemmyError> {
|
2021-08-12 12:48:09 +00:00
|
|
|
let ap_id = Some(note.id(expected_domain)?.clone().into());
|
2021-09-25 15:44:52 +00:00
|
|
|
let creator = note
|
|
|
|
.attributed_to
|
|
|
|
.dereference(context, request_counter)
|
|
|
|
.await?;
|
2021-07-31 14:57:37 +00:00
|
|
|
let (post, parent_comment_id) = note.get_parents(context, request_counter).await?;
|
2021-09-25 15:44:52 +00:00
|
|
|
if post.locked {
|
|
|
|
return Err(anyhow!("Post is locked").into());
|
|
|
|
}
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2021-10-21 17:25:35 +00:00
|
|
|
let content = if let SourceCompat::Lemmy(source) = ¬e.source {
|
|
|
|
source.content.clone()
|
|
|
|
} else {
|
|
|
|
parse_html(¬e.content)
|
|
|
|
};
|
|
|
|
let content_slurs_removed = remove_slurs(&content, &context.settings().slur_regex());
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2021-07-31 14:57:37 +00:00
|
|
|
let form = CommentForm {
|
2020-10-12 14:10:09 +00:00
|
|
|
creator_id: creator.id,
|
|
|
|
post_id: post.id,
|
2021-07-31 14:57:37 +00:00
|
|
|
parent_id: parent_comment_id,
|
2020-10-12 14:10:09 +00:00
|
|
|
content: content_slurs_removed,
|
|
|
|
removed: None,
|
|
|
|
read: None,
|
2021-10-21 17:25:35 +00:00
|
|
|
published: note.published.map(|u| u.to_owned().naive_local()),
|
2021-07-31 14:57:37 +00:00
|
|
|
updated: note.updated.map(|u| u.to_owned().naive_local()),
|
2020-10-12 14:10:09 +00:00
|
|
|
deleted: None,
|
2021-08-12 12:48:09 +00:00
|
|
|
ap_id,
|
2021-03-20 20:59:07 +00:00
|
|
|
local: Some(false),
|
2021-07-31 14:57:37 +00:00
|
|
|
};
|
2021-10-18 21:36:44 +00:00
|
|
|
let comment = blocking(context.pool(), move |conn| Comment::upsert(conn, &form)).await??;
|
|
|
|
Ok(comment.into())
|
2020-10-12 14:10:09 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-21 17:25:35 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use crate::objects::{
|
|
|
|
community::ApubCommunity,
|
|
|
|
tests::{file_to_json_object, init_context},
|
|
|
|
};
|
|
|
|
use assert_json_diff::assert_json_include;
|
|
|
|
use serial_test::serial;
|
|
|
|
|
|
|
|
async fn prepare_comment_test(
|
|
|
|
url: &Url,
|
|
|
|
context: &LemmyContext,
|
|
|
|
) -> (ApubPerson, ApubCommunity, ApubPost) {
|
|
|
|
let person_json = file_to_json_object("assets/lemmy-person.json");
|
|
|
|
let person = ApubPerson::from_apub(&person_json, context, url, &mut 0)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let community_json = file_to_json_object("assets/lemmy-community.json");
|
|
|
|
let community = ApubCommunity::from_apub(&community_json, context, url, &mut 0)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let post_json = file_to_json_object("assets/lemmy-post.json");
|
|
|
|
let post = ApubPost::from_apub(&post_json, context, url, &mut 0)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
(person, community, post)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cleanup(data: (ApubPerson, ApubCommunity, ApubPost), context: &LemmyContext) {
|
|
|
|
Post::delete(&*context.pool().get().unwrap(), data.2.id).unwrap();
|
|
|
|
Community::delete(&*context.pool().get().unwrap(), data.1.id).unwrap();
|
|
|
|
Person::delete(&*context.pool().get().unwrap(), data.0.id).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
#[serial]
|
2021-10-22 16:21:26 +00:00
|
|
|
async fn test_parse_lemmy_comment() {
|
2021-10-21 17:25:35 +00:00
|
|
|
let context = init_context();
|
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 json = file_to_json_object("assets/lemmy-comment.json");
|
|
|
|
let mut request_counter = 0;
|
|
|
|
let comment = ApubComment::from_apub(&json, &context, &url, &mut request_counter)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(comment.ap_id.clone().into_inner(), url);
|
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);
|
|
|
|
assert_eq!(request_counter, 0);
|
|
|
|
|
|
|
|
let to_apub = comment.to_apub(context.pool()).await.unwrap();
|
|
|
|
assert_json_include!(actual: json, expected: to_apub);
|
|
|
|
|
|
|
|
Comment::delete(&*context.pool().get().unwrap(), comment.id).unwrap();
|
|
|
|
cleanup(data, &context);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
#[serial]
|
2021-10-22 16:21:26 +00:00
|
|
|
async fn test_parse_pleroma_comment() {
|
2021-10-21 17:25:35 +00:00
|
|
|
let context = init_context();
|
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();
|
|
|
|
let person_json = file_to_json_object("assets/pleroma-person.json");
|
|
|
|
ApubPerson::from_apub(&person_json, &context, &pleroma_url, &mut 0)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let json = file_to_json_object("assets/pleroma-comment.json");
|
|
|
|
let mut request_counter = 0;
|
|
|
|
let comment = ApubComment::from_apub(&json, &context, &pleroma_url, &mut request_counter)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(comment.ap_id.clone().into_inner(), pleroma_url);
|
|
|
|
assert_eq!(comment.content.len(), 64);
|
|
|
|
assert!(!comment.local);
|
|
|
|
assert_eq!(request_counter, 0);
|
|
|
|
|
|
|
|
Comment::delete(&*context.pool().get().unwrap(), comment.id).unwrap();
|
|
|
|
cleanup(data, &context);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
#[serial]
|
|
|
|
async fn test_html_to_markdown_sanitize() {
|
|
|
|
let parsed = parse_html("<script></script><b>hello</b>");
|
|
|
|
assert_eq!(parsed, "**hello**");
|
|
|
|
}
|
|
|
|
}
|