Send out like.object as URL, instead of full object (fixes #1283)

This commit is contained in:
Felix Ableitner 2020-12-03 16:49:56 +01:00
parent 429b7f6e5c
commit ac73470a17
9 changed files with 94 additions and 132 deletions

View file

@ -438,7 +438,7 @@ Sent to: Community
"cc": [ "cc": [
"https://ds9.lemmy.ml/c/main/" "https://ds9.lemmy.ml/c/main/"
], ],
"object": ... "object": "https://enterprise.lemmy.ml/p/123"
} }
``` ```
@ -465,7 +465,7 @@ Sent to: Community
"cc": [ "cc": [
"https://ds9.lemmy.ml/c/main/" "https://ds9.lemmy.ml/c/main/"
], ],
"object": ... "object": "https://enterprise.lemmy.ml/p/123"
} }
``` ```

View file

@ -1,20 +1,6 @@
use crate::{ use crate::{activities::receive::get_actor_as_user, objects::FromApub, ActorType, NoteExt};
activities::receive::{get_actor_as_user, get_like_object_id},
fetcher::get_or_fetch_and_insert_comment,
objects::FromApub,
ActorType,
NoteExt,
};
use activitystreams::{ use activitystreams::{
activity::{ activity::{ActorAndObjectRefExt, Create, Dislike, Like, Remove, Update},
kind::{DislikeType, LikeType},
ActorAndObjectRefExt,
Create,
Dislike,
Like,
Remove,
Update,
},
base::ExtendsExt, base::ExtendsExt,
}; };
use anyhow::Context; use anyhow::Context;
@ -113,12 +99,11 @@ pub(crate) async fn receive_update_comment(
pub(crate) async fn receive_like_comment( pub(crate) async fn receive_like_comment(
like: Like, like: Like,
comment: Comment,
context: &LemmyContext, context: &LemmyContext,
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let user = get_actor_as_user(&like, context, request_counter).await?; let user = get_actor_as_user(&like, context, request_counter).await?;
let comment_id = get_like_object_id::<Like, LikeType>(&like)?;
let comment = get_or_fetch_and_insert_comment(&comment_id, context, request_counter).await?;
let comment_id = comment.id; let comment_id = comment.id;
let like_form = CommentLikeForm { let like_form = CommentLikeForm {
@ -159,12 +144,11 @@ pub(crate) async fn receive_like_comment(
pub(crate) async fn receive_dislike_comment( pub(crate) async fn receive_dislike_comment(
dislike: Dislike, dislike: Dislike,
comment: Comment,
context: &LemmyContext, context: &LemmyContext,
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let user = get_actor_as_user(&dislike, context, request_counter).await?; let user = get_actor_as_user(&dislike, context, request_counter).await?;
let comment_id = get_like_object_id::<Dislike, DislikeType>(&dislike)?;
let comment = get_or_fetch_and_insert_comment(&comment_id, context, request_counter).await?;
let comment_id = comment.id; let comment_id = comment.id;
let like_form = CommentLikeForm { let like_form = CommentLikeForm {

View file

@ -1,11 +1,5 @@
use crate::{ use crate::activities::receive::get_actor_as_user;
activities::receive::{get_actor_as_user, get_like_object_id}, use activitystreams::activity::{Dislike, Like};
fetcher::get_or_fetch_and_insert_comment,
};
use activitystreams::activity::{
kind::{DislikeType, LikeType},
*,
};
use lemmy_db::{ use lemmy_db::{
comment::{Comment, CommentLike}, comment::{Comment, CommentLike},
comment_view::CommentView, comment_view::CommentView,
@ -17,12 +11,11 @@ use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperation};
pub(crate) async fn receive_undo_like_comment( pub(crate) async fn receive_undo_like_comment(
like: &Like, like: &Like,
comment: Comment,
context: &LemmyContext, context: &LemmyContext,
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let user = get_actor_as_user(like, context, request_counter).await?; let user = get_actor_as_user(like, context, request_counter).await?;
let comment_id = get_like_object_id::<Like, LikeType>(like)?;
let comment = get_or_fetch_and_insert_comment(&comment_id, context, request_counter).await?;
let comment_id = comment.id; let comment_id = comment.id;
let user_id = user.id; let user_id = user.id;
@ -56,12 +49,11 @@ pub(crate) async fn receive_undo_like_comment(
pub(crate) async fn receive_undo_dislike_comment( pub(crate) async fn receive_undo_dislike_comment(
dislike: &Dislike, dislike: &Dislike,
comment: Comment,
context: &LemmyContext, context: &LemmyContext,
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let user = get_actor_as_user(dislike, context, request_counter).await?; let user = get_actor_as_user(dislike, context, request_counter).await?;
let comment_id = get_like_object_id::<Dislike, DislikeType>(dislike)?;
let comment = get_or_fetch_and_insert_comment(&comment_id, context, request_counter).await?;
let comment_id = comment.id; let comment_id = comment.id;
let user_id = user.id; let user_id = user.id;

View file

@ -79,28 +79,3 @@ where
Ok(()) Ok(())
} }
pub(in crate::activities::receive) fn get_like_object_id<Activity, Kind>(
like_or_dislike: &Activity,
) -> Result<Url, LemmyError>
where
Activity: ActorAndObjectRefExt,
{
// For backwards compatibility with older Lemmy versions where like.object contains a full
// post/comment. This can be removed after some time, using
// `activity.oject().as_single_xsd_any_uri()` instead.
let object = like_or_dislike.object();
if let Some(xsd_uri) = object.as_single_xsd_any_uri() {
Ok(xsd_uri.to_owned())
} else {
Ok(
object
.to_owned()
.one()
.context(location_info!())?
.id()
.context(location_info!())?
.to_owned(),
)
}
}

View file

@ -1,19 +1,6 @@
use crate::{ use crate::{activities::receive::get_actor_as_user, objects::FromApub, ActorType, PageExt};
activities::receive::{get_actor_as_user, get_like_object_id},
fetcher::get_or_fetch_and_insert_post,
objects::FromApub,
ActorType,
PageExt,
};
use activitystreams::{ use activitystreams::{
activity::{ activity::{Create, Dislike, Like, Remove, Update},
kind::{DislikeType, LikeType},
Create,
Dislike,
Like,
Remove,
Update,
},
prelude::*, prelude::*,
}; };
use anyhow::Context; use anyhow::Context;
@ -86,12 +73,11 @@ pub(crate) async fn receive_update_post(
pub(crate) async fn receive_like_post( pub(crate) async fn receive_like_post(
like: Like, like: Like,
post: Post,
context: &LemmyContext, context: &LemmyContext,
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let user = get_actor_as_user(&like, context, request_counter).await?; let user = get_actor_as_user(&like, context, request_counter).await?;
let post_id = get_like_object_id::<Like, LikeType>(&like)?;
let post = get_or_fetch_and_insert_post(&post_id, context, request_counter).await?;
let post_id = post.id; let post_id = post.id;
let like_form = PostLikeForm { let like_form = PostLikeForm {
@ -125,12 +111,11 @@ pub(crate) async fn receive_like_post(
pub(crate) async fn receive_dislike_post( pub(crate) async fn receive_dislike_post(
dislike: Dislike, dislike: Dislike,
post: Post,
context: &LemmyContext, context: &LemmyContext,
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let user = get_actor_as_user(&dislike, context, request_counter).await?; let user = get_actor_as_user(&dislike, context, request_counter).await?;
let post_id = get_like_object_id::<Dislike, DislikeType>(&dislike)?;
let post = get_or_fetch_and_insert_post(&post_id, context, request_counter).await?;
let post_id = post.id; let post_id = post.id;
let like_form = PostLikeForm { let like_form = PostLikeForm {

View file

@ -1,11 +1,5 @@
use crate::{ use crate::activities::receive::get_actor_as_user;
activities::receive::{get_actor_as_user, get_like_object_id}, use activitystreams::activity::{Dislike, Like};
fetcher::get_or_fetch_and_insert_post,
};
use activitystreams::activity::{
kind::{DislikeType, LikeType},
*,
};
use lemmy_db::{ use lemmy_db::{
post::{Post, PostLike}, post::{Post, PostLike},
post_view::PostView, post_view::PostView,
@ -17,12 +11,11 @@ use lemmy_websocket::{messages::SendPost, LemmyContext, UserOperation};
pub(crate) async fn receive_undo_like_post( pub(crate) async fn receive_undo_like_post(
like: &Like, like: &Like,
post: Post,
context: &LemmyContext, context: &LemmyContext,
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let user = get_actor_as_user(like, context, request_counter).await?; let user = get_actor_as_user(like, context, request_counter).await?;
let post_id = get_like_object_id::<Like, LikeType>(&like)?;
let post = get_or_fetch_and_insert_post(&post_id, context, request_counter).await?;
let post_id = post.id; let post_id = post.id;
let user_id = user.id; let user_id = user.id;
@ -50,12 +43,11 @@ pub(crate) async fn receive_undo_like_post(
pub(crate) async fn receive_undo_dislike_post( pub(crate) async fn receive_undo_dislike_post(
dislike: &Dislike, dislike: &Dislike,
post: Post,
context: &LemmyContext, context: &LemmyContext,
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let user = get_actor_as_user(dislike, context, request_counter).await?; let user = get_actor_as_user(dislike, context, request_counter).await?;
let post_id = get_like_object_id::<Dislike, DislikeType>(&dislike)?;
let post = get_or_fetch_and_insert_post(&post_id, context, request_counter).await?;
let post_id = post.id; let post_id = post.id;
let user_id = user.id; let user_id = user.id;

View file

@ -218,8 +218,6 @@ impl ApubObjectType for Comment {
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl ApubLikeableType for Comment { impl ApubLikeableType for Comment {
async fn send_like(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> { async fn send_like(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
let note = self.to_apub(context.pool()).await?;
let post_id = self.post_id; let post_id = self.post_id;
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??; let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
@ -229,7 +227,7 @@ impl ApubLikeableType for Comment {
}) })
.await??; .await??;
let mut like = Like::new(creator.actor_id.to_owned(), note.into_any_base()?); let mut like = Like::new(creator.actor_id.to_owned(), Url::parse(&self.ap_id)?);
like like
.set_many_contexts(lemmy_context()?) .set_many_contexts(lemmy_context()?)
.set_id(generate_activity_id(LikeType::Like)?) .set_id(generate_activity_id(LikeType::Like)?)
@ -241,8 +239,6 @@ impl ApubLikeableType for Comment {
} }
async fn send_dislike(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> { async fn send_dislike(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
let note = self.to_apub(context.pool()).await?;
let post_id = self.post_id; let post_id = self.post_id;
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??; let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
@ -252,7 +248,7 @@ impl ApubLikeableType for Comment {
}) })
.await??; .await??;
let mut dislike = Dislike::new(creator.actor_id.to_owned(), note.into_any_base()?); let mut dislike = Dislike::new(creator.actor_id.to_owned(), Url::parse(&self.ap_id)?);
dislike dislike
.set_many_contexts(lemmy_context()?) .set_many_contexts(lemmy_context()?)
.set_id(generate_activity_id(DislikeType::Dislike)?) .set_id(generate_activity_id(DislikeType::Dislike)?)
@ -268,8 +264,6 @@ impl ApubLikeableType for Comment {
creator: &User_, creator: &User_,
context: &LemmyContext, context: &LemmyContext,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let note = self.to_apub(context.pool()).await?;
let post_id = self.post_id; let post_id = self.post_id;
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??; let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
@ -279,7 +273,7 @@ impl ApubLikeableType for Comment {
}) })
.await??; .await??;
let mut like = Like::new(creator.actor_id.to_owned(), note.into_any_base()?); let mut like = Like::new(creator.actor_id.to_owned(), Url::parse(&self.ap_id)?);
like like
.set_many_contexts(lemmy_context()?) .set_many_contexts(lemmy_context()?)
.set_id(generate_activity_id(DislikeType::Dislike)?) .set_id(generate_activity_id(DislikeType::Dislike)?)

View file

@ -167,15 +167,13 @@ impl ApubObjectType for Post {
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl ApubLikeableType for Post { impl ApubLikeableType for Post {
async fn send_like(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> { async fn send_like(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
let page = self.to_apub(context.pool()).await?;
let community_id = self.community_id; let community_id = self.community_id;
let community = blocking(context.pool(), move |conn| { let community = blocking(context.pool(), move |conn| {
Community::read(conn, community_id) Community::read(conn, community_id)
}) })
.await??; .await??;
let mut like = Like::new(creator.actor_id.to_owned(), page.into_any_base()?); let mut like = Like::new(creator.actor_id.to_owned(), Url::parse(&self.ap_id)?);
like like
.set_many_contexts(lemmy_context()?) .set_many_contexts(lemmy_context()?)
.set_id(generate_activity_id(LikeType::Like)?) .set_id(generate_activity_id(LikeType::Like)?)
@ -187,15 +185,13 @@ impl ApubLikeableType for Post {
} }
async fn send_dislike(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> { async fn send_dislike(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
let page = self.to_apub(context.pool()).await?;
let community_id = self.community_id; let community_id = self.community_id;
let community = blocking(context.pool(), move |conn| { let community = blocking(context.pool(), move |conn| {
Community::read(conn, community_id) Community::read(conn, community_id)
}) })
.await??; .await??;
let mut dislike = Dislike::new(creator.actor_id.to_owned(), page.into_any_base()?); let mut dislike = Dislike::new(creator.actor_id.to_owned(), Url::parse(&self.ap_id)?);
dislike dislike
.set_many_contexts(lemmy_context()?) .set_many_contexts(lemmy_context()?)
.set_id(generate_activity_id(DislikeType::Dislike)?) .set_id(generate_activity_id(DislikeType::Dislike)?)
@ -211,15 +207,13 @@ impl ApubLikeableType for Post {
creator: &User_, creator: &User_,
context: &LemmyContext, context: &LemmyContext,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let page = self.to_apub(context.pool()).await?;
let community_id = self.community_id; let community_id = self.community_id;
let community = blocking(context.pool(), move |conn| { let community = blocking(context.pool(), move |conn| {
Community::read(conn, community_id) Community::read(conn, community_id)
}) })
.await??; .await??;
let mut like = Like::new(creator.actor_id.to_owned(), page.into_any_base()?); let mut like = Like::new(creator.actor_id.to_owned(), Url::parse(&self.ap_id)?);
like like
.set_many_contexts(lemmy_context()?) .set_many_contexts(lemmy_context()?)
.set_id(generate_activity_id(LikeType::Like)?) .set_id(generate_activity_id(LikeType::Like)?)

View file

@ -31,6 +31,7 @@ use crate::{
receive_unhandled_activity, receive_unhandled_activity,
verify_activity_domains_valid, verify_activity_domains_valid,
}, },
fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post},
inbox::is_addressed_to_public, inbox::is_addressed_to_public,
}; };
use activitystreams::{ use activitystreams::{
@ -96,10 +97,12 @@ pub(in crate::inbox) async fn receive_like_for_community(
verify_activity_domains_valid(&like, &expected_domain, false)?; verify_activity_domains_valid(&like, &expected_domain, false)?;
is_addressed_to_public(&like)?; is_addressed_to_public(&like)?;
match like.object().as_single_kind_str() { let object_id = get_like_object_id(&like)?;
Some("Page") => receive_like_post(like, context, request_counter).await, match fetch_post_or_comment_by_id(&object_id, context, request_counter).await? {
Some("Note") => receive_like_comment(like, context, request_counter).await, PostOrComment::Post(post) => receive_like_post(like, post, context, request_counter).await,
_ => receive_unhandled_activity(like), PostOrComment::Comment(comment) => {
receive_like_comment(like, comment, context, request_counter).await
}
} }
} }
@ -122,10 +125,14 @@ pub(in crate::inbox) async fn receive_dislike_for_community(
verify_activity_domains_valid(&dislike, &expected_domain, false)?; verify_activity_domains_valid(&dislike, &expected_domain, false)?;
is_addressed_to_public(&dislike)?; is_addressed_to_public(&dislike)?;
match dislike.object().as_single_kind_str() { let object_id = get_like_object_id(&dislike)?;
Some("Page") => receive_dislike_post(dislike, context, request_counter).await, match fetch_post_or_comment_by_id(&object_id, context, request_counter).await? {
Some("Note") => receive_dislike_comment(dislike, context, request_counter).await, PostOrComment::Post(post) => {
_ => receive_unhandled_activity(dislike), receive_dislike_post(dislike, post, context, request_counter).await
}
PostOrComment::Comment(comment) => {
receive_dislike_comment(dislike, comment, context, request_counter).await
}
} }
} }
@ -275,14 +282,14 @@ pub(in crate::inbox) async fn receive_undo_like_for_community(
verify_activity_domains_valid(&like, &expected_domain, false)?; verify_activity_domains_valid(&like, &expected_domain, false)?;
is_addressed_to_public(&like)?; is_addressed_to_public(&like)?;
let type_ = like let object_id = get_like_object_id(&like)?;
.object() match fetch_post_or_comment_by_id(&object_id, context, request_counter).await? {
.as_single_kind_str() PostOrComment::Post(post) => {
.context(location_info!())?; receive_undo_like_post(&like, post, context, request_counter).await
match type_ { }
"Note" => receive_undo_like_comment(&like, context, request_counter).await, PostOrComment::Comment(comment) => {
"Page" => receive_undo_like_post(&like, context, request_counter).await, receive_undo_like_comment(&like, comment, context, request_counter).await
_ => receive_unhandled_activity(like), }
} }
} }
@ -298,14 +305,14 @@ pub(in crate::inbox) async fn receive_undo_dislike_for_community(
verify_activity_domains_valid(&dislike, &expected_domain, false)?; verify_activity_domains_valid(&dislike, &expected_domain, false)?;
is_addressed_to_public(&dislike)?; is_addressed_to_public(&dislike)?;
let type_ = dislike let object_id = get_like_object_id(&dislike)?;
.object() match fetch_post_or_comment_by_id(&object_id, context, request_counter).await? {
.as_single_kind_str() PostOrComment::Post(post) => {
.context(location_info!())?; receive_undo_dislike_post(&dislike, post, context, request_counter).await
match type_ { }
"Note" => receive_undo_dislike_comment(&dislike, context, request_counter).await, PostOrComment::Comment(comment) => {
"Page" => receive_undo_dislike_post(&dislike, context, request_counter).await, receive_undo_dislike_comment(&dislike, comment, context, request_counter).await
_ => receive_unhandled_activity(dislike), }
} }
} }
@ -341,3 +348,42 @@ async fn find_post_or_comment_by_id(
return Err(NotFound.into()); return Err(NotFound.into());
} }
async fn fetch_post_or_comment_by_id(
apub_id: &Url,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<PostOrComment, LemmyError> {
if let Ok(post) = get_or_fetch_and_insert_post(apub_id, context, request_counter).await {
return Ok(PostOrComment::Post(post));
}
if let Ok(comment) = get_or_fetch_and_insert_comment(apub_id, context, request_counter).await {
return Ok(PostOrComment::Comment(comment));
}
return Err(NotFound.into());
}
fn get_like_object_id<Activity>(like_or_dislike: &Activity) -> Result<Url, LemmyError>
where
Activity: ActorAndObjectRefExt,
{
// For backwards compatibility with older Lemmy versions where like.object contains a full
// post/comment. This can be removed after some time, using
// `activity.oject().as_single_xsd_any_uri()` instead.
let object = like_or_dislike.object();
if let Some(xsd_uri) = object.as_single_xsd_any_uri() {
Ok(xsd_uri.to_owned())
} else {
Ok(
object
.to_owned()
.one()
.context(location_info!())?
.id()
.context(location_info!())?
.to_owned(),
)
}
}