2020-10-12 14:10:09 +00:00
|
|
|
use crate::{
|
2020-11-25 17:44:49 +00:00
|
|
|
extensions::context::lemmy_context,
|
2021-01-12 16:12:41 +00:00
|
|
|
fetcher::objects::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post},
|
2021-04-21 13:36:07 +00:00
|
|
|
get_community_from_to_or_cc,
|
2020-11-24 17:53:43 +00:00
|
|
|
objects::{
|
|
|
|
check_object_domain,
|
2020-12-08 17:38:48 +00:00
|
|
|
check_object_for_community_or_site_ban,
|
2020-11-24 17:53:43 +00:00
|
|
|
create_tombstone,
|
2020-12-08 17:38:48 +00:00
|
|
|
get_object_from_apub,
|
2021-03-10 22:33:55 +00:00
|
|
|
get_or_fetch_and_upsert_person,
|
2020-11-24 17:53:43 +00:00
|
|
|
get_source_markdown_value,
|
|
|
|
set_content_and_source,
|
2020-12-08 17:38:48 +00:00
|
|
|
FromApub,
|
|
|
|
FromApubToForm,
|
|
|
|
ToApub,
|
2020-11-24 17:53:43 +00:00
|
|
|
},
|
|
|
|
NoteExt,
|
2020-10-12 14:10:09 +00:00
|
|
|
};
|
|
|
|
use activitystreams::{
|
2020-11-24 17:53:43 +00:00
|
|
|
object::{kind::NoteType, ApObject, Note, Tombstone},
|
2020-10-12 14:10:09 +00:00
|
|
|
prelude::*,
|
2021-02-10 13:01:02 +00:00
|
|
|
public,
|
2020-10-12 14:10:09 +00:00
|
|
|
};
|
2020-12-08 17:38:48 +00:00
|
|
|
use anyhow::{anyhow, Context};
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_api_common::blocking;
|
2020-12-21 23:27:42 +00:00
|
|
|
use lemmy_db_queries::{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;
|
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl ToApub for Comment {
|
2020-11-24 17:53:43 +00:00
|
|
|
type ApubType = NoteExt;
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2020-11-24 17:53:43 +00:00
|
|
|
async fn to_apub(&self, pool: &DbPool) -> Result<NoteExt, LemmyError> {
|
|
|
|
let mut comment = ApObject::new(Note::new());
|
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-03-22 14:08:06 +00:00
|
|
|
let community_id = post.community_id;
|
|
|
|
let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??;
|
|
|
|
|
2020-10-12 14:10:09 +00:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
comment
|
|
|
|
// Not needed when the Post is embedded in a collection (like for community outbox)
|
2021-07-29 08:58:29 +00:00
|
|
|
.set_many_contexts(lemmy_context())
|
2021-01-27 16:42:23 +00:00
|
|
|
.set_id(self.ap_id.to_owned().into_inner())
|
2020-10-12 14:10:09 +00:00
|
|
|
.set_published(convert_datetime(self.published))
|
2021-03-22 14:08:06 +00:00
|
|
|
// NOTE: included community id for compatibility with lemmy v0.9.9
|
|
|
|
.set_many_tos(vec![community.actor_id.into_inner(), public()])
|
2020-10-12 14:10:09 +00:00
|
|
|
.set_many_in_reply_tos(in_reply_to_vec)
|
2021-01-27 16:42:23 +00:00
|
|
|
.set_attributed_to(creator.actor_id.into_inner());
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2020-11-24 17:53:43 +00:00
|
|
|
set_content_and_source(&mut comment, &self.content)?;
|
|
|
|
|
2020-10-12 14:10:09 +00:00
|
|
|
if let Some(u) = self.updated {
|
|
|
|
comment.set_updated(convert_datetime(u));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(comment)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2020-11-24 17:53:43 +00:00
|
|
|
type ApubType = NoteExt;
|
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(
|
2020-11-24 17:53:43 +00:00
|
|
|
note: &NoteExt,
|
2020-10-12 14:10:09 +00:00
|
|
|
context: &LemmyContext,
|
2020-12-08 17:38:48 +00:00
|
|
|
expected_domain: Url,
|
|
|
|
request_counter: &mut i32,
|
2021-03-18 13:24:29 +00:00
|
|
|
mod_action_allowed: bool,
|
2020-12-08 17:38:48 +00:00
|
|
|
) -> Result<Comment, LemmyError> {
|
2021-03-16 17:26:19 +00:00
|
|
|
let comment: Comment = get_object_from_apub(
|
|
|
|
note,
|
|
|
|
context,
|
|
|
|
expected_domain,
|
|
|
|
request_counter,
|
2021-03-18 13:24:29 +00:00
|
|
|
mod_action_allowed,
|
2021-03-16 17:26:19 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2020-12-08 17:38:48 +00:00
|
|
|
|
|
|
|
let post_id = comment.post_id;
|
|
|
|
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
|
2021-02-23 18:00:47 +00:00
|
|
|
check_object_for_community_or_site_ban(note, post.community_id, context, request_counter)
|
|
|
|
.await?;
|
Apub inbox rewrite (#1652)
* start to implement apub inbox routing lib
* got something that almost works
* it compiles!
* implemented some more
* move library code to separate crate (most of it)
* convert private message handlers
* convert all comment receivers (except undo comment)
* convert post receiver
* add verify trait
* convert community receivers
* add cc field for all activities which i forgot before
* convert inbox functions, add missing checks
* convert undo like/dislike receivers
* convert undo_delete and undo_remove receivers
* move block/unblock activities
* convert remaining activity receivers
* reimplement http signature verification and other checks
* also use actor type for routing, VerifyActivity and SendActivity traits
* cleanup and restructure apub_receive code
* wip: try to fix activity routing
* implement a (very bad) derive macro for activityhandler
* working activity routing!
* rework pm verify(), fix tests and confirm manually
also remove inbox username check which was broken
* rework following verify(), fix tests and test manually
* fix post/comment create/update, rework voting
* Rewrite remove/delete post/comment, fix tests, test manually
* Rework and fix (un)block user, announce, update post
* some code cleanup
* rework delete/remove activity receivers (still quite messy)
* rewrite, test and fix add/remove mod, update community handlers
* add docs for ActivityHandler derive macro
* dont try to compile macro comments
2021-07-17 16:08:46 +00:00
|
|
|
Ok(comment)
|
2020-12-08 17:38:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl FromApubToForm<NoteExt> for CommentForm {
|
|
|
|
async fn from_apub(
|
|
|
|
note: &NoteExt,
|
|
|
|
context: &LemmyContext,
|
|
|
|
expected_domain: Url,
|
2020-10-22 18:27:32 +00:00
|
|
|
request_counter: &mut i32,
|
2021-03-18 13:24:29 +00:00
|
|
|
_mod_action_allowed: bool,
|
2020-10-12 14:10:09 +00:00
|
|
|
) -> Result<CommentForm, LemmyError> {
|
2021-04-21 13:36:07 +00:00
|
|
|
let community = get_community_from_to_or_cc(note, context, request_counter).await?;
|
|
|
|
let ap_id = Some(check_object_domain(note, expected_domain, community.local)?);
|
2020-10-12 14:10:09 +00:00
|
|
|
let creator_actor_id = ¬e
|
|
|
|
.attributed_to()
|
|
|
|
.context(location_info!())?
|
|
|
|
.as_single_xsd_any_uri()
|
|
|
|
.context(location_info!())?;
|
|
|
|
|
2021-03-11 04:43:11 +00:00
|
|
|
let creator =
|
|
|
|
get_or_fetch_and_upsert_person(creator_actor_id, context, request_counter).await?;
|
2020-10-12 14:10:09 +00:00
|
|
|
|
|
|
|
let mut in_reply_tos = note
|
|
|
|
.in_reply_to()
|
|
|
|
.as_ref()
|
|
|
|
.context(location_info!())?
|
|
|
|
.as_many()
|
|
|
|
.context(location_info!())?
|
|
|
|
.iter()
|
|
|
|
.map(|i| i.as_xsd_any_uri().context(""));
|
|
|
|
let post_ap_id = in_reply_tos.next().context(location_info!())??;
|
|
|
|
|
|
|
|
// This post, or the parent comment might not yet exist on this server yet, fetch them.
|
2021-03-23 16:03:14 +00:00
|
|
|
let post = Box::pin(get_or_fetch_and_insert_post(
|
2021-07-05 16:07:26 +00:00
|
|
|
post_ap_id,
|
2021-03-23 16:03:14 +00:00
|
|
|
context,
|
|
|
|
request_counter,
|
|
|
|
))
|
|
|
|
.await?;
|
Apub inbox rewrite (#1652)
* start to implement apub inbox routing lib
* got something that almost works
* it compiles!
* implemented some more
* move library code to separate crate (most of it)
* convert private message handlers
* convert all comment receivers (except undo comment)
* convert post receiver
* add verify trait
* convert community receivers
* add cc field for all activities which i forgot before
* convert inbox functions, add missing checks
* convert undo like/dislike receivers
* convert undo_delete and undo_remove receivers
* move block/unblock activities
* convert remaining activity receivers
* reimplement http signature verification and other checks
* also use actor type for routing, VerifyActivity and SendActivity traits
* cleanup and restructure apub_receive code
* wip: try to fix activity routing
* implement a (very bad) derive macro for activityhandler
* working activity routing!
* rework pm verify(), fix tests and confirm manually
also remove inbox username check which was broken
* rework following verify(), fix tests and test manually
* fix post/comment create/update, rework voting
* Rewrite remove/delete post/comment, fix tests, test manually
* Rework and fix (un)block user, announce, update post
* some code cleanup
* rework delete/remove activity receivers (still quite messy)
* rewrite, test and fix add/remove mod, update community handlers
* add docs for ActivityHandler derive macro
* dont try to compile macro comments
2021-07-17 16:08:46 +00:00
|
|
|
if post.locked {
|
|
|
|
return Err(anyhow!("Post is locked").into());
|
|
|
|
}
|
2020-10-12 14:10:09 +00:00
|
|
|
|
|
|
|
// The 2nd item, if it exists, is the parent comment apub_id
|
|
|
|
// For deeply nested comments, FromApub automatically gets called recursively
|
2021-03-18 20:25:21 +00:00
|
|
|
let parent_id: Option<CommentId> = match in_reply_tos.next() {
|
2020-10-12 14:10:09 +00:00
|
|
|
Some(parent_comment_uri) => {
|
|
|
|
let parent_comment_ap_id = &parent_comment_uri?;
|
2021-03-23 16:03:14 +00:00
|
|
|
let parent_comment = Box::pin(get_or_fetch_and_insert_comment(
|
2021-07-05 16:07:26 +00:00
|
|
|
parent_comment_ap_id,
|
2021-03-23 16:03:14 +00:00
|
|
|
context,
|
|
|
|
request_counter,
|
|
|
|
))
|
|
|
|
.await?;
|
2020-10-12 14:10:09 +00:00
|
|
|
|
|
|
|
Some(parent_comment.id)
|
|
|
|
}
|
|
|
|
None => None,
|
|
|
|
};
|
2020-11-24 17:53:43 +00:00
|
|
|
|
|
|
|
let content = get_source_markdown_value(note)?.context(location_info!())?;
|
2020-10-12 14:10:09 +00:00
|
|
|
let content_slurs_removed = remove_slurs(&content);
|
|
|
|
|
|
|
|
Ok(CommentForm {
|
|
|
|
creator_id: creator.id,
|
|
|
|
post_id: post.id,
|
|
|
|
parent_id,
|
|
|
|
content: content_slurs_removed,
|
|
|
|
removed: None,
|
|
|
|
read: None,
|
|
|
|
published: note.published().map(|u| u.to_owned().naive_local()),
|
|
|
|
updated: note.updated().map(|u| u.to_owned().naive_local()),
|
|
|
|
deleted: None,
|
2021-04-21 13:36:07 +00:00
|
|
|
ap_id,
|
2021-03-20 20:59:07 +00:00
|
|
|
local: Some(false),
|
2020-10-12 14:10:09 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|