2 check_is_apub_id_valid,
4 generate_moderators_url,
6 objects::{community::ApubCommunity, person::ApubPerson},
8 use activitystreams_kinds::public;
9 use lemmy_api_common::blocking;
11 activity_queue::send_activity,
14 verify::verify_domains_match,
16 use lemmy_db_schema::source::community::Community;
17 use lemmy_db_views_actor::{
18 community_person_ban_view::CommunityPersonBanView,
19 community_view::CommunityView,
21 use lemmy_utils::{settings::structs::Settings, LemmyError};
22 use lemmy_websocket::LemmyContext;
25 use url::{ParseError, Url};
33 pub mod private_message;
36 /// Checks that the specified Url actually identifies a Person (by fetching it), and that the person
37 /// doesn't have a site ban.
38 #[tracing::instrument(skip_all)]
39 async fn verify_person(
40 person_id: &ObjectId<ApubPerson>,
41 context: &LemmyContext,
42 request_counter: &mut i32,
43 ) -> Result<(), LemmyError> {
44 let person = person_id
45 .dereference(context, context.client(), request_counter)
48 let error = LemmyError::from(anyhow::anyhow!("Person {} is banned", person_id));
49 return Err(error.with_message("banned"));
54 /// Fetches the person and community to verify their type, then checks if person is banned from site
56 #[tracing::instrument(skip_all)]
57 pub(crate) async fn verify_person_in_community(
58 person_id: &ObjectId<ApubPerson>,
59 community: &ApubCommunity,
60 context: &LemmyContext,
61 request_counter: &mut i32,
62 ) -> Result<(), LemmyError> {
63 let person = person_id
64 .dereference(context, context.client(), request_counter)
67 return Err(LemmyError::from_message("Person is banned from site"));
69 let person_id = person.id;
70 let community_id = community.id;
72 move |conn: &'_ _| CommunityPersonBanView::get(conn, person_id, community_id).is_ok();
73 if blocking(context.pool(), is_banned).await? {
74 return Err(LemmyError::from_message("Person is banned from community"));
80 fn verify_activity(id: &Url, actor: &Url, settings: &Settings) -> Result<(), LemmyError> {
81 check_is_apub_id_valid(actor, false, settings)?;
82 verify_domains_match(id, actor)?;
86 /// Verify that the actor is a community mod. This check is only run if the community is local,
87 /// because in case of remote communities, admins can also perform mod actions. As admin status
88 /// is not federated, we cant verify their actions remotely.
89 #[tracing::instrument(skip_all)]
90 pub(crate) async fn verify_mod_action(
91 actor_id: &ObjectId<ApubPerson>,
92 community: &ApubCommunity,
93 context: &LemmyContext,
94 request_counter: &mut i32,
95 ) -> Result<(), LemmyError> {
98 .dereference(context, context.client(), request_counter)
101 // Note: this will also return true for admins in addition to mods, but as we dont know about
102 // remote admins, it doesnt make any difference.
103 let community_id = community.id;
104 let actor_id = actor.id;
105 let is_mod_or_admin = blocking(context.pool(), move |conn| {
106 CommunityView::is_mod_or_admin(conn, actor_id, community_id)
109 if !is_mod_or_admin {
110 return Err(LemmyError::from_message("Not a mod"));
116 /// For Add/Remove community moderator activities, check that the target field actually contains
117 /// /c/community/moderators. Any different values are unsupported.
118 fn verify_add_remove_moderator_target(
120 community: &ApubCommunity,
121 ) -> Result<(), LemmyError> {
122 if target != &generate_moderators_url(&community.actor_id)?.into() {
123 return Err(LemmyError::from_message("Unkown target url"));
128 pub(crate) fn verify_is_public(to: &[Url], cc: &[Url]) -> Result<(), LemmyError> {
129 if ![to, cc].iter().any(|set| set.contains(&public())) {
130 return Err(LemmyError::from_message("Object is not public"));
135 pub(crate) fn check_community_deleted_or_removed(community: &Community) -> Result<(), LemmyError> {
136 if community.deleted || community.removed {
137 Err(LemmyError::from_message(
138 "New post or comment cannot be created in deleted or removed community",
145 /// Generate a unique ID for an activity, in the format:
146 /// `http(s)://example.com/receive/create/202daf0a-1489-45df-8d2e-c8a3173fed36`
147 fn generate_activity_id<T>(kind: T, protocol_and_hostname: &str) -> Result<Url, ParseError>
152 "{}/activities/{}/{}",
153 protocol_and_hostname,
154 kind.to_string().to_lowercase(),
160 #[tracing::instrument(skip_all)]
161 async fn send_lemmy_activity<T: Serialize>(
162 context: &LemmyContext,
165 actor: &dyn ActorType,
168 ) -> Result<(), LemmyError> {
169 if !context.settings().federation.enabled || inboxes.is_empty() {
172 let activity = WithContext::new(activity);
174 info!("Sending activity {}", activity_id.to_string());
176 // Don't send anything to ourselves
177 // TODO: this should be a debug assert
178 let hostname = context.settings().get_hostname_without_port()?;
179 let inboxes: Vec<&Url> = inboxes
181 .filter(|i| i.domain().expect("valid inbox url") != hostname)
184 let serialised_activity = serde_json::to_string(&activity)?;
186 let object_value = serde_json::to_value(&activity)?;
187 insert_activity(activity_id, object_value, true, sensitive, context.pool()).await?;
195 context.activity_queue(),