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_api_structs::blocking;
16 use lemmy_db_queries::{
17 source::{activity::Activity_, community::Community_},
21 use lemmy_db_schema::source::{activity::Activity, community::Community, user::User_};
22 use lemmy_utils::{location_info, settings::Settings, LemmyError};
23 use lemmy_websocket::LemmyContext;
28 pub mod community_inbox;
29 mod receive_for_community;
33 pub(crate) fn get_activity_id<T, Kind>(activity: &T, creator_uri: &Url) -> Result<Url, LemmyError>
35 T: BaseExt<Kind> + Extends<Kind> + Debug,
37 <T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
39 let creator_domain = creator_uri.host_str().context(location_info!())?;
40 let activity_id = activity.id(creator_domain)?;
41 Ok(activity_id.context(location_info!())?.to_owned())
44 pub(crate) async fn is_activity_already_known(
47 ) -> Result<bool, LemmyError> {
48 let activity_id = activity_id.to_string();
49 let existing = blocking(pool, move |conn| {
50 Activity::read_from_apub_id(&conn, &activity_id)
59 pub(crate) fn get_activity_to_and_cc<T, Kind>(activity: &T) -> Vec<Url>
61 T: AsBase<Kind> + AsObject<Kind> + ActorAndObjectRefExt,
63 let mut to_and_cc = vec![];
64 if let Some(to) = activity.to() {
65 let to = to.to_owned().unwrap_to_vec();
68 .map(|t| t.as_xsd_any_uri())
70 .map(|t| t.to_owned())
72 to_and_cc.append(&mut to);
74 if let Some(cc) = activity.cc() {
75 let cc = cc.to_owned().unwrap_to_vec();
78 .map(|c| c.as_xsd_any_uri())
80 .map(|c| c.to_owned())
82 to_and_cc.append(&mut cc);
87 pub(crate) fn is_addressed_to_public<T, Kind>(activity: &T) -> Result<(), LemmyError>
89 T: AsBase<Kind> + AsObject<Kind> + ActorAndObjectRefExt,
91 let to_and_cc = get_activity_to_and_cc(activity);
92 if to_and_cc.contains(&public()) {
95 Err(anyhow!("Activity is not addressed to public").into())
99 pub(crate) async fn inbox_verify_http_signature<T, Kind>(
101 context: &LemmyContext,
102 request: HttpRequest,
103 request_counter: &mut i32,
104 ) -> Result<Box<dyn ActorType>, LemmyError>
106 T: AsObject<Kind> + ActorAndObjectRefExt + Extends<Kind> + AsBase<Kind>,
108 <T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
110 let actor_id = activity
113 .single_xsd_any_uri()
114 .context(location_info!())?;
115 check_is_apub_id_valid(&actor_id)?;
116 let actor = get_or_fetch_and_upsert_actor(&actor_id, &context, request_counter).await?;
117 verify_signature(&request, actor.as_ref())?;
121 /// Returns true if `to_and_cc` contains at least one local user.
122 pub(crate) async fn is_addressed_to_local_user(
125 ) -> Result<bool, LemmyError> {
126 for url in to_and_cc {
127 let url = url.to_owned();
128 let user = blocking(&pool, move |conn| {
129 User_::read_from_apub_id(&conn, &url.into())
132 if let Ok(u) = user {
141 /// If `to_and_cc` contains the followers collection of a remote community, returns this community
142 /// (like `https://example.com/c/main/followers`)
143 pub(crate) async fn is_addressed_to_community_followers(
146 ) -> Result<Option<Community>, LemmyError> {
147 for url in to_and_cc {
148 let url = url.to_owned().into();
149 let community = blocking(&pool, move |conn| {
150 // ignore errors here, because the current url might not actually be a followers url
151 Community::read_from_followers_url(&conn, &url).ok()
154 if let Some(c) = community {
163 pub(in crate::inbox) fn assert_activity_not_local<T, Kind>(activity: &T) -> Result<(), LemmyError>
165 T: BaseExt<Kind> + Debug,
167 let id = activity.id_unchecked().context(location_info!())?;
168 let activity_domain = id.domain().context(location_info!())?;
170 if activity_domain == Settings::get().hostname {
173 "Error: received activity which was sent by local instance: {:?}",