4 objects::{community::ApubCommunity, person::ApubPerson},
8 use activitypub_federation::{
9 core::{activity_queue::send_activity, object_id::ObjectId},
10 deser::context::WithContext,
11 traits::{ActivityHandler, Actor},
13 use activitystreams_kinds::public;
15 use lemmy_api_common::context::LemmyContext;
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;
25 use url::{ParseError, Url};
30 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 pub(crate) fn verify_is_public(to: &[Url], cc: &[Url]) -> Result<(), LemmyError> {
115 if ![to, cc].iter().any(|set| set.contains(&public())) {
116 return Err(LemmyError::from_message("Object is not public"));
121 pub(crate) fn verify_community_matches<T>(
122 a: &ObjectId<ApubCommunity>,
124 ) -> Result<(), LemmyError>
126 T: Into<ObjectId<ApubCommunity>>,
128 let b: ObjectId<ApubCommunity> = b.into();
130 return Err(LemmyError::from_message("Invalid community"));
135 pub(crate) fn check_community_deleted_or_removed(community: &Community) -> Result<(), LemmyError> {
136 if community.deleted || community.removed {
137 Err(LemmyError::from_message(
138 "New post or comment cannot be created in deleted or removed community",
145 /// Generate a unique ID for an activity, in the format:
146 /// `http(s)://example.com/receive/create/202daf0a-1489-45df-8d2e-c8a3173fed36`
147 fn generate_activity_id<T>(kind: T, protocol_and_hostname: &str) -> Result<Url, ParseError>
152 "{}/activities/{}/{}",
153 protocol_and_hostname,
154 kind.to_string().to_lowercase(),
160 #[tracing::instrument(skip_all)]
161 async fn send_lemmy_activity<Activity, ActorT>(
162 context: &LemmyContext,
167 ) -> Result<(), LemmyError>
169 Activity: ActivityHandler + Serialize,
170 ActorT: Actor + ActorType,
171 Activity: ActivityHandler<Error = LemmyError>,
173 let federation_enabled = LocalSite::read(context.pool())
175 .map(|l| l.federation_enabled)
177 if !federation_enabled {
181 info!("Sending activity {}", activity.id().to_string());
182 let activity = WithContext::new(activity, CONTEXT.deref().clone());
184 let object_value = serde_json::to_value(&activity)?;
185 insert_activity(activity.id(), object_value, true, sensitive, context.pool()).await?;
189 actor.get_public_key(),
190 actor.private_key().expect("actor has private key"),
192 local_instance(context).await,