mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-11-22 20:31:19 +00:00
Change to_apub and from_apub to take by value and avoid cloning
This commit is contained in:
parent
2edf8ba157
commit
c725514841
29 changed files with 150 additions and 152 deletions
|
@ -169,7 +169,7 @@ impl Perform for LockPost {
|
||||||
|
|
||||||
// apub updates
|
// apub updates
|
||||||
CreateOrUpdatePost::send(
|
CreateOrUpdatePost::send(
|
||||||
&updated_post,
|
updated_post,
|
||||||
&local_user_view.person.clone().into(),
|
&local_user_view.person.clone().into(),
|
||||||
CreateOrUpdateType::Update,
|
CreateOrUpdateType::Update,
|
||||||
context,
|
context,
|
||||||
|
@ -242,7 +242,7 @@ impl Perform for StickyPost {
|
||||||
// Apub updates
|
// Apub updates
|
||||||
// TODO stickied should pry work like locked for ease of use
|
// TODO stickied should pry work like locked for ease of use
|
||||||
CreateOrUpdatePost::send(
|
CreateOrUpdatePost::send(
|
||||||
&updated_post,
|
updated_post,
|
||||||
&local_user_view.person.clone().into(),
|
&local_user_view.person.clone().into(),
|
||||||
CreateOrUpdateType::Update,
|
CreateOrUpdateType::Update,
|
||||||
context,
|
context,
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
|
use crate::PerformCrud;
|
||||||
use actix_web::web::Data;
|
use actix_web::web::Data;
|
||||||
|
|
||||||
use lemmy_api_common::{
|
use lemmy_api_common::{
|
||||||
blocking,
|
blocking,
|
||||||
check_community_ban,
|
check_community_ban,
|
||||||
|
@ -13,6 +13,7 @@ use lemmy_api_common::{
|
||||||
use lemmy_apub::{
|
use lemmy_apub::{
|
||||||
fetcher::post_or_comment::PostOrComment,
|
fetcher::post_or_comment::PostOrComment,
|
||||||
generate_local_apub_endpoint,
|
generate_local_apub_endpoint,
|
||||||
|
objects::comment::ApubComment,
|
||||||
protocol::activities::{
|
protocol::activities::{
|
||||||
create_or_update::comment::CreateOrUpdateComment,
|
create_or_update::comment::CreateOrUpdateComment,
|
||||||
voting::vote::{Vote, VoteType},
|
voting::vote::{Vote, VoteType},
|
||||||
|
@ -40,8 +41,6 @@ use lemmy_websocket::{
|
||||||
UserOperationCrud,
|
UserOperationCrud,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::PerformCrud;
|
|
||||||
|
|
||||||
#[async_trait::async_trait(?Send)]
|
#[async_trait::async_trait(?Send)]
|
||||||
impl PerformCrud for CreateComment {
|
impl PerformCrud for CreateComment {
|
||||||
type Response = CommentResponse;
|
type Response = CommentResponse;
|
||||||
|
@ -121,14 +120,6 @@ impl PerformCrud for CreateComment {
|
||||||
.await?
|
.await?
|
||||||
.map_err(|e| ApiError::err("couldnt_create_comment", e))?;
|
.map_err(|e| ApiError::err("couldnt_create_comment", e))?;
|
||||||
|
|
||||||
CreateOrUpdateComment::send(
|
|
||||||
&updated_comment.clone().into(),
|
|
||||||
&local_user_view.person.clone().into(),
|
|
||||||
CreateOrUpdateType::Create,
|
|
||||||
context,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
// Scan the comment for user mentions, add those rows
|
// Scan the comment for user mentions, add those rows
|
||||||
let post_id = post.id;
|
let post_id = post.id;
|
||||||
let mentions = scrape_text_for_mentions(&comment_form.content);
|
let mentions = scrape_text_for_mentions(&comment_form.content);
|
||||||
|
@ -155,7 +146,15 @@ impl PerformCrud for CreateComment {
|
||||||
.await?
|
.await?
|
||||||
.map_err(|e| ApiError::err("couldnt_like_comment", e))?;
|
.map_err(|e| ApiError::err("couldnt_like_comment", e))?;
|
||||||
|
|
||||||
let object = PostOrComment::Comment(updated_comment.into());
|
let apub_comment: ApubComment = updated_comment.into();
|
||||||
|
CreateOrUpdateComment::send(
|
||||||
|
apub_comment.clone(),
|
||||||
|
&local_user_view.person.clone().into(),
|
||||||
|
CreateOrUpdateType::Create,
|
||||||
|
context,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
let object = PostOrComment::Comment(apub_comment);
|
||||||
Vote::send(
|
Vote::send(
|
||||||
&object,
|
&object,
|
||||||
&local_user_view.person.clone().into(),
|
&local_user_view.person.clone().into(),
|
||||||
|
|
|
@ -72,15 +72,6 @@ impl PerformCrud for EditComment {
|
||||||
.await?
|
.await?
|
||||||
.map_err(|e| ApiError::err("couldnt_update_comment", e))?;
|
.map_err(|e| ApiError::err("couldnt_update_comment", e))?;
|
||||||
|
|
||||||
// Send the apub update
|
|
||||||
CreateOrUpdateComment::send(
|
|
||||||
&updated_comment.clone().into(),
|
|
||||||
&local_user_view.person.clone().into(),
|
|
||||||
CreateOrUpdateType::Update,
|
|
||||||
context,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
// Do the mentions / recipients
|
// Do the mentions / recipients
|
||||||
let updated_comment_content = updated_comment.content.to_owned();
|
let updated_comment_content = updated_comment.content.to_owned();
|
||||||
let mentions = scrape_text_for_mentions(&updated_comment_content);
|
let mentions = scrape_text_for_mentions(&updated_comment_content);
|
||||||
|
@ -94,6 +85,15 @@ impl PerformCrud for EditComment {
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
// Send the apub update
|
||||||
|
CreateOrUpdateComment::send(
|
||||||
|
updated_comment.into(),
|
||||||
|
&local_user_view.person.into(),
|
||||||
|
CreateOrUpdateType::Update,
|
||||||
|
context,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
send_comment_ws_message(
|
send_comment_ws_message(
|
||||||
data.comment_id,
|
data.comment_id,
|
||||||
UserOperationCrud::EditComment,
|
UserOperationCrud::EditComment,
|
||||||
|
|
|
@ -72,7 +72,7 @@ impl PerformCrud for EditCommunity {
|
||||||
.map_err(|e| ApiError::err("couldnt_update_community", e))?;
|
.map_err(|e| ApiError::err("couldnt_update_community", e))?;
|
||||||
|
|
||||||
UpdateCommunity::send(
|
UpdateCommunity::send(
|
||||||
&updated_community.into(),
|
updated_community.into(),
|
||||||
&local_user_view.person.into(),
|
&local_user_view.person.into(),
|
||||||
context,
|
context,
|
||||||
)
|
)
|
||||||
|
|
|
@ -12,6 +12,7 @@ use lemmy_api_common::{
|
||||||
use lemmy_apub::{
|
use lemmy_apub::{
|
||||||
fetcher::post_or_comment::PostOrComment,
|
fetcher::post_or_comment::PostOrComment,
|
||||||
generate_local_apub_endpoint,
|
generate_local_apub_endpoint,
|
||||||
|
objects::post::ApubPost,
|
||||||
protocol::activities::{
|
protocol::activities::{
|
||||||
create_or_update::post::CreateOrUpdatePost,
|
create_or_update::post::CreateOrUpdatePost,
|
||||||
voting::vote::{Vote, VoteType},
|
voting::vote::{Vote, VoteType},
|
||||||
|
@ -109,14 +110,6 @@ impl PerformCrud for CreatePost {
|
||||||
.await?
|
.await?
|
||||||
.map_err(|e| ApiError::err("couldnt_create_post", e))?;
|
.map_err(|e| ApiError::err("couldnt_create_post", e))?;
|
||||||
|
|
||||||
CreateOrUpdatePost::send(
|
|
||||||
&updated_post.clone().into(),
|
|
||||||
&local_user_view.person.clone().into(),
|
|
||||||
CreateOrUpdateType::Create,
|
|
||||||
context,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
// They like their own post by default
|
// They like their own post by default
|
||||||
let person_id = local_user_view.person.id;
|
let person_id = local_user_view.person.id;
|
||||||
let post_id = inserted_post.id;
|
let post_id = inserted_post.id;
|
||||||
|
@ -145,7 +138,15 @@ impl PerformCrud for CreatePost {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let object = PostOrComment::Post(Box::new(updated_post.into()));
|
let apub_post: ApubPost = updated_post.into();
|
||||||
|
CreateOrUpdatePost::send(
|
||||||
|
apub_post.clone(),
|
||||||
|
&local_user_view.person.clone().into(),
|
||||||
|
CreateOrUpdateType::Create,
|
||||||
|
context,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
let object = PostOrComment::Post(Box::new(apub_post));
|
||||||
Vote::send(
|
Vote::send(
|
||||||
&object,
|
&object,
|
||||||
&local_user_view.person.clone().into(),
|
&local_user_view.person.clone().into(),
|
||||||
|
|
|
@ -109,7 +109,7 @@ impl PerformCrud for EditPost {
|
||||||
|
|
||||||
// Send apub update
|
// Send apub update
|
||||||
CreateOrUpdatePost::send(
|
CreateOrUpdatePost::send(
|
||||||
&updated_post.into(),
|
updated_post.into(),
|
||||||
&local_user_view.person.clone().into(),
|
&local_user_view.person.clone().into(),
|
||||||
CreateOrUpdateType::Update,
|
CreateOrUpdateType::Update,
|
||||||
context,
|
context,
|
||||||
|
|
|
@ -83,7 +83,7 @@ impl PerformCrud for CreatePrivateMessage {
|
||||||
.map_err(|e| ApiError::err("couldnt_create_private_message", e))?;
|
.map_err(|e| ApiError::err("couldnt_create_private_message", e))?;
|
||||||
|
|
||||||
CreateOrUpdatePrivateMessage::send(
|
CreateOrUpdatePrivateMessage::send(
|
||||||
&updated_private_message.into(),
|
updated_private_message.into(),
|
||||||
&local_user_view.person.into(),
|
&local_user_view.person.into(),
|
||||||
CreateOrUpdateType::Create,
|
CreateOrUpdateType::Create,
|
||||||
context,
|
context,
|
||||||
|
|
|
@ -47,7 +47,7 @@ impl PerformCrud for EditPrivateMessage {
|
||||||
|
|
||||||
// Send the apub update
|
// Send the apub update
|
||||||
CreateOrUpdatePrivateMessage::send(
|
CreateOrUpdatePrivateMessage::send(
|
||||||
&updated_private_message.into(),
|
updated_private_message.into(),
|
||||||
&local_user_view.person.into(),
|
&local_user_view.person.into(),
|
||||||
CreateOrUpdateType::Update,
|
CreateOrUpdateType::Update,
|
||||||
context,
|
context,
|
||||||
|
|
|
@ -29,7 +29,7 @@ use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperation
|
||||||
|
|
||||||
impl CreateOrUpdateComment {
|
impl CreateOrUpdateComment {
|
||||||
pub async fn send(
|
pub async fn send(
|
||||||
comment: &ApubComment,
|
comment: ApubComment,
|
||||||
actor: &ApubPerson,
|
actor: &ApubPerson,
|
||||||
kind: CreateOrUpdateType,
|
kind: CreateOrUpdateType,
|
||||||
context: &LemmyContext,
|
context: &LemmyContext,
|
||||||
|
@ -48,12 +48,12 @@ impl CreateOrUpdateComment {
|
||||||
kind.clone(),
|
kind.clone(),
|
||||||
&context.settings().get_protocol_and_hostname(),
|
&context.settings().get_protocol_and_hostname(),
|
||||||
)?;
|
)?;
|
||||||
let maa = collect_non_local_mentions(comment, &community, context).await?;
|
let maa = collect_non_local_mentions(&comment, &community, context).await?;
|
||||||
|
|
||||||
let create_or_update = CreateOrUpdateComment {
|
let create_or_update = CreateOrUpdateComment {
|
||||||
actor: ObjectId::new(actor.actor_id()),
|
actor: ObjectId::new(actor.actor_id()),
|
||||||
to: vec![public()],
|
to: vec![public()],
|
||||||
object: comment.to_apub(context).await?,
|
object: comment.into_apub(context).await?,
|
||||||
cc: maa.ccs,
|
cc: maa.ccs,
|
||||||
tag: maa.tags,
|
tag: maa.tags,
|
||||||
kind,
|
kind,
|
||||||
|
@ -97,7 +97,7 @@ impl ActivityHandler for CreateOrUpdateComment {
|
||||||
request_counter: &mut i32,
|
request_counter: &mut i32,
|
||||||
) -> Result<(), LemmyError> {
|
) -> Result<(), LemmyError> {
|
||||||
let comment =
|
let comment =
|
||||||
ApubComment::from_apub(&self.object, context, self.actor.inner(), request_counter).await?;
|
ApubComment::from_apub(self.object, context, self.actor.inner(), request_counter).await?;
|
||||||
let recipients = get_notif_recipients(&self.actor, &comment, context, request_counter).await?;
|
let recipients = get_notif_recipients(&self.actor, &comment, context, request_counter).await?;
|
||||||
let notif_type = match self.kind {
|
let notif_type = match self.kind {
|
||||||
CreateOrUpdateType::Create => UserOperationCrud::CreateComment,
|
CreateOrUpdateType::Create => UserOperationCrud::CreateComment,
|
||||||
|
|
|
@ -27,7 +27,7 @@ use lemmy_websocket::{send::send_community_ws_message, LemmyContext, UserOperati
|
||||||
|
|
||||||
impl UpdateCommunity {
|
impl UpdateCommunity {
|
||||||
pub async fn send(
|
pub async fn send(
|
||||||
community: &ApubCommunity,
|
community: ApubCommunity,
|
||||||
actor: &ApubPerson,
|
actor: &ApubPerson,
|
||||||
context: &LemmyContext,
|
context: &LemmyContext,
|
||||||
) -> Result<(), LemmyError> {
|
) -> Result<(), LemmyError> {
|
||||||
|
@ -38,7 +38,7 @@ impl UpdateCommunity {
|
||||||
let update = UpdateCommunity {
|
let update = UpdateCommunity {
|
||||||
actor: ObjectId::new(actor.actor_id()),
|
actor: ObjectId::new(actor.actor_id()),
|
||||||
to: vec![public()],
|
to: vec![public()],
|
||||||
object: community.to_apub(context).await?,
|
object: community.clone().into_apub(context).await?,
|
||||||
cc: vec![community.actor_id()],
|
cc: vec![community.actor_id()],
|
||||||
kind: UpdateType::Update,
|
kind: UpdateType::Update,
|
||||||
id: id.clone(),
|
id: id.clone(),
|
||||||
|
@ -46,7 +46,7 @@ impl UpdateCommunity {
|
||||||
};
|
};
|
||||||
|
|
||||||
let activity = AnnouncableActivities::UpdateCommunity(Box::new(update));
|
let activity = AnnouncableActivities::UpdateCommunity(Box::new(update));
|
||||||
send_to_community(activity, &id, actor, community, vec![], context).await
|
send_to_community(activity, &id, actor, &community, vec![], context).await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,8 +73,8 @@ impl ActivityHandler for UpdateCommunity {
|
||||||
) -> Result<(), LemmyError> {
|
) -> Result<(), LemmyError> {
|
||||||
let community = self.get_community(context, request_counter).await?;
|
let community = self.get_community(context, request_counter).await?;
|
||||||
|
|
||||||
let updated_community = Group::from_apub_to_form(
|
let updated_community = Group::into_form(
|
||||||
&self.object,
|
self.object,
|
||||||
&community.actor_id.clone().into(),
|
&community.actor_id.clone().into(),
|
||||||
&context.settings(),
|
&context.settings(),
|
||||||
)
|
)
|
||||||
|
|
|
@ -27,7 +27,7 @@ use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCru
|
||||||
|
|
||||||
impl CreateOrUpdatePost {
|
impl CreateOrUpdatePost {
|
||||||
pub(crate) async fn new(
|
pub(crate) async fn new(
|
||||||
post: &ApubPost,
|
post: ApubPost,
|
||||||
actor: &ApubPerson,
|
actor: &ApubPerson,
|
||||||
community: &ApubCommunity,
|
community: &ApubCommunity,
|
||||||
kind: CreateOrUpdateType,
|
kind: CreateOrUpdateType,
|
||||||
|
@ -40,7 +40,7 @@ impl CreateOrUpdatePost {
|
||||||
Ok(CreateOrUpdatePost {
|
Ok(CreateOrUpdatePost {
|
||||||
actor: ObjectId::new(actor.actor_id()),
|
actor: ObjectId::new(actor.actor_id()),
|
||||||
to: vec![public()],
|
to: vec![public()],
|
||||||
object: post.to_apub(context).await?,
|
object: post.into_apub(context).await?,
|
||||||
cc: vec![community.actor_id()],
|
cc: vec![community.actor_id()],
|
||||||
kind,
|
kind,
|
||||||
id: id.clone(),
|
id: id.clone(),
|
||||||
|
@ -48,7 +48,7 @@ impl CreateOrUpdatePost {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
pub async fn send(
|
pub async fn send(
|
||||||
post: &ApubPost,
|
post: ApubPost,
|
||||||
actor: &ApubPerson,
|
actor: &ApubPerson,
|
||||||
kind: CreateOrUpdateType,
|
kind: CreateOrUpdateType,
|
||||||
context: &LemmyContext,
|
context: &LemmyContext,
|
||||||
|
@ -115,7 +115,7 @@ impl ActivityHandler for CreateOrUpdatePost {
|
||||||
) -> Result<(), LemmyError> {
|
) -> Result<(), LemmyError> {
|
||||||
let actor = self.actor.dereference(context, request_counter).await?;
|
let actor = self.actor.dereference(context, request_counter).await?;
|
||||||
let post =
|
let post =
|
||||||
ApubPost::from_apub(&self.object, context, &actor.actor_id(), request_counter).await?;
|
ApubPost::from_apub(self.object, context, &actor.actor_id(), request_counter).await?;
|
||||||
|
|
||||||
let notif_type = match self.kind {
|
let notif_type = match self.kind {
|
||||||
CreateOrUpdateType::Create => UserOperationCrud::CreatePost,
|
CreateOrUpdateType::Create => UserOperationCrud::CreatePost,
|
||||||
|
|
|
@ -19,7 +19,7 @@ use lemmy_websocket::{send::send_pm_ws_message, LemmyContext, UserOperationCrud}
|
||||||
|
|
||||||
impl CreateOrUpdatePrivateMessage {
|
impl CreateOrUpdatePrivateMessage {
|
||||||
pub async fn send(
|
pub async fn send(
|
||||||
private_message: &ApubPrivateMessage,
|
private_message: ApubPrivateMessage,
|
||||||
actor: &ApubPerson,
|
actor: &ApubPerson,
|
||||||
kind: CreateOrUpdateType,
|
kind: CreateOrUpdateType,
|
||||||
context: &LemmyContext,
|
context: &LemmyContext,
|
||||||
|
@ -38,7 +38,7 @@ impl CreateOrUpdatePrivateMessage {
|
||||||
id: id.clone(),
|
id: id.clone(),
|
||||||
actor: ObjectId::new(actor.actor_id()),
|
actor: ObjectId::new(actor.actor_id()),
|
||||||
to: [ObjectId::new(recipient.actor_id())],
|
to: [ObjectId::new(recipient.actor_id())],
|
||||||
object: private_message.to_apub(context).await?,
|
object: private_message.into_apub(context).await?,
|
||||||
kind,
|
kind,
|
||||||
unparsed: Default::default(),
|
unparsed: Default::default(),
|
||||||
};
|
};
|
||||||
|
@ -67,7 +67,7 @@ impl ActivityHandler for CreateOrUpdatePrivateMessage {
|
||||||
request_counter: &mut i32,
|
request_counter: &mut i32,
|
||||||
) -> Result<(), LemmyError> {
|
) -> Result<(), LemmyError> {
|
||||||
let private_message =
|
let private_message =
|
||||||
ApubPrivateMessage::from_apub(&self.object, context, self.actor.inner(), request_counter)
|
ApubPrivateMessage::from_apub(self.object, context, self.actor.inner(), request_counter)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let notif_type = match self.kind {
|
let notif_type = match self.kind {
|
||||||
|
|
|
@ -49,11 +49,11 @@ impl ApubObject for ApubCommunityModerators {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn to_apub(&self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
|
async fn into_apub(self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
|
||||||
let ordered_items = self
|
let ordered_items = self
|
||||||
.0
|
.0
|
||||||
.iter()
|
.into_iter()
|
||||||
.map(|m| ObjectId::<ApubPerson>::new(m.moderator.actor_id.clone()))
|
.map(|m| ObjectId::<ApubPerson>::new(m.moderator.actor_id))
|
||||||
.collect();
|
.collect();
|
||||||
Ok(GroupModerators {
|
Ok(GroupModerators {
|
||||||
r#type: OrderedCollectionType::OrderedCollection,
|
r#type: OrderedCollectionType::OrderedCollection,
|
||||||
|
@ -67,7 +67,7 @@ impl ApubObject for ApubCommunityModerators {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn from_apub(
|
async fn from_apub(
|
||||||
apub: &Self::ApubType,
|
apub: Self::ApubType,
|
||||||
data: &Self::DataType,
|
data: &Self::DataType,
|
||||||
expected_domain: &Url,
|
expected_domain: &Url,
|
||||||
request_counter: &mut i32,
|
request_counter: &mut i32,
|
||||||
|
@ -94,12 +94,11 @@ impl ApubObject for ApubCommunityModerators {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add new mods to database which have been added to moderators collection
|
// Add new mods to database which have been added to moderators collection
|
||||||
for mod_id in &apub.ordered_items {
|
for mod_id in apub.ordered_items {
|
||||||
let mod_id = ObjectId::new(mod_id.clone());
|
let mod_id = ObjectId::new(mod_id);
|
||||||
let mod_user: ApubPerson = mod_id.dereference(&data.1, request_counter).await?;
|
let mod_user: ApubPerson = mod_id.dereference(&data.1, request_counter).await?;
|
||||||
|
|
||||||
if !current_moderators
|
if !current_moderators
|
||||||
.clone()
|
|
||||||
.iter()
|
.iter()
|
||||||
.map(|c| c.moderator.actor_id.clone())
|
.map(|c| c.moderator.actor_id.clone())
|
||||||
.any(|x| x == mod_user.actor_id)
|
.any(|x| x == mod_user.actor_id)
|
||||||
|
@ -166,7 +165,7 @@ mod tests {
|
||||||
0: community,
|
0: community,
|
||||||
1: context,
|
1: context,
|
||||||
};
|
};
|
||||||
ApubCommunityModerators::from_apub(&json, &community_context, &url, &mut request_counter)
|
ApubCommunityModerators::from_apub(json, &community_context, &url, &mut request_counter)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(request_counter, 0);
|
assert_eq!(request_counter, 0);
|
||||||
|
|
|
@ -60,9 +60,9 @@ impl ApubObject for ApubCommunityOutbox {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn to_apub(&self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
|
async fn into_apub(self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
|
||||||
let mut ordered_items = vec![];
|
let mut ordered_items = vec![];
|
||||||
for post in &self.0 {
|
for post in self.0 {
|
||||||
let actor = post.creator_id;
|
let actor = post.creator_id;
|
||||||
let actor: ApubPerson = blocking(data.1.pool(), move |conn| Person::read(conn, actor))
|
let actor: ApubPerson = blocking(data.1.pool(), move |conn| Person::read(conn, actor))
|
||||||
.await??
|
.await??
|
||||||
|
@ -86,13 +86,13 @@ impl ApubObject for ApubCommunityOutbox {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn from_apub(
|
async fn from_apub(
|
||||||
apub: &Self::ApubType,
|
apub: Self::ApubType,
|
||||||
data: &Self::DataType,
|
data: &Self::DataType,
|
||||||
expected_domain: &Url,
|
expected_domain: &Url,
|
||||||
request_counter: &mut i32,
|
request_counter: &mut i32,
|
||||||
) -> Result<Self, LemmyError> {
|
) -> Result<Self, LemmyError> {
|
||||||
verify_domains_match(expected_domain, &apub.id)?;
|
verify_domains_match(expected_domain, &apub.id)?;
|
||||||
let mut outbox_activities = apub.ordered_items.clone();
|
let mut outbox_activities = apub.ordered_items;
|
||||||
if outbox_activities.len() > 20 {
|
if outbox_activities.len() > 20 {
|
||||||
outbox_activities = outbox_activities[0..20].to_vec();
|
outbox_activities = outbox_activities[0..20].to_vec();
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,7 +53,7 @@ impl ApubObject for PostOrComment {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn to_apub(&self, _data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
|
async fn into_apub(self, _data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,17 +62,17 @@ impl ApubObject for PostOrComment {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn from_apub(
|
async fn from_apub(
|
||||||
apub: &PageOrNote,
|
apub: PageOrNote,
|
||||||
context: &LemmyContext,
|
context: &LemmyContext,
|
||||||
expected_domain: &Url,
|
expected_domain: &Url,
|
||||||
request_counter: &mut i32,
|
request_counter: &mut i32,
|
||||||
) -> Result<Self, LemmyError> {
|
) -> Result<Self, LemmyError> {
|
||||||
Ok(match apub {
|
Ok(match apub {
|
||||||
PageOrNote::Page(p) => PostOrComment::Post(Box::new(
|
PageOrNote::Page(p) => PostOrComment::Post(Box::new(
|
||||||
ApubPost::from_apub(p, context, expected_domain, request_counter).await?,
|
ApubPost::from_apub(*p, context, expected_domain, request_counter).await?,
|
||||||
)),
|
)),
|
||||||
PageOrNote::Note(n) => PostOrComment::Comment(
|
PageOrNote::Note(n) => PostOrComment::Comment(
|
||||||
ApubComment::from_apub(n, context, expected_domain, request_counter).await?,
|
ApubComment::from_apub(*n, context, expected_domain, request_counter).await?,
|
||||||
),
|
),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -155,7 +155,7 @@ impl ApubObject for SearchableObjects {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn to_apub(&self, _data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
|
async fn into_apub(self, _data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -164,7 +164,7 @@ impl ApubObject for SearchableObjects {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn from_apub(
|
async fn from_apub(
|
||||||
apub: &Self::ApubType,
|
apub: Self::ApubType,
|
||||||
context: &LemmyContext,
|
context: &LemmyContext,
|
||||||
ed: &Url,
|
ed: &Url,
|
||||||
rc: &mut i32,
|
rc: &mut i32,
|
||||||
|
|
|
@ -54,7 +54,7 @@ impl ApubObject for UserOrCommunity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn to_apub(&self, _data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
|
async fn into_apub(self, _data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,7 +63,7 @@ impl ApubObject for UserOrCommunity {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn from_apub(
|
async fn from_apub(
|
||||||
apub: &Self::ApubType,
|
apub: Self::ApubType,
|
||||||
data: &Self::DataType,
|
data: &Self::DataType,
|
||||||
expected_domain: &Url,
|
expected_domain: &Url,
|
||||||
request_counter: &mut i32,
|
request_counter: &mut i32,
|
||||||
|
|
|
@ -30,7 +30,7 @@ pub(crate) async fn get_apub_comment(
|
||||||
}
|
}
|
||||||
|
|
||||||
if !comment.deleted {
|
if !comment.deleted {
|
||||||
Ok(create_apub_response(&comment.to_apub(&**context).await?))
|
Ok(create_apub_response(&comment.into_apub(&**context).await?))
|
||||||
} else {
|
} else {
|
||||||
Ok(create_apub_tombstone_response(&comment.to_tombstone()?))
|
Ok(create_apub_tombstone_response(&comment.to_tombstone()?))
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,7 +47,7 @@ pub(crate) async fn get_apub_community_http(
|
||||||
.into();
|
.into();
|
||||||
|
|
||||||
if !community.deleted {
|
if !community.deleted {
|
||||||
let apub = community.to_apub(&**context).await?;
|
let apub = community.into_apub(&**context).await?;
|
||||||
|
|
||||||
Ok(create_apub_response(&apub))
|
Ok(create_apub_response(&apub))
|
||||||
} else {
|
} else {
|
||||||
|
@ -118,7 +118,7 @@ pub(crate) async fn get_apub_community_outbox(
|
||||||
let id = ObjectId::new(generate_outbox_url(&community.actor_id)?);
|
let id = ObjectId::new(generate_outbox_url(&community.actor_id)?);
|
||||||
let outbox_data = CommunityContext(community.into(), context.get_ref().clone());
|
let outbox_data = CommunityContext(community.into(), context.get_ref().clone());
|
||||||
let outbox: ApubCommunityOutbox = id.dereference(&outbox_data, &mut 0).await?;
|
let outbox: ApubCommunityOutbox = id.dereference(&outbox_data, &mut 0).await?;
|
||||||
Ok(create_apub_response(&outbox.to_apub(&outbox_data).await?))
|
Ok(create_apub_response(&outbox.into_apub(&outbox_data).await?))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn get_apub_community_moderators(
|
pub(crate) async fn get_apub_community_moderators(
|
||||||
|
@ -134,6 +134,6 @@ pub(crate) async fn get_apub_community_moderators(
|
||||||
let outbox_data = CommunityContext(community, context.get_ref().clone());
|
let outbox_data = CommunityContext(community, context.get_ref().clone());
|
||||||
let moderators: ApubCommunityModerators = id.dereference(&outbox_data, &mut 0).await?;
|
let moderators: ApubCommunityModerators = id.dereference(&outbox_data, &mut 0).await?;
|
||||||
Ok(create_apub_response(
|
Ok(create_apub_response(
|
||||||
&moderators.to_apub(&outbox_data).await?,
|
&moderators.into_apub(&outbox_data).await?,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,7 +39,7 @@ pub(crate) async fn get_apub_person_http(
|
||||||
.into();
|
.into();
|
||||||
|
|
||||||
if !person.deleted {
|
if !person.deleted {
|
||||||
let apub = person.to_apub(&context).await?;
|
let apub = person.into_apub(&context).await?;
|
||||||
|
|
||||||
Ok(create_apub_response(&apub))
|
Ok(create_apub_response(&apub))
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -30,7 +30,7 @@ pub(crate) async fn get_apub_post(
|
||||||
}
|
}
|
||||||
|
|
||||||
if !post.deleted {
|
if !post.deleted {
|
||||||
Ok(create_apub_response(&post.to_apub(&context).await?))
|
Ok(create_apub_response(&post.into_apub(&context).await?))
|
||||||
} else {
|
} else {
|
||||||
Ok(create_apub_tombstone_response(&post.to_tombstone()?))
|
Ok(create_apub_tombstone_response(&post.to_tombstone()?))
|
||||||
}
|
}
|
||||||
|
|
|
@ -87,7 +87,7 @@ impl ApubObject for ApubComment {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn to_apub(&self, context: &LemmyContext) -> Result<Note, LemmyError> {
|
async fn into_apub(self, context: &LemmyContext) -> Result<Note, LemmyError> {
|
||||||
let creator_id = self.creator_id;
|
let creator_id = self.creator_id;
|
||||||
let creator = blocking(context.pool(), move |conn| Person::read(conn, creator_id)).await??;
|
let creator = blocking(context.pool(), move |conn| Person::read(conn, creator_id)).await??;
|
||||||
|
|
||||||
|
@ -104,7 +104,7 @@ impl ApubObject for ApubComment {
|
||||||
|
|
||||||
let note = Note {
|
let note = Note {
|
||||||
r#type: NoteType::Note,
|
r#type: NoteType::Note,
|
||||||
id: ObjectId::new(self.ap_id.to_owned()),
|
id: ObjectId::new(self.ap_id.clone()),
|
||||||
attributed_to: ObjectId::new(creator.actor_id),
|
attributed_to: ObjectId::new(creator.actor_id),
|
||||||
to: vec![public()],
|
to: vec![public()],
|
||||||
content: markdown_to_html(&self.content),
|
content: markdown_to_html(&self.content),
|
||||||
|
@ -133,13 +133,12 @@ impl ApubObject for ApubComment {
|
||||||
///
|
///
|
||||||
/// If the parent community, post and comment(s) are not known locally, these are also fetched.
|
/// If the parent community, post and comment(s) are not known locally, these are also fetched.
|
||||||
async fn from_apub(
|
async fn from_apub(
|
||||||
note: &Note,
|
note: Note,
|
||||||
context: &LemmyContext,
|
context: &LemmyContext,
|
||||||
expected_domain: &Url,
|
expected_domain: &Url,
|
||||||
request_counter: &mut i32,
|
request_counter: &mut i32,
|
||||||
) -> Result<ApubComment, LemmyError> {
|
) -> Result<ApubComment, LemmyError> {
|
||||||
verify_domains_match(note.id.inner(), expected_domain)?;
|
verify_domains_match(note.id.inner(), expected_domain)?;
|
||||||
let ap_id = Some(note.id.clone().into());
|
|
||||||
let creator = note
|
let creator = note
|
||||||
.attributed_to
|
.attributed_to
|
||||||
.dereference(context, request_counter)
|
.dereference(context, request_counter)
|
||||||
|
@ -176,10 +175,10 @@ impl ApubObject for ApubComment {
|
||||||
content: content_slurs_removed,
|
content: content_slurs_removed,
|
||||||
removed: None,
|
removed: None,
|
||||||
read: None,
|
read: None,
|
||||||
published: note.published.map(|u| u.to_owned().naive_local()),
|
published: note.published.map(|u| u.naive_local()),
|
||||||
updated: note.updated.map(|u| u.to_owned().naive_local()),
|
updated: note.updated.map(|u| u.naive_local()),
|
||||||
deleted: None,
|
deleted: None,
|
||||||
ap_id,
|
ap_id: Some(note.id.into()),
|
||||||
local: Some(false),
|
local: Some(false),
|
||||||
};
|
};
|
||||||
let comment = blocking(context.pool(), move |conn| Comment::upsert(conn, &form)).await??;
|
let comment = blocking(context.pool(), move |conn| Comment::upsert(conn, &form)).await??;
|
||||||
|
@ -206,7 +205,7 @@ pub(crate) mod tests {
|
||||||
let person = parse_lemmy_person(context).await;
|
let person = parse_lemmy_person(context).await;
|
||||||
let community = parse_lemmy_community(context).await;
|
let community = parse_lemmy_community(context).await;
|
||||||
let post_json = file_to_json_object("assets/lemmy/objects/page.json");
|
let post_json = file_to_json_object("assets/lemmy/objects/page.json");
|
||||||
let post = ApubPost::from_apub(&post_json, context, url, &mut 0)
|
let post = ApubPost::from_apub(post_json, context, url, &mut 0)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
(person, community, post)
|
(person, community, post)
|
||||||
|
@ -225,9 +224,9 @@ pub(crate) mod tests {
|
||||||
let url = Url::parse("https://enterprise.lemmy.ml/comment/38741").unwrap();
|
let url = Url::parse("https://enterprise.lemmy.ml/comment/38741").unwrap();
|
||||||
let data = prepare_comment_test(&url, &context).await;
|
let data = prepare_comment_test(&url, &context).await;
|
||||||
|
|
||||||
let json = file_to_json_object("assets/lemmy/objects/note.json");
|
let json: Note = file_to_json_object("assets/lemmy/objects/note.json");
|
||||||
let mut request_counter = 0;
|
let mut request_counter = 0;
|
||||||
let comment = ApubComment::from_apub(&json, &context, &url, &mut request_counter)
|
let comment = ApubComment::from_apub(json.clone(), &context, &url, &mut request_counter)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
@ -236,10 +235,11 @@ pub(crate) mod tests {
|
||||||
assert!(!comment.local);
|
assert!(!comment.local);
|
||||||
assert_eq!(request_counter, 0);
|
assert_eq!(request_counter, 0);
|
||||||
|
|
||||||
let to_apub = comment.to_apub(&context).await.unwrap();
|
let comment_id = comment.id;
|
||||||
|
let to_apub = comment.into_apub(&context).await.unwrap();
|
||||||
assert_json_include!(actual: json, expected: to_apub);
|
assert_json_include!(actual: json, expected: to_apub);
|
||||||
|
|
||||||
Comment::delete(&*context.pool().get().unwrap(), comment.id).unwrap();
|
Comment::delete(&*context.pool().get().unwrap(), comment_id).unwrap();
|
||||||
cleanup(data, &context);
|
cleanup(data, &context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -254,12 +254,12 @@ pub(crate) mod tests {
|
||||||
Url::parse("https://queer.hacktivis.me/objects/8d4973f4-53de-49cd-8c27-df160e16a9c2")
|
Url::parse("https://queer.hacktivis.me/objects/8d4973f4-53de-49cd-8c27-df160e16a9c2")
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let person_json = file_to_json_object("assets/pleroma/objects/person.json");
|
let person_json = file_to_json_object("assets/pleroma/objects/person.json");
|
||||||
ApubPerson::from_apub(&person_json, &context, &pleroma_url, &mut 0)
|
ApubPerson::from_apub(person_json, &context, &pleroma_url, &mut 0)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let json = file_to_json_object("assets/pleroma/objects/note.json");
|
let json = file_to_json_object("assets/pleroma/objects/note.json");
|
||||||
let mut request_counter = 0;
|
let mut request_counter = 0;
|
||||||
let comment = ApubComment::from_apub(&json, &context, &pleroma_url, &mut request_counter)
|
let comment = ApubComment::from_apub(json, &context, &pleroma_url, &mut request_counter)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
|
|
@ -79,7 +79,7 @@ impl ApubObject for ApubCommunity {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn to_apub(&self, _context: &LemmyContext) -> Result<Group, LemmyError> {
|
async fn into_apub(self, _context: &LemmyContext) -> Result<Group, LemmyError> {
|
||||||
let source = self.description.clone().map(|bio| Source {
|
let source = self.description.clone().map(|bio| Source {
|
||||||
content: bio,
|
content: bio,
|
||||||
media_type: MediaTypeMarkdown::Markdown,
|
media_type: MediaTypeMarkdown::Markdown,
|
||||||
|
@ -130,12 +130,12 @@ impl ApubObject for ApubCommunity {
|
||||||
|
|
||||||
/// Converts a `Group` to `Community`, inserts it into the database and updates moderators.
|
/// Converts a `Group` to `Community`, inserts it into the database and updates moderators.
|
||||||
async fn from_apub(
|
async fn from_apub(
|
||||||
group: &Group,
|
group: Group,
|
||||||
context: &LemmyContext,
|
context: &LemmyContext,
|
||||||
expected_domain: &Url,
|
expected_domain: &Url,
|
||||||
request_counter: &mut i32,
|
request_counter: &mut i32,
|
||||||
) -> Result<ApubCommunity, LemmyError> {
|
) -> Result<ApubCommunity, LemmyError> {
|
||||||
let form = Group::from_apub_to_form(group, expected_domain, &context.settings()).await?;
|
let form = Group::into_form(group.clone(), expected_domain, &context.settings()).await?;
|
||||||
|
|
||||||
// Fetching mods and outbox is not necessary for Lemmy to work, so ignore errors. Besides,
|
// Fetching mods and outbox is not necessary for Lemmy to work, so ignore errors. Besides,
|
||||||
// we need to ignore these errors so that tests can work entirely offline.
|
// we need to ignore these errors so that tests can work entirely offline.
|
||||||
|
@ -242,7 +242,7 @@ pub(crate) mod tests {
|
||||||
|
|
||||||
let url = Url::parse("https://enterprise.lemmy.ml/c/tenforward").unwrap();
|
let url = Url::parse("https://enterprise.lemmy.ml/c/tenforward").unwrap();
|
||||||
let mut request_counter = 0;
|
let mut request_counter = 0;
|
||||||
let community = ApubCommunity::from_apub(&json, context, &url, &mut request_counter)
|
let community = ApubCommunity::from_apub(json, context, &url, &mut request_counter)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
// this makes two requests to the (intentionally) broken outbox/moderators collections
|
// this makes two requests to the (intentionally) broken outbox/moderators collections
|
||||||
|
|
|
@ -76,7 +76,7 @@ impl ApubObject for ApubPerson {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn to_apub(&self, _pool: &LemmyContext) -> Result<Person, LemmyError> {
|
async fn into_apub(self, _pool: &LemmyContext) -> Result<Person, LemmyError> {
|
||||||
let kind = if self.bot_account {
|
let kind = if self.bot_account {
|
||||||
UserTypes::Service
|
UserTypes::Service
|
||||||
} else {
|
} else {
|
||||||
|
@ -124,17 +124,16 @@ impl ApubObject for ApubPerson {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn from_apub(
|
async fn from_apub(
|
||||||
person: &Person,
|
person: Person,
|
||||||
context: &LemmyContext,
|
context: &LemmyContext,
|
||||||
expected_domain: &Url,
|
expected_domain: &Url,
|
||||||
_request_counter: &mut i32,
|
_request_counter: &mut i32,
|
||||||
) -> Result<ApubPerson, LemmyError> {
|
) -> Result<ApubPerson, LemmyError> {
|
||||||
verify_domains_match(person.id.inner(), expected_domain)?;
|
verify_domains_match(person.id.inner(), expected_domain)?;
|
||||||
let actor_id = Some(person.id.clone().into());
|
let name = person.preferred_username;
|
||||||
let name = person.preferred_username.clone();
|
let display_name: Option<String> = person.name;
|
||||||
let display_name: Option<String> = person.name.clone();
|
|
||||||
let bio = get_summary_from_string_or_source(&person.summary, &person.source);
|
let bio = get_summary_from_string_or_source(&person.summary, &person.source);
|
||||||
let shared_inbox = person.endpoints.shared_inbox.clone().map(|s| s.into());
|
let shared_inbox = person.endpoints.shared_inbox.map(|s| s.into());
|
||||||
let bot_account = match person.kind {
|
let bot_account = match person.kind {
|
||||||
UserTypes::Person => false,
|
UserTypes::Person => false,
|
||||||
UserTypes::Service => true,
|
UserTypes::Service => true,
|
||||||
|
@ -152,21 +151,21 @@ impl ApubObject for ApubPerson {
|
||||||
display_name: Some(display_name),
|
display_name: Some(display_name),
|
||||||
banned: None,
|
banned: None,
|
||||||
deleted: None,
|
deleted: None,
|
||||||
avatar: Some(person.icon.clone().map(|i| i.url.into())),
|
avatar: Some(person.icon.map(|i| i.url.into())),
|
||||||
banner: Some(person.image.clone().map(|i| i.url.into())),
|
banner: Some(person.image.map(|i| i.url.into())),
|
||||||
published: person.published.map(|u| u.clone().naive_local()),
|
published: person.published.map(|u| u.naive_local()),
|
||||||
updated: person.updated.map(|u| u.clone().naive_local()),
|
updated: person.updated.map(|u| u.naive_local()),
|
||||||
actor_id,
|
actor_id: Some(person.id.into()),
|
||||||
bio: Some(bio),
|
bio: Some(bio),
|
||||||
local: Some(false),
|
local: Some(false),
|
||||||
admin: Some(false),
|
admin: Some(false),
|
||||||
bot_account: Some(bot_account),
|
bot_account: Some(bot_account),
|
||||||
private_key: None,
|
private_key: None,
|
||||||
public_key: Some(Some(person.public_key.public_key_pem.clone())),
|
public_key: Some(Some(person.public_key.public_key_pem)),
|
||||||
last_refreshed_at: Some(naive_now()),
|
last_refreshed_at: Some(naive_now()),
|
||||||
inbox_url: Some(person.inbox.to_owned().into()),
|
inbox_url: Some(person.inbox.into()),
|
||||||
shared_inbox_url: Some(shared_inbox),
|
shared_inbox_url: Some(shared_inbox),
|
||||||
matrix_user_id: Some(person.matrix_user_id.clone()),
|
matrix_user_id: Some(person.matrix_user_id),
|
||||||
};
|
};
|
||||||
let person = blocking(context.pool(), move |conn| {
|
let person = blocking(context.pool(), move |conn| {
|
||||||
DbPerson::upsert(conn, &person_form)
|
DbPerson::upsert(conn, &person_form)
|
||||||
|
@ -215,7 +214,7 @@ pub(crate) mod tests {
|
||||||
let json = file_to_json_object("assets/lemmy/objects/person.json");
|
let json = file_to_json_object("assets/lemmy/objects/person.json");
|
||||||
let url = Url::parse("https://enterprise.lemmy.ml/u/picard").unwrap();
|
let url = Url::parse("https://enterprise.lemmy.ml/u/picard").unwrap();
|
||||||
let mut request_counter = 0;
|
let mut request_counter = 0;
|
||||||
let person = ApubPerson::from_apub(&json, context, &url, &mut request_counter)
|
let person = ApubPerson::from_apub(json, context, &url, &mut request_counter)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(request_counter, 0);
|
assert_eq!(request_counter, 0);
|
||||||
|
@ -243,7 +242,7 @@ pub(crate) mod tests {
|
||||||
let json = file_to_json_object("assets/pleroma/objects/person.json");
|
let json = file_to_json_object("assets/pleroma/objects/person.json");
|
||||||
let url = Url::parse("https://queer.hacktivis.me/users/lanodan").unwrap();
|
let url = Url::parse("https://queer.hacktivis.me/users/lanodan").unwrap();
|
||||||
let mut request_counter = 0;
|
let mut request_counter = 0;
|
||||||
let person = ApubPerson::from_apub(&json, &context, &url, &mut request_counter)
|
let person = ApubPerson::from_apub(json, &context, &url, &mut request_counter)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
|
|
@ -87,7 +87,7 @@ impl ApubObject for ApubPost {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Turn a Lemmy post into an ActivityPub page that can be sent out over the network.
|
// Turn a Lemmy post into an ActivityPub page that can be sent out over the network.
|
||||||
async fn to_apub(&self, context: &LemmyContext) -> Result<Page, LemmyError> {
|
async fn into_apub(self, context: &LemmyContext) -> Result<Page, LemmyError> {
|
||||||
let creator_id = self.creator_id;
|
let creator_id = self.creator_id;
|
||||||
let creator = blocking(context.pool(), move |conn| Person::read(conn, creator_id)).await??;
|
let creator = blocking(context.pool(), move |conn| Person::read(conn, creator_id)).await??;
|
||||||
let community_id = self.community_id;
|
let community_id = self.community_id;
|
||||||
|
@ -134,7 +134,7 @@ impl ApubObject for ApubPost {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn from_apub(
|
async fn from_apub(
|
||||||
page: &Page,
|
page: Page,
|
||||||
context: &LemmyContext,
|
context: &LemmyContext,
|
||||||
expected_domain: &Url,
|
expected_domain: &Url,
|
||||||
request_counter: &mut i32,
|
request_counter: &mut i32,
|
||||||
|
@ -144,7 +144,6 @@ impl ApubObject for ApubPost {
|
||||||
if !page.is_mod_action(context).await? {
|
if !page.is_mod_action(context).await? {
|
||||||
verify_domains_match(page.id.inner(), expected_domain)?;
|
verify_domains_match(page.id.inner(), expected_domain)?;
|
||||||
};
|
};
|
||||||
let ap_id = Some(page.id.clone().into());
|
|
||||||
let creator = page
|
let creator = page
|
||||||
.attributed_to
|
.attributed_to
|
||||||
.dereference(context, request_counter)
|
.dereference(context, request_counter)
|
||||||
|
@ -153,7 +152,7 @@ impl ApubObject for ApubPost {
|
||||||
check_is_apub_id_valid(page.id.inner(), community.local, &context.settings())?;
|
check_is_apub_id_valid(page.id.inner(), community.local, &context.settings())?;
|
||||||
verify_person_in_community(&page.attributed_to, &community, context, request_counter).await?;
|
verify_person_in_community(&page.attributed_to, &community, context, request_counter).await?;
|
||||||
|
|
||||||
let thumbnail_url: Option<Url> = page.image.clone().map(|i| i.url);
|
let thumbnail_url: Option<Url> = page.image.map(|i| i.url);
|
||||||
let (metadata_res, pictrs_thumbnail) = if let Some(url) = &page.url {
|
let (metadata_res, pictrs_thumbnail) = if let Some(url) = &page.url {
|
||||||
fetch_site_data(context.client(), &context.settings(), Some(url)).await
|
fetch_site_data(context.client(), &context.settings(), Some(url)).await
|
||||||
} else {
|
} else {
|
||||||
|
@ -168,8 +167,8 @@ impl ApubObject for ApubPost {
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|s| remove_slurs(&s.content, &context.settings().slur_regex()));
|
.map(|s| remove_slurs(&s.content, &context.settings().slur_regex()));
|
||||||
let form = PostForm {
|
let form = PostForm {
|
||||||
name: page.name.clone(),
|
name: page.name,
|
||||||
url: page.url.clone().map(|u| u.into()),
|
url: page.url.map(|u| u.into()),
|
||||||
body: body_slurs_removed,
|
body: body_slurs_removed,
|
||||||
creator_id: creator.id,
|
creator_id: creator.id,
|
||||||
community_id: community.id,
|
community_id: community.id,
|
||||||
|
@ -184,7 +183,7 @@ impl ApubObject for ApubPost {
|
||||||
embed_description,
|
embed_description,
|
||||||
embed_html,
|
embed_html,
|
||||||
thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
|
thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
|
||||||
ap_id,
|
ap_id: Some(page.id.into()),
|
||||||
local: Some(false),
|
local: Some(false),
|
||||||
};
|
};
|
||||||
let post = blocking(context.pool(), move |conn| Post::upsert(conn, &form)).await??;
|
let post = blocking(context.pool(), move |conn| Post::upsert(conn, &form)).await??;
|
||||||
|
@ -213,7 +212,7 @@ mod tests {
|
||||||
let json = file_to_json_object("assets/lemmy/objects/page.json");
|
let json = file_to_json_object("assets/lemmy/objects/page.json");
|
||||||
let url = Url::parse("https://enterprise.lemmy.ml/post/55143").unwrap();
|
let url = Url::parse("https://enterprise.lemmy.ml/post/55143").unwrap();
|
||||||
let mut request_counter = 0;
|
let mut request_counter = 0;
|
||||||
let post = ApubPost::from_apub(&json, &context, &url, &mut request_counter)
|
let post = ApubPost::from_apub(json, &context, &url, &mut request_counter)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
|
|
@ -70,7 +70,7 @@ impl ApubObject for ApubPrivateMessage {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn to_apub(&self, context: &LemmyContext) -> Result<ChatMessage, LemmyError> {
|
async fn into_apub(self, context: &LemmyContext) -> Result<ChatMessage, LemmyError> {
|
||||||
let creator_id = self.creator_id;
|
let creator_id = self.creator_id;
|
||||||
let creator = blocking(context.pool(), move |conn| Person::read(conn, creator_id)).await??;
|
let creator = blocking(context.pool(), move |conn| Person::read(conn, creator_id)).await??;
|
||||||
|
|
||||||
|
@ -101,13 +101,13 @@ impl ApubObject for ApubPrivateMessage {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn from_apub(
|
async fn from_apub(
|
||||||
note: &ChatMessage,
|
note: ChatMessage,
|
||||||
context: &LemmyContext,
|
context: &LemmyContext,
|
||||||
expected_domain: &Url,
|
expected_domain: &Url,
|
||||||
request_counter: &mut i32,
|
request_counter: &mut i32,
|
||||||
) -> Result<ApubPrivateMessage, LemmyError> {
|
) -> Result<ApubPrivateMessage, LemmyError> {
|
||||||
verify_domains_match(note.id.inner(), expected_domain)?;
|
verify_domains_match(note.id.inner(), expected_domain)?;
|
||||||
let ap_id = Some(note.id.clone().into());
|
let ap_id = Some(note.id.into());
|
||||||
let creator = note
|
let creator = note
|
||||||
.attributed_to
|
.attributed_to
|
||||||
.dereference(context, request_counter)
|
.dereference(context, request_counter)
|
||||||
|
@ -123,8 +123,8 @@ impl ApubObject for ApubPrivateMessage {
|
||||||
creator_id: creator.id,
|
creator_id: creator.id,
|
||||||
recipient_id: recipient.id,
|
recipient_id: recipient.id,
|
||||||
content,
|
content,
|
||||||
published: note.published.map(|u| u.to_owned().naive_local()),
|
published: note.published.map(|u| u.naive_local()),
|
||||||
updated: note.updated.map(|u| u.to_owned().naive_local()),
|
updated: note.updated.map(|u| u.naive_local()),
|
||||||
deleted: None,
|
deleted: None,
|
||||||
read: None,
|
read: None,
|
||||||
ap_id,
|
ap_id,
|
||||||
|
@ -150,12 +150,12 @@ mod tests {
|
||||||
|
|
||||||
async fn prepare_comment_test(url: &Url, context: &LemmyContext) -> (ApubPerson, ApubPerson) {
|
async fn prepare_comment_test(url: &Url, context: &LemmyContext) -> (ApubPerson, ApubPerson) {
|
||||||
let lemmy_person = file_to_json_object("assets/lemmy/objects/person.json");
|
let lemmy_person = file_to_json_object("assets/lemmy/objects/person.json");
|
||||||
let person1 = ApubPerson::from_apub(&lemmy_person, context, url, &mut 0)
|
let person1 = ApubPerson::from_apub(lemmy_person, context, url, &mut 0)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let pleroma_person = file_to_json_object("assets/pleroma/objects/person.json");
|
let pleroma_person = file_to_json_object("assets/pleroma/objects/person.json");
|
||||||
let pleroma_url = Url::parse("https://queer.hacktivis.me/users/lanodan").unwrap();
|
let pleroma_url = Url::parse("https://queer.hacktivis.me/users/lanodan").unwrap();
|
||||||
let person2 = ApubPerson::from_apub(&pleroma_person, context, &pleroma_url, &mut 0)
|
let person2 = ApubPerson::from_apub(pleroma_person, context, &pleroma_url, &mut 0)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
(person1, person2)
|
(person1, person2)
|
||||||
|
@ -172,9 +172,9 @@ mod tests {
|
||||||
let context = init_context();
|
let context = init_context();
|
||||||
let url = Url::parse("https://enterprise.lemmy.ml/private_message/1621").unwrap();
|
let url = Url::parse("https://enterprise.lemmy.ml/private_message/1621").unwrap();
|
||||||
let data = prepare_comment_test(&url, &context).await;
|
let data = prepare_comment_test(&url, &context).await;
|
||||||
let json = file_to_json_object("assets/lemmy/objects/chat_message.json");
|
let json: ChatMessage = file_to_json_object("assets/lemmy/objects/chat_message.json");
|
||||||
let mut request_counter = 0;
|
let mut request_counter = 0;
|
||||||
let pm = ApubPrivateMessage::from_apub(&json, &context, &url, &mut request_counter)
|
let pm = ApubPrivateMessage::from_apub(json.clone(), &context, &url, &mut request_counter)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
@ -182,10 +182,11 @@ mod tests {
|
||||||
assert_eq!(pm.content.len(), 20);
|
assert_eq!(pm.content.len(), 20);
|
||||||
assert_eq!(request_counter, 0);
|
assert_eq!(request_counter, 0);
|
||||||
|
|
||||||
let to_apub = pm.to_apub(&context).await.unwrap();
|
let pm_id = pm.id;
|
||||||
|
let to_apub = pm.into_apub(&context).await.unwrap();
|
||||||
assert_json_include!(actual: json, expected: to_apub);
|
assert_json_include!(actual: json, expected: to_apub);
|
||||||
|
|
||||||
PrivateMessage::delete(&*context.pool().get().unwrap(), pm.id).unwrap();
|
PrivateMessage::delete(&*context.pool().get().unwrap(), pm_id).unwrap();
|
||||||
cleanup(data, &context);
|
cleanup(data, &context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -198,7 +199,7 @@ mod tests {
|
||||||
let pleroma_url = Url::parse("https://queer.hacktivis.me/objects/2").unwrap();
|
let pleroma_url = Url::parse("https://queer.hacktivis.me/objects/2").unwrap();
|
||||||
let json = file_to_json_object("assets/pleroma/objects/chat_message.json");
|
let json = file_to_json_object("assets/pleroma/objects/chat_message.json");
|
||||||
let mut request_counter = 0;
|
let mut request_counter = 0;
|
||||||
let pm = ApubPrivateMessage::from_apub(&json, &context, &pleroma_url, &mut request_counter)
|
let pm = ApubPrivateMessage::from_apub(json, &context, &pleroma_url, &mut request_counter)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
|
|
@ -55,17 +55,17 @@ pub struct Group {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Group {
|
impl Group {
|
||||||
pub(crate) async fn from_apub_to_form(
|
pub(crate) async fn into_form(
|
||||||
group: &Group,
|
self,
|
||||||
expected_domain: &Url,
|
expected_domain: &Url,
|
||||||
settings: &Settings,
|
settings: &Settings,
|
||||||
) -> Result<CommunityForm, LemmyError> {
|
) -> Result<CommunityForm, LemmyError> {
|
||||||
check_is_apub_id_valid(group.id.inner(), true, settings)?;
|
check_is_apub_id_valid(self.id.inner(), true, settings)?;
|
||||||
verify_domains_match(expected_domain, group.id.inner())?;
|
verify_domains_match(expected_domain, self.id.inner())?;
|
||||||
let name = group.preferred_username.clone();
|
let name = self.preferred_username;
|
||||||
let title = group.name.clone();
|
let title = self.name;
|
||||||
let description = get_summary_from_string_or_source(&group.summary, &group.source);
|
let description = get_summary_from_string_or_source(&self.summary, &self.source);
|
||||||
let shared_inbox = group.endpoints.shared_inbox.clone().map(|s| s.into());
|
let shared_inbox = self.endpoints.shared_inbox.map(|s| s.into());
|
||||||
|
|
||||||
let slur_regex = &settings.slur_regex();
|
let slur_regex = &settings.slur_regex();
|
||||||
check_slurs(&name, slur_regex)?;
|
check_slurs(&name, slur_regex)?;
|
||||||
|
@ -78,19 +78,19 @@ impl Group {
|
||||||
title,
|
title,
|
||||||
description,
|
description,
|
||||||
removed: None,
|
removed: None,
|
||||||
published: group.published.map(|u| u.naive_local()),
|
published: self.published.map(|u| u.naive_local()),
|
||||||
updated: group.updated.map(|u| u.naive_local()),
|
updated: self.updated.map(|u| u.naive_local()),
|
||||||
deleted: None,
|
deleted: None,
|
||||||
nsfw: Some(group.sensitive.unwrap_or(false)),
|
nsfw: Some(self.sensitive.unwrap_or(false)),
|
||||||
actor_id: Some(group.id.clone().into()),
|
actor_id: Some(self.id.into()),
|
||||||
local: Some(false),
|
local: Some(false),
|
||||||
private_key: None,
|
private_key: None,
|
||||||
public_key: Some(group.public_key.public_key_pem.clone()),
|
public_key: Some(self.public_key.public_key_pem),
|
||||||
last_refreshed_at: Some(naive_now()),
|
last_refreshed_at: Some(naive_now()),
|
||||||
icon: Some(group.icon.clone().map(|i| i.url.into())),
|
icon: Some(self.icon.map(|i| i.url.into())),
|
||||||
banner: Some(group.image.clone().map(|i| i.url.into())),
|
banner: Some(self.image.map(|i| i.url.into())),
|
||||||
followers_url: Some(group.followers.clone().into()),
|
followers_url: Some(self.followers.into()),
|
||||||
inbox_url: Some(group.inbox.clone().into()),
|
inbox_url: Some(self.inbox.into()),
|
||||||
shared_inbox_url: Some(shared_inbox),
|
shared_inbox_url: Some(shared_inbox),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -139,7 +139,7 @@ where
|
||||||
|
|
||||||
let res2: Kind::ApubType = res.json().await?;
|
let res2: Kind::ApubType = res.json().await?;
|
||||||
|
|
||||||
Ok(Kind::from_apub(&res2, data, self.inner(), request_counter).await?)
|
Ok(Kind::from_apub(res2, data, self.inner(), request_counter).await?)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,7 @@ pub trait ApubObject {
|
||||||
async fn delete(self, data: &Self::DataType) -> Result<(), LemmyError>;
|
async fn delete(self, data: &Self::DataType) -> Result<(), LemmyError>;
|
||||||
|
|
||||||
/// Trait for converting an object or actor into the respective ActivityPub type.
|
/// Trait for converting an object or actor into the respective ActivityPub type.
|
||||||
async fn to_apub(&self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError>;
|
async fn into_apub(self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError>;
|
||||||
fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError>;
|
fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError>;
|
||||||
|
|
||||||
/// Converts an object from ActivityPub type to Lemmy internal type.
|
/// Converts an object from ActivityPub type to Lemmy internal type.
|
||||||
|
@ -51,7 +51,7 @@ pub trait ApubObject {
|
||||||
/// * `expected_domain` Domain where the object was received from. None in case of mod action.
|
/// * `expected_domain` Domain where the object was received from. None in case of mod action.
|
||||||
/// * `mod_action_allowed` True if the object can be a mod activity, ignore `expected_domain` in this case
|
/// * `mod_action_allowed` True if the object can be a mod activity, ignore `expected_domain` in this case
|
||||||
async fn from_apub(
|
async fn from_apub(
|
||||||
apub: &Self::ApubType,
|
apub: Self::ApubType,
|
||||||
data: &Self::DataType,
|
data: &Self::DataType,
|
||||||
expected_domain: &Url,
|
expected_domain: &Url,
|
||||||
request_counter: &mut i32,
|
request_counter: &mut i32,
|
||||||
|
|
Loading…
Reference in a new issue