Change to_apub and from_apub to take by value and avoid cloning

This commit is contained in:
Felix Ableitner 2021-11-06 13:37:55 +01:00
parent 2edf8ba157
commit c725514841
29 changed files with 150 additions and 152 deletions

View file

@ -169,7 +169,7 @@ impl Perform for LockPost {
// apub updates
CreateOrUpdatePost::send(
&updated_post,
updated_post,
&local_user_view.person.clone().into(),
CreateOrUpdateType::Update,
context,
@ -242,7 +242,7 @@ impl Perform for StickyPost {
// Apub updates
// TODO stickied should pry work like locked for ease of use
CreateOrUpdatePost::send(
&updated_post,
updated_post,
&local_user_view.person.clone().into(),
CreateOrUpdateType::Update,
context,

View file

@ -1,5 +1,5 @@
use crate::PerformCrud;
use actix_web::web::Data;
use lemmy_api_common::{
blocking,
check_community_ban,
@ -13,6 +13,7 @@ use lemmy_api_common::{
use lemmy_apub::{
fetcher::post_or_comment::PostOrComment,
generate_local_apub_endpoint,
objects::comment::ApubComment,
protocol::activities::{
create_or_update::comment::CreateOrUpdateComment,
voting::vote::{Vote, VoteType},
@ -40,8 +41,6 @@ use lemmy_websocket::{
UserOperationCrud,
};
use crate::PerformCrud;
#[async_trait::async_trait(?Send)]
impl PerformCrud for CreateComment {
type Response = CommentResponse;
@ -121,14 +120,6 @@ impl PerformCrud for CreateComment {
.await?
.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
let post_id = post.id;
let mentions = scrape_text_for_mentions(&comment_form.content);
@ -155,7 +146,15 @@ impl PerformCrud for CreateComment {
.await?
.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(
&object,
&local_user_view.person.clone().into(),

View file

@ -72,15 +72,6 @@ impl PerformCrud for EditComment {
.await?
.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
let updated_comment_content = updated_comment.content.to_owned();
let mentions = scrape_text_for_mentions(&updated_comment_content);
@ -94,6 +85,15 @@ impl PerformCrud for EditComment {
)
.await?;
// Send the apub update
CreateOrUpdateComment::send(
updated_comment.into(),
&local_user_view.person.into(),
CreateOrUpdateType::Update,
context,
)
.await?;
send_comment_ws_message(
data.comment_id,
UserOperationCrud::EditComment,

View file

@ -72,7 +72,7 @@ impl PerformCrud for EditCommunity {
.map_err(|e| ApiError::err("couldnt_update_community", e))?;
UpdateCommunity::send(
&updated_community.into(),
updated_community.into(),
&local_user_view.person.into(),
context,
)

View file

@ -12,6 +12,7 @@ use lemmy_api_common::{
use lemmy_apub::{
fetcher::post_or_comment::PostOrComment,
generate_local_apub_endpoint,
objects::post::ApubPost,
protocol::activities::{
create_or_update::post::CreateOrUpdatePost,
voting::vote::{Vote, VoteType},
@ -109,14 +110,6 @@ impl PerformCrud for CreatePost {
.await?
.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
let person_id = local_user_view.person.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(
&object,
&local_user_view.person.clone().into(),

View file

@ -109,7 +109,7 @@ impl PerformCrud for EditPost {
// Send apub update
CreateOrUpdatePost::send(
&updated_post.into(),
updated_post.into(),
&local_user_view.person.clone().into(),
CreateOrUpdateType::Update,
context,

View file

@ -83,7 +83,7 @@ impl PerformCrud for CreatePrivateMessage {
.map_err(|e| ApiError::err("couldnt_create_private_message", e))?;
CreateOrUpdatePrivateMessage::send(
&updated_private_message.into(),
updated_private_message.into(),
&local_user_view.person.into(),
CreateOrUpdateType::Create,
context,

View file

@ -47,7 +47,7 @@ impl PerformCrud for EditPrivateMessage {
// Send the apub update
CreateOrUpdatePrivateMessage::send(
&updated_private_message.into(),
updated_private_message.into(),
&local_user_view.person.into(),
CreateOrUpdateType::Update,
context,

View file

@ -29,7 +29,7 @@ use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperation
impl CreateOrUpdateComment {
pub async fn send(
comment: &ApubComment,
comment: ApubComment,
actor: &ApubPerson,
kind: CreateOrUpdateType,
context: &LemmyContext,
@ -48,12 +48,12 @@ impl CreateOrUpdateComment {
kind.clone(),
&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 {
actor: ObjectId::new(actor.actor_id()),
to: vec![public()],
object: comment.to_apub(context).await?,
object: comment.into_apub(context).await?,
cc: maa.ccs,
tag: maa.tags,
kind,
@ -97,7 +97,7 @@ impl ActivityHandler for CreateOrUpdateComment {
request_counter: &mut i32,
) -> Result<(), LemmyError> {
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 notif_type = match self.kind {
CreateOrUpdateType::Create => UserOperationCrud::CreateComment,

View file

@ -27,7 +27,7 @@ use lemmy_websocket::{send::send_community_ws_message, LemmyContext, UserOperati
impl UpdateCommunity {
pub async fn send(
community: &ApubCommunity,
community: ApubCommunity,
actor: &ApubPerson,
context: &LemmyContext,
) -> Result<(), LemmyError> {
@ -38,7 +38,7 @@ impl UpdateCommunity {
let update = UpdateCommunity {
actor: ObjectId::new(actor.actor_id()),
to: vec![public()],
object: community.to_apub(context).await?,
object: community.clone().into_apub(context).await?,
cc: vec![community.actor_id()],
kind: UpdateType::Update,
id: id.clone(),
@ -46,7 +46,7 @@ impl UpdateCommunity {
};
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> {
let community = self.get_community(context, request_counter).await?;
let updated_community = Group::from_apub_to_form(
&self.object,
let updated_community = Group::into_form(
self.object,
&community.actor_id.clone().into(),
&context.settings(),
)

View file

@ -27,7 +27,7 @@ use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCru
impl CreateOrUpdatePost {
pub(crate) async fn new(
post: &ApubPost,
post: ApubPost,
actor: &ApubPerson,
community: &ApubCommunity,
kind: CreateOrUpdateType,
@ -40,7 +40,7 @@ impl CreateOrUpdatePost {
Ok(CreateOrUpdatePost {
actor: ObjectId::new(actor.actor_id()),
to: vec![public()],
object: post.to_apub(context).await?,
object: post.into_apub(context).await?,
cc: vec![community.actor_id()],
kind,
id: id.clone(),
@ -48,7 +48,7 @@ impl CreateOrUpdatePost {
})
}
pub async fn send(
post: &ApubPost,
post: ApubPost,
actor: &ApubPerson,
kind: CreateOrUpdateType,
context: &LemmyContext,
@ -115,7 +115,7 @@ impl ActivityHandler for CreateOrUpdatePost {
) -> Result<(), LemmyError> {
let actor = self.actor.dereference(context, request_counter).await?;
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 {
CreateOrUpdateType::Create => UserOperationCrud::CreatePost,

View file

@ -19,7 +19,7 @@ use lemmy_websocket::{send::send_pm_ws_message, LemmyContext, UserOperationCrud}
impl CreateOrUpdatePrivateMessage {
pub async fn send(
private_message: &ApubPrivateMessage,
private_message: ApubPrivateMessage,
actor: &ApubPerson,
kind: CreateOrUpdateType,
context: &LemmyContext,
@ -38,7 +38,7 @@ impl CreateOrUpdatePrivateMessage {
id: id.clone(),
actor: ObjectId::new(actor.actor_id()),
to: [ObjectId::new(recipient.actor_id())],
object: private_message.to_apub(context).await?,
object: private_message.into_apub(context).await?,
kind,
unparsed: Default::default(),
};
@ -67,7 +67,7 @@ impl ActivityHandler for CreateOrUpdatePrivateMessage {
request_counter: &mut i32,
) -> Result<(), LemmyError> {
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?;
let notif_type = match self.kind {

View file

@ -49,11 +49,11 @@ impl ApubObject for ApubCommunityModerators {
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
.0
.iter()
.map(|m| ObjectId::<ApubPerson>::new(m.moderator.actor_id.clone()))
.into_iter()
.map(|m| ObjectId::<ApubPerson>::new(m.moderator.actor_id))
.collect();
Ok(GroupModerators {
r#type: OrderedCollectionType::OrderedCollection,
@ -67,7 +67,7 @@ impl ApubObject for ApubCommunityModerators {
}
async fn from_apub(
apub: &Self::ApubType,
apub: Self::ApubType,
data: &Self::DataType,
expected_domain: &Url,
request_counter: &mut i32,
@ -94,12 +94,11 @@ impl ApubObject for ApubCommunityModerators {
}
// Add new mods to database which have been added to moderators collection
for mod_id in &apub.ordered_items {
let mod_id = ObjectId::new(mod_id.clone());
for mod_id in apub.ordered_items {
let mod_id = ObjectId::new(mod_id);
let mod_user: ApubPerson = mod_id.dereference(&data.1, request_counter).await?;
if !current_moderators
.clone()
.iter()
.map(|c| c.moderator.actor_id.clone())
.any(|x| x == mod_user.actor_id)
@ -166,7 +165,7 @@ mod tests {
0: community,
1: context,
};
ApubCommunityModerators::from_apub(&json, &community_context, &url, &mut request_counter)
ApubCommunityModerators::from_apub(json, &community_context, &url, &mut request_counter)
.await
.unwrap();
assert_eq!(request_counter, 0);

View file

@ -60,9 +60,9 @@ impl ApubObject for ApubCommunityOutbox {
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![];
for post in &self.0 {
for post in self.0 {
let actor = post.creator_id;
let actor: ApubPerson = blocking(data.1.pool(), move |conn| Person::read(conn, actor))
.await??
@ -86,13 +86,13 @@ impl ApubObject for ApubCommunityOutbox {
}
async fn from_apub(
apub: &Self::ApubType,
apub: Self::ApubType,
data: &Self::DataType,
expected_domain: &Url,
request_counter: &mut i32,
) -> Result<Self, LemmyError> {
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 {
outbox_activities = outbox_activities[0..20].to_vec();
}

View file

@ -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!()
}
@ -62,17 +62,17 @@ impl ApubObject for PostOrComment {
}
async fn from_apub(
apub: &PageOrNote,
apub: PageOrNote,
context: &LemmyContext,
expected_domain: &Url,
request_counter: &mut i32,
) -> Result<Self, LemmyError> {
Ok(match apub {
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(
ApubComment::from_apub(n, context, expected_domain, request_counter).await?,
ApubComment::from_apub(*n, context, expected_domain, request_counter).await?,
),
})
}

View file

@ -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!()
}
@ -164,7 +164,7 @@ impl ApubObject for SearchableObjects {
}
async fn from_apub(
apub: &Self::ApubType,
apub: Self::ApubType,
context: &LemmyContext,
ed: &Url,
rc: &mut i32,

View file

@ -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!()
}
@ -63,7 +63,7 @@ impl ApubObject for UserOrCommunity {
}
async fn from_apub(
apub: &Self::ApubType,
apub: Self::ApubType,
data: &Self::DataType,
expected_domain: &Url,
request_counter: &mut i32,

View file

@ -30,7 +30,7 @@ pub(crate) async fn get_apub_comment(
}
if !comment.deleted {
Ok(create_apub_response(&comment.to_apub(&**context).await?))
Ok(create_apub_response(&comment.into_apub(&**context).await?))
} else {
Ok(create_apub_tombstone_response(&comment.to_tombstone()?))
}

View file

@ -47,7 +47,7 @@ pub(crate) async fn get_apub_community_http(
.into();
if !community.deleted {
let apub = community.to_apub(&**context).await?;
let apub = community.into_apub(&**context).await?;
Ok(create_apub_response(&apub))
} else {
@ -118,7 +118,7 @@ pub(crate) async fn get_apub_community_outbox(
let id = ObjectId::new(generate_outbox_url(&community.actor_id)?);
let outbox_data = CommunityContext(community.into(), context.get_ref().clone());
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(
@ -134,6 +134,6 @@ pub(crate) async fn get_apub_community_moderators(
let outbox_data = CommunityContext(community, context.get_ref().clone());
let moderators: ApubCommunityModerators = id.dereference(&outbox_data, &mut 0).await?;
Ok(create_apub_response(
&moderators.to_apub(&outbox_data).await?,
&moderators.into_apub(&outbox_data).await?,
))
}

View file

@ -39,7 +39,7 @@ pub(crate) async fn get_apub_person_http(
.into();
if !person.deleted {
let apub = person.to_apub(&context).await?;
let apub = person.into_apub(&context).await?;
Ok(create_apub_response(&apub))
} else {

View file

@ -30,7 +30,7 @@ pub(crate) async fn get_apub_post(
}
if !post.deleted {
Ok(create_apub_response(&post.to_apub(&context).await?))
Ok(create_apub_response(&post.into_apub(&context).await?))
} else {
Ok(create_apub_tombstone_response(&post.to_tombstone()?))
}

View file

@ -87,7 +87,7 @@ impl ApubObject for ApubComment {
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 = blocking(context.pool(), move |conn| Person::read(conn, creator_id)).await??;
@ -104,7 +104,7 @@ impl ApubObject for ApubComment {
let note = 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),
to: vec![public()],
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.
async fn from_apub(
note: &Note,
note: Note,
context: &LemmyContext,
expected_domain: &Url,
request_counter: &mut i32,
) -> Result<ApubComment, LemmyError> {
verify_domains_match(note.id.inner(), expected_domain)?;
let ap_id = Some(note.id.clone().into());
let creator = note
.attributed_to
.dereference(context, request_counter)
@ -176,10 +175,10 @@ impl ApubObject for ApubComment {
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()),
published: note.published.map(|u| u.naive_local()),
updated: note.updated.map(|u| u.naive_local()),
deleted: None,
ap_id,
ap_id: Some(note.id.into()),
local: Some(false),
};
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 community = parse_lemmy_community(context).await;
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
.unwrap();
(person, community, post)
@ -225,9 +224,9 @@ pub(crate) mod tests {
let url = Url::parse("https://enterprise.lemmy.ml/comment/38741").unwrap();
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 comment = ApubComment::from_apub(&json, &context, &url, &mut request_counter)
let comment = ApubComment::from_apub(json.clone(), &context, &url, &mut request_counter)
.await
.unwrap();
@ -236,10 +235,11 @@ pub(crate) mod tests {
assert!(!comment.local);
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);
Comment::delete(&*context.pool().get().unwrap(), comment.id).unwrap();
Comment::delete(&*context.pool().get().unwrap(), comment_id).unwrap();
cleanup(data, &context);
}
@ -254,12 +254,12 @@ pub(crate) mod tests {
Url::parse("https://queer.hacktivis.me/objects/8d4973f4-53de-49cd-8c27-df160e16a9c2")
.unwrap();
let person_json = file_to_json_object("assets/pleroma/objects/person.json");
ApubPerson::from_apub(&person_json, &context, &pleroma_url, &mut 0)
ApubPerson::from_apub(person_json, &context, &pleroma_url, &mut 0)
.await
.unwrap();
let json = file_to_json_object("assets/pleroma/objects/note.json");
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
.unwrap();

View file

@ -79,7 +79,7 @@ impl ApubObject for ApubCommunity {
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 {
content: bio,
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.
async fn from_apub(
group: &Group,
group: Group,
context: &LemmyContext,
expected_domain: &Url,
request_counter: &mut i32,
) -> 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,
// 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 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
.unwrap();
// this makes two requests to the (intentionally) broken outbox/moderators collections

View file

@ -76,7 +76,7 @@ impl ApubObject for ApubPerson {
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 {
UserTypes::Service
} else {
@ -124,17 +124,16 @@ impl ApubObject for ApubPerson {
}
async fn from_apub(
person: &Person,
person: Person,
context: &LemmyContext,
expected_domain: &Url,
_request_counter: &mut i32,
) -> Result<ApubPerson, LemmyError> {
verify_domains_match(person.id.inner(), expected_domain)?;
let actor_id = Some(person.id.clone().into());
let name = person.preferred_username.clone();
let display_name: Option<String> = person.name.clone();
let name = person.preferred_username;
let display_name: Option<String> = person.name;
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 {
UserTypes::Person => false,
UserTypes::Service => true,
@ -152,21 +151,21 @@ impl ApubObject for ApubPerson {
display_name: Some(display_name),
banned: None,
deleted: None,
avatar: Some(person.icon.clone().map(|i| i.url.into())),
banner: Some(person.image.clone().map(|i| i.url.into())),
published: person.published.map(|u| u.clone().naive_local()),
updated: person.updated.map(|u| u.clone().naive_local()),
actor_id,
avatar: Some(person.icon.map(|i| i.url.into())),
banner: Some(person.image.map(|i| i.url.into())),
published: person.published.map(|u| u.naive_local()),
updated: person.updated.map(|u| u.naive_local()),
actor_id: Some(person.id.into()),
bio: Some(bio),
local: Some(false),
admin: Some(false),
bot_account: Some(bot_account),
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()),
inbox_url: Some(person.inbox.to_owned().into()),
inbox_url: Some(person.inbox.into()),
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| {
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 url = Url::parse("https://enterprise.lemmy.ml/u/picard").unwrap();
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
.unwrap();
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 url = Url::parse("https://queer.hacktivis.me/users/lanodan").unwrap();
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
.unwrap();

View file

@ -87,7 +87,7 @@ impl ApubObject for ApubPost {
}
// 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 = blocking(context.pool(), move |conn| Person::read(conn, creator_id)).await??;
let community_id = self.community_id;
@ -134,7 +134,7 @@ impl ApubObject for ApubPost {
}
async fn from_apub(
page: &Page,
page: Page,
context: &LemmyContext,
expected_domain: &Url,
request_counter: &mut i32,
@ -144,7 +144,6 @@ impl ApubObject for ApubPost {
if !page.is_mod_action(context).await? {
verify_domains_match(page.id.inner(), expected_domain)?;
};
let ap_id = Some(page.id.clone().into());
let creator = page
.attributed_to
.dereference(context, request_counter)
@ -153,7 +152,7 @@ impl ApubObject for ApubPost {
check_is_apub_id_valid(page.id.inner(), community.local, &context.settings())?;
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 {
fetch_site_data(context.client(), &context.settings(), Some(url)).await
} else {
@ -168,8 +167,8 @@ impl ApubObject for ApubPost {
.as_ref()
.map(|s| remove_slurs(&s.content, &context.settings().slur_regex()));
let form = PostForm {
name: page.name.clone(),
url: page.url.clone().map(|u| u.into()),
name: page.name,
url: page.url.map(|u| u.into()),
body: body_slurs_removed,
creator_id: creator.id,
community_id: community.id,
@ -184,7 +183,7 @@ impl ApubObject for ApubPost {
embed_description,
embed_html,
thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
ap_id,
ap_id: Some(page.id.into()),
local: Some(false),
};
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 url = Url::parse("https://enterprise.lemmy.ml/post/55143").unwrap();
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
.unwrap();

View file

@ -70,7 +70,7 @@ impl ApubObject for ApubPrivateMessage {
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 = blocking(context.pool(), move |conn| Person::read(conn, creator_id)).await??;
@ -101,13 +101,13 @@ impl ApubObject for ApubPrivateMessage {
}
async fn from_apub(
note: &ChatMessage,
note: ChatMessage,
context: &LemmyContext,
expected_domain: &Url,
request_counter: &mut i32,
) -> Result<ApubPrivateMessage, LemmyError> {
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
.attributed_to
.dereference(context, request_counter)
@ -123,8 +123,8 @@ impl ApubObject for ApubPrivateMessage {
creator_id: creator.id,
recipient_id: recipient.id,
content,
published: note.published.map(|u| u.to_owned().naive_local()),
updated: note.updated.map(|u| u.to_owned().naive_local()),
published: note.published.map(|u| u.naive_local()),
updated: note.updated.map(|u| u.naive_local()),
deleted: None,
read: None,
ap_id,
@ -150,12 +150,12 @@ mod tests {
async fn prepare_comment_test(url: &Url, context: &LemmyContext) -> (ApubPerson, ApubPerson) {
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
.unwrap();
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 person2 = ApubPerson::from_apub(&pleroma_person, context, &pleroma_url, &mut 0)
let person2 = ApubPerson::from_apub(pleroma_person, context, &pleroma_url, &mut 0)
.await
.unwrap();
(person1, person2)
@ -172,9 +172,9 @@ mod tests {
let context = init_context();
let url = Url::parse("https://enterprise.lemmy.ml/private_message/1621").unwrap();
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 pm = ApubPrivateMessage::from_apub(&json, &context, &url, &mut request_counter)
let pm = ApubPrivateMessage::from_apub(json.clone(), &context, &url, &mut request_counter)
.await
.unwrap();
@ -182,10 +182,11 @@ mod tests {
assert_eq!(pm.content.len(), 20);
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);
PrivateMessage::delete(&*context.pool().get().unwrap(), pm.id).unwrap();
PrivateMessage::delete(&*context.pool().get().unwrap(), pm_id).unwrap();
cleanup(data, &context);
}
@ -198,7 +199,7 @@ mod tests {
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 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
.unwrap();

View file

@ -55,17 +55,17 @@ pub struct Group {
}
impl Group {
pub(crate) async fn from_apub_to_form(
group: &Group,
pub(crate) async fn into_form(
self,
expected_domain: &Url,
settings: &Settings,
) -> Result<CommunityForm, LemmyError> {
check_is_apub_id_valid(group.id.inner(), true, settings)?;
verify_domains_match(expected_domain, group.id.inner())?;
let name = group.preferred_username.clone();
let title = group.name.clone();
let description = get_summary_from_string_or_source(&group.summary, &group.source);
let shared_inbox = group.endpoints.shared_inbox.clone().map(|s| s.into());
check_is_apub_id_valid(self.id.inner(), true, settings)?;
verify_domains_match(expected_domain, self.id.inner())?;
let name = self.preferred_username;
let title = self.name;
let description = get_summary_from_string_or_source(&self.summary, &self.source);
let shared_inbox = self.endpoints.shared_inbox.map(|s| s.into());
let slur_regex = &settings.slur_regex();
check_slurs(&name, slur_regex)?;
@ -78,19 +78,19 @@ impl Group {
title,
description,
removed: None,
published: group.published.map(|u| u.naive_local()),
updated: group.updated.map(|u| u.naive_local()),
published: self.published.map(|u| u.naive_local()),
updated: self.updated.map(|u| u.naive_local()),
deleted: None,
nsfw: Some(group.sensitive.unwrap_or(false)),
actor_id: Some(group.id.clone().into()),
nsfw: Some(self.sensitive.unwrap_or(false)),
actor_id: Some(self.id.into()),
local: Some(false),
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()),
icon: Some(group.icon.clone().map(|i| i.url.into())),
banner: Some(group.image.clone().map(|i| i.url.into())),
followers_url: Some(group.followers.clone().into()),
inbox_url: Some(group.inbox.clone().into()),
icon: Some(self.icon.map(|i| i.url.into())),
banner: Some(self.image.map(|i| i.url.into())),
followers_url: Some(self.followers.into()),
inbox_url: Some(self.inbox.into()),
shared_inbox_url: Some(shared_inbox),
})
}

View file

@ -139,7 +139,7 @@ where
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?)
}
}

View file

@ -41,7 +41,7 @@ pub trait ApubObject {
async fn delete(self, data: &Self::DataType) -> Result<(), LemmyError>;
/// 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>;
/// 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.
/// * `mod_action_allowed` True if the object can be a mod activity, ignore `expected_domain` in this case
async fn from_apub(
apub: &Self::ApubType,
apub: Self::ApubType,
data: &Self::DataType,
expected_domain: &Url,
request_counter: &mut i32,