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_api_common::utils::blocking;
17 use lemmy_db_schema::{newtypes::CommunityId, source::community::Community};
18 use lemmy_db_views_actor::structs::{CommunityPersonBanView, CommunityView};
19 use lemmy_utils::error::LemmyError;
20 use lemmy_websocket::LemmyContext;
24 use url::{ParseError, Url};
29 pub mod create_or_update;
34 /// Checks that the specified Url actually identifies a Person (by fetching it), and that the person
35 /// doesn't have a site ban.
36 #[tracing::instrument(skip_all)]
37 async fn verify_person(
38 person_id: &ObjectId<ApubPerson>,
39 context: &LemmyContext,
40 request_counter: &mut i32,
41 ) -> Result<(), LemmyError> {
42 let person = person_id
43 .dereference(context, local_instance(context), request_counter)
46 let err = anyhow!("Person {} is banned", person_id);
47 return Err(LemmyError::from_error_message(err, "banned"));
52 /// Fetches the person and community to verify their type, then checks if person is banned from site
54 #[tracing::instrument(skip_all)]
55 pub(crate) async fn verify_person_in_community(
56 person_id: &ObjectId<ApubPerson>,
57 community: &ApubCommunity,
58 context: &LemmyContext,
59 request_counter: &mut i32,
60 ) -> Result<(), LemmyError> {
61 let person = person_id
62 .dereference(context, local_instance(context), request_counter)
65 return Err(LemmyError::from_message("Person is banned from site"));
67 let person_id = person.id;
68 let community_id = community.id;
70 move |conn: &mut _| CommunityPersonBanView::get(conn, person_id, community_id).is_ok();
71 if blocking(context.pool(), is_banned).await? {
72 return Err(LemmyError::from_message("Person is banned from community"));
78 /// Verify that mod action in community was performed by a moderator.
80 /// * `mod_id` - Activitypub ID of the mod or admin who performed the action
81 /// * `object_id` - Activitypub ID of the actor or object that is being moderated
82 /// * `community` - The community inside which moderation is happening
83 #[tracing::instrument(skip_all)]
84 pub(crate) async fn verify_mod_action(
85 mod_id: &ObjectId<ApubPerson>,
87 community_id: CommunityId,
88 context: &LemmyContext,
89 request_counter: &mut i32,
90 ) -> Result<(), LemmyError> {
92 .dereference(context, local_instance(context), request_counter)
95 let is_mod_or_admin = blocking(context.pool(), move |conn| {
96 CommunityView::is_mod_or_admin(conn, mod_.id, community_id)
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 check_community_deleted_or_removed(community: &Community) -> Result<(), LemmyError> {
133 if community.deleted || community.removed {
134 Err(LemmyError::from_message(
135 "New post or comment cannot be created in deleted or removed community",
142 /// Generate a unique ID for an activity, in the format:
143 /// `http(s)://example.com/receive/create/202daf0a-1489-45df-8d2e-c8a3173fed36`
144 fn generate_activity_id<T>(kind: T, protocol_and_hostname: &str) -> Result<Url, ParseError>
149 "{}/activities/{}/{}",
150 protocol_and_hostname,
151 kind.to_string().to_lowercase(),
157 #[tracing::instrument(skip_all)]
158 async fn send_lemmy_activity<Activity, ActorT>(
159 context: &LemmyContext,
164 ) -> Result<(), LemmyError>
166 Activity: ActivityHandler + Serialize,
167 ActorT: Actor + ActorType,
168 Activity: ActivityHandler<Error = LemmyError>,
170 if !context.settings().federation.enabled {
173 info!("Sending activity {}", activity.id().to_string());
174 let activity = WithContext::new(activity, CONTEXT.deref().clone());
176 let object_value = serde_json::to_value(&activity)?;
177 insert_activity(activity.id(), object_value, true, sensitive, context.pool()).await?;
181 actor.get_public_key(),
182 actor.private_key().expect("actor has private key"),
184 local_instance(context),