2 check_is_apub_id_valid,
3 extensions::signatures::verify_signature,
4 fetcher::get_or_fetch_and_upsert_actor,
8 activity::ActorAndObjectRefExt,
9 base::{AsBase, BaseExt, Extends},
10 object::{AsObject, ObjectExt},
13 use actix_web::HttpRequest;
14 use anyhow::{anyhow, Context};
15 use lemmy_db::{activity::Activity, community::Community, user::User_, ApubObject, DbPool};
16 use lemmy_structs::blocking;
17 use lemmy_utils::{location_info, settings::Settings, LemmyError};
18 use lemmy_websocket::LemmyContext;
19 use serde::{export::fmt::Debug, Serialize};
22 pub mod community_inbox;
23 mod receive_for_community;
27 pub(crate) fn get_activity_id<T, Kind>(activity: &T, creator_uri: &Url) -> Result<Url, LemmyError>
29 T: BaseExt<Kind> + Extends<Kind> + Debug,
31 <T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
33 let creator_domain = creator_uri.host_str().context(location_info!())?;
34 let activity_id = activity.id(creator_domain)?;
35 Ok(activity_id.context(location_info!())?.to_owned())
38 pub(crate) async fn is_activity_already_known(
41 ) -> Result<bool, LemmyError> {
42 let activity_id = activity_id.to_string();
43 let existing = blocking(pool, move |conn| {
44 Activity::read_from_apub_id(&conn, &activity_id)
53 pub(crate) fn get_activity_to_and_cc<T, Kind>(activity: &T) -> Vec<Url>
55 T: AsBase<Kind> + AsObject<Kind> + ActorAndObjectRefExt,
57 let mut to_and_cc = vec![];
58 if let Some(to) = activity.to() {
59 let to = to.to_owned().unwrap_to_vec();
62 .map(|t| t.as_xsd_any_uri())
64 .map(|t| t.to_owned())
66 to_and_cc.append(&mut to);
68 if let Some(cc) = activity.cc() {
69 let cc = cc.to_owned().unwrap_to_vec();
72 .map(|c| c.as_xsd_any_uri())
74 .map(|c| c.to_owned())
76 to_and_cc.append(&mut cc);
81 pub(crate) fn is_addressed_to_public<T, Kind>(activity: &T) -> Result<(), LemmyError>
83 T: AsBase<Kind> + AsObject<Kind> + ActorAndObjectRefExt,
85 let to_and_cc = get_activity_to_and_cc(activity);
86 if to_and_cc.contains(&public()) {
89 Err(anyhow!("Activity is not addressed to public").into())
93 pub(crate) async fn inbox_verify_http_signature<T, Kind>(
95 context: &LemmyContext,
97 request_counter: &mut i32,
98 ) -> Result<Box<dyn ActorType>, LemmyError>
100 T: AsObject<Kind> + ActorAndObjectRefExt + Extends<Kind> + AsBase<Kind>,
102 <T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
104 let actor_id = activity
107 .single_xsd_any_uri()
108 .context(location_info!())?;
109 check_is_apub_id_valid(&actor_id)?;
110 let actor = get_or_fetch_and_upsert_actor(&actor_id, &context, request_counter).await?;
111 verify_signature(&request, actor.as_ref())?;
115 /// Returns true if `to_and_cc` contains at least one local user.
116 pub(crate) async fn is_addressed_to_local_user(
119 ) -> Result<bool, LemmyError> {
120 for url in to_and_cc {
121 let url = url.to_string();
122 let user = blocking(&pool, move |conn| User_::read_from_apub_id(&conn, &url)).await?;
123 if let Ok(u) = user {
132 /// If `to_and_cc` contains the followers collection of a remote community, returns this community
133 /// (like `https://example.com/c/main/followers`)
134 pub(crate) async fn is_addressed_to_community_followers(
137 ) -> Result<Option<Community>, LemmyError> {
138 for url in to_and_cc {
139 let url = url.to_string();
140 // TODO: extremely hacky, we should just store the followers url for each community in the db
141 if url.ends_with("/followers") {
142 let community_url = url.replace("/followers", "");
143 let community = blocking(&pool, move |conn| {
144 Community::read_from_apub_id(&conn, &community_url)
147 if !community.local {
148 return Ok(Some(community));
155 pub(in crate::inbox) fn assert_activity_not_local<T, Kind>(activity: &T) -> Result<(), LemmyError>
157 T: BaseExt<Kind> + Debug,
159 let id = activity.id_unchecked().context(location_info!())?;
160 let activity_domain = id.domain().context(location_info!())?;
162 if activity_domain == Settings::get().hostname {
165 "Error: received activity which was sent by local instance: {:?}",