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::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 the actor is a community mod. This check is only run if the community is local,
79 /// because in case of remote communities, admins can also perform mod actions. As admin status
80 /// is not federated, we cant verify their actions remotely.
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: &ApubCommunity,
90 context: &LemmyContext,
91 request_counter: &mut i32,
92 ) -> Result<(), LemmyError> {
95 .dereference(context, local_instance(context), request_counter)
98 // Note: this will also return true for admins in addition to mods, but as we dont know about
99 // remote admins, it doesnt make any difference.
100 let community_id = community.id;
101 let actor_id = actor.id;
103 let is_mod_or_admin = blocking(context.pool(), move |conn| {
104 CommunityView::is_mod_or_admin(conn, actor_id, community_id)
108 // mod action was done either by a community mod or a local admin, so its allowed
113 // mod action comes from the same instance as the moderated object, so it was presumably done
114 // by an instance admin and is legitimate (admin status is not federated).
115 if mod_id.inner().domain() == object_id.domain() {
119 // the user is not a valid mod
120 return Err(LemmyError::from_message("Not a mod"));
125 /// For Add/Remove community moderator activities, check that the target field actually contains
126 /// /c/community/moderators. Any different values are unsupported.
127 fn verify_add_remove_moderator_target(
129 community: &ApubCommunity,
130 ) -> Result<(), LemmyError> {
131 if target != &generate_moderators_url(&community.actor_id)?.into() {
132 return Err(LemmyError::from_message("Unkown target url"));
137 pub(crate) fn verify_is_public(to: &[Url], cc: &[Url]) -> Result<(), LemmyError> {
138 if ![to, cc].iter().any(|set| set.contains(&public())) {
139 return Err(LemmyError::from_message("Object is not public"));
144 pub(crate) fn check_community_deleted_or_removed(community: &Community) -> Result<(), LemmyError> {
145 if community.deleted || community.removed {
146 Err(LemmyError::from_message(
147 "New post or comment cannot be created in deleted or removed community",
154 /// Generate a unique ID for an activity, in the format:
155 /// `http(s)://example.com/receive/create/202daf0a-1489-45df-8d2e-c8a3173fed36`
156 fn generate_activity_id<T>(kind: T, protocol_and_hostname: &str) -> Result<Url, ParseError>
161 "{}/activities/{}/{}",
162 protocol_and_hostname,
163 kind.to_string().to_lowercase(),
169 #[tracing::instrument(skip_all)]
170 async fn send_lemmy_activity<Activity, ActorT>(
171 context: &LemmyContext,
176 ) -> Result<(), LemmyError>
178 Activity: ActivityHandler + Serialize,
179 ActorT: Actor + ActorType,
180 Activity: ActivityHandler<Error = LemmyError>,
182 info!("Sending activity {}", activity.id().to_string());
183 let activity = WithContext::new(activity, CONTEXT.deref().clone());
185 let object_value = serde_json::to_value(&activity)?;
186 insert_activity(activity.id(), object_value, true, sensitive, context.pool()).await?;
190 actor.get_public_key(),
191 actor.private_key().expect("actor has private key"),
193 local_instance(context),