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::{generate_moderators_url, 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;
35 /// Checks that the specified Url actually identifies a Person (by fetching it), and that the person
36 /// doesn't have a site ban.
37 #[tracing::instrument(skip_all)]
38 async fn verify_person(
39 person_id: &ObjectId<ApubPerson>,
40 context: &LemmyContext,
41 request_counter: &mut i32,
42 ) -> Result<(), LemmyError> {
43 let person = person_id
44 .dereference(context, local_instance(context).await, request_counter)
47 let err = anyhow!("Person {} is banned", person_id);
48 return Err(LemmyError::from_error_message(err, "banned"));
53 /// Fetches the person and community to verify their type, then checks if person is banned from site
55 #[tracing::instrument(skip_all)]
56 pub(crate) async fn verify_person_in_community(
57 person_id: &ObjectId<ApubPerson>,
58 community: &ApubCommunity,
59 context: &LemmyContext,
60 request_counter: &mut i32,
61 ) -> Result<(), LemmyError> {
62 let person = person_id
63 .dereference(context, local_instance(context).await, request_counter)
66 return Err(LemmyError::from_message("Person is banned from site"));
68 let person_id = person.id;
69 let community_id = community.id;
70 let is_banned = CommunityPersonBanView::get(context.pool(), person_id, community_id)
74 return Err(LemmyError::from_message("Person is banned from community"));
80 /// Verify that mod action in community was performed by a moderator.
82 /// * `mod_id` - Activitypub ID of the mod or admin who performed the action
83 /// * `object_id` - Activitypub ID of the actor or object that is being moderated
84 /// * `community` - The community inside which moderation is happening
85 #[tracing::instrument(skip_all)]
86 pub(crate) async fn verify_mod_action(
87 mod_id: &ObjectId<ApubPerson>,
89 community_id: CommunityId,
90 context: &LemmyContext,
91 request_counter: &mut i32,
92 ) -> Result<(), LemmyError> {
94 .dereference(context, local_instance(context).await, request_counter)
98 CommunityView::is_mod_or_admin(context.pool(), mod_.id, community_id).await?;
103 // mod action comes from the same instance as the moderated object, so it was presumably done
104 // by an instance admin.
105 // TODO: federate instance admin status and check it here
106 if mod_id.inner().domain() == object_id.domain() {
110 Err(LemmyError::from_message("Not a mod"))
113 /// For Add/Remove community moderator activities, check that the target field actually contains
114 /// /c/community/moderators. Any different values are unsupported.
115 fn verify_add_remove_moderator_target(
117 community: &ApubCommunity,
118 ) -> Result<(), LemmyError> {
119 if target != &generate_moderators_url(&community.actor_id)?.into() {
120 return Err(LemmyError::from_message("Unkown target url"));
125 pub(crate) fn verify_is_public(to: &[Url], cc: &[Url]) -> Result<(), LemmyError> {
126 if ![to, cc].iter().any(|set| set.contains(&public())) {
127 return Err(LemmyError::from_message("Object is not public"));
132 pub(crate) fn verify_community_matches(
135 ) -> Result<(), LemmyError> {
137 return Err(LemmyError::from_message("Invalid community"));
142 pub(crate) fn check_community_deleted_or_removed(community: &Community) -> Result<(), LemmyError> {
143 if community.deleted || community.removed {
144 Err(LemmyError::from_message(
145 "New post or comment cannot be created in deleted or removed community",
152 /// Generate a unique ID for an activity, in the format:
153 /// `http(s)://example.com/receive/create/202daf0a-1489-45df-8d2e-c8a3173fed36`
154 fn generate_activity_id<T>(kind: T, protocol_and_hostname: &str) -> Result<Url, ParseError>
159 "{}/activities/{}/{}",
160 protocol_and_hostname,
161 kind.to_string().to_lowercase(),
167 #[tracing::instrument(skip_all)]
168 async fn send_lemmy_activity<Activity, ActorT>(
169 context: &LemmyContext,
174 ) -> Result<(), LemmyError>
176 Activity: ActivityHandler + Serialize,
177 ActorT: Actor + ActorType,
178 Activity: ActivityHandler<Error = LemmyError>,
180 let federation_enabled = LocalSite::read(context.pool())
182 .map(|l| l.federation_enabled)
184 if !federation_enabled {
188 info!("Sending activity {}", activity.id().to_string());
189 let activity = WithContext::new(activity, CONTEXT.deref().clone());
191 let object_value = serde_json::to_value(&activity)?;
192 insert_activity(activity.id(), object_value, true, sensitive, context.pool()).await?;
196 actor.get_public_key(),
197 actor.private_key().expect("actor has private key"),
199 local_instance(context).await,