Use audience field to federate items in groups (fixes #2464) (#2584)

This commit is contained in:
Nutomic 2022-12-01 20:52:49 +00:00 committed by GitHub
parent 41d4852efc
commit bcf5c91f81
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
70 changed files with 750 additions and 423 deletions

View file

@ -11,7 +11,7 @@ use lemmy_api_common::{
}; };
use lemmy_apub::{ use lemmy_apub::{
objects::post::ApubPost, objects::post::ApubPost,
protocol::activities::{create_or_update::post::CreateOrUpdatePost, CreateOrUpdateType}, protocol::activities::{create_or_update::page::CreateOrUpdatePage, CreateOrUpdateType},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
@ -76,7 +76,7 @@ impl Perform for LockPost {
ModLockPost::create(context.pool(), &form).await?; ModLockPost::create(context.pool(), &form).await?;
// apub updates // apub updates
CreateOrUpdatePost::send( CreateOrUpdatePage::send(
updated_post, updated_post,
&local_user_view.person.clone().into(), &local_user_view.person.clone().into(),
CreateOrUpdateType::Update, CreateOrUpdateType::Update,

View file

@ -11,7 +11,7 @@ use lemmy_api_common::{
}; };
use lemmy_apub::{ use lemmy_apub::{
objects::post::ApubPost, objects::post::ApubPost,
protocol::activities::{create_or_update::post::CreateOrUpdatePost, CreateOrUpdateType}, protocol::activities::{create_or_update::page::CreateOrUpdatePage, CreateOrUpdateType},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
@ -78,7 +78,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( CreateOrUpdatePage::send(
updated_post, updated_post,
&local_user_view.person.clone().into(), &local_user_view.person.clone().into(),
CreateOrUpdateType::Update, CreateOrUpdateType::Update,

View file

@ -14,7 +14,7 @@ use lemmy_api_common::{
use lemmy_apub::{ use lemmy_apub::{
generate_local_apub_endpoint, generate_local_apub_endpoint,
objects::comment::ApubComment, objects::comment::ApubComment,
protocol::activities::{create_or_update::comment::CreateOrUpdateComment, CreateOrUpdateType}, protocol::activities::{create_or_update::note::CreateOrUpdateNote, CreateOrUpdateType},
EndpointType, EndpointType,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
@ -158,7 +158,7 @@ impl PerformCrud for CreateComment {
.map_err(|e| LemmyError::from_error_message(e, "couldnt_like_comment"))?; .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_comment"))?;
let apub_comment: ApubComment = updated_comment.into(); let apub_comment: ApubComment = updated_comment.into();
CreateOrUpdateComment::send( CreateOrUpdateNote::send(
apub_comment.clone(), apub_comment.clone(),
&local_user_view.person.clone().into(), &local_user_view.person.clone().into(),
CreateOrUpdateType::Create, CreateOrUpdateType::Create,

View file

@ -12,7 +12,7 @@ use lemmy_api_common::{
}, },
}; };
use lemmy_apub::protocol::activities::{ use lemmy_apub::protocol::activities::{
create_or_update::comment::CreateOrUpdateComment, create_or_update::note::CreateOrUpdateNote,
CreateOrUpdateType, CreateOrUpdateType,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
@ -115,7 +115,7 @@ impl PerformCrud for EditComment {
.await?; .await?;
// Send the apub update // Send the apub update
CreateOrUpdateComment::send( CreateOrUpdateNote::send(
updated_comment.into(), updated_comment.into(),
&local_user_view.person.into(), &local_user_view.person.into(),
CreateOrUpdateType::Update, CreateOrUpdateType::Update,

View file

@ -15,7 +15,7 @@ use lemmy_api_common::{
use lemmy_apub::{ use lemmy_apub::{
generate_local_apub_endpoint, generate_local_apub_endpoint,
objects::post::ApubPost, objects::post::ApubPost,
protocol::activities::{create_or_update::post::CreateOrUpdatePost, CreateOrUpdateType}, protocol::activities::{create_or_update::page::CreateOrUpdatePage, CreateOrUpdateType},
EndpointType, EndpointType,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
@ -174,7 +174,7 @@ impl PerformCrud for CreatePost {
} }
let apub_post: ApubPost = updated_post.into(); let apub_post: ApubPost = updated_post.into();
CreateOrUpdatePost::send( CreateOrUpdatePage::send(
apub_post.clone(), apub_post.clone(),
&local_user_view.person.clone().into(), &local_user_view.person.clone().into(),
CreateOrUpdateType::Create, CreateOrUpdateType::Create,

View file

@ -11,7 +11,7 @@ use lemmy_api_common::{
}, },
}; };
use lemmy_apub::protocol::activities::{ use lemmy_apub::protocol::activities::{
create_or_update::post::CreateOrUpdatePost, create_or_update::page::CreateOrUpdatePage,
CreateOrUpdateType, CreateOrUpdateType,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
@ -122,7 +122,7 @@ impl PerformCrud for EditPost {
}; };
// Send apub update // Send apub update
CreateOrUpdatePost::send( CreateOrUpdatePage::send(
updated_post.into(), updated_post.into(),
&local_user_view.person.clone().into(), &local_user_view.person.clone().into(),
CreateOrUpdateType::Update, CreateOrUpdateType::Update,

View file

@ -13,7 +13,7 @@ use lemmy_api_common::{
use lemmy_apub::{ use lemmy_apub::{
generate_local_apub_endpoint, generate_local_apub_endpoint,
protocol::activities::{ protocol::activities::{
create_or_update::private_message::CreateOrUpdatePrivateMessage, create_or_update::chat_message::CreateOrUpdateChatMessage,
CreateOrUpdateType, CreateOrUpdateType,
}, },
EndpointType, EndpointType,
@ -85,7 +85,7 @@ impl PerformCrud for CreatePrivateMessage {
.await .await
.map_err(|e| LemmyError::from_error_message(e, "couldnt_create_private_message"))?; .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_private_message"))?;
CreateOrUpdatePrivateMessage::send( CreateOrUpdateChatMessage::send(
updated_private_message.into(), updated_private_message.into(),
&local_user_view.person.into(), &local_user_view.person.into(),
CreateOrUpdateType::Create, CreateOrUpdateType::Create,

View file

@ -5,7 +5,7 @@ use lemmy_api_common::{
utils::{get_local_user_view_from_jwt, local_site_to_slur_regex}, utils::{get_local_user_view_from_jwt, local_site_to_slur_regex},
}; };
use lemmy_apub::protocol::activities::{ use lemmy_apub::protocol::activities::{
create_or_update::private_message::CreateOrUpdatePrivateMessage, create_or_update::chat_message::CreateOrUpdateChatMessage,
CreateOrUpdateType, CreateOrUpdateType,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
@ -56,7 +56,7 @@ impl PerformCrud for EditPrivateMessage {
.map_err(|e| LemmyError::from_error_message(e, "couldnt_update_private_message"))?; .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_private_message"))?;
// Send the apub update // Send the apub update
CreateOrUpdatePrivateMessage::send( CreateOrUpdateChatMessage::send(
updated_private_message.into(), updated_private_message.into(),
&local_user_view.person.into(), &local_user_view.person.into(),
CreateOrUpdateType::Update, CreateOrUpdateType::Update,

View file

@ -7,6 +7,7 @@
"cc": [ "cc": [
"http://enterprise.lemmy.ml/c/main" "http://enterprise.lemmy.ml/c/main"
], ],
"audience": "http://enterprise.lemmy.ml/u/main",
"target": "http://enterprise.lemmy.ml/c/main", "target": "http://enterprise.lemmy.ml/c/main",
"type": "Block", "type": "Block",
"removeData": true, "removeData": true,

View file

@ -12,6 +12,7 @@
"cc": [ "cc": [
"http://enterprise.lemmy.ml/c/main" "http://enterprise.lemmy.ml/c/main"
], ],
"audience": "http://enterprise.lemmy.ml/u/main",
"target": "http://enterprise.lemmy.ml/c/main", "target": "http://enterprise.lemmy.ml/c/main",
"type": "Block", "type": "Block",
"removeData": true, "removeData": true,
@ -22,6 +23,7 @@
"cc": [ "cc": [
"http://enterprise.lemmy.ml/c/main" "http://enterprise.lemmy.ml/c/main"
], ],
"audience": "http://enterprise.lemmy.ml/u/main",
"type": "Undo", "type": "Undo",
"id": "http://enterprise.lemmy.ml/activities/undo/06a20ffb-3e32-42fb-8f4c-674b36d7c557" "id": "http://enterprise.lemmy.ml/activities/undo/06a20ffb-3e32-42fb-8f4c-674b36d7c557"
} }

View file

@ -8,6 +8,7 @@
"cc": [ "cc": [
"http://enterprise.lemmy.ml/c/main" "http://enterprise.lemmy.ml/c/main"
], ],
"audience": "http://enterprise.lemmy.ml/u/main",
"type": "Add", "type": "Add",
"id": "http://enterprise.lemmy.ml/activities/add/ec069147-77c3-447f-88c8-0ef1df10403f" "id": "http://enterprise.lemmy.ml/activities/add/ec069147-77c3-447f-88c8-0ef1df10403f"
} }

View file

@ -9,5 +9,6 @@
], ],
"type": "Remove", "type": "Remove",
"target": "http://enterprise.lemmy.ml/c/main/moderators", "target": "http://enterprise.lemmy.ml/c/main/moderators",
"audience": "http://enterprise.lemmy.ml/u/main",
"id": "http://enterprise.lemmy.ml/activities/remove/aab114f8-cfbd-4935-a5b7-e1a64603650d" "id": "http://enterprise.lemmy.ml/activities/remove/aab114f8-cfbd-4935-a5b7-e1a64603650d"
} }

View file

@ -3,6 +3,7 @@
"to": [ "to": [
"http://enterprise.lemmy.ml/c/main" "http://enterprise.lemmy.ml/c/main"
], ],
"audience": "http://enterprise.lemmy.ml/u/main",
"object": "http://enterprise.lemmy.ml/post/7", "object": "http://enterprise.lemmy.ml/post/7",
"summary": "report this post", "summary": "report this post",
"type": "Flag", "type": "Flag",

View file

@ -43,6 +43,7 @@
"cc": [ "cc": [
"http://enterprise.lemmy.ml/c/main" "http://enterprise.lemmy.ml/c/main"
], ],
"audience": "http://enterprise.lemmy.ml/u/main",
"type": "Update", "type": "Update",
"id": "http://ds9.lemmy.ml/activities/update/d3717cf5-096d-473f-9530-5d52f9d51f5f" "id": "http://ds9.lemmy.ml/activities/update/d3717cf5-096d-473f-9530-5d52f9d51f5f"
} }

View file

@ -10,6 +10,11 @@
"to": [ "to": [
"https://www.w3.org/ns/activitystreams#Public" "https://www.w3.org/ns/activitystreams#Public"
], ],
"cc": [
"http://enterprise.lemmy.ml/c/main",
"http://ds9.lemmy.ml/u/lemmy_alpha"
],
"audience": "http://ds9.lemmy.ml/u/lemmy_alpha",
"content": "hello", "content": "hello",
"mediaType": "text/html", "mediaType": "text/html",
"source": { "source": {
@ -23,6 +28,7 @@
"http://enterprise.lemmy.ml/c/main", "http://enterprise.lemmy.ml/c/main",
"http://ds9.lemmy.ml/u/lemmy_alpha" "http://ds9.lemmy.ml/u/lemmy_alpha"
], ],
"audience": "http://ds9.lemmy.ml/u/lemmy_alpha",
"tag": [ "tag": [
{ {
"href": "http://ds9.lemmy.ml/u/lemmy_alpha", "href": "http://ds9.lemmy.ml/u/lemmy_alpha",

View file

@ -11,6 +11,7 @@
"http://enterprise.lemmy.ml/c/main", "http://enterprise.lemmy.ml/c/main",
"https://www.w3.org/ns/activitystreams#Public" "https://www.w3.org/ns/activitystreams#Public"
], ],
"audience": "https://enterprise.lemmy.ml/c/main",
"name": "test post", "name": "test post",
"content": "<p>test body</p>\n", "content": "<p>test body</p>\n",
"mediaType": "text/html", "mediaType": "text/html",
@ -37,6 +38,7 @@
"cc": [ "cc": [
"http://enterprise.lemmy.ml/c/main" "http://enterprise.lemmy.ml/c/main"
], ],
"audience": "https://enterprise.lemmy.ml/c/main",
"type": "Create", "type": "Create",
"id": "http://ds9.lemmy.ml/activities/create/eee6a57a-622f-464d-b560-73ae1fcd3ddf" "id": "http://ds9.lemmy.ml/activities/create/eee6a57a-622f-464d-b560-73ae1fcd3ddf"
} }

View file

@ -11,6 +11,7 @@
"http://enterprise.lemmy.ml/c/main", "http://enterprise.lemmy.ml/c/main",
"https://www.w3.org/ns/activitystreams#Public" "https://www.w3.org/ns/activitystreams#Public"
], ],
"audience": "https://enterprise.lemmy.ml/c/main",
"name": "test post 1", "name": "test post 1",
"content": "<p>test body</p>\n", "content": "<p>test body</p>\n",
"mediaType": "text/html", "mediaType": "text/html",
@ -34,6 +35,7 @@
"cc": [ "cc": [
"http://enterprise.lemmy.ml/c/main" "http://enterprise.lemmy.ml/c/main"
], ],
"audience": "https://enterprise.lemmy.ml/c/main",
"type": "Update", "type": "Update",
"id": "http://ds9.lemmy.ml/activities/update/ab360117-e165-4de4-b7fc-906b62c98631" "id": "http://ds9.lemmy.ml/activities/update/ab360117-e165-4de4-b7fc-906b62c98631"
} }

View file

@ -7,6 +7,7 @@
"cc": [ "cc": [
"http://enterprise.lemmy.ml/c/main" "http://enterprise.lemmy.ml/c/main"
], ],
"audience": "http://enterprise.lemmy.ml/u/main",
"type": "Delete", "type": "Delete",
"id": "http://ds9.lemmy.ml/activities/delete/f2abee48-c7bb-41d5-9e27-8775ff32db12" "id": "http://ds9.lemmy.ml/activities/delete/f2abee48-c7bb-41d5-9e27-8775ff32db12"
} }

View file

@ -4,9 +4,6 @@
"https://www.w3.org/ns/activitystreams#Public" "https://www.w3.org/ns/activitystreams#Public"
], ],
"object": "http://ds9.lemmy.ml/u/lemmy_alpha", "object": "http://ds9.lemmy.ml/u/lemmy_alpha",
"cc": [
"http://enterprise.lemmy.ml/c/main"
],
"type": "Delete", "type": "Delete",
"id": "http://ds9.lemmy.ml/activities/delete/f2abee48-c7bb-41d5-9e27-8775ff32db12" "id": "http://ds9.lemmy.ml/activities/delete/f2abee48-c7bb-41d5-9e27-8775ff32db12"
} }

View file

@ -7,6 +7,7 @@
"cc": [ "cc": [
"http://enterprise.lemmy.ml/c/main" "http://enterprise.lemmy.ml/c/main"
], ],
"audience": "http://enterprise.lemmy.ml/u/main",
"type": "Delete", "type": "Delete",
"summary": "bad comment", "summary": "bad comment",
"id": "http://enterprise.lemmy.ml/activities/delete/42ca1a79-f99e-4518-a2ca-ba2df221eb5e" "id": "http://enterprise.lemmy.ml/activities/delete/42ca1a79-f99e-4518-a2ca-ba2df221eb5e"

View file

@ -12,12 +12,14 @@
"cc": [ "cc": [
"http://enterprise.lemmy.ml/c/main" "http://enterprise.lemmy.ml/c/main"
], ],
"audience": "http://enterprise.lemmy.ml/u/main",
"type": "Delete", "type": "Delete",
"id": "http://ds9.lemmy.ml/activities/delete/b13cca96-7737-41e1-9769-8fbf972b3509" "id": "http://ds9.lemmy.ml/activities/delete/b13cca96-7737-41e1-9769-8fbf972b3509"
}, },
"cc": [ "cc": [
"http://enterprise.lemmy.ml/c/main" "http://enterprise.lemmy.ml/c/main"
], ],
"audience": "http://enterprise.lemmy.ml/u/main",
"type": "Undo", "type": "Undo",
"id": "http://ds9.lemmy.ml/activities/undo/5e939cfb-b8a1-4de8-950f-9d684e9162b9" "id": "http://ds9.lemmy.ml/activities/undo/5e939cfb-b8a1-4de8-950f-9d684e9162b9"
} }

View file

@ -12,6 +12,7 @@
"cc": [ "cc": [
"http://enterprise.lemmy.ml/c/main" "http://enterprise.lemmy.ml/c/main"
], ],
"audience": "http://enterprise.lemmy.ml/u/main",
"type": "Delete", "type": "Delete",
"summary": "bad comment", "summary": "bad comment",
"id": "http://enterprise.lemmy.ml/activities/delete/2598435c-87a3-49cd-81f3-a44b03b7af9d" "id": "http://enterprise.lemmy.ml/activities/delete/2598435c-87a3-49cd-81f3-a44b03b7af9d"
@ -19,6 +20,7 @@
"cc": [ "cc": [
"http://enterprise.lemmy.ml/c/main" "http://enterprise.lemmy.ml/c/main"
], ],
"audience": "http://enterprise.lemmy.ml/u/main",
"type": "Undo", "type": "Undo",
"id": "http://enterprise.lemmy.ml/activities/undo/a850cf21-3866-4b3a-b80b-56aa00997fee" "id": "http://enterprise.lemmy.ml/activities/undo/a850cf21-3866-4b3a-b80b-56aa00997fee"
} }

View file

@ -1,6 +1,7 @@
{ {
"actor": "http://enterprise.lemmy.ml/u/lemmy_beta", "actor": "http://enterprise.lemmy.ml/u/lemmy_beta",
"object": "http://ds9.lemmy.ml/post/1", "object": "http://ds9.lemmy.ml/post/1",
"audience": "https://enterprise.lemmy.ml/c/tenforward",
"type": "Dislike", "type": "Dislike",
"id": "http://enterprise.lemmy.ml/activities/dislike/64d40d40-a829-43a5-8247-1fb595b3ca1c" "id": "http://enterprise.lemmy.ml/activities/dislike/64d40d40-a829-43a5-8247-1fb595b3ca1c"
} }

View file

@ -1,6 +1,7 @@
{ {
"actor": "http://ds9.lemmy.ml/u/lemmy_alpha", "actor": "http://ds9.lemmy.ml/u/lemmy_alpha",
"object": "http://ds9.lemmy.ml/comment/1", "object": "http://ds9.lemmy.ml/comment/1",
"audience": "https://enterprise.lemmy.ml/c/tenforward",
"type": "Like", "type": "Like",
"id": "http://ds9.lemmy.ml/activities/like/fd61d070-7382-46a9-b2b7-6bb253732877" "id": "http://ds9.lemmy.ml/activities/like/fd61d070-7382-46a9-b2b7-6bb253732877"
} }

View file

@ -3,9 +3,11 @@
"object": { "object": {
"actor": "http://enterprise.lemmy.ml/u/lemmy_beta", "actor": "http://enterprise.lemmy.ml/u/lemmy_beta",
"object": "http://ds9.lemmy.ml/post/1", "object": "http://ds9.lemmy.ml/post/1",
"audience": "https://enterprise.lemmy.ml/c/tenforward",
"type": "Like", "type": "Like",
"id": "http://enterprise.lemmy.ml/activities/like/2227ab2c-79e2-4fca-a1d2-1d67dacf2457" "id": "http://enterprise.lemmy.ml/activities/like/2227ab2c-79e2-4fca-a1d2-1d67dacf2457"
}, },
"audience": "https://enterprise.lemmy.ml/c/tenforward",
"type": "Undo", "type": "Undo",
"id": "http://enterprise.lemmy.ml/activities/undo/6cc6fb71-39fe-49ea-9506-f0423b101e98" "id": "http://enterprise.lemmy.ml/activities/undo/6cc6fb71-39fe-49ea-9506-f0423b101e98"
} }

View file

@ -3,9 +3,11 @@
"object": { "object": {
"actor": "http://ds9.lemmy.ml/u/lemmy_alpha", "actor": "http://ds9.lemmy.ml/u/lemmy_alpha",
"object": "http://ds9.lemmy.ml/comment/1", "object": "http://ds9.lemmy.ml/comment/1",
"audience": "https://enterprise.lemmy.ml/c/tenforward",
"type": "Like", "type": "Like",
"id": "http://ds9.lemmy.ml/activities/like/efcf7ae2-dfcc-4ff4-9ce4-6adf251ff004" "id": "http://ds9.lemmy.ml/activities/like/efcf7ae2-dfcc-4ff4-9ce4-6adf251ff004"
}, },
"audience": "https://enterprise.lemmy.ml/c/tenforward",
"type": "Undo", "type": "Undo",
"id": "http://ds9.lemmy.ml/activities/undo/3518565c-24a7-4d9e-8e0a-f7a2f45ac618" "id": "http://ds9.lemmy.ml/activities/undo/3518565c-24a7-4d9e-8e0a-f7a2f45ac618"
} }

View file

@ -7,6 +7,7 @@
"https://enterprise.lemmy.ml/c/tenforward", "https://enterprise.lemmy.ml/c/tenforward",
"https://enterprise.lemmy.ml/u/picard" "https://enterprise.lemmy.ml/u/picard"
], ],
"audience": "https://enterprise.lemmy.ml/c/tenforward",
"inReplyTo": "https://enterprise.lemmy.ml/post/55143", "inReplyTo": "https://enterprise.lemmy.ml/post/55143",
"content": "<p>first comment!</p>\n", "content": "<p>first comment!</p>\n",
"mediaType": "text/html", "mediaType": "text/html",

View file

@ -6,6 +6,7 @@
"https://enterprise.lemmy.ml/c/tenforward", "https://enterprise.lemmy.ml/c/tenforward",
"https://www.w3.org/ns/activitystreams#Public" "https://www.w3.org/ns/activitystreams#Public"
], ],
"audience": "https://enterprise.lemmy.ml/c/tenforward",
"name": "Post title", "name": "Post title",
"content": "<p>This is a post in the /c/tenforward community</p>\n", "content": "<p>This is a post in the /c/tenforward community</p>\n",
"mediaType": "text/html", "mediaType": "text/html",

View file

@ -1,7 +1,7 @@
use crate::{ use crate::{
activities::{ activities::{
block::{generate_cc, SiteOrCommunity}, block::{generate_cc, SiteOrCommunity},
community::{announce::GetCommunity, send_activity_in_community}, community::send_activity_in_community,
generate_activity_id, generate_activity_id,
send_lemmy_activity, send_lemmy_activity,
verify_is_public, verify_is_public,
@ -10,7 +10,7 @@ use crate::{
}, },
activity_lists::AnnouncableActivities, activity_lists::AnnouncableActivities,
local_instance, local_instance,
objects::{community::ApubCommunity, instance::remote_instance_inboxes, person::ApubPerson}, objects::{instance::remote_instance_inboxes, person::ApubPerson},
protocol::activities::block::block_user::BlockUser, protocol::activities::block::block_user::BlockUser,
ActorType, ActorType,
}; };
@ -51,6 +51,11 @@ impl BlockUser {
expires: Option<NaiveDateTime>, expires: Option<NaiveDateTime>,
context: &LemmyContext, context: &LemmyContext,
) -> Result<BlockUser, LemmyError> { ) -> Result<BlockUser, LemmyError> {
let audience = if let SiteOrCommunity::Community(c) = target {
Some(ObjectId::new(c.actor_id()))
} else {
None
};
Ok(BlockUser { Ok(BlockUser {
actor: ObjectId::new(mod_.actor_id()), actor: ObjectId::new(mod_.actor_id()),
to: vec![public()], to: vec![public()],
@ -64,6 +69,7 @@ impl BlockUser {
BlockType::Block, BlockType::Block,
&context.settings().get_protocol_and_hostname(), &context.settings().get_protocol_and_hostname(),
)?, )?,
audience,
expires: expires.map(convert_datetime), expires: expires.map(convert_datetime),
}) })
} }
@ -242,22 +248,3 @@ impl ActivityHandler for BlockUser {
Ok(()) Ok(())
} }
} }
#[async_trait::async_trait(?Send)]
impl GetCommunity for BlockUser {
#[tracing::instrument(skip_all)]
async fn get_community(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError> {
let target = self
.target
.dereference(context, local_instance(context).await, request_counter)
.await?;
match target {
SiteOrCommunity::Community(c) => Ok(c),
SiteOrCommunity::Site(_) => Err(anyhow!("Calling get_community() on site activity").into()),
}
}
}

View file

@ -1,14 +1,14 @@
use crate::{ use crate::{
activities::{ activities::{
block::{generate_cc, SiteOrCommunity}, block::{generate_cc, SiteOrCommunity},
community::{announce::GetCommunity, send_activity_in_community}, community::send_activity_in_community,
generate_activity_id, generate_activity_id,
send_lemmy_activity, send_lemmy_activity,
verify_is_public, verify_is_public,
}, },
activity_lists::AnnouncableActivities, activity_lists::AnnouncableActivities,
local_instance, local_instance,
objects::{community::ApubCommunity, instance::remote_instance_inboxes, person::ApubPerson}, objects::{instance::remote_instance_inboxes, person::ApubPerson},
protocol::activities::block::{block_user::BlockUser, undo_block_user::UndoBlockUser}, protocol::activities::block::{block_user::BlockUser, undo_block_user::UndoBlockUser},
ActorType, ActorType,
}; };
@ -41,6 +41,11 @@ impl UndoBlockUser {
context: &LemmyContext, context: &LemmyContext,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let block = BlockUser::new(target, user, mod_, None, reason, None, context).await?; let block = BlockUser::new(target, user, mod_, None, reason, None, context).await?;
let audience = if let SiteOrCommunity::Community(c) = target {
Some(ObjectId::new(c.actor_id()))
} else {
None
};
let id = generate_activity_id( let id = generate_activity_id(
UndoType::Undo, UndoType::Undo,
@ -53,6 +58,7 @@ impl UndoBlockUser {
cc: generate_cc(target, context.pool()).await?, cc: generate_cc(target, context.pool()).await?,
kind: UndoType::Undo, kind: UndoType::Undo,
id: id.clone(), id: id.clone(),
audience,
}; };
let mut inboxes = vec![user.shared_inbox_or_inbox()]; let mut inboxes = vec![user.shared_inbox_or_inbox()];
@ -162,15 +168,3 @@ impl ActivityHandler for UndoBlockUser {
Ok(()) Ok(())
} }
} }
#[async_trait::async_trait(?Send)]
impl GetCommunity for UndoBlockUser {
#[tracing::instrument(skip_all)]
async fn get_community(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError> {
self.object.get_community(context, request_counter).await
}
}

View file

@ -1,10 +1,6 @@
use crate::{ use crate::{
activities::{ activities::{
community::{ community::send_activity_in_community,
announce::GetCommunity,
get_community_from_moderators_url,
send_activity_in_community,
},
generate_activity_id, generate_activity_id,
verify_add_remove_moderator_target, verify_add_remove_moderator_target,
verify_is_public, verify_is_public,
@ -15,7 +11,7 @@ use crate::{
generate_moderators_url, generate_moderators_url,
local_instance, local_instance,
objects::{community::ApubCommunity, person::ApubPerson}, objects::{community::ApubCommunity, person::ApubPerson},
protocol::activities::community::add_mod::AddMod, protocol::{activities::community::add_mod::AddMod, InCommunity},
ActorType, ActorType,
}; };
use activitypub_federation::{ use activitypub_federation::{
@ -55,6 +51,7 @@ impl AddMod {
cc: vec![community.actor_id()], cc: vec![community.actor_id()],
kind: AddType::Add, kind: AddType::Add,
id: id.clone(), id: id.clone(),
audience: Some(ObjectId::new(community.actor_id())),
}; };
let activity = AnnouncableActivities::AddMod(add); let activity = AnnouncableActivities::AddMod(add);
@ -83,7 +80,7 @@ impl ActivityHandler for AddMod {
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
verify_is_public(&self.to, &self.cc)?; verify_is_public(&self.to, &self.cc)?;
let community = self.get_community(context, request_counter).await?; let community = self.community(context, request_counter).await?;
verify_person_in_community(&self.actor, &community, context, request_counter).await?; verify_person_in_community(&self.actor, &community, context, request_counter).await?;
verify_mod_action( verify_mod_action(
&self.actor, &self.actor,
@ -103,7 +100,7 @@ impl ActivityHandler for AddMod {
context: &Data<LemmyContext>, context: &Data<LemmyContext>,
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let community = self.get_community(context, request_counter).await?; let community = self.community(context, request_counter).await?;
let new_mod = self let new_mod = self
.object .object
.dereference(context, local_instance(context).await, request_counter) .dereference(context, local_instance(context).await, request_counter)
@ -138,15 +135,3 @@ impl ActivityHandler for AddMod {
Ok(()) Ok(())
} }
} }
#[async_trait::async_trait(?Send)]
impl GetCommunity for AddMod {
#[tracing::instrument(skip_all)]
async fn get_community(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError> {
get_community_from_moderators_url(&self.target, context, request_counter).await
}
}

View file

@ -12,6 +12,7 @@ use crate::{
activities::community::announce::{AnnounceActivity, RawAnnouncableActivities}, activities::community::announce::{AnnounceActivity, RawAnnouncableActivities},
Id, Id,
IdOrNestedObject, IdOrNestedObject,
InCommunity,
}, },
ActorType, ActorType,
}; };
@ -56,7 +57,7 @@ impl ActivityHandler for RawAnnouncableActivities {
if let AnnouncableActivities::Page(_) = activity { if let AnnouncableActivities::Page(_) = activity {
return Err(LemmyError::from_message("Cant receive page")); return Err(LemmyError::from_message("Cant receive page"));
} }
let community = activity.get_community(data, &mut 0).await?; let community = activity.community(data, &mut 0).await?;
let actor_id = ObjectId::new(activity.actor().clone()); let actor_id = ObjectId::new(activity.actor().clone());
// verify and receive activity // verify and receive activity
@ -72,15 +73,6 @@ impl ActivityHandler for RawAnnouncableActivities {
} }
} }
#[async_trait::async_trait(?Send)]
pub(crate) trait GetCommunity {
async fn get_community(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError>;
}
impl AnnounceActivity { impl AnnounceActivity {
pub(crate) fn new( pub(crate) fn new(
object: RawAnnouncableActivities, object: RawAnnouncableActivities,

View file

@ -64,7 +64,7 @@ pub(crate) async fn send_activity_in_community(
} }
#[tracing::instrument(skip_all)] #[tracing::instrument(skip_all)]
async fn get_community_from_moderators_url( pub(crate) async fn get_community_from_moderators_url(
moderators: &Url, moderators: &Url,
context: &LemmyContext, context: &LemmyContext,
request_counter: &mut i32, request_counter: &mut i32,

View file

@ -1,10 +1,6 @@
use crate::{ use crate::{
activities::{ activities::{
community::{ community::send_activity_in_community,
announce::GetCommunity,
get_community_from_moderators_url,
send_activity_in_community,
},
generate_activity_id, generate_activity_id,
verify_add_remove_moderator_target, verify_add_remove_moderator_target,
verify_is_public, verify_is_public,
@ -15,7 +11,7 @@ use crate::{
generate_moderators_url, generate_moderators_url,
local_instance, local_instance,
objects::{community::ApubCommunity, person::ApubPerson}, objects::{community::ApubCommunity, person::ApubPerson},
protocol::activities::community::remove_mod::RemoveMod, protocol::{activities::community::remove_mod::RemoveMod, InCommunity},
ActorType, ActorType,
}; };
use activitypub_federation::{ use activitypub_federation::{
@ -55,6 +51,7 @@ impl RemoveMod {
id: id.clone(), id: id.clone(),
cc: vec![community.actor_id()], cc: vec![community.actor_id()],
kind: RemoveType::Remove, kind: RemoveType::Remove,
audience: Some(ObjectId::new(community.actor_id())),
}; };
let activity = AnnouncableActivities::RemoveMod(remove); let activity = AnnouncableActivities::RemoveMod(remove);
@ -83,7 +80,7 @@ impl ActivityHandler for RemoveMod {
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
verify_is_public(&self.to, &self.cc)?; verify_is_public(&self.to, &self.cc)?;
let community = self.get_community(context, request_counter).await?; let community = self.community(context, request_counter).await?;
verify_person_in_community(&self.actor, &community, context, request_counter).await?; verify_person_in_community(&self.actor, &community, context, request_counter).await?;
verify_mod_action( verify_mod_action(
&self.actor, &self.actor,
@ -103,7 +100,7 @@ impl ActivityHandler for RemoveMod {
context: &Data<LemmyContext>, context: &Data<LemmyContext>,
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let community = self.get_community(context, request_counter).await?; let community = self.community(context, request_counter).await?;
let remove_mod = self let remove_mod = self
.object .object
.dereference(context, local_instance(context).await, request_counter) .dereference(context, local_instance(context).await, request_counter)
@ -132,15 +129,3 @@ impl ActivityHandler for RemoveMod {
Ok(()) Ok(())
} }
} }
#[async_trait::async_trait(?Send)]
impl GetCommunity for RemoveMod {
#[tracing::instrument(skip_all)]
async fn get_community(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError> {
get_community_from_moderators_url(&self.target, context, request_counter).await
}
}

View file

@ -2,7 +2,7 @@ use crate::{
activities::{generate_activity_id, send_lemmy_activity, verify_person_in_community}, activities::{generate_activity_id, send_lemmy_activity, verify_person_in_community},
local_instance, local_instance,
objects::{community::ApubCommunity, person::ApubPerson}, objects::{community::ApubCommunity, person::ApubPerson},
protocol::activities::community::report::Report, protocol::{activities::community::report::Report, InCommunity},
ActorType, ActorType,
PostOrComment, PostOrComment,
}; };
@ -47,6 +47,7 @@ impl Report {
summary: reason, summary: reason,
kind, kind,
id: id.clone(), id: id.clone(),
audience: Some(ObjectId::new(community.actor_id())),
}; };
let inbox = vec![community.shared_inbox_or_inbox()]; let inbox = vec![community.shared_inbox_or_inbox()];
@ -73,9 +74,7 @@ impl ActivityHandler for Report {
context: &Data<LemmyContext>, context: &Data<LemmyContext>,
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let community = self.to[0] let community = self.community(context, request_counter).await?;
.dereference(context, local_instance(context).await, request_counter)
.await?;
verify_person_in_community(&self.actor, &community, context, request_counter).await?; verify_person_in_community(&self.actor, &community, context, request_counter).await?;
Ok(()) Ok(())
} }

View file

@ -1,15 +1,14 @@
use crate::{ use crate::{
activities::{ activities::{
community::{announce::GetCommunity, send_activity_in_community}, community::send_activity_in_community,
generate_activity_id, generate_activity_id,
verify_is_public, verify_is_public,
verify_mod_action, verify_mod_action,
verify_person_in_community, verify_person_in_community,
}, },
activity_lists::AnnouncableActivities, activity_lists::AnnouncableActivities,
local_instance,
objects::{community::ApubCommunity, person::ApubPerson}, objects::{community::ApubCommunity, person::ApubPerson},
protocol::activities::community::update::UpdateCommunity, protocol::{activities::community::update::UpdateCommunity, InCommunity},
ActorType, ActorType,
}; };
use activitypub_federation::{ use activitypub_federation::{
@ -41,6 +40,7 @@ impl UpdateCommunity {
cc: vec![community.actor_id()], cc: vec![community.actor_id()],
kind: UpdateType::Update, kind: UpdateType::Update,
id: id.clone(), id: id.clone(),
audience: Some(ObjectId::new(community.actor_id())),
}; };
let activity = AnnouncableActivities::UpdateCommunity(update); let activity = AnnouncableActivities::UpdateCommunity(update);
@ -68,7 +68,7 @@ impl ActivityHandler for UpdateCommunity {
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
verify_is_public(&self.to, &self.cc)?; verify_is_public(&self.to, &self.cc)?;
let community = self.get_community(context, request_counter).await?; let community = self.community(context, request_counter).await?;
verify_person_in_community(&self.actor, &community, context, request_counter).await?; verify_person_in_community(&self.actor, &community, context, request_counter).await?;
verify_mod_action( verify_mod_action(
&self.actor, &self.actor,
@ -94,7 +94,7 @@ impl ActivityHandler for UpdateCommunity {
context: &Data<LemmyContext>, context: &Data<LemmyContext>,
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let community = self.get_community(context, request_counter).await?; let community = self.community(context, request_counter).await?;
let community_update_form = self.object.into_update_form(); let community_update_form = self.object.into_update_form();
@ -112,18 +112,3 @@ impl ActivityHandler for UpdateCommunity {
Ok(()) Ok(())
} }
} }
#[async_trait::async_trait(?Send)]
impl GetCommunity for UpdateCommunity {
#[tracing::instrument(skip_all)]
async fn get_community(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError> {
let cid = ObjectId::new(self.object.id.clone());
cid
.dereference(context, local_instance(context).await, request_counter)
.await
}
}

View file

@ -1,7 +1,7 @@
use crate::{ use crate::{
activities::{ activities::{
check_community_deleted_or_removed, check_community_deleted_or_removed,
community::{announce::GetCommunity, send_activity_in_community}, community::send_activity_in_community,
create_or_update::get_comment_notif_recipients, create_or_update::get_comment_notif_recipients,
generate_activity_id, generate_activity_id,
verify_is_public, verify_is_public,
@ -11,7 +11,10 @@ use crate::{
local_instance, local_instance,
mentions::MentionOrValue, mentions::MentionOrValue,
objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson}, objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson},
protocol::activities::{create_or_update::comment::CreateOrUpdateComment, CreateOrUpdateType}, protocol::{
activities::{create_or_update::note::CreateOrUpdateNote, CreateOrUpdateType},
InCommunity,
},
ActorType, ActorType,
}; };
use activitypub_federation::{ use activitypub_federation::{
@ -34,7 +37,7 @@ use lemmy_utils::error::LemmyError;
use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperationCrud}; use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperationCrud};
use url::Url; use url::Url;
impl CreateOrUpdateComment { impl CreateOrUpdateNote {
#[tracing::instrument(skip(comment, actor, kind, context))] #[tracing::instrument(skip(comment, actor, kind, context))]
pub async fn send( pub async fn send(
comment: ApubComment, comment: ApubComment,
@ -55,7 +58,7 @@ impl CreateOrUpdateComment {
)?; )?;
let note = comment.into_apub(context).await?; let note = comment.into_apub(context).await?;
let create_or_update = CreateOrUpdateComment { let create_or_update = CreateOrUpdateNote {
actor: ObjectId::new(actor.actor_id()), actor: ObjectId::new(actor.actor_id()),
to: vec![public()], to: vec![public()],
cc: note.cc.clone(), cc: note.cc.clone(),
@ -63,6 +66,7 @@ impl CreateOrUpdateComment {
object: note, object: note,
kind, kind,
id: id.clone(), id: id.clone(),
audience: Some(ObjectId::new(community.actor_id())),
}; };
let tagged_users: Vec<ObjectId<ApubPerson>> = create_or_update let tagged_users: Vec<ObjectId<ApubPerson>> = create_or_update
@ -92,7 +96,7 @@ impl CreateOrUpdateComment {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl ActivityHandler for CreateOrUpdateComment { impl ActivityHandler for CreateOrUpdateNote {
type DataType = LemmyContext; type DataType = LemmyContext;
type Error = LemmyError; type Error = LemmyError;
@ -112,7 +116,7 @@ impl ActivityHandler for CreateOrUpdateComment {
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
verify_is_public(&self.to, &self.cc)?; verify_is_public(&self.to, &self.cc)?;
let post = self.object.get_parents(context, request_counter).await?.0; let post = self.object.get_parents(context, request_counter).await?.0;
let community = self.get_community(context, request_counter).await?; let community = self.community(context, request_counter).await?;
verify_person_in_community(&self.actor, &community, context, request_counter).await?; verify_person_in_community(&self.actor, &community, context, request_counter).await?;
verify_domains_match(self.actor.inner(), self.object.id.inner())?; verify_domains_match(self.actor.inner(), self.object.id.inner())?;
@ -160,17 +164,3 @@ impl ActivityHandler for CreateOrUpdateComment {
Ok(()) Ok(())
} }
} }
#[async_trait::async_trait(?Send)]
impl GetCommunity for CreateOrUpdateComment {
#[tracing::instrument(skip_all)]
async fn get_community(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError> {
let post = self.object.get_parents(context, request_counter).await?.0;
let community = Community::read(context.pool(), post.community_id).await?;
Ok(community.into())
}
}

View file

@ -1,7 +1,7 @@
use crate::{ use crate::{
activities::{ activities::{
check_community_deleted_or_removed, check_community_deleted_or_removed,
community::{announce::GetCommunity, send_activity_in_community}, community::send_activity_in_community,
generate_activity_id, generate_activity_id,
verify_is_public, verify_is_public,
verify_mod_action, verify_mod_action,
@ -9,7 +9,10 @@ use crate::{
}, },
activity_lists::AnnouncableActivities, activity_lists::AnnouncableActivities,
objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost}, objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost},
protocol::activities::{create_or_update::post::CreateOrUpdatePost, CreateOrUpdateType}, protocol::{
activities::{create_or_update::page::CreateOrUpdatePage, CreateOrUpdateType},
InCommunity,
},
ActorType, ActorType,
}; };
use activitypub_federation::{ use activitypub_federation::{
@ -30,25 +33,26 @@ use lemmy_utils::error::LemmyError;
use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud}; use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
use url::Url; use url::Url;
impl CreateOrUpdatePost { impl CreateOrUpdatePage {
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,
context: &LemmyContext, context: &LemmyContext,
) -> Result<CreateOrUpdatePost, LemmyError> { ) -> Result<CreateOrUpdatePage, LemmyError> {
let id = generate_activity_id( let id = generate_activity_id(
kind.clone(), kind.clone(),
&context.settings().get_protocol_and_hostname(), &context.settings().get_protocol_and_hostname(),
)?; )?;
Ok(CreateOrUpdatePost { Ok(CreateOrUpdatePage {
actor: ObjectId::new(actor.actor_id()), actor: ObjectId::new(actor.actor_id()),
to: vec![public()], to: vec![public()],
object: post.into_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(),
audience: Some(ObjectId::new(community.actor_id())),
}) })
} }
@ -62,7 +66,7 @@ impl CreateOrUpdatePost {
let community_id = post.community_id; let community_id = post.community_id;
let community: ApubCommunity = Community::read(context.pool(), community_id).await?.into(); let community: ApubCommunity = Community::read(context.pool(), community_id).await?.into();
let create_or_update = CreateOrUpdatePost::new(post, actor, &community, kind, context).await?; let create_or_update = CreateOrUpdatePage::new(post, actor, &community, kind, context).await?;
let is_mod_action = create_or_update.object.is_mod_action(context).await?; let is_mod_action = create_or_update.object.is_mod_action(context).await?;
let activity = AnnouncableActivities::CreateOrUpdatePost(create_or_update); let activity = AnnouncableActivities::CreateOrUpdatePost(create_or_update);
send_activity_in_community(activity, actor, &community, vec![], is_mod_action, context).await?; send_activity_in_community(activity, actor, &community, vec![], is_mod_action, context).await?;
@ -71,7 +75,7 @@ impl CreateOrUpdatePost {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl ActivityHandler for CreateOrUpdatePost { impl ActivityHandler for CreateOrUpdatePage {
type DataType = LemmyContext; type DataType = LemmyContext;
type Error = LemmyError; type Error = LemmyError;
@ -90,7 +94,7 @@ impl ActivityHandler for CreateOrUpdatePost {
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
verify_is_public(&self.to, &self.cc)?; verify_is_public(&self.to, &self.cc)?;
let community = self.get_community(context, request_counter).await?; let community = self.community(context, request_counter).await?;
verify_person_in_community(&self.actor, &community, context, request_counter).await?; verify_person_in_community(&self.actor, &community, context, request_counter).await?;
check_community_deleted_or_removed(&community)?; check_community_deleted_or_removed(&community)?;
@ -155,18 +159,3 @@ impl ActivityHandler for CreateOrUpdatePost {
Ok(()) Ok(())
} }
} }
#[async_trait::async_trait(?Send)]
impl GetCommunity for CreateOrUpdatePost {
#[tracing::instrument(skip_all)]
async fn get_community(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError> {
self
.object
.extract_community(context, request_counter)
.await
}
}

View file

@ -2,7 +2,7 @@ use crate::{
activities::{generate_activity_id, send_lemmy_activity, verify_person}, activities::{generate_activity_id, send_lemmy_activity, verify_person},
objects::{person::ApubPerson, private_message::ApubPrivateMessage}, objects::{person::ApubPerson, private_message::ApubPrivateMessage},
protocol::activities::{ protocol::activities::{
create_or_update::private_message::CreateOrUpdatePrivateMessage, create_or_update::chat_message::CreateOrUpdateChatMessage,
CreateOrUpdateType, CreateOrUpdateType,
}, },
ActorType, ActorType,
@ -18,7 +18,7 @@ use lemmy_utils::error::LemmyError;
use lemmy_websocket::{send::send_pm_ws_message, LemmyContext, UserOperationCrud}; use lemmy_websocket::{send::send_pm_ws_message, LemmyContext, UserOperationCrud};
use url::Url; use url::Url;
impl CreateOrUpdatePrivateMessage { impl CreateOrUpdateChatMessage {
#[tracing::instrument(skip_all)] #[tracing::instrument(skip_all)]
pub async fn send( pub async fn send(
private_message: ApubPrivateMessage, private_message: ApubPrivateMessage,
@ -33,7 +33,7 @@ impl CreateOrUpdatePrivateMessage {
kind.clone(), kind.clone(),
&context.settings().get_protocol_and_hostname(), &context.settings().get_protocol_and_hostname(),
)?; )?;
let create_or_update = CreateOrUpdatePrivateMessage { let create_or_update = CreateOrUpdateChatMessage {
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())],
@ -46,7 +46,7 @@ impl CreateOrUpdatePrivateMessage {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl ActivityHandler for CreateOrUpdatePrivateMessage { impl ActivityHandler for CreateOrUpdateChatMessage {
type DataType = LemmyContext; type DataType = LemmyContext;
type Error = LemmyError; type Error = LemmyError;

View file

@ -1,6 +1,5 @@
use crate::{ use crate::{
activities::{ activities::{
community::announce::GetCommunity,
deletion::{receive_delete_action, verify_delete_activity, DeletableObjects}, deletion::{receive_delete_action, verify_delete_activity, DeletableObjects},
generate_activity_id, generate_activity_id,
}, },
@ -10,7 +9,6 @@ use crate::{
}; };
use activitypub_federation::{core::object_id::ObjectId, data::Data, traits::ActivityHandler}; use activitypub_federation::{core::object_id::ObjectId, data::Data, traits::ActivityHandler};
use activitystreams_kinds::activity::DeleteType; use activitystreams_kinds::activity::DeleteType;
use anyhow::anyhow;
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
comment::{Comment, CommentUpdateForm}, comment::{Comment, CommentUpdateForm},
@ -117,6 +115,7 @@ impl Delete {
kind: DeleteType::Delete, kind: DeleteType::Delete,
summary, summary,
id, id,
audience: community.map(|c| ObjectId::<ApubCommunity>::new(c.actor_id.clone())),
}) })
} }
} }
@ -191,27 +190,3 @@ pub(in crate::activities) async fn receive_remove_action(
} }
Ok(()) Ok(())
} }
#[async_trait::async_trait(?Send)]
impl GetCommunity for Delete {
#[tracing::instrument(skip_all)]
async fn get_community(
&self,
context: &LemmyContext,
_request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError> {
let community_id = match DeletableObjects::read_from_db(self.object.id(), context).await? {
DeletableObjects::Community(c) => c.id,
DeletableObjects::Comment(c) => {
let post = Post::read(context.pool(), c.post_id).await?;
post.community_id
}
DeletableObjects::Post(p) => p.community_id,
DeletableObjects::PrivateMessage(_) => {
return Err(anyhow!("Private message is not part of community").into())
}
};
let community = Community::read(context.pool(), community_id).await?;
Ok(community.into())
}
}

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
activities::{ activities::{
community::{announce::GetCommunity, send_activity_in_community}, community::send_activity_in_community,
send_lemmy_activity, send_lemmy_activity,
verify_is_public, verify_is_public,
verify_mod_action, verify_mod_action,
@ -16,7 +16,10 @@ use crate::{
post::ApubPost, post::ApubPost,
private_message::ApubPrivateMessage, private_message::ApubPrivateMessage,
}, },
protocol::activities::deletion::{delete::Delete, undo_delete::UndoDelete}, protocol::{
activities::deletion::{delete::Delete, undo_delete::UndoDelete},
InCommunity,
},
ActorType, ActorType,
}; };
use activitypub_federation::{ use activitypub_federation::{
@ -175,7 +178,7 @@ pub(in crate::activities) async fn verify_delete_activity(
verify_delete_post_or_comment( verify_delete_post_or_comment(
&activity.actor, &activity.actor,
&p.ap_id.clone().into(), &p.ap_id.clone().into(),
&activity.get_community(context, request_counter).await?, &activity.community(context, request_counter).await?,
is_mod_action, is_mod_action,
context, context,
request_counter, request_counter,
@ -187,7 +190,7 @@ pub(in crate::activities) async fn verify_delete_activity(
verify_delete_post_or_comment( verify_delete_post_or_comment(
&activity.actor, &activity.actor,
&c.ap_id.clone().into(), &c.ap_id.clone().into(),
&activity.get_community(context, request_counter).await?, &activity.community(context, request_counter).await?,
is_mod_action, is_mod_action,
context, context,
request_counter, request_counter,

View file

@ -1,6 +1,5 @@
use crate::{ use crate::{
activities::{ activities::{
community::announce::GetCommunity,
deletion::{receive_delete_action, verify_delete_activity, DeletableObjects}, deletion::{receive_delete_action, verify_delete_activity, DeletableObjects},
generate_activity_id, generate_activity_id,
}, },
@ -117,6 +116,7 @@ impl UndoDelete {
cc: cc.into_iter().collect(), cc: cc.into_iter().collect(),
kind: UndoType::Undo, kind: UndoType::Undo,
id, id,
audience: community.map(|c| ObjectId::<ApubCommunity>::new(c.actor_id.clone())),
}) })
} }
@ -187,15 +187,3 @@ impl UndoDelete {
Ok(()) Ok(())
} }
} }
#[async_trait::async_trait(?Send)]
impl GetCommunity for UndoDelete {
#[tracing::instrument(skip_all)]
async fn get_community(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError> {
self.object.get_community(context, request_counter).await
}
}

View file

@ -130,6 +130,16 @@ pub(crate) fn verify_is_public(to: &[Url], cc: &[Url]) -> Result<(), LemmyError>
Ok(()) Ok(())
} }
pub(crate) fn verify_community_matches(
a: &ApubCommunity,
b: CommunityId,
) -> Result<(), LemmyError> {
if a.id != b {
return Err(LemmyError::from_message("Invalid community"));
}
Ok(())
}
pub(crate) fn check_community_deleted_or_removed(community: &Community) -> Result<(), LemmyError> { pub(crate) fn check_community_deleted_or_removed(community: &Community) -> Result<(), LemmyError> {
if community.deleted || community.removed { if community.deleted || community.removed {
Err(LemmyError::from_message( Err(LemmyError::from_message(

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
activities::{ activities::{
community::{announce::GetCommunity, send_activity_in_community}, community::send_activity_in_community,
generate_activity_id, generate_activity_id,
verify_person_in_community, verify_person_in_community,
voting::{undo_vote_comment, undo_vote_post}, voting::{undo_vote_comment, undo_vote_post},
@ -8,9 +8,12 @@ use crate::{
activity_lists::AnnouncableActivities, activity_lists::AnnouncableActivities,
local_instance, local_instance,
objects::{community::ApubCommunity, person::ApubPerson}, objects::{community::ApubCommunity, person::ApubPerson},
protocol::activities::voting::{ protocol::{
undo_vote::UndoVote, activities::voting::{
vote::{Vote, VoteType}, undo_vote::UndoVote,
vote::{Vote, VoteType},
},
InCommunity,
}, },
ActorType, ActorType,
PostOrComment, PostOrComment,
@ -41,7 +44,7 @@ impl UndoVote {
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let community: ApubCommunity = Community::read(context.pool(), community_id).await?.into(); let community: ApubCommunity = Community::read(context.pool(), community_id).await?.into();
let object = Vote::new(object, actor, kind.clone(), context)?; let object = Vote::new(object, actor, &community, kind.clone(), context)?;
let id = generate_activity_id( let id = generate_activity_id(
UndoType::Undo, UndoType::Undo,
&context.settings().get_protocol_and_hostname(), &context.settings().get_protocol_and_hostname(),
@ -51,6 +54,7 @@ impl UndoVote {
object, object,
kind: UndoType::Undo, kind: UndoType::Undo,
id: id.clone(), id: id.clone(),
audience: Some(ObjectId::new(community.actor_id())),
}; };
let activity = AnnouncableActivities::UndoVote(undo_vote); let activity = AnnouncableActivities::UndoVote(undo_vote);
send_activity_in_community(activity, actor, &community, vec![], false, context).await send_activity_in_community(activity, actor, &community, vec![], false, context).await
@ -76,7 +80,7 @@ impl ActivityHandler for UndoVote {
context: &Data<LemmyContext>, context: &Data<LemmyContext>,
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let community = self.get_community(context, request_counter).await?; let community = self.community(context, request_counter).await?;
verify_person_in_community(&self.actor, &community, context, request_counter).await?; verify_person_in_community(&self.actor, &community, context, request_counter).await?;
verify_urls_match(self.actor.inner(), self.object.actor.inner())?; verify_urls_match(self.actor.inner(), self.object.actor.inner())?;
self.object.verify(context, request_counter).await?; self.object.verify(context, request_counter).await?;
@ -104,15 +108,3 @@ impl ActivityHandler for UndoVote {
} }
} }
} }
#[async_trait::async_trait(?Send)]
impl GetCommunity for UndoVote {
#[tracing::instrument(skip_all)]
async fn get_community(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError> {
self.object.get_community(context, request_counter).await
}
}

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
activities::{ activities::{
community::{announce::GetCommunity, send_activity_in_community}, community::send_activity_in_community,
generate_activity_id, generate_activity_id,
verify_person_in_community, verify_person_in_community,
voting::{vote_comment, vote_post}, voting::{vote_comment, vote_post},
@ -8,7 +8,10 @@ use crate::{
activity_lists::AnnouncableActivities, activity_lists::AnnouncableActivities,
local_instance, local_instance,
objects::{community::ApubCommunity, person::ApubPerson}, objects::{community::ApubCommunity, person::ApubPerson},
protocol::activities::voting::vote::{Vote, VoteType}, protocol::{
activities::voting::vote::{Vote, VoteType},
InCommunity,
},
ActorType, ActorType,
PostOrComment, PostOrComment,
}; };
@ -16,7 +19,7 @@ use activitypub_federation::{core::object_id::ObjectId, data::Data, traits::Acti
use anyhow::anyhow; use anyhow::anyhow;
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::CommunityId, newtypes::CommunityId,
source::{community::Community, local_site::LocalSite, post::Post}, source::{community::Community, local_site::LocalSite},
traits::Crud, traits::Crud,
}; };
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
@ -29,6 +32,7 @@ impl Vote {
pub(in crate::activities::voting) fn new( pub(in crate::activities::voting) fn new(
object: &PostOrComment, object: &PostOrComment,
actor: &ApubPerson, actor: &ApubPerson,
community: &ApubCommunity,
kind: VoteType, kind: VoteType,
context: &LemmyContext, context: &LemmyContext,
) -> Result<Vote, LemmyError> { ) -> Result<Vote, LemmyError> {
@ -37,6 +41,7 @@ impl Vote {
object: ObjectId::new(object.ap_id()), object: ObjectId::new(object.ap_id()),
kind: kind.clone(), kind: kind.clone(),
id: generate_activity_id(kind, &context.settings().get_protocol_and_hostname())?, id: generate_activity_id(kind, &context.settings().get_protocol_and_hostname())?,
audience: Some(ObjectId::new(community.actor_id())),
}) })
} }
@ -49,7 +54,7 @@ impl Vote {
context: &LemmyContext, context: &LemmyContext,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let community = Community::read(context.pool(), community_id).await?.into(); let community = Community::read(context.pool(), community_id).await?.into();
let vote = Vote::new(object, actor, kind, context)?; let vote = Vote::new(object, actor, &community, kind, context)?;
let activity = AnnouncableActivities::Vote(vote); let activity = AnnouncableActivities::Vote(vote);
send_activity_in_community(activity, actor, &community, vec![], false, context).await send_activity_in_community(activity, actor, &community, vec![], false, context).await
@ -75,7 +80,7 @@ impl ActivityHandler for Vote {
context: &Data<LemmyContext>, context: &Data<LemmyContext>,
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let community = self.get_community(context, request_counter).await?; let community = self.community(context, request_counter).await?;
verify_person_in_community(&self.actor, &community, context, request_counter).await?; verify_person_in_community(&self.actor, &community, context, request_counter).await?;
let enable_downvotes = LocalSite::read(context.pool()) let enable_downvotes = LocalSite::read(context.pool())
.await .await
@ -107,24 +112,3 @@ impl ActivityHandler for Vote {
} }
} }
} }
#[async_trait::async_trait(?Send)]
impl GetCommunity for Vote {
#[tracing::instrument(skip_all)]
async fn get_community(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError> {
let object = self
.object
.dereference(context, local_instance(context).await, request_counter)
.await?;
let cid = match object {
PostOrComment::Post(p) => p.community_id,
PostOrComment::Comment(c) => Post::read(context.pool(), c.post_id).await?.community_id,
};
let community = Community::read(context.pool(), cid).await?;
Ok(community.into())
}
}

View file

@ -1,5 +1,4 @@
use crate::{ use crate::{
activities::community::announce::GetCommunity,
objects::community::ApubCommunity, objects::community::ApubCommunity,
protocol::{ protocol::{
activities::{ activities::{
@ -12,15 +11,16 @@ use crate::{
update::UpdateCommunity, update::UpdateCommunity,
}, },
create_or_update::{ create_or_update::{
comment::CreateOrUpdateComment, chat_message::CreateOrUpdateChatMessage,
post::CreateOrUpdatePost, note::CreateOrUpdateNote,
private_message::CreateOrUpdatePrivateMessage, page::CreateOrUpdatePage,
}, },
deletion::{delete::Delete, delete_user::DeleteUser, undo_delete::UndoDelete}, deletion::{delete::Delete, delete_user::DeleteUser, undo_delete::UndoDelete},
following::{accept::AcceptFollow, follow::Follow, undo_follow::UndoFollow}, following::{accept::AcceptFollow, follow::Follow, undo_follow::UndoFollow},
voting::{undo_vote::UndoVote, vote::Vote}, voting::{undo_vote::UndoVote, vote::Vote},
}, },
objects::page::Page, objects::page::Page,
InCommunity,
}, },
}; };
use activitypub_federation::{data::Data, deser::context::WithContext, traits::ActivityHandler}; use activitypub_federation::{data::Data, deser::context::WithContext, traits::ActivityHandler};
@ -54,8 +54,7 @@ pub enum GroupInboxActivities {
pub enum PersonInboxActivities { pub enum PersonInboxActivities {
AcceptFollow(AcceptFollow), AcceptFollow(AcceptFollow),
UndoFollow(UndoFollow), UndoFollow(UndoFollow),
FollowCommunity(Follow), CreateOrUpdatePrivateMessage(CreateOrUpdateChatMessage),
CreateOrUpdatePrivateMessage(CreateOrUpdatePrivateMessage),
Delete(Delete), Delete(Delete),
UndoDelete(UndoDelete), UndoDelete(UndoDelete),
AnnounceActivity(AnnounceActivity), AnnounceActivity(AnnounceActivity),
@ -76,8 +75,8 @@ pub enum PersonInboxActivitiesWithAnnouncable {
#[serde(untagged)] #[serde(untagged)]
#[enum_delegate::implement(ActivityHandler)] #[enum_delegate::implement(ActivityHandler)]
pub enum AnnouncableActivities { pub enum AnnouncableActivities {
CreateOrUpdateComment(CreateOrUpdateComment), CreateOrUpdateComment(CreateOrUpdateNote),
CreateOrUpdatePost(CreateOrUpdatePost), CreateOrUpdatePost(CreateOrUpdatePage),
Vote(Vote), Vote(Vote),
UndoVote(UndoVote), UndoVote(UndoVote),
Delete(Delete), Delete(Delete),
@ -102,29 +101,28 @@ pub enum SiteInboxActivities {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl GetCommunity for AnnouncableActivities { impl InCommunity for AnnouncableActivities {
#[tracing::instrument(skip(self, context))] #[tracing::instrument(skip(self, context))]
async fn get_community( async fn community(
&self, &self,
context: &LemmyContext, context: &LemmyContext,
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError> { ) -> Result<ApubCommunity, LemmyError> {
use AnnouncableActivities::*; use AnnouncableActivities::*;
let community = match self { match self {
CreateOrUpdateComment(a) => a.get_community(context, request_counter).await?, CreateOrUpdateComment(a) => a.community(context, request_counter).await,
CreateOrUpdatePost(a) => a.get_community(context, request_counter).await?, CreateOrUpdatePost(a) => a.community(context, request_counter).await,
Vote(a) => a.get_community(context, request_counter).await?, Vote(a) => a.community(context, request_counter).await,
UndoVote(a) => a.get_community(context, request_counter).await?, UndoVote(a) => a.community(context, request_counter).await,
Delete(a) => a.get_community(context, request_counter).await?, Delete(a) => a.community(context, request_counter).await,
UndoDelete(a) => a.get_community(context, request_counter).await?, UndoDelete(a) => a.community(context, request_counter).await,
UpdateCommunity(a) => a.get_community(context, request_counter).await?, UpdateCommunity(a) => a.community(context, request_counter).await,
BlockUser(a) => a.get_community(context, request_counter).await?, BlockUser(a) => a.community(context, request_counter).await,
UndoBlockUser(a) => a.get_community(context, request_counter).await?, UndoBlockUser(a) => a.community(context, request_counter).await,
AddMod(a) => a.get_community(context, request_counter).await?, AddMod(a) => a.community(context, request_counter).await,
RemoveMod(a) => a.get_community(context, request_counter).await?, RemoveMod(a) => a.community(context, request_counter).await,
Page(_) => unimplemented!(), Page(_) => unimplemented!(),
}; }
Ok(community)
} }
} }

View file

@ -6,7 +6,7 @@ use crate::{
protocol::{ protocol::{
activities::{ activities::{
community::announce::AnnounceActivity, community::announce::AnnounceActivity,
create_or_update::post::CreateOrUpdatePost, create_or_update::page::CreateOrUpdatePage,
CreateOrUpdateType, CreateOrUpdateType,
}, },
collections::group_outbox::GroupOutbox, collections::group_outbox::GroupOutbox,
@ -70,7 +70,7 @@ impl ApubObject for ApubCommunityOutbox {
for post in self.0 { for post in self.0 {
let person = Person::read(data.1.pool(), post.creator_id).await?.into(); let person = Person::read(data.1.pool(), post.creator_id).await?.into();
let create = let create =
CreateOrUpdatePost::new(post, &person, &data.0, CreateOrUpdateType::Create, &data.1) CreateOrUpdatePage::new(post, &person, &data.0, CreateOrUpdateType::Create, &data.1)
.await?; .await?;
let announcable = AnnouncableActivities::CreateOrUpdatePost(create); let announcable = AnnouncableActivities::CreateOrUpdatePost(create);
let announce = AnnounceActivity::new(announcable.try_into()?, &data.0, &data.1)?; let announce = AnnounceActivity::new(announcable.try_into()?, &data.0, &data.1)?;

View file

@ -1,9 +1,16 @@
use crate::{ use crate::{
objects::{comment::ApubComment, post::ApubPost}, objects::{comment::ApubComment, community::ApubCommunity, post::ApubPost},
protocol::objects::{note::Note, page::Page}, protocol::{
objects::{note::Note, page::Page},
InCommunity,
},
}; };
use activitypub_federation::traits::ApubObject; use activitypub_federation::traits::ApubObject;
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use lemmy_db_schema::{
source::{community::Community, post::Post},
traits::Crud,
};
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;
use serde::Deserialize; use serde::Deserialize;
@ -99,3 +106,18 @@ impl PostOrComment {
.into() .into()
} }
} }
#[async_trait::async_trait(?Send)]
impl InCommunity for PostOrComment {
async fn community(
&self,
context: &LemmyContext,
_: &mut i32,
) -> Result<ApubCommunity, LemmyError> {
let cid = match self {
PostOrComment::Post(p) => p.community_id,
PostOrComment::Comment(c) => Post::read(context.pool(), c.post_id).await?.community_id,
};
Ok(Community::read(context.pool(), cid).await?.into())
}
}

View file

@ -7,6 +7,7 @@ use crate::{
objects::{read_from_string_or_source, verify_is_remote_object}, objects::{read_from_string_or_source, verify_is_remote_object},
protocol::{ protocol::{
objects::{note::Note, LanguageTag}, objects::{note::Note, LanguageTag},
InCommunity,
Source, Source,
}, },
PostOrComment, PostOrComment,
@ -103,8 +104,13 @@ impl ApubObject for ApubComment {
ObjectId::<PostOrComment>::new(post.ap_id) ObjectId::<PostOrComment>::new(post.ap_id)
}; };
let language = LanguageTag::new_single(self.language_id, context.pool()).await?; let language = LanguageTag::new_single(self.language_id, context.pool()).await?;
let maa = let maa = collect_non_local_mentions(
collect_non_local_mentions(&self, ObjectId::new(community.actor_id), context, &mut 0).await?; &self,
ObjectId::new(community.actor_id.clone()),
context,
&mut 0,
)
.await?;
let note = Note { let note = Note {
r#type: NoteType::Note, r#type: NoteType::Note,
@ -121,6 +127,7 @@ impl ApubObject for ApubComment {
tag: maa.tags, tag: maa.tags,
distinguished: Some(self.distinguished), distinguished: Some(self.distinguished),
language, language,
audience: Some(ObjectId::new(community.actor_id)),
}; };
Ok(note) Ok(note)
@ -136,9 +143,7 @@ impl ApubObject for ApubComment {
verify_domains_match(note.id.inner(), expected_domain)?; verify_domains_match(note.id.inner(), expected_domain)?;
verify_domains_match(note.attributed_to.inner(), note.id.inner())?; verify_domains_match(note.attributed_to.inner(), note.id.inner())?;
verify_is_public(&note.to, &note.cc)?; verify_is_public(&note.to, &note.cc)?;
let (post, _) = note.get_parents(context, request_counter).await?; let community = note.community(context, request_counter).await?;
let community_id = post.community_id;
let community = Community::read(context.pool(), community_id).await?;
let local_site_data = fetch_local_site_data(context.pool()).await?; let local_site_data = fetch_local_site_data(context.pool()).await?;
check_apub_id_valid_with_strictness( check_apub_id_valid_with_strictness(
@ -148,13 +153,8 @@ impl ApubObject for ApubComment {
context.settings(), context.settings(),
)?; )?;
verify_is_remote_object(note.id.inner(), context.settings())?; verify_is_remote_object(note.id.inner(), context.settings())?;
verify_person_in_community( verify_person_in_community(&note.attributed_to, &community, context, request_counter).await?;
&note.attributed_to, let (post, _) = note.get_parents(context, request_counter).await?;
&community.into(),
context,
request_counter,
)
.await?;
if post.locked { if post.locked {
return Err(LemmyError::from_message("Post is locked")); return Err(LemmyError::from_message("Post is locked"));
} }

View file

@ -10,6 +10,7 @@ use crate::{
LanguageTag, LanguageTag,
}, },
ImageObject, ImageObject,
InCommunity,
Source, Source,
}, },
}; };
@ -102,7 +103,7 @@ impl ApubObject for ApubPost {
kind: PageType::Page, kind: PageType::Page,
id: ObjectId::new(self.ap_id.clone()), id: ObjectId::new(self.ap_id.clone()),
attributed_to: AttributedTo::Lemmy(ObjectId::new(creator.actor_id)), attributed_to: AttributedTo::Lemmy(ObjectId::new(creator.actor_id)),
to: vec![community.actor_id.into(), public()], to: vec![community.actor_id.clone().into(), public()],
cc: vec![], cc: vec![],
name: self.name.clone(), name: self.name.clone(),
content: self.body.as_ref().map(|b| markdown_to_html(b)), content: self.body.as_ref().map(|b| markdown_to_html(b)),
@ -117,6 +118,7 @@ impl ApubObject for ApubPost {
language, language,
published: Some(convert_datetime(self.published)), published: Some(convert_datetime(self.published)),
updated: self.updated.map(convert_datetime), updated: self.updated.map(convert_datetime),
audience: Some(ObjectId::new(community.actor_id)),
}; };
Ok(page) Ok(page)
} }
@ -137,7 +139,7 @@ impl ApubObject for ApubPost {
let local_site_data = fetch_local_site_data(context.pool()).await?; let local_site_data = fetch_local_site_data(context.pool()).await?;
let community = page.extract_community(context, request_counter).await?; let community = page.community(context, request_counter).await?;
check_apub_id_valid_with_strictness( check_apub_id_valid_with_strictness(
page.id.inner(), page.id.inner(),
community.local, community.local,
@ -164,7 +166,7 @@ impl ApubObject for ApubPost {
.creator()? .creator()?
.dereference(context, local_instance(context).await, request_counter) .dereference(context, local_instance(context).await, request_counter)
.await?; .await?;
let community = page.extract_community(context, request_counter).await?; let community = page.community(context, request_counter).await?;
let form = if !page.is_mod_action(context).await? { let form = if !page.is_mod_action(context).await? {
let first_attachment = page.attachment.into_iter().map(Attachment::url).next(); let first_attachment = page.attachment.into_iter().map(Attachment::url).next();

View file

@ -1,10 +1,20 @@
use crate::{activities::block::SiteOrCommunity, objects::person::ApubPerson}; use crate::{
activities::{block::SiteOrCommunity, verify_community_matches},
local_instance,
objects::{community::ApubCommunity, person::ApubPerson},
protocol::InCommunity,
};
use activitypub_federation::{core::object_id::ObjectId, deser::helpers::deserialize_one_or_many}; use activitypub_federation::{core::object_id::ObjectId, deser::helpers::deserialize_one_or_many};
use activitystreams_kinds::activity::BlockType; use activitystreams_kinds::activity::BlockType;
use anyhow::anyhow;
use chrono::{DateTime, FixedOffset}; use chrono::{DateTime, FixedOffset};
use lemmy_utils::error::LemmyError;
use lemmy_websocket::LemmyContext;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use url::Url; use url::Url;
#[skip_serializing_none]
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct BlockUser { pub struct BlockUser {
@ -18,6 +28,7 @@ pub struct BlockUser {
#[serde(rename = "type")] #[serde(rename = "type")]
pub(crate) kind: BlockType, pub(crate) kind: BlockType,
pub(crate) id: Url, pub(crate) id: Url,
pub(crate) audience: Option<ObjectId<ApubCommunity>>,
/// Quick and dirty solution. /// Quick and dirty solution.
/// TODO: send a separate Delete activity instead /// TODO: send a separate Delete activity instead
@ -26,3 +37,30 @@ pub struct BlockUser {
pub(crate) summary: Option<String>, pub(crate) summary: Option<String>,
pub(crate) expires: Option<DateTime<FixedOffset>>, pub(crate) expires: Option<DateTime<FixedOffset>>,
} }
#[async_trait::async_trait(?Send)]
impl InCommunity for BlockUser {
async fn community(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError> {
let target = self
.target
.dereference(context, local_instance(context).await, request_counter)
.await?;
let target_community = match target {
SiteOrCommunity::Community(c) => c,
SiteOrCommunity::Site(_) => return Err(anyhow!("activity is not in community").into()),
};
if let Some(audience) = &self.audience {
let audience = audience
.dereference(context, local_instance(context).await, request_counter)
.await?;
verify_community_matches(&audience, target_community.id)?;
Ok(audience)
} else {
Ok(target_community)
}
}
}

View file

@ -1,9 +1,18 @@
use crate::{objects::person::ApubPerson, protocol::activities::block::block_user::BlockUser}; use crate::{
activities::verify_community_matches,
local_instance,
objects::{community::ApubCommunity, person::ApubPerson},
protocol::{activities::block::block_user::BlockUser, InCommunity},
};
use activitypub_federation::{core::object_id::ObjectId, deser::helpers::deserialize_one_or_many}; use activitypub_federation::{core::object_id::ObjectId, deser::helpers::deserialize_one_or_many};
use activitystreams_kinds::activity::UndoType; use activitystreams_kinds::activity::UndoType;
use lemmy_utils::error::LemmyError;
use lemmy_websocket::LemmyContext;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use url::Url; use url::Url;
#[skip_serializing_none]
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct UndoBlockUser { pub struct UndoBlockUser {
@ -16,4 +25,25 @@ pub struct UndoBlockUser {
#[serde(rename = "type")] #[serde(rename = "type")]
pub(crate) kind: UndoType, pub(crate) kind: UndoType,
pub(crate) id: Url, pub(crate) id: Url,
pub(crate) audience: Option<ObjectId<ApubCommunity>>,
}
#[async_trait::async_trait(?Send)]
impl InCommunity for UndoBlockUser {
async fn community(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError> {
let object_community = self.object.community(context, request_counter).await?;
if let Some(audience) = &self.audience {
let audience = audience
.dereference(context, local_instance(context).await, request_counter)
.await?;
verify_community_matches(&audience, object_community.id)?;
Ok(audience)
} else {
Ok(object_community)
}
}
} }

View file

@ -1,6 +1,13 @@
use crate::objects::person::ApubPerson; use crate::{
activities::{community::get_community_from_moderators_url, verify_community_matches},
local_instance,
objects::{community::ApubCommunity, person::ApubPerson},
protocol::InCommunity,
};
use activitypub_federation::{core::object_id::ObjectId, deser::helpers::deserialize_one_or_many}; use activitypub_federation::{core::object_id::ObjectId, deser::helpers::deserialize_one_or_many};
use activitystreams_kinds::activity::AddType; use activitystreams_kinds::activity::AddType;
use lemmy_utils::error::LemmyError;
use lemmy_websocket::LemmyContext;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use url::Url; use url::Url;
@ -17,4 +24,26 @@ pub struct AddMod {
#[serde(rename = "type")] #[serde(rename = "type")]
pub(crate) kind: AddType, pub(crate) kind: AddType,
pub(crate) id: Url, pub(crate) id: Url,
pub(crate) audience: Option<ObjectId<ApubCommunity>>,
}
#[async_trait::async_trait(?Send)]
impl InCommunity for AddMod {
async fn community(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError> {
let mod_community =
get_community_from_moderators_url(&self.target, context, request_counter).await?;
if let Some(audience) = &self.audience {
let audience = audience
.dereference(context, local_instance(context).await, request_counter)
.await?;
verify_community_matches(&audience, mod_community.id)?;
Ok(audience)
} else {
Ok(mod_community)
}
}
} }

View file

@ -1,6 +1,13 @@
use crate::objects::person::ApubPerson; use crate::{
activities::{community::get_community_from_moderators_url, verify_community_matches},
local_instance,
objects::{community::ApubCommunity, person::ApubPerson},
protocol::InCommunity,
};
use activitypub_federation::{core::object_id::ObjectId, deser::helpers::deserialize_one_or_many}; use activitypub_federation::{core::object_id::ObjectId, deser::helpers::deserialize_one_or_many};
use activitystreams_kinds::activity::RemoveType; use activitystreams_kinds::activity::RemoveType;
use lemmy_utils::error::LemmyError;
use lemmy_websocket::LemmyContext;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use url::Url; use url::Url;
@ -17,4 +24,26 @@ pub struct RemoveMod {
pub(crate) kind: RemoveType, pub(crate) kind: RemoveType,
pub(crate) target: Url, pub(crate) target: Url,
pub(crate) id: Url, pub(crate) id: Url,
pub(crate) audience: Option<ObjectId<ApubCommunity>>,
}
#[async_trait::async_trait(?Send)]
impl InCommunity for RemoveMod {
async fn community(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError> {
let mod_community =
get_community_from_moderators_url(&self.target, context, request_counter).await?;
if let Some(audience) = &self.audience {
let audience = audience
.dereference(context, local_instance(context).await, request_counter)
.await?;
verify_community_matches(&audience, mod_community.id)?;
Ok(audience)
} else {
Ok(mod_community)
}
}
} }

View file

@ -1,9 +1,14 @@
use crate::{ use crate::{
activities::verify_community_matches,
fetcher::post_or_comment::PostOrComment, fetcher::post_or_comment::PostOrComment,
local_instance,
objects::{community::ApubCommunity, person::ApubPerson}, objects::{community::ApubCommunity, person::ApubPerson},
protocol::InCommunity,
}; };
use activitypub_federation::{core::object_id::ObjectId, deser::helpers::deserialize_one}; use activitypub_federation::{core::object_id::ObjectId, deser::helpers::deserialize_one};
use activitystreams_kinds::activity::FlagType; use activitystreams_kinds::activity::FlagType;
use lemmy_utils::error::LemmyError;
use lemmy_websocket::LemmyContext;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use url::Url; use url::Url;
@ -18,4 +23,27 @@ pub struct Report {
#[serde(rename = "type")] #[serde(rename = "type")]
pub(crate) kind: FlagType, pub(crate) kind: FlagType,
pub(crate) id: Url, pub(crate) id: Url,
pub(crate) audience: Option<ObjectId<ApubCommunity>>,
}
#[async_trait::async_trait(?Send)]
impl InCommunity for Report {
async fn community(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError> {
let to_community = self.to[0]
.dereference(context, local_instance(context).await, request_counter)
.await?;
if let Some(audience) = &self.audience {
let audience = audience
.dereference(context, local_instance(context).await, request_counter)
.await?;
verify_community_matches(&audience, to_community.id)?;
Ok(audience)
} else {
Ok(to_community)
}
}
} }

View file

@ -1,6 +1,13 @@
use crate::{objects::person::ApubPerson, protocol::objects::group::Group}; use crate::{
activities::verify_community_matches,
local_instance,
objects::{community::ApubCommunity, person::ApubPerson},
protocol::{objects::group::Group, InCommunity},
};
use activitypub_federation::{core::object_id::ObjectId, deser::helpers::deserialize_one_or_many}; use activitypub_federation::{core::object_id::ObjectId, deser::helpers::deserialize_one_or_many};
use activitystreams_kinds::activity::UpdateType; use activitystreams_kinds::activity::UpdateType;
use lemmy_utils::error::LemmyError;
use lemmy_websocket::LemmyContext;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use url::Url; use url::Url;
@ -19,4 +26,27 @@ pub struct UpdateCommunity {
#[serde(rename = "type")] #[serde(rename = "type")]
pub(crate) kind: UpdateType, pub(crate) kind: UpdateType,
pub(crate) id: Url, pub(crate) id: Url,
pub(crate) audience: Option<ObjectId<ApubCommunity>>,
}
#[async_trait::async_trait(?Send)]
impl InCommunity for UpdateCommunity {
async fn community(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError> {
let object_community: ApubCommunity = ObjectId::new(self.object.id.clone())
.dereference(context, local_instance(context).await, request_counter)
.await?;
if let Some(audience) = &self.audience {
let audience = audience
.dereference(context, local_instance(context).await, request_counter)
.await?;
verify_community_matches(&audience, object_community.id)?;
Ok(audience)
} else {
Ok(object_community)
}
}
} }

View file

@ -8,7 +8,7 @@ use url::Url;
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct CreateOrUpdatePrivateMessage { pub struct CreateOrUpdateChatMessage {
pub(crate) id: Url, pub(crate) id: Url,
pub(crate) actor: ObjectId<ApubPerson>, pub(crate) actor: ObjectId<ApubPerson>,
#[serde(deserialize_with = "deserialize_one")] #[serde(deserialize_with = "deserialize_one")]

View file

@ -1,24 +0,0 @@
use crate::{
mentions::MentionOrValue,
objects::person::ApubPerson,
protocol::{activities::CreateOrUpdateType, objects::note::Note},
};
use activitypub_federation::{core::object_id::ObjectId, deser::helpers::deserialize_one_or_many};
use serde::{Deserialize, Serialize};
use url::Url;
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateOrUpdateComment {
pub(crate) actor: ObjectId<ApubPerson>,
#[serde(deserialize_with = "deserialize_one_or_many")]
pub(crate) to: Vec<Url>,
pub(crate) object: Note,
#[serde(deserialize_with = "deserialize_one_or_many")]
pub(crate) cc: Vec<Url>,
#[serde(default)]
pub(crate) tag: Vec<MentionOrValue>,
#[serde(rename = "type")]
pub(crate) kind: CreateOrUpdateType,
pub(crate) id: Url,
}

View file

@ -1,33 +1,33 @@
pub mod comment; pub mod chat_message;
pub mod post; pub mod note;
pub mod private_message; pub mod page;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::protocol::{ use crate::protocol::{
activities::create_or_update::{ activities::create_or_update::{
comment::CreateOrUpdateComment, chat_message::CreateOrUpdateChatMessage,
post::CreateOrUpdatePost, note::CreateOrUpdateNote,
private_message::CreateOrUpdatePrivateMessage, page::CreateOrUpdatePage,
}, },
tests::test_parse_lemmy_item, tests::test_parse_lemmy_item,
}; };
#[test] #[test]
fn test_parse_lemmy_create_or_update() { fn test_parse_lemmy_create_or_update() {
test_parse_lemmy_item::<CreateOrUpdatePost>( test_parse_lemmy_item::<CreateOrUpdatePage>(
"assets/lemmy/activities/create_or_update/create_page.json", "assets/lemmy/activities/create_or_update/create_page.json",
) )
.unwrap(); .unwrap();
test_parse_lemmy_item::<CreateOrUpdatePost>( test_parse_lemmy_item::<CreateOrUpdatePage>(
"assets/lemmy/activities/create_or_update/update_page.json", "assets/lemmy/activities/create_or_update/update_page.json",
) )
.unwrap(); .unwrap();
test_parse_lemmy_item::<CreateOrUpdateComment>( test_parse_lemmy_item::<CreateOrUpdateNote>(
"assets/lemmy/activities/create_or_update/create_note.json", "assets/lemmy/activities/create_or_update/create_note.json",
) )
.unwrap(); .unwrap();
test_parse_lemmy_item::<CreateOrUpdatePrivateMessage>( test_parse_lemmy_item::<CreateOrUpdateChatMessage>(
"assets/lemmy/activities/create_or_update/create_private_message.json", "assets/lemmy/activities/create_or_update/create_private_message.json",
) )
.unwrap(); .unwrap();

View file

@ -0,0 +1,51 @@
use crate::{
activities::verify_community_matches,
local_instance,
mentions::MentionOrValue,
objects::{community::ApubCommunity, person::ApubPerson},
protocol::{activities::CreateOrUpdateType, objects::note::Note, InCommunity},
};
use activitypub_federation::{core::object_id::ObjectId, deser::helpers::deserialize_one_or_many};
use lemmy_db_schema::{source::community::Community, traits::Crud};
use lemmy_utils::error::LemmyError;
use lemmy_websocket::LemmyContext;
use serde::{Deserialize, Serialize};
use url::Url;
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateOrUpdateNote {
pub(crate) actor: ObjectId<ApubPerson>,
#[serde(deserialize_with = "deserialize_one_or_many")]
pub(crate) to: Vec<Url>,
pub(crate) object: Note,
#[serde(deserialize_with = "deserialize_one_or_many")]
pub(crate) cc: Vec<Url>,
#[serde(default)]
pub(crate) tag: Vec<MentionOrValue>,
#[serde(rename = "type")]
pub(crate) kind: CreateOrUpdateType,
pub(crate) id: Url,
pub(crate) audience: Option<ObjectId<ApubCommunity>>,
}
#[async_trait::async_trait(?Send)]
impl InCommunity for CreateOrUpdateNote {
async fn community(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError> {
let post = self.object.get_parents(context, request_counter).await?.0;
if let Some(audience) = &self.audience {
let audience = audience
.dereference(context, local_instance(context).await, request_counter)
.await?;
verify_community_matches(&audience, post.community_id)?;
Ok(audience)
} else {
let community = Community::read(context.pool(), post.community_id).await?;
Ok(community.into())
}
}
}

View file

@ -0,0 +1,46 @@
use crate::{
activities::verify_community_matches,
local_instance,
objects::{community::ApubCommunity, person::ApubPerson},
protocol::{activities::CreateOrUpdateType, objects::page::Page, InCommunity},
};
use activitypub_federation::{core::object_id::ObjectId, deser::helpers::deserialize_one_or_many};
use lemmy_utils::error::LemmyError;
use lemmy_websocket::LemmyContext;
use serde::{Deserialize, Serialize};
use url::Url;
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateOrUpdatePage {
pub(crate) actor: ObjectId<ApubPerson>,
#[serde(deserialize_with = "deserialize_one_or_many")]
pub(crate) to: Vec<Url>,
pub(crate) object: Page,
#[serde(deserialize_with = "deserialize_one_or_many")]
pub(crate) cc: Vec<Url>,
#[serde(rename = "type")]
pub(crate) kind: CreateOrUpdateType,
pub(crate) id: Url,
pub(crate) audience: Option<ObjectId<ApubCommunity>>,
}
#[async_trait::async_trait(?Send)]
impl InCommunity for CreateOrUpdatePage {
async fn community(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError> {
let object_community = self.object.community(context, request_counter).await?;
if let Some(audience) = &self.audience {
let audience = audience
.dereference(context, local_instance(context).await, request_counter)
.await?;
verify_community_matches(&audience, object_community.id)?;
Ok(audience)
} else {
Ok(object_community)
}
}
}

View file

@ -1,21 +0,0 @@
use crate::{
objects::person::ApubPerson,
protocol::{activities::CreateOrUpdateType, objects::page::Page},
};
use activitypub_federation::{core::object_id::ObjectId, deser::helpers::deserialize_one_or_many};
use serde::{Deserialize, Serialize};
use url::Url;
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateOrUpdatePost {
pub(crate) actor: ObjectId<ApubPerson>,
#[serde(deserialize_with = "deserialize_one_or_many")]
pub(crate) to: Vec<Url>,
pub(crate) object: Page,
#[serde(deserialize_with = "deserialize_one_or_many")]
pub(crate) cc: Vec<Url>,
#[serde(rename = "type")]
pub(crate) kind: CreateOrUpdateType,
pub(crate) id: Url,
}

View file

@ -1,9 +1,18 @@
use crate::{ use crate::{
objects::person::ApubPerson, activities::{deletion::DeletableObjects, verify_community_matches},
protocol::{objects::tombstone::Tombstone, IdOrNestedObject}, local_instance,
objects::{community::ApubCommunity, person::ApubPerson},
protocol::{objects::tombstone::Tombstone, IdOrNestedObject, InCommunity},
}; };
use activitypub_federation::{core::object_id::ObjectId, deser::helpers::deserialize_one_or_many}; use activitypub_federation::{core::object_id::ObjectId, deser::helpers::deserialize_one_or_many};
use activitystreams_kinds::activity::DeleteType; use activitystreams_kinds::activity::DeleteType;
use anyhow::anyhow;
use lemmy_db_schema::{
source::{community::Community, post::Post},
traits::Crud,
};
use lemmy_utils::error::LemmyError;
use lemmy_websocket::LemmyContext;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none; use serde_with::skip_serializing_none;
use url::Url; use url::Url;
@ -19,6 +28,7 @@ pub struct Delete {
#[serde(rename = "type")] #[serde(rename = "type")]
pub(crate) kind: DeleteType, pub(crate) kind: DeleteType,
pub(crate) id: Url, pub(crate) id: Url,
pub(crate) audience: Option<ObjectId<ApubCommunity>>,
#[serde(deserialize_with = "deserialize_one_or_many")] #[serde(deserialize_with = "deserialize_one_or_many")]
#[serde(default)] #[serde(default)]
@ -28,3 +38,34 @@ pub struct Delete {
/// deleting their own content. /// deleting their own content.
pub(crate) summary: Option<String>, pub(crate) summary: Option<String>,
} }
#[async_trait::async_trait(?Send)]
impl InCommunity for Delete {
async fn community(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError> {
let community_id = match DeletableObjects::read_from_db(self.object.id(), context).await? {
DeletableObjects::Community(c) => c.id,
DeletableObjects::Comment(c) => {
let post = Post::read(context.pool(), c.post_id).await?;
post.community_id
}
DeletableObjects::Post(p) => p.community_id,
DeletableObjects::PrivateMessage(_) => {
return Err(anyhow!("Private message is not part of community").into())
}
};
if let Some(audience) = &self.audience {
let audience = audience
.dereference(context, local_instance(context).await, request_counter)
.await?;
verify_community_matches(&audience, community_id)?;
Ok(audience)
} else {
let community = Community::read(context.pool(), community_id).await?;
Ok(community.into())
}
}
}

View file

@ -1,9 +1,18 @@
use crate::{objects::person::ApubPerson, protocol::activities::deletion::delete::Delete}; use crate::{
activities::verify_community_matches,
local_instance,
objects::{community::ApubCommunity, person::ApubPerson},
protocol::{activities::deletion::delete::Delete, InCommunity},
};
use activitypub_federation::{core::object_id::ObjectId, deser::helpers::deserialize_one_or_many}; use activitypub_federation::{core::object_id::ObjectId, deser::helpers::deserialize_one_or_many};
use activitystreams_kinds::activity::UndoType; use activitystreams_kinds::activity::UndoType;
use lemmy_utils::error::LemmyError;
use lemmy_websocket::LemmyContext;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use url::Url; use url::Url;
#[skip_serializing_none]
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct UndoDelete { pub struct UndoDelete {
@ -14,8 +23,29 @@ pub struct UndoDelete {
#[serde(rename = "type")] #[serde(rename = "type")]
pub(crate) kind: UndoType, pub(crate) kind: UndoType,
pub(crate) id: Url, pub(crate) id: Url,
pub(crate) audience: Option<ObjectId<ApubCommunity>>,
#[serde(deserialize_with = "deserialize_one_or_many", default)] #[serde(deserialize_with = "deserialize_one_or_many", default)]
#[serde(skip_serializing_if = "Vec::is_empty")] #[serde(skip_serializing_if = "Vec::is_empty")]
pub(crate) cc: Vec<Url>, pub(crate) cc: Vec<Url>,
} }
#[async_trait::async_trait(?Send)]
impl InCommunity for UndoDelete {
async fn community(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError> {
let object_community = self.object.community(context, request_counter).await?;
if let Some(audience) = &self.audience {
let audience = audience
.dereference(context, local_instance(context).await, request_counter)
.await?;
verify_community_matches(&audience, object_community.id)?;
Ok(audience)
} else {
Ok(object_community)
}
}
}

View file

@ -19,7 +19,7 @@ mod tests {
use crate::protocol::{ use crate::protocol::{
activities::{ activities::{
community::announce::AnnounceActivity, community::announce::AnnounceActivity,
create_or_update::{comment::CreateOrUpdateComment, post::CreateOrUpdatePost}, create_or_update::{note::CreateOrUpdateNote, page::CreateOrUpdatePage},
deletion::delete::Delete, deletion::delete::Delete,
following::{follow::Follow, undo_follow::UndoFollow}, following::{follow::Follow, undo_follow::UndoFollow},
voting::{undo_vote::UndoVote, vote::Vote}, voting::{undo_vote::UndoVote, vote::Vote},
@ -29,19 +29,19 @@ mod tests {
#[test] #[test]
fn test_parse_smithereen_activities() { fn test_parse_smithereen_activities() {
test_json::<CreateOrUpdateComment>("assets/smithereen/activities/create_note.json").unwrap(); test_json::<CreateOrUpdateNote>("assets/smithereen/activities/create_note.json").unwrap();
} }
#[test] #[test]
fn test_parse_pleroma_activities() { fn test_parse_pleroma_activities() {
test_json::<CreateOrUpdateComment>("assets/pleroma/activities/create_note.json").unwrap(); test_json::<CreateOrUpdateNote>("assets/pleroma/activities/create_note.json").unwrap();
test_json::<Delete>("assets/pleroma/activities/delete.json").unwrap(); test_json::<Delete>("assets/pleroma/activities/delete.json").unwrap();
test_json::<Follow>("assets/pleroma/activities/follow.json").unwrap(); test_json::<Follow>("assets/pleroma/activities/follow.json").unwrap();
} }
#[test] #[test]
fn test_parse_mastodon_activities() { fn test_parse_mastodon_activities() {
test_json::<CreateOrUpdateComment>("assets/mastodon/activities/create_note.json").unwrap(); test_json::<CreateOrUpdateNote>("assets/mastodon/activities/create_note.json").unwrap();
test_json::<Delete>("assets/mastodon/activities/delete.json").unwrap(); test_json::<Delete>("assets/mastodon/activities/delete.json").unwrap();
test_json::<Follow>("assets/mastodon/activities/follow.json").unwrap(); test_json::<Follow>("assets/mastodon/activities/follow.json").unwrap();
test_json::<UndoFollow>("assets/mastodon/activities/undo_follow.json").unwrap(); test_json::<UndoFollow>("assets/mastodon/activities/undo_follow.json").unwrap();
@ -51,17 +51,17 @@ mod tests {
#[test] #[test]
fn test_parse_lotide_activities() { fn test_parse_lotide_activities() {
test_json::<CreateOrUpdatePost>("assets/lotide/activities/create_page.json").unwrap(); test_json::<CreateOrUpdatePage>("assets/lotide/activities/create_page.json").unwrap();
test_json::<CreateOrUpdatePost>("assets/lotide/activities/create_page_image.json").unwrap(); test_json::<CreateOrUpdatePage>("assets/lotide/activities/create_page_image.json").unwrap();
test_json::<CreateOrUpdateComment>("assets/lotide/activities/create_note_reply.json").unwrap(); test_json::<CreateOrUpdateNote>("assets/lotide/activities/create_note_reply.json").unwrap();
} }
#[test] #[test]
fn test_parse_friendica_activities() { fn test_parse_friendica_activities() {
test_json::<CreateOrUpdatePost>("assets/friendica/activities/create_page_1.json").unwrap(); test_json::<CreateOrUpdatePage>("assets/friendica/activities/create_page_1.json").unwrap();
test_json::<CreateOrUpdatePost>("assets/friendica/activities/create_page_2.json").unwrap(); test_json::<CreateOrUpdatePage>("assets/friendica/activities/create_page_2.json").unwrap();
test_json::<CreateOrUpdateComment>("assets/friendica/activities/create_note.json").unwrap(); test_json::<CreateOrUpdateNote>("assets/friendica/activities/create_note.json").unwrap();
test_json::<CreateOrUpdateComment>("assets/friendica/activities/update_note.json").unwrap(); test_json::<CreateOrUpdateNote>("assets/friendica/activities/update_note.json").unwrap();
test_json::<Delete>("assets/friendica/activities/delete.json").unwrap(); test_json::<Delete>("assets/friendica/activities/delete.json").unwrap();
test_json::<Vote>("assets/friendica/activities/like_page.json").unwrap(); test_json::<Vote>("assets/friendica/activities/like_page.json").unwrap();
test_json::<Vote>("assets/friendica/activities/dislike_page.json").unwrap(); test_json::<Vote>("assets/friendica/activities/dislike_page.json").unwrap();
@ -70,8 +70,8 @@ mod tests {
#[test] #[test]
fn test_parse_gnusocial_activities() { fn test_parse_gnusocial_activities() {
test_json::<CreateOrUpdatePost>("assets/gnusocial/activities/create_page.json").unwrap(); test_json::<CreateOrUpdatePage>("assets/gnusocial/activities/create_page.json").unwrap();
test_json::<CreateOrUpdateComment>("assets/gnusocial/activities/create_note.json").unwrap(); test_json::<CreateOrUpdateNote>("assets/gnusocial/activities/create_note.json").unwrap();
test_json::<Vote>("assets/gnusocial/activities/like_note.json").unwrap(); test_json::<Vote>("assets/gnusocial/activities/like_note.json").unwrap();
} }

View file

@ -1,6 +1,13 @@
use crate::{objects::person::ApubPerson, protocol::activities::voting::vote::Vote}; use crate::{
activities::verify_community_matches,
local_instance,
objects::{community::ApubCommunity, person::ApubPerson},
protocol::{activities::voting::vote::Vote, InCommunity},
};
use activitypub_federation::core::object_id::ObjectId; use activitypub_federation::core::object_id::ObjectId;
use activitystreams_kinds::activity::UndoType; use activitystreams_kinds::activity::UndoType;
use lemmy_utils::error::LemmyError;
use lemmy_websocket::LemmyContext;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use url::Url; use url::Url;
@ -12,4 +19,26 @@ pub struct UndoVote {
#[serde(rename = "type")] #[serde(rename = "type")]
pub(crate) kind: UndoType, pub(crate) kind: UndoType,
pub(crate) id: Url, pub(crate) id: Url,
pub(crate) audience: Option<ObjectId<ApubCommunity>>,
}
#[async_trait::async_trait(?Send)]
impl InCommunity for UndoVote {
async fn community(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError> {
let local_instance = local_instance(context).await;
let object_community = self.object.community(context, request_counter).await?;
if let Some(audience) = &self.audience {
let audience = audience
.dereference(context, local_instance, request_counter)
.await?;
verify_community_matches(&audience, object_community.id)?;
Ok(audience)
} else {
Ok(object_community)
}
}
} }

View file

@ -1,6 +1,13 @@
use crate::{fetcher::post_or_comment::PostOrComment, objects::person::ApubPerson}; use crate::{
activities::verify_community_matches,
fetcher::post_or_comment::PostOrComment,
local_instance,
objects::{community::ApubCommunity, person::ApubPerson},
protocol::InCommunity,
};
use activitypub_federation::core::object_id::ObjectId; use activitypub_federation::core::object_id::ObjectId;
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
use lemmy_websocket::LemmyContext;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::convert::TryFrom; use std::convert::TryFrom;
use strum_macros::Display; use strum_macros::Display;
@ -14,6 +21,7 @@ pub struct Vote {
#[serde(rename = "type")] #[serde(rename = "type")]
pub(crate) kind: VoteType, pub(crate) kind: VoteType,
pub(crate) id: Url, pub(crate) id: Url,
pub(crate) audience: Option<ObjectId<ApubCommunity>>,
} }
#[derive(Clone, Debug, Display, Deserialize, Serialize, PartialEq, Eq)] #[derive(Clone, Debug, Display, Deserialize, Serialize, PartialEq, Eq)]
@ -42,3 +50,29 @@ impl From<&VoteType> for i16 {
} }
} }
} }
#[async_trait::async_trait(?Send)]
impl InCommunity for Vote {
async fn community(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError> {
let local_instance = local_instance(context).await;
let object_community = self
.object
.dereference(context, local_instance, request_counter)
.await?
.community(context, request_counter)
.await?;
if let Some(audience) = &self.audience {
let audience = audience
.dereference(context, local_instance, request_counter)
.await?;
verify_community_matches(&audience, object_community.id)?;
Ok(audience)
} else {
Ok(object_community)
}
}
}

View file

@ -1,4 +1,4 @@
use crate::local_instance; use crate::{local_instance, objects::community::ApubCommunity};
use activitypub_federation::{deser::values::MediaTypeMarkdown, utils::fetch_object_http}; use activitypub_federation::{deser::values::MediaTypeMarkdown, utils::fetch_object_http};
use activitystreams_kinds::object::ImageType; use activitystreams_kinds::object::ImageType;
use lemmy_db_schema::newtypes::DbUrl; use lemmy_db_schema::newtypes::DbUrl;
@ -81,6 +81,17 @@ impl<Kind: Id + DeserializeOwned> IdOrNestedObject<Kind> {
} }
} }
#[async_trait::async_trait(?Send)]
pub trait InCommunity {
// TODO: after we use audience field and remove backwards compat, it should be possible to change
// this to simply `fn community(&self) -> Result<ObjectId<ApubCommunity>, LemmyError>`
async fn community(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError>;
}
#[cfg(test)] #[cfg(test)]
pub(crate) mod tests { pub(crate) mod tests {
use activitypub_federation::deser::context::WithContext; use activitypub_federation::deser::context::WithContext;

View file

@ -1,9 +1,10 @@
use crate::{ use crate::{
activities::verify_community_matches,
fetcher::post_or_comment::PostOrComment, fetcher::post_or_comment::PostOrComment,
local_instance, local_instance,
mentions::MentionOrValue, mentions::MentionOrValue,
objects::{comment::ApubComment, person::ApubPerson, post::ApubPost}, objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson, post::ApubPost},
protocol::{objects::LanguageTag, Source}, protocol::{objects::LanguageTag, InCommunity, Source},
}; };
use activitypub_federation::{ use activitypub_federation::{
core::object_id::ObjectId, core::object_id::ObjectId,
@ -14,7 +15,10 @@ use activitypub_federation::{
}; };
use activitystreams_kinds::object::NoteType; use activitystreams_kinds::object::NoteType;
use chrono::{DateTime, FixedOffset}; use chrono::{DateTime, FixedOffset};
use lemmy_db_schema::{source::post::Post, traits::Crud}; use lemmy_db_schema::{
source::{community::Community, post::Post},
traits::Crud,
};
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -46,6 +50,7 @@ pub struct Note {
// lemmy extension // lemmy extension
pub(crate) distinguished: Option<bool>, pub(crate) distinguished: Option<bool>,
pub(crate) language: Option<LanguageTag>, pub(crate) language: Option<LanguageTag>,
pub(crate) audience: Option<ObjectId<ApubCommunity>>,
} }
impl Note { impl Note {
@ -75,3 +80,24 @@ impl Note {
} }
} }
} }
#[async_trait::async_trait(?Send)]
impl InCommunity for Note {
async fn community(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError> {
let (post, _) = self.get_parents(context, request_counter).await?;
let community_id = post.community_id;
if let Some(audience) = &self.audience {
let audience = audience
.dereference(context, local_instance(context).await, request_counter)
.await?;
verify_community_matches(&audience, community_id)?;
Ok(audience)
} else {
Ok(Community::read(context.pool(), community_id).await?.into())
}
}
}

View file

@ -1,8 +1,9 @@
use crate::{ use crate::{
activities::verify_community_matches,
fetcher::user_or_community::{PersonOrGroupType, UserOrCommunity}, fetcher::user_or_community::{PersonOrGroupType, UserOrCommunity},
local_instance, local_instance,
objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost}, objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost},
protocol::{objects::LanguageTag, ImageObject, Source}, protocol::{objects::LanguageTag, ImageObject, InCommunity, Source},
}; };
use activitypub_federation::{ use activitypub_federation::{
core::object_id::ObjectId, core::object_id::ObjectId,
@ -67,6 +68,7 @@ pub struct Page {
pub(crate) published: Option<DateTime<FixedOffset>>, pub(crate) published: Option<DateTime<FixedOffset>>,
pub(crate) updated: Option<DateTime<FixedOffset>>, pub(crate) updated: Option<DateTime<FixedOffset>>,
pub(crate) language: Option<LanguageTag>, pub(crate) language: Option<LanguageTag>,
pub(crate) audience: Option<ObjectId<ApubCommunity>>,
} }
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
@ -169,39 +171,6 @@ impl Page {
false false
} }
pub(crate) async fn extract_community(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError> {
match &self.attributed_to {
AttributedTo::Lemmy(_) => {
let mut iter = self.to.iter().merge(self.cc.iter());
loop {
if let Some(cid) = iter.next() {
let cid = ObjectId::new(cid.clone());
if let Ok(c) = cid
.dereference(context, local_instance(context).await, request_counter)
.await
{
break Ok(c);
}
} else {
return Err(LemmyError::from_message("No community found in cc"));
}
}
}
AttributedTo::Peertube(p) => {
p.iter()
.find(|a| a.kind == PersonOrGroupType::Group)
.map(|a| ObjectId::<ApubCommunity>::new(a.id.clone().into_inner()))
.ok_or_else(|| LemmyError::from_message("page does not specify group"))?
.dereference(context, local_instance(context).await, request_counter)
.await
}
}
}
pub(crate) fn creator(&self) -> Result<ObjectId<ApubPerson>, LemmyError> { pub(crate) fn creator(&self) -> Result<ObjectId<ApubPerson>, LemmyError> {
match &self.attributed_to { match &self.attributed_to {
AttributedTo::Lemmy(l) => Ok(l.clone()), AttributedTo::Lemmy(l) => Ok(l.clone()),
@ -250,3 +219,46 @@ impl ActivityHandler for Page {
Ok(()) Ok(())
} }
} }
#[async_trait::async_trait(?Send)]
impl InCommunity for Page {
async fn community(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError> {
let instance = local_instance(context).await;
let community = match &self.attributed_to {
AttributedTo::Lemmy(_) => {
let mut iter = self.to.iter().merge(self.cc.iter());
loop {
if let Some(cid) = iter.next() {
let cid = ObjectId::new(cid.clone());
if let Ok(c) = cid.dereference(context, instance, request_counter).await {
break c;
}
} else {
return Err(LemmyError::from_message("No community found in cc"));
}
}
}
AttributedTo::Peertube(p) => {
p.iter()
.find(|a| a.kind == PersonOrGroupType::Group)
.map(|a| ObjectId::<ApubCommunity>::new(a.id.clone().into_inner()))
.ok_or_else(|| LemmyError::from_message("page does not specify group"))?
.dereference(context, instance, request_counter)
.await?
}
};
if let Some(audience) = &self.audience {
let audience = audience
.dereference(context, instance, request_counter)
.await?;
verify_community_matches(&audience, community.id)?;
Ok(audience)
} else {
Ok(community)
}
}
}