2 generate_moderators_url,
5 objects::{community::ApubCommunity, person::ApubPerson},
9 use activitypub_federation::{
10 core::{activity_queue::send_activity, object_id::ObjectId},
11 deser::context::WithContext,
12 traits::{ActivityHandler, Actor},
14 use activitystreams_kinds::public;
16 use lemmy_db_schema::{
17 newtypes::CommunityId,
18 source::{community::Community, local_site::LocalSite},
20 use lemmy_db_views_actor::structs::{CommunityPersonBanView, CommunityView};
21 use lemmy_utils::error::LemmyError;
22 use lemmy_websocket::LemmyContext;
26 use url::{ParseError, Url};
31 pub mod create_or_update;
36 /// Checks that the specified Url actually identifies a Person (by fetching it), and that the person
37 /// doesn't have a site ban.
38 #[tracing::instrument(skip_all)]
39 async fn verify_person(
40 person_id: &ObjectId<ApubPerson>,
41 context: &LemmyContext,
42 request_counter: &mut i32,
43 ) -> Result<(), LemmyError> {
44 let person = person_id
45 .dereference(context, local_instance(context).await, request_counter)
48 let err = anyhow!("Person {} is banned", person_id);
49 return Err(LemmyError::from_error_message(err, "banned"));
54 /// Fetches the person and community to verify their type, then checks if person is banned from site
56 #[tracing::instrument(skip_all)]
57 pub(crate) async fn verify_person_in_community(
58 person_id: &ObjectId<ApubPerson>,
59 community: &ApubCommunity,
60 context: &LemmyContext,
61 request_counter: &mut i32,
62 ) -> Result<(), LemmyError> {
63 let person = person_id
64 .dereference(context, local_instance(context).await, request_counter)
67 return Err(LemmyError::from_message("Person is banned from site"));
69 let person_id = person.id;
70 let community_id = community.id;
71 let is_banned = CommunityPersonBanView::get(context.pool(), person_id, community_id)
75 return Err(LemmyError::from_message("Person is banned from community"));
81 /// Verify that mod action in community was performed by a moderator.
83 /// * `mod_id` - Activitypub ID of the mod or admin who performed the action
84 /// * `object_id` - Activitypub ID of the actor or object that is being moderated
85 /// * `community` - The community inside which moderation is happening
86 #[tracing::instrument(skip_all)]
87 pub(crate) async fn verify_mod_action(
88 mod_id: &ObjectId<ApubPerson>,
90 community_id: CommunityId,
91 context: &LemmyContext,
92 request_counter: &mut i32,
93 ) -> Result<(), LemmyError> {
95 .dereference(context, local_instance(context).await, request_counter)
99 CommunityView::is_mod_or_admin(context.pool(), mod_.id, community_id).await?;
104 // mod action comes from the same instance as the moderated object, so it was presumably done
105 // by an instance admin.
106 // TODO: federate instance admin status and check it here
107 if mod_id.inner().domain() == object_id.domain() {
111 Err(LemmyError::from_message("Not a mod"))
114 /// For Add/Remove community moderator activities, check that the target field actually contains
115 /// /c/community/moderators. Any different values are unsupported.
116 fn verify_add_remove_moderator_target(
118 community: &ApubCommunity,
119 ) -> Result<(), LemmyError> {
120 if target != &generate_moderators_url(&community.actor_id)?.into() {
121 return Err(LemmyError::from_message("Unkown target url"));
126 pub(crate) fn verify_is_public(to: &[Url], cc: &[Url]) -> Result<(), LemmyError> {
127 if ![to, cc].iter().any(|set| set.contains(&public())) {
128 return Err(LemmyError::from_message("Object is not public"));
133 pub(crate) fn check_community_deleted_or_removed(community: &Community) -> Result<(), LemmyError> {
134 if community.deleted || community.removed {
135 Err(LemmyError::from_message(
136 "New post or comment cannot be created in deleted or removed community",
143 /// Generate a unique ID for an activity, in the format:
144 /// `http(s)://example.com/receive/create/202daf0a-1489-45df-8d2e-c8a3173fed36`
145 fn generate_activity_id<T>(kind: T, protocol_and_hostname: &str) -> Result<Url, ParseError>
150 "{}/activities/{}/{}",
151 protocol_and_hostname,
152 kind.to_string().to_lowercase(),
158 #[tracing::instrument(skip_all)]
159 async fn send_lemmy_activity<Activity, ActorT>(
160 context: &LemmyContext,
165 ) -> Result<(), LemmyError>
167 Activity: ActivityHandler + Serialize,
168 ActorT: Actor + ActorType,
169 Activity: ActivityHandler<Error = LemmyError>,
171 let federation_enabled = LocalSite::read(context.pool())
173 .map(|l| l.federation_enabled)
175 if !federation_enabled {
179 info!("Sending activity {}", activity.id().to_string());
180 let activity = WithContext::new(activity, CONTEXT.deref().clone());
182 let object_value = serde_json::to_value(&activity)?;
183 insert_activity(activity.id(), object_value, true, sensitive, context.pool()).await?;
187 actor.get_public_key(),
188 actor.private_key().expect("actor has private key"),
190 local_instance(context).await,