2 generate_moderators_url,
5 objects::{community::ApubCommunity, person::ApubPerson},
9 use activitypub_federation::{
10 core::{activity_queue::SendActivity, object_id::ObjectId},
11 deser::context::WithContext,
13 use activitystreams_kinds::public;
15 use lemmy_api_common::utils::blocking;
16 use lemmy_db_schema::source::community::Community;
17 use lemmy_db_views_actor::structs::{CommunityPersonBanView, CommunityView};
18 use lemmy_utils::error::LemmyError;
19 use lemmy_websocket::LemmyContext;
23 use url::{ParseError, Url};
28 pub mod create_or_update;
33 /// Checks that the specified Url actually identifies a Person (by fetching it), and that the person
34 /// doesn't have a site ban.
35 #[tracing::instrument(skip_all)]
36 async fn verify_person(
37 person_id: &ObjectId<ApubPerson>,
38 context: &LemmyContext,
39 request_counter: &mut i32,
40 ) -> Result<(), LemmyError> {
41 let person = person_id
42 .dereference::<LemmyError>(context, local_instance(context), request_counter)
45 let err = anyhow!("Person {} is banned", person_id);
46 return Err(LemmyError::from_error_message(err, "banned"));
51 /// Fetches the person and community to verify their type, then checks if person is banned from site
53 #[tracing::instrument(skip_all)]
54 pub(crate) async fn verify_person_in_community(
55 person_id: &ObjectId<ApubPerson>,
56 community: &ApubCommunity,
57 context: &LemmyContext,
58 request_counter: &mut i32,
59 ) -> Result<(), LemmyError> {
60 let person = person_id
61 .dereference::<LemmyError>(context, local_instance(context), request_counter)
64 return Err(LemmyError::from_message("Person is banned from site"));
66 let person_id = person.id;
67 let community_id = community.id;
69 move |conn: &'_ _| CommunityPersonBanView::get(conn, person_id, community_id).is_ok();
70 if blocking(context.pool(), is_banned).await? {
71 return Err(LemmyError::from_message("Person is banned from community"));
77 /// Verify that the actor is a community mod. This check is only run if the community is local,
78 /// because in case of remote communities, admins can also perform mod actions. As admin status
79 /// is not federated, we cant verify their actions remotely.
81 /// * `mod_id` - Activitypub ID of the mod or admin who performed the action
82 /// * `object_id` - Activitypub ID of the actor or object that is being moderated
83 /// * `community` - The community inside which moderation is happening
84 #[tracing::instrument(skip_all)]
85 pub(crate) async fn verify_mod_action(
86 mod_id: &ObjectId<ApubPerson>,
88 community: &ApubCommunity,
89 context: &LemmyContext,
90 request_counter: &mut i32,
91 ) -> Result<(), LemmyError> {
94 .dereference::<LemmyError>(context, local_instance(context), request_counter)
97 // Note: this will also return true for admins in addition to mods, but as we dont know about
98 // remote admins, it doesnt make any difference.
99 let community_id = community.id;
100 let actor_id = actor.id;
102 let is_mod_or_admin = blocking(context.pool(), move |conn| {
103 CommunityView::is_mod_or_admin(conn, actor_id, community_id)
107 // mod action was done either by a community mod or a local admin, so its allowed
112 // mod action comes from the same instance as the moderated object, so it was presumably done
113 // by an instance admin and is legitimate (admin status is not federated).
114 if mod_id.inner().domain() == object_id.domain() {
118 // the user is not a valid mod
119 return Err(LemmyError::from_message("Not a mod"));
124 /// For Add/Remove community moderator activities, check that the target field actually contains
125 /// /c/community/moderators. Any different values are unsupported.
126 fn verify_add_remove_moderator_target(
128 community: &ApubCommunity,
129 ) -> Result<(), LemmyError> {
130 if target != &generate_moderators_url(&community.actor_id)?.into() {
131 return Err(LemmyError::from_message("Unkown target url"));
136 pub(crate) fn verify_is_public(to: &[Url], cc: &[Url]) -> Result<(), LemmyError> {
137 if ![to, cc].iter().any(|set| set.contains(&public())) {
138 return Err(LemmyError::from_message("Object is not public"));
143 pub(crate) fn check_community_deleted_or_removed(community: &Community) -> Result<(), LemmyError> {
144 if community.deleted || community.removed {
145 Err(LemmyError::from_message(
146 "New post or comment cannot be created in deleted or removed community",
153 /// Generate a unique ID for an activity, in the format:
154 /// `http(s)://example.com/receive/create/202daf0a-1489-45df-8d2e-c8a3173fed36`
155 fn generate_activity_id<T>(kind: T, protocol_and_hostname: &str) -> Result<Url, ParseError>
160 "{}/activities/{}/{}",
161 protocol_and_hostname,
162 kind.to_string().to_lowercase(),
168 #[tracing::instrument(skip_all)]
169 async fn send_lemmy_activity<T: Serialize>(
170 context: &LemmyContext,
173 actor: &dyn ActorType,
176 ) -> Result<(), LemmyError> {
177 if !context.settings().federation.enabled || inboxes.is_empty() {
180 let activity = WithContext::new(activity, CONTEXT.deref().clone());
182 info!("Sending activity {}", activity_id.to_string());
184 // Don't send anything to ourselves
185 // TODO: this should be a debug assert
186 let hostname = context.settings().get_hostname_without_port()?;
187 let inboxes: Vec<Url> = inboxes
189 .filter(|i| i.domain().expect("valid inbox url") != hostname)
192 let serialised_activity = serde_json::to_string(&activity)?;
194 let object_value = serde_json::to_value(&activity)?;
195 insert_activity(activity_id, object_value, true, sensitive, context.pool()).await?;
198 activity_id: activity_id.clone(),
199 actor_public_key: actor.get_public_key(),
200 actor_private_key: actor.private_key().expect("actor has private key"),
202 activity: serialised_activity,
204 .send(local_instance(context))