2 check_is_apub_id_valid,
4 generate_moderators_url,
6 objects::{community::ApubCommunity, person::ApubPerson},
8 use activitystreams_kinds::public;
10 use lemmy_api_common::utils::blocking;
12 activity_queue::send_activity,
15 verify::verify_domains_match,
17 use lemmy_db_schema::source::community::Community;
18 use lemmy_db_views_actor::structs::{CommunityPersonBanView, CommunityView};
19 use lemmy_utils::{settings::structs::Settings, LemmyError};
20 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(context, context.client(), 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(context, context.client(), 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 fn verify_activity(id: &Url, actor: &Url, settings: &Settings) -> Result<(), LemmyError> {
78 check_is_apub_id_valid(actor, false, settings)?;
79 verify_domains_match(id, actor)?;
83 /// Verify that the actor is a community mod. This check is only run if the community is local,
84 /// because in case of remote communities, admins can also perform mod actions. As admin status
85 /// is not federated, we cant verify their actions remotely.
87 /// * `mod_id` - Activitypub ID of the mod or admin who performed the action
88 /// * `object_id` - Activitypub ID of the actor or object that is being moderated
89 /// * `community` - The community inside which moderation is happening
90 #[tracing::instrument(skip_all)]
91 pub(crate) async fn verify_mod_action(
92 mod_id: &ObjectId<ApubPerson>,
94 community: &ApubCommunity,
95 context: &LemmyContext,
96 request_counter: &mut i32,
97 ) -> Result<(), LemmyError> {
100 .dereference(context, context.client(), request_counter)
103 // Note: this will also return true for admins in addition to mods, but as we dont know about
104 // remote admins, it doesnt make any difference.
105 let community_id = community.id;
106 let actor_id = actor.id;
108 let is_mod_or_admin = blocking(context.pool(), move |conn| {
109 CommunityView::is_mod_or_admin(conn, actor_id, community_id)
113 // mod action was done either by a community mod or a local admin, so its allowed
118 // mod action comes from the same instance as the moderated object, so it was presumably done
119 // by an instance admin and is legitimate (admin status is not federated).
120 if mod_id.inner().domain() == object_id.domain() {
124 // the user is not a valid mod
125 return Err(LemmyError::from_message("Not a mod"));
130 /// For Add/Remove community moderator activities, check that the target field actually contains
131 /// /c/community/moderators. Any different values are unsupported.
132 fn verify_add_remove_moderator_target(
134 community: &ApubCommunity,
135 ) -> Result<(), LemmyError> {
136 if target != &generate_moderators_url(&community.actor_id)?.into() {
137 return Err(LemmyError::from_message("Unkown target url"));
142 pub(crate) fn verify_is_public(to: &[Url], cc: &[Url]) -> Result<(), LemmyError> {
143 if ![to, cc].iter().any(|set| set.contains(&public())) {
144 return Err(LemmyError::from_message("Object is not public"));
149 pub(crate) fn check_community_deleted_or_removed(community: &Community) -> Result<(), LemmyError> {
150 if community.deleted || community.removed {
151 Err(LemmyError::from_message(
152 "New post or comment cannot be created in deleted or removed community",
159 /// Generate a unique ID for an activity, in the format:
160 /// `http(s)://example.com/receive/create/202daf0a-1489-45df-8d2e-c8a3173fed36`
161 fn generate_activity_id<T>(kind: T, protocol_and_hostname: &str) -> Result<Url, ParseError>
166 "{}/activities/{}/{}",
167 protocol_and_hostname,
168 kind.to_string().to_lowercase(),
174 #[tracing::instrument(skip_all)]
175 async fn send_lemmy_activity<T: Serialize>(
176 context: &LemmyContext,
179 actor: &dyn ActorType,
182 ) -> Result<(), LemmyError> {
183 if !context.settings().federation.enabled || inboxes.is_empty() {
186 let activity = WithContext::new(activity);
188 info!("Sending activity {}", activity_id.to_string());
190 // Don't send anything to ourselves
191 // TODO: this should be a debug assert
192 let hostname = context.settings().get_hostname_without_port()?;
193 let inboxes: Vec<&Url> = inboxes
195 .filter(|i| i.domain().expect("valid inbox url") != hostname)
198 let serialised_activity = serde_json::to_string(&activity)?;
200 let object_value = serde_json::to_value(&activity)?;
201 insert_activity(activity_id, object_value, true, sensitive, context.pool()).await?;
209 context.activity_queue(),