2020-10-12 14:10:09 +00:00
|
|
|
use crate::{
|
2021-07-31 14:57:37 +00:00
|
|
|
activities::verify_person_in_community,
|
2020-11-25 17:44:49 +00:00
|
|
|
extensions::context::lemmy_context,
|
2021-09-25 15:44:52 +00:00
|
|
|
fetcher::object_id::ObjectId,
|
2021-08-05 11:00:29 +00:00
|
|
|
migrations::CommentInReplyToMigration,
|
2021-09-25 15:44:52 +00:00
|
|
|
objects::{create_tombstone, FromApub, Source, ToApub},
|
2021-07-31 14:57:37 +00:00
|
|
|
ActorType,
|
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,
|
|
|
|
object::{kind::NoteType, Tombstone},
|
|
|
|
primitives::OneOrMany,
|
|
|
|
unparsed::Unparsed,
|
2020-10-12 14:10:09 +00:00
|
|
|
};
|
2020-12-08 17:38:48 +00:00
|
|
|
use anyhow::{anyhow, Context};
|
2021-07-31 14:57:37 +00:00
|
|
|
use chrono::{DateTime, FixedOffset};
|
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::{
|
|
|
|
values::{MediaTypeHtml, MediaTypeMarkdown, PublicUrl},
|
|
|
|
verify_domains_match,
|
|
|
|
};
|
2021-09-25 15:44:52 +00:00
|
|
|
use lemmy_db_queries::{source::comment::Comment_, Crud, DbPool};
|
2021-03-18 20:25:21 +00:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
CommentId,
|
2020-10-12 14:10:09 +00:00
|
|
|
};
|
|
|
|
use lemmy_utils::{
|
|
|
|
location_info,
|
|
|
|
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-09-25 15:44:52 +00:00
|
|
|
pub(crate) attributed_to: ObjectId<Person>,
|
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`]).
|
|
|
|
to: PublicUrl,
|
|
|
|
content: String,
|
|
|
|
media_type: MediaTypeHtml,
|
|
|
|
source: Source,
|
2021-08-05 11:00:29 +00:00
|
|
|
in_reply_to: CommentInReplyToMigration,
|
2021-07-31 14:57:37 +00:00
|
|
|
published: DateTime<FixedOffset>,
|
|
|
|
updated: Option<DateTime<FixedOffset>>,
|
|
|
|
#[serde(flatten)]
|
|
|
|
unparsed: Unparsed,
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-31 14:57:37 +00:00
|
|
|
async fn get_parents(
|
|
|
|
&self,
|
|
|
|
context: &LemmyContext,
|
|
|
|
request_counter: &mut i32,
|
|
|
|
) -> Result<(Post, Option<CommentId>), LemmyError> {
|
2021-08-05 11:00:29 +00:00
|
|
|
match &self.in_reply_to {
|
|
|
|
CommentInReplyToMigration::Old(in_reply_to) => {
|
|
|
|
// This post, or the parent comment might not yet exist on this server yet, fetch them.
|
|
|
|
let post_id = in_reply_to.get(0).context(location_info!())?;
|
2021-09-25 15:44:52 +00:00
|
|
|
let post_id = ObjectId::new(post_id.clone());
|
|
|
|
let post = Box::pin(post_id.dereference(context, request_counter)).await?;
|
2021-07-31 14:57:37 +00:00
|
|
|
|
2021-08-05 11:00:29 +00:00
|
|
|
// The 2nd item, if it exists, is the parent comment apub_id
|
|
|
|
// Nested comments will automatically get fetched recursively
|
|
|
|
let parent_id: Option<CommentId> = match in_reply_to.get(1) {
|
2021-09-25 15:44:52 +00:00
|
|
|
Some(comment_id) => {
|
|
|
|
let comment_id = ObjectId::<Comment>::new(comment_id.clone());
|
|
|
|
let parent_comment = Box::pin(comment_id.dereference(context, request_counter)).await?;
|
2021-08-05 11:00:29 +00:00
|
|
|
|
|
|
|
Some(parent_comment.id)
|
|
|
|
}
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok((post, parent_id))
|
2021-07-31 14:57:37 +00:00
|
|
|
}
|
2021-08-05 11:00:29 +00:00
|
|
|
CommentInReplyToMigration::New(in_reply_to) => {
|
2021-09-25 15:44:52 +00:00
|
|
|
let parent = Box::pin(in_reply_to.dereference(context, request_counter).await?);
|
2021-08-05 11:00:29 +00:00
|
|
|
match parent.deref() {
|
|
|
|
PostOrComment::Post(p) => {
|
|
|
|
// Workaround because I cant figure ut 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, None))
|
|
|
|
}
|
|
|
|
PostOrComment::Comment(c) => {
|
|
|
|
let post_id = c.post_id;
|
|
|
|
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
|
|
|
|
Ok((post, Some(c.id)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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-09-25 15:44:52 +00:00
|
|
|
&ObjectId::new(community.actor_id()),
|
2021-07-31 14:57:37 +00:00
|
|
|
context,
|
|
|
|
request_counter,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:10:09 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl ToApub for Comment {
|
2021-07-31 14:57:37 +00:00
|
|
|
type ApubType = Note;
|
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??;
|
|
|
|
|
|
|
|
// Add a vector containing some important info to the "in_reply_to" field
|
|
|
|
// [post_ap_id, Option(parent_comment_ap_id)]
|
2021-01-27 16:42:23 +00:00
|
|
|
let mut in_reply_to_vec = vec![post.ap_id.into_inner()];
|
2020-10-12 14:10:09 +00:00
|
|
|
|
|
|
|
if let Some(parent_id) = self.parent_id {
|
|
|
|
let parent_comment = blocking(pool, move |conn| Comment::read(conn, parent_id)).await??;
|
|
|
|
|
2021-01-27 16:42:23 +00:00
|
|
|
in_reply_to_vec.push(parent_comment.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-07-31 14:57:37 +00:00
|
|
|
to: PublicUrl::Public,
|
|
|
|
content: self.content.clone(),
|
|
|
|
media_type: MediaTypeHtml::Html,
|
|
|
|
source: Source {
|
|
|
|
content: self.content.clone(),
|
|
|
|
media_type: MediaTypeMarkdown::Markdown,
|
|
|
|
},
|
2021-08-05 11:00:29 +00:00
|
|
|
in_reply_to: CommentInReplyToMigration::Old(in_reply_to_vec),
|
2021-07-31 14:57:37 +00:00
|
|
|
published: convert_datetime(self.published),
|
|
|
|
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)]
|
2020-12-08 17:38:48 +00:00
|
|
|
impl FromApub for Comment {
|
2021-07-31 14:57:37 +00:00
|
|
|
type ApubType = Note;
|
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-07-31 14:57:37 +00:00
|
|
|
) -> Result<Comment, 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-07-31 14:57:37 +00:00
|
|
|
let content = ¬e.source.content;
|
|
|
|
let content_slurs_removed = remove_slurs(content);
|
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-07-31 14:57:37 +00:00
|
|
|
published: Some(note.published.naive_local()),
|
|
|
|
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
|
|
|
};
|
|
|
|
Ok(blocking(context.pool(), move |conn| Comment::upsert(conn, &form)).await??)
|
2020-10-12 14:10:09 +00:00
|
|
|
}
|
|
|
|
}
|